I'm currently working on a angular project and I don't understand the error message. How the error occurs:When triggering the mouseevent (mousedown: simply a mouseclick event) I call the mousedown method, in this method the method addNode is called and the error occurs
Typerror: your function this.addNode() is not a function at HTMLCanvasElement
export class Graph {
private ctx : CanvasRenderingContext2D;
private canvas: HTMLCanvasElement;
private readonly width: number;
private readonly height: number;
nodes: Node[] = [];
constructor(canvas: HTMLCanvasElement){
this.canvas = canvas;
this.ctx = this.canvas.getContext('2d');
this.width = canvas.width;
this.height = canvas.height;
this.nodes = [];
canvas.addEventListener('mousedown', this.onDown, false);
this.draw1();
}
onDown(e:MouseEvent){
***this.addNode(e.offsetX,e.offsetY);*** //This Line causes the error
console.log("LOG LOG " + e.offsetX + ", " + e.offsetY);
console.log("My Array: " + this.nodes.length);
}
addNode(x:number,y:number){
this.nodes.push(new Node(x,y));
}
draw1():void{
// console.log("Should be drawing");
//console.log("Node legnth : " + this.nodes.length);
this.nodes.forEach(node => {
console.log("Why you not doing");
this.ctx.fillStyle = "green";
this.ctx.beginPath();
this.ctx.arc(node.x,node.y,25,0,2*Math.PI);
this.ctx.stroke();
this.ctx.fill();
});
window.requestAnimationFrame(()=>this.draw1());
}
}
class Node{
x:number;
y:number;
constructor(x:number,y:number){
this.x = x;
this.y = y;
}
}
I thank you all in advance :)