How do I get mouse coordinates on Fabric.js?

StAR_161 picture StAR_161 · Dec 9, 2013 · Viewed 15.3k times · Source

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

StAR_161 picture StAR_161 · Nov 5, 2014

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
}