From c67435719f6d2ba0f5f5b392ab78d95336a6f3cd Mon Sep 17 00:00:00 2001 From: Guillermo Peralta Scura Date: Sun, 12 Jan 2020 14:05:08 -0300 Subject: [PATCH] Use cursor delta to resize shape (#341) --- src/index.tsx | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 59b07a90..ef13910f 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -636,36 +636,42 @@ export class App extends React.Component<{}, AppState> { if (selectedElements.length === 1) { const { x, y } = viewportCoordsToSceneCoords(e, this.state); + let deltaX = 0; + let deltaY = 0; selectedElements.forEach(element => { switch (resizeHandle) { case "nw": - element.width += element.x - lastX; - element.x = lastX; + deltaX = lastX - x; + element.width += deltaX; + element.x -= deltaX; if (e.shiftKey) { element.y += element.height - element.width; element.height = element.width; } else { - element.height += element.y - lastY; - element.y = lastY; + const deltaY = lastY - y; + element.height += deltaY; + element.y -= deltaY; } break; case "ne": - element.width = lastX - element.x; + element.width += x - lastX; if (e.shiftKey) { element.y += element.height - element.width; element.height = element.width; } else { - element.height += element.y - lastY; - element.y = lastY; + deltaY = lastY - y; + element.height += deltaY; + element.y -= deltaY; } break; case "sw": - element.width += element.x - lastX; - element.x = lastX; + deltaX = lastX - x; + element.width += deltaX; + element.x -= deltaX; if (e.shiftKey) { element.height = element.width; } else { - element.height = lastY - element.y; + element.height += y - lastY; } break; case "se": @@ -677,18 +683,20 @@ export class App extends React.Component<{}, AppState> { } break; case "n": - element.height += element.y - lastY; - element.y = lastY; + deltaY = lastY - y; + element.height += deltaY; + element.y -= deltaY; break; case "w": - element.width += element.x - lastX; - element.x = lastX; + deltaX = lastX - x; + element.width += deltaX; + element.x -= deltaX; break; case "s": - element.height = lastY - element.y; + element.height += y - lastY; break; case "e": - element.width = lastX - element.x; + element.width += x - lastX; break; }