0
当我运行这段代码:控制台说,我创建函数不存在
//Function to see if a line is colliding with a certain point Has an accuracy of about 1 pixel
this.lineIsColliding = function(startX, startY, endX, endY, testX, testY) {
const v1 = {
x: endX - startX,
y: endY - startY
};
const l2 = v1.x * v1.x + v1.y * v1.y;
if (l2 === 0) {
return false;
} // line has no length so can't be near anything
const v2 = {
x: testX - startX,
y: testY - startY
};
const u = (v1.x * v2.x + v1.y * v2.y)/l2;
return u >= 0 && u <= 1 && Math.abs((v1.x * v2.y - v1.y * v2.x)/Math.sqrt(l2)) < 1;
};
//The Canvas to draw on
this.src = src;
//The context of source(used for drawing)
this.ctx = this.src.getContext("2d");
//The Mouse Move Function
this.showCoordinates = function(e) {
console.log(e);
label.innerHTML = "<b>x: </b>" + e.offsetX + " <b>y: </b>" + e.offsetY + ", " + this.lineIsColliding(358, 277, 365, 268, e.offsetX, e.offsetY);
};
(不是整个代码剪断,只显示重要的部分)
控制台说,我的lineIsColliding函数不存在!这正是它说: 遗漏的类型错误:this.lineIsColliding不是一个函数
想这取决于你是如何调用'lineIsColliding' - 因为显然你'this'是不是你认为它是('this'能混乱) –
ahh,'showCoordinates'处理一个事件 - 这就是为什么'this'不是你所想的 –