Title: Custom Bounding Box Follows Rotation But Not After Deselection and Reselection in Fabric.js
Description: Hello fellow developers,
I'm working on a project using Fabric.js, and I'm facing an issue with creating a custom bounding box (ruler) around an object. Here are the specifics:
- Initially, I tried using lines as rulers, but they didn't sync with the object during rotations.
- Now, I've switched to using a rectangle around the object, which works well during rotation.
- However, after deselecting the item and then reselecting it (especially when it's already been rotated), the ruler dimensions are not accurate. The ruler doesn't follow the rectangle's position correctly.
Additional Details:
- You can see the attached image for reference (unfortunately, I cannot directly share the image here).
- Here's the relevant code snippet I'm using for the custom bounding box:
export class CustomFabricCanvas extends fabric.Canvas {
constructor(element, options) {
super(element, options);
this.rulerLine = null;
this.on("selection:created", this.onSelectionCreated.bind(this));
this.on("selection:cleared", this.onSelectionCleared.bind(this));
this.on("object:scaling", this.onObjectScaling.bind(this));
this.on("object:moving", this.onObjectMoving.bind(this));
this.on("object:rotating", this.onObjectRotating.bind(this));
}
onSelectionCreated(event) {
const activeObject = event.selected;
if (activeObject?.length > 0) {
activeObject.forEach((obj) => {
this.showRuler({ ...obj });
});
}
}
onSelectionCleared() {
this.hideRuler();
}
showRuler(options) {
if (!this.rulerLine) {
this.rulerLine = new fabric.Rect({
left: options.left + 50, //
top: options.top + 50, // +
height: options.height + 10,
width: options.width + 10,
stroke: "black",
strokeWidth: 1,
fill: "transparent",
selectable: false,
originX: "center",
originY: "center",
angle: options.angle,
});
this.add(this.rulerLine);
this.renderAll();
}
}
hideRuler() {
if (this.rulerLine) {
this.remove(this.rulerLine);
this.rulerLine = null;
this.renderAll();
}
}
onObjectRotating(e) {
const target = e.target;
console.log(e.target);
this.rulerLine.set({
angle: target.angle,
});
this.renderAll();
}
}
Request for Help: I need assistance with implementing a solution that ensures the ruler remains proportionate to the object's position even after deselection and reselection. How can I make the rectangle-based ruler persist correctly?
Image Ref:
- after rotation

- before rotation

I attempted to create a custom bounding box (ruler) around an object in Fabric.js. Initially, I used lines as rulers, but they didn’t sync with the object during rotations. Now, I’ve switched to using a rectangle around the object, which works well during rotation.
However, after deselecting the item and then reselecting it (especially when it’s already been rotated), the ruler dimensions are not accurate. The ruler doesn’t follow the rectangle’s position correctly.
I expected the ruler to consistently adjust with the object’s position, even after deselection and reselection.
Your problem is best seen if you visualize it real time ...
See my sample code below (mostly your code), I changed the function
onObjectRotatingto also set the left and top, that way we can see what you are doing when the rotation event not only at select.if you want to see a different behavior try without the:
Here is how that visually looks like