[英]How do I fire fabric.js 'object: moving' event when object is being dragged on a canvas?
I am currently developing an application using the fabric.js library where I draw a rectangle object on a fabric.js canvas which has an image background set on it. 我目前正在使用fabric.js库开发一个应用程序,在该库中,我在fabric.js画布上绘制了一个矩形对象,并在画布上设置了图像背景。 I can draw the rectangle but then when I click on the rectangle to move it, the 'object:moving' event does not get fired at all and a new rectangle gets drawn while the current selected one is being moved.
我可以绘制矩形,但是当我单击矩形将其移动时,根本不会触发'object:moving'事件,并且在移动当前选定的矩形时会绘制一个新的矩形。 Is there a way to stop drawing a new rectangle object while moving the current selected one?
有没有一种方法可以在移动当前选定的矩形对象时停止绘制新的矩形对象? Below is my JavaScript code.$scope.c is the canvas variable in scope.
下面是我的JavaScript代码。$ scope.c是作用域中的canvas变量。
$scope.rect = new fabric.Rect({
top: $scope.origY,
left: $scope.origX,
width: 0,
height: 0,
fill: 'gold',
stroke: 'red',
strokewidth: 4.5,
opacity: 0.5,
name: 'UserArea_'
});
$scope.c.add($scope.rect);
});
$scope.c.on('mouse:move', function(o) {
if (!$scope.isDown) {
return
};
var pointer = $scope.c.getPointer(o.e);
if ($scope.origX > pointer.x) {
$scope.rect.set({
left: Math.abs(pointer.x)
});
}
if ($scope.origY > pointer.y) {
$scope.rect.set({
top: Math.abs(pointer.y)
});
}
$scope.rect.set({
width: Math.abs($scope.origX - pointer.x)
});
$scope.rect.set({
height: Math.abs($scope.origY - pointer.y)
});
$scope.rect.setCoords();
});
$scope.c.on('mouse:up', function(o) {
$scope.isDown = false;
});
$scope.c.on('object:selected', function(e) {
var activeObj = $scope.c.getActiveObject(); {
console.log(activeObj.width);
}
});
$scope.c.on('object: moving', function(e) {
console.log('object is moving');
});
You have a space inside event name 'object: moving', should be: 事件名称“ object:moving”内有一个空格,应为:
$scope.c.on('object:moving', function(e) {
console.log('object is moving');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.