Skip to content

Commit

Permalink
draw: constrain object move with Shift
Browse files Browse the repository at this point in the history
Issue #7933

Signed-off-by: Hubert Figuière <[email protected]>
Change-Id: Ie39c15a8552276bc5919ffbbd5dd859695048b9c
  • Loading branch information
hfiguiere authored and Minion3665 committed Jan 11, 2024
1 parent d31134f commit 10c1365
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions browser/src/layer/vector/Path.Drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,54 @@ L.Handler.PathDrag = L.Handler.extend(/** @lends L.Path.Drag.prototype */ {
x = this._startPoint.x + dx;
y = this._startPoint.y + dy;
}
} if (evt.shiftKey) {
var angleOf = function (x, y) {
var angle;
if (x != 0) {
angle = Math.atan(y / Math.abs(x));
} else if (y > 0) {
angle = Math.PI / 2;
} else {
angle = - Math.PI / 2;
}
if (x < 0) {
angle = Math.PI - angle;
}
return angle;
};
/* Near mean within +/- PI / 8 since we do diagonals */
var angleNear = function (angle, near) {
return Math.abs(near - angle) < (Math.PI / 8);
};

var dsx = x - this._dragStartPoint.x;
var dsy = y - this._dragStartPoint.y;
var angle = angleOf(dsx, dsy);
console.log('Drag started: Shift is on', this._dragStartPoint.x, this._dragStartPoint.y);
console.log('Drag: Shift is on', dsx, dsy, angle / (Math.PI / 180));
if (angleNear(angle, 0) || angleNear(angle, Math.PI)) {
dy = -this._matrix[5];
y = this._startPoint.y;
} else if (angleNear(angle, Math.PI / 2) || angleNear(angle, -Math.PI / 2)) {
dx = -this._matrix[4];
x = this._startPoint.x;
} else {
console.log('neither');
var delta = (Math.abs(dsx) + Math.abs(dsy)) / 2;
if (angleNear(angle, Math.PI / 4)) {
dx = -this._matrix[4] + delta;
dy = -this._matrix[5] + delta;
} else if (angleNear(angle, -Math.PI / 4)) {
dx = -this._matrix[4] + delta;
dy = -this._matrix[5] - delta;
} else if (angleNear(angle, Math.PI * 0.75)) {
dx = -this._matrix[4] - delta;
dy = -this._matrix[5] + delta;
} else if (angleNear(angle, -Math.PI * 0.75)) {
dx = -this._matrix[4] - delta;
dy = -this._matrix[5] - delta;
}
}
}

// Send events only if point was moved
Expand Down

0 comments on commit 10c1365

Please sign in to comment.