From fb7d54cb3a86d5f1c1c1f8deda3741a50ed25630 Mon Sep 17 00:00:00 2001 From: William Moore Date: Wed, 1 May 2024 09:45:26 +0100 Subject: [PATCH] Try to handle Rectangle rotation when resizing --- src/js/shape_editor/rect.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/js/shape_editor/rect.js b/src/js/shape_editor/rect.js index 0842d0d2c..ce9af2549 100644 --- a/src/js/shape_editor/rect.js +++ b/src/js/shape_editor/rect.js @@ -301,6 +301,22 @@ Rect.prototype.getHandleCoords = function getHandleCoords() { return handleIds; }; +function correct_rotation(dx, dy, rotation) { + if (dx === 0 && dy === 0) { + return {x: dx, y: dy}; + } + var length = Math.sqrt(dx * dx + dy * dy), + ang1 = Math.atan(dy/dx); + if (dx < 0) { + ang1 = Math.PI + ang1; + } + var angr = rotation * (Math.PI/180), // deg -> rad + ang2 = ang1 - angr; + dx = Math.cos(ang2) * length; + dy = Math.sin(ang2) * length; + return {x: dx, y: dy}; +} + // ---- Create Handles ----- Rect.prototype.createHandles = function createHandles() { var self = this, @@ -320,6 +336,12 @@ Rect.prototype.createHandles = function createHandles() { return function (dx, dy, mouseX, mouseY, event) { dx = dx / self._zoomFraction; dy = dy / self._zoomFraction; + // need to handle rotation... + if (self._rotation != 0) { + let xy = correct_rotation(dx, dy, self._rotation); + dx = xy.x; + dy = xy.y; + } // If drag on corner handle, retain aspect ratio. dx/dy = aspect var keep_ratio = self.fixed_ratio || event.shiftKey;