Ionic FabricJs Problem with access to function

Viewed 81
ionViewDidEnter(){
    this.loadCanvas();
}
loadCanvas(){
    canvas.on('mouse:down', function(e){
        this.canvasMouseDown(e);    
    });
}
canvasMouseDown(e){
    console.log(this.lock);
    console.log(this.pausePanning);
    if(!this.lock){
        this.pausePanning = true;
    }
}

That is code. Every time when I click I get error. Like below

Ionic error

I tried every combination and still have this same problem

1 Answers

Fabric events are bound to an object which triggers them, which means that here:

canvas.on('mouse:down', function(e){
    this.canvasMouseDown(e);    
});

this is an instance of canvas. And when you're calling this.canvasMouseDown(), you're essentially calling canvas.canvasMouseDown(), so this is where the error comes from. Try using the arrow function as the event handler:

loadCanvas(){
    canvas.on('mouse:down', (e) => {
        this.canvasMouseDown(e);    
    });
}
Related