I've got the following class that initializes a canvas element for mouse events. The listeners are initialized in the constructor but when I call findxy
when the event is fired, the references to variables like this.flag
result in an error because they are undefined which leads me to believe the listener is losing its reference to this
when calling findxy
. I'm not entirely sure how to fix this. Thanks in advance.
class Signature {
constructor() {
this.signed = false;
this.prevX = 0;
this.currX = 0;
this.prevY = 0;
this.currY = 0;
this.dot_flag = false;
this.flag = false;
this.canvas = document.getElementById('can');
this.ctx = this.canvas.getContext("2d");
this.w = this.canvas.width;
this.h = this.canvas.height;
this.canvas.addEventListener("touchmove", function(e) {
mobilexy('move', e)
}, false);
this.canvas.addEventListener("touchstart", function(e) {
mobilexy('down', e)
}, false);
this.canvas.addEventListener("touchleave", function(e) {
mobilexy('up', e)
}, false);
this.canvas.addEventListener("mousemove", function(e) {
findxy('move', e)
}, false);
this.canvas.addEventListener("mousedown", function(e) {
findxy('down', e)
}, false);
this.canvas.addEventListener("mouseup", function(e) {
findxy('up', e)
}, false);
this.canvas.addEventListener("mouseout", function(e) {
findxy('out', e)
}, false);
findxy(res, e) {
if (res == 'down') {
this.prevX = this.currX;
this.prevY = this.currY;
this.currX = e.pageX - this.canvas.offsetLeft;
this.currY = e.pageY - this.canvas.offsetTop;
this.flag = true;
this.dot_flag = true;
if (this.dot_flag) {
this.ctx.beginPath();
this.ctx.fillStyle = x;
this.ctx.fillRect(currX, currY, 2, 2);
this.ctx.closePath();
this.dot_flag = false;
}
}
if (res == 'up' || res == "out") {
this.flag = false;
}
if (res == 'move') {
if (this.flag) {
this.prevX = this.currX;
this.prevY = this.currY;
this.currX = e.pageX - canvas.offsetLeft;
this.currY = e.pageY - canvas.offsetTop;
draw();
}
}
}
}
My error:
Uncaught ReferenceError: flag is not defined
at findxy (jobs.self-09776d4c973306a740403ee614a19882dd0d4d402c0c72bba61747ef44c6ab2b.js?body=1:191)
at HTMLCanvasElement. (signature.self-e4f9a6f8d0069a7e6488dd64f3234fbdf4b0c2004f9de362da627d0176111f06.js?body=1:31)
findxy @ jobs.self-09776d4c973306a740403ee614a19882dd0d4d402c0c72bba61747ef44c6ab2b.js?body=1:191
(anonymous) @ signature.self-e4f9a6f8d0069a7e6488dd64f3234fbdf4b0c2004f9de362da627d0176111f06.js?body=1:31 09:37:56.162