CANVAS = window._canvas = new fabric.Canvas('c');
let rect = new fabric.Rect({
left: 100,
top: 100,
width: 200,
height: 100,
originX: 'center',
originY: 'center',
fill: 'blue'
});
CANVAS.add(rect);
// Rotate 90 degrees when 'Alt' is pressed
window.addEventListener("keydown", eventListenerKeydown);
function eventListenerKeydown(evt) {
if (evt.key == "Alt") {
// Saving pre-rotation [x,y] to use in bugFix()
rect._left = rect.left;
rect._top = rect.top;
rect.rotate(rect.angle + 90);
bugFix();
CANVAS.renderAll();
evt.preventDefault();
}
}
// Update the current transform used in dragging the object to account for the rotation
// Determined by code in _setupCurrentTransform()
function bugFix() {
if (CANVAS._currentTransform?.target == rect) {
// [x,y] shift due to rotate
let diffLeft = rect._left - rect.left;
let diffTop = rect._top - rect.top;
// Original [x,y] upon piece being picked up
let origLeft = CANVAS._currentTransform.ex - CANVAS._currentTransform.offsetX;
let origTop = CANVAS._currentTransform.ey - CANVAS._currentTransform.offsetY;
// Recalc the original offsets accounting for the rotation difference
CANVAS._currentTransform.offsetX = CANVAS._currentTransform.ex - (origLeft - diffLeft);
CANVAS._currentTransform.offsetY = CANVAS._currentTransform.ey - (origTop - diffTop);
// Update the angle
CANVAS._currentTransform.theta = fabric.util.degreesToRadians(CANVAS._currentTransform.target.angle);
}
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.