我正在尝试读取Fabric.js上鼠标点击的X坐标。
这是我的代码。控制台每次都会记录undefined
。
var canvas = new fabric.Canvas('c1');
canvas.on('mouse:down', function(e){
getMouse(e);
});
function getMouse(e) {
console.log(e.clientX);
}
答案 0 :(得分:25)
最佳解决方法是this方法
实现:
function getMouseCoords(event)
{
var pointer = canvas.getPointer(event.e);
var posX = pointer.x;
var posY = pointer.y;
console.log(posX+", "+posY); // Log to console
}
答案 1 :(得分:6)
要根据画布本身的设置宽度和高度获取坐标,请使用layerX和layerY属性。
canvas.on('mouse:move', function(options) {
console.log(options.e.layerX, options.e.layerY);
});
答案 2 :(得分:4)
试试这个,
function getMouse(e) {
console.log(e.e.clientX);
}
已更新,,因为canvas events
将options
视为argument
而不是an event
,
canvas.on('mouse:down', function(options){
getMouse(options);// its not an event its options of your canvas object
});
function getMouse(options) {
console.log(options);// you can check all options here
console.log(options.e.clientX);
}
答案 3 :(得分:0)
也许这会有所帮助:
//Convert To Local Point
function toLocalPoint(event, canvas) {
var offset = fabric.util.getElementOffset(canvas.lowerCanvasEl), localX = event.e.clientX - offset.left, localY = event.e.clientY - offset.top;
return new fabric.Point(localX, localY);
}
答案 4 :(得分:-1)
只需使用e.e.clientX
或
e.e.clientY
获取职位