gpt4 book ai didi

javascript - 在 Fabric 对象移动事件中获取 x/y 偏移

转载 作者:行者123 更新时间:2023-12-01 00:05:51 26 4
gpt4 key购买 nike

在此jsFiddle我有一个可以拖动的 Fabric 矩形。我需要知道的是矩形被水平(X)和垂直(Y)拖动了多少。

我正在捕捉移动事件,但很难找到 X 和 Y 的偏移。如何打印从事件中获取的 X/Y 测量值?请注意,初始位置为 x=50,y=50。如果我移动矩形,然后停止然后再次移动,则移位应该相对于 x=50,y=50。

var canvas = new fabric.Canvas('c');

var coords = new fabric.Textbox("not moved yet", {
fontSize: 16,
width: 300
});

var text = new fabric.Textbox("Drag me", {
left: 50,
top: 50,
width: 100,
backgroundColor: 'yellow',
});

canvas.add(coords,text);

canvas.on('object:moving', function(e) {
console.log(e);
var x = e.target.transform.ex;
var y = e.target.transform.ey;
coords.text = 'Moved x = ' + x + ', y = ' + y;
});

最佳答案

您可以自己跟踪更改,只需将初始坐标保存到单独的变量中即可。

let lastX = text.left
let lastY = text.top

canvas.on('object:moving', function (e) {
const diffX = e.target.left - lastX
const diffY = e.target.top - lastY
coords.text = 'Moved x = ' + diffX + ', y = ' + diffY
})

关于javascript - 在 Fabric 对象移动事件中获取 x/y 偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60402612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com