Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/studio/src/components/editor/resizeDraft.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import type { GestureState } from "./domEditOverlayGestures";
import { resizeRemainderShift } from "./domEditResizeLocal";
import { sealedResizeAnchor } from "./resizeDraft";

const gesture = (resizeGrow?: GestureState["resizeGrow"]) =>
Expand All @@ -8,6 +9,7 @@ const gesture = (resizeGrow?: GestureState["resizeGrow"]) =>
actualHeight: 160,
editScaleX: 0.5,
editScaleY: 0.5,
...{ actualRotation: 0, centerX: 0, centerY: 0, startX: 60, startY: 40 },
resizeGrow,
}) as GestureState;
const written = { width: 340, height: 220 };
Expand All @@ -24,3 +26,27 @@ describe("a sealed resize's centre pin", () => {
expect(sealedResizeAnchor(gesture(), written)).toEqual({ dx: -25, dy: -15 });
});
});

// fails without the fix: the sealed pin ignored the wanted size, so the grabbed corner sat up to half a px short of the
// pointer where an unsealed draft, which measures the corners, shifts the box by half the rounding remainder.
it("moves a sealed box by the same rounding remainder an unsealed draft measures", () => {
const angle = 30 * (Math.PI / 180);
const g = {
...gesture(),
...{ actualWidth: 300, actualHeight: 150, editScaleX: 1.5, editScaleY: 1.5 },
...{ actualRotation: 30, centerX: 400, centerY: 300, startX: 680, startY: 500 },
} as GestureState;
const sizes = { wanted: { width: 300.6, height: 150.3 }, written: { width: 301, height: 150 } };
const at = (u: number, v: number) => ({
x: 400 + 1.5 * (u * Math.cos(angle) - v * Math.sin(angle)),
y: 300 + 1.5 * (u * Math.sin(angle) + v * Math.cos(angle)),
});
const [w, h] = [301 / 2, 150 / 2];
const measured = { nw: at(-w, -h), ne: at(w, -h), sw: at(-w, h), se: at(w, h) };
const unsealed = resizeRemainderShift(measured, { x: 280, y: 200 }, sizes);
const pinned = sealedResizeAnchor(g, sizes.written, sizes.wanted);
const plain = sealedResizeAnchor(g, sizes.written);
expect(Math.hypot(unsealed.x, unsealed.y)).toBeGreaterThan(0.1);
expect(Math.abs(pinned.dx - plain.dx - unsealed.x)).toBeLessThan(0.07);
expect(Math.abs(pinned.dy - plain.dy - unsealed.y)).toBeLessThan(0.07);
});
41 changes: 31 additions & 10 deletions packages/studio/src/components/editor/resizeDraft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ function resolveResizeAnchor(
): { dx: number; dy: number } {
const fixedStart = g.resizeFixedCenterStart;
if (corners && fixedStart) {
const press = g.resizePressFromCorner ?? { x: 0, y: 0 };
const grab = { x: g.startX - press.x - g.centerX, y: g.startY - press.y - g.centerY };
const shift = resizeRemainderShift(corners, grab, sizes);
const shift = resizeRemainderShift(corners, grabOf(g), sizes);
const target = { x: fixedStart.x + shift.x, y: fixedStart.y + shift.y };
return computeNextResizeAnchor(g.lastResizeAnchor, target, overlayCornersCentroid(corners));
}
Expand Down Expand Up @@ -83,34 +81,57 @@ function resolveAnchoredResizeDraft(
);
}

/** A frame Studio cannot measure: the box grows about its centre by the size the draft wrote. */
function grabOf(g: GestureState): { x: number; y: number } {
const press = g.resizePressFromCorner ?? { x: 0, y: 0 };
return { x: g.startX - press.x - g.centerX, y: g.startY - press.y - g.centerY };
}

/** The box's corners at the written size from the gesture's own geometry, for a frame Studio cannot measure. */
function gestureCorners(
g: GestureState,
written: { width: number; height: number },
): NonNullable<Corners> {
const rad = (g.actualRotation * Math.PI) / 180;
const [w, h] = [(written.width * g.editScaleX) / 2, (written.height * g.editScaleY) / 2];
const at = (u: number, v: number) => ({
x: g.centerX + u * Math.cos(rad) - v * Math.sin(rad),
y: g.centerY + u * Math.sin(rad) + v * Math.cos(rad),
});
return { nw: at(-w, -h), ne: at(w, -h), sw: at(-w, h), se: at(w, h) };
}

/** The pin that keeps a sealed resize's centre still: back by what the frame measured the drawn size moving it. */
export function sealedResizeAnchor(
g: GestureState,
written: { width: number; height: number },
wanted = written,
): { dx: number; dy: number } {
const [w, h] = [g.actualWidth - written.width, g.actualHeight - written.height];
const grow = g.resizeGrow;
if (!grow) return { dx: (w / 2) * g.editScaleX, dy: (h / 2) * g.editScaleY };
return { dx: grow.a * w + grow.c * h, dy: grow.b * w + grow.d * h };
const shift = resizeRemainderShift(gestureCorners(g, written), grabOf(g), { wanted, written });
if (!grow) return { dx: (w / 2) * g.editScaleX + shift.x, dy: (h / 2) * g.editScaleY + shift.y };
return { dx: grow.a * w + grow.c * h + shift.x, dy: grow.b * w + grow.d * h + shift.y };
}

function resolveUnmeasuredResizeDraft(
g: GestureState,
element: HTMLElement,
{ written }: ResizeDraftSizes,
sizes: ResizeDraftSizes,
): OverlayRect {
const { written } = sizes;
const width = (g.originWidth * written.width) / g.actualWidth;
const height = (g.originHeight * written.height) / g.actualHeight;
let shift = { x: 0, y: 0 };
if (g.pathOffsetMember) {
const anchor = sealedResizeAnchor(g, written);
const anchor = sealedResizeAnchor(g, written, sizes.wanted);
shift = resizeRemainderShift(gestureCorners(g, written), grabOf(g), sizes);
g.lastResizeAnchor = anchor;
applyManualOffsetDragDraft(g.pathOffsetMember, anchor.dx, anchor.dy);
}
drawPreviewDraft(element, { size: written });
return {
left: g.originLeft + (g.originWidth - width) / 2,
top: g.originTop + (g.originHeight - height) / 2,
left: g.originLeft + (g.originWidth - width) / 2 + shift.x,
top: g.originTop + (g.originHeight - height) / 2 + shift.y,
width,
height,
editScaleX: g.editScaleX,
Expand Down
Loading