JackGab
JackGab

Reputation: 37

Detecting A click on the Canvas

In my code I have a 800*800 canvas. It has a grid on it, and I have found this function:

    function getCursorPosition(canvas, event) {
    var x, y;

    canoffset = $(canvas).offset();
    x = event.clientX + document.body.scrollLeft + 
    document.documentElement.scrollLeft - Math.floor(canoffset.left);
    y = event.clientY + document.body.scrollTop + 
    document.documentElement.scrollTop - Math.floor(canoffset.top) + 1;

    return [x,y];
}

But how/where would I call this to detect when my canvas has been clicked?

Upvotes: 1

Views: 84

Answers (1)

J-Cake
J-Cake

Reputation: 1638

Aren't you able to add an event Listener?

document.querySelector("canvas").addEventListener("click", e => handle(e))

Where handle is your event handler.

Upvotes: 1

Related Questions