javascript - Updating fabricjs object coordinates while moving - Stack Overflow
I'm trying to get the x and y coordinates of the object while its moving. I use getLeft()
and getTop()
methods on object:moving
. But these methods don't help if the object is rotated.
Then I tried getting the top and left of the bounding box of the object using object.getBoundRect().top
. But this doesn't get updated dynamically. It gives the bounding box value at the beginning of move operation. Is there a way to get the bounding box value while moving?
canvas.on('object:moving', function(e) {
var scaledObject = e.target;
$('#mouse-info').text("X:"+parseInt(scaledObject.getBoundingRect().left)+", Y:"+parseInt(scaledObject.getBoundingRect().top));
});
I'm trying to get the x and y coordinates of the object while its moving. I use getLeft()
and getTop()
methods on object:moving
. But these methods don't help if the object is rotated.
Then I tried getting the top and left of the bounding box of the object using object.getBoundRect().top
. But this doesn't get updated dynamically. It gives the bounding box value at the beginning of move operation. Is there a way to get the bounding box value while moving?
canvas.on('object:moving', function(e) {
var scaledObject = e.target;
$('#mouse-info').text("X:"+parseInt(scaledObject.getBoundingRect().left)+", Y:"+parseInt(scaledObject.getBoundingRect().top));
});
Share
Improve this question
edited Feb 21, 2017 at 17:16
Chris Hamilton
5555 silver badges22 bronze badges
asked Feb 21, 2017 at 17:12
AshTysonAshTyson
4937 silver badges24 bronze badges
2 Answers
Reset to default 4As of Fabric.js version 2.4.4, the getLeft()
and getTop()
methods have been removed from the Fabric Object class. The properties top and left are updated on the object:moving
event, so you can access them directly; however, these properties reflect the aCoords
, or Absolute Coordinates, of the Fabric Object without any regard to how the zoom value of the canvas changes that Object's coordinates.
The oCoords
take into account a Fabric Object's position including that Object's viewportTransform
and padding
properties.
Despite the fact that the Fabric.js documentation currently claims, "You can calculate [the oCoords
] without updating with @method calcCoords," the oCoords
do not in fact appear to be updated by the object:moving
event.
You therefore need to call the calcCoords
method on the object returned by the event in order to get coordinates which account for the canvas zoom value and object padding.
Here is a working solution:
fabricCanvas.on("object:moving", function(e) {
var actObj = e.target;
var coords = actObj.calcCoords();
// calcCoords returns an object of corner points like this
//{bl:{x:val, y:val},tl:{x:val, y:val},br:{x:val, y:val},tr:{x:val, y:val}}
var left = coords.tl.x;
var top = coords.tl.y;
return {left:left,top:top};
})
Hope this helps!
Sammy,
You are doing something wrong. getTop and getLeft work fine for version 1.7
Check this code:
canvas.on('object:moving',function(e){
if (e.target){
console.log(e.target.getTop());
console.log(e.target.getLeft());
}
});
UPDATE:
If you want to have with rotating you have to do after each render:
canvas.on('after:render', function() {
canvas.contextContainer.strokeStyle = '#555';
canvas.forEachObject(function(obj) {
var bound = obj.getBoundingRect();
canvas.contextContainer.strokeRect(
bound.left + 0.5,
bound.top + 0.5,
bound.width,
bound.height
);
});
var ao = canvas.getActiveObject();
if (ao) {
var bound = ao.getBoundingRect();
console.log(bound.left);
console.log(bound.top);
}
});
This code will draw for you bounding box for each shape after each render. If you want to hide bounding box just delete this code:
canvas.contextContainer.strokeStyle = '#555';
canvas.forEachObject(function(obj) {
var bound = obj.getBoundingRect();
canvas.contextContainer.strokeRect(
bound.left + 0.5,
bound.top + 0.5,
bound.width,
bound.height
);
});
Here is a fiddle
Hopefully, it will help you.
- 2020年世界信息安全大会将于11月24日在蓉举办
- reactjs - The difference between @Vite4 and @Vite5 - Stack Overflow
- spi - Interrupt safety of `HAL_SPI_TransmitReceive` - Stack Overflow
- javascript - How to check if a track is already added to a WebRTC peer connection before adding it? - Stack Overflow
- node.js - Mongoose schema worked with the main db but not with test db - Stack Overflow
- exception - codeceptjs 3 run-workers with 11 worker threads encounter session out or time out problem - Stack Overflow
- mip sdk - Can you use the mip sdk to apply a mpip sensitivity label to a .json? - Stack Overflow
- Parsing Swift Predicates into Database Statements with PredicateExpressions - Stack Overflow
- c++ - GLOG - flags.cc' is being linked both statically and dynamically into this executable - Stack Overflow
- javascript - PHP Cross-Domain POST Request - Session Cookie Not Persisting After Redirect - Stack Overflow
- x86 - how does internal functions of kernel resolve after paging? - Stack Overflow
- macos - Restriction of media fetched with API in browser in iOS and Mac systems - Stack Overflow
- reactjs - How to change output directory of wxt extension framework - Stack Overflow
- Symfony 7 - Autocomplete form field - Stack Overflow
- javascript - Use HTML5 canvas 2d API with WebGL - Stack Overflow
- Rust error: lifetime may not live long enough, how to express lifetimes? - Stack Overflow
- flutter - How to write LOGs in an APP made in fletpython, which appear in the DDMS? - Stack Overflow