StAR_161 StAR_161 - 6 months ago 78
jQuery Question

How do I get mouse coordinates on Fabric.js?

I'm trying to read the X coordinate of a mouse click on Fabric.js.

Here is my code. The console logs

undefined
every time.

var canvas = new fabric.Canvas('c1');
canvas.on('mouse:down', function(e){
getMouse(e);
});

function getMouse(e) {
console.log(e.clientX);
}

Answer

The best fix is this method

Implementation:

function getMouseCoords(event)
{
  var pointer = canvas.getPointer(event.e);
  var posX = pointer.x;
  var posY = pointer.y;
  console.log(posX+", "+posY);    // Log to console
}
Comments