Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(editor): add canvas element dragging with snapping guides

Richie McIlroye12d30b7e364
diff --git a/apps/desktop/src/routes/editor/CanvasElementsOverlay.tsx b/apps/desktop/src/routes/editor/CanvasElementsOverlay.tsx
new file mode 100644
index 000000000..91401884b
--- /dev/null
+++ b/apps/desktop/src/routes/editor/CanvasElementsOverlay.tsx
@@ -0,0 +1,793 @@
+import {
+ createEventListener,
+ createEventListenerMap,
+} from "@solid-primitives/event-listener";
+import { throttle } from "@solid-primitives/scheduled";
+import { cx } from "cva";
+import {
+ batch,
+ createEffect,
+ createMemo,
+ createRoot,
+ createSignal,
+ For,
+ on,
+ Show,
+} from "solid-js";
+import type { FrameLayoutEvent } from "~/utils/tauri";
+import { FPS, useEditorContext } from "./context";
+import {
+ buildSnapTargets,
+ type NormRect,
+ SNAP_PX,
+ type SnapTargets,
+ snapMovingRect,
+ snapResizeCorner,
+} from "./snapping";
+
+// In-canvas move/resize controls for the screen recording (display) and the
+// camera. Boxes track the exact rects the renderer reported for the latest
+// frame (FrameLayoutEvent), so they always match the pixels on screen; drags
+// write normalized centers back to the project config, which both the preview
+// and the exporter consume through the same layout code.
+
+type Size = { width: number; height: number };
+
+const clamp = (value: number, min: number, max: number) =>
+ min > max ? (min + max) / 2 : Math.min(Math.max(value, min), max);
+
+function normRect(
+ bounds: [number, number, number, number],
+ layout: FrameLayoutEvent,
+): NormRect {
+ return {
+ x: bounds[0] / layout.output_width,
+ y: bounds[1] / layout.output_height,
+ w: (bounds[2] - bounds[0]) / layout.output_width,
+ h: (bounds[3] - bounds[1]) / layout.output_height,
+ };
+}
+
+// The renderer's classic camera inset (CAMERA_PADDING px at 1080p), exposed
+// as snap lines so a freely-dragged camera can land back on the stock look.
+function classicMargin(layout: FrameLayoutEvent) {
+ const padPx = 50 * (layout.output_height / 1080);
+ return { x: padPx / layout.output_width, y: padPx / layout.output_height };
+}
+
+type SnapExclude =
+ | "display"
+ | "camera"
+ | { text: number }
+ | { mask: number }
+ | null;
+
+/**
+ * Snap-target factory shared by every preview overlay: frame edges/center,
+ * the classic camera margin, and the other visible elements' edges/centers,
+ * excluding whichever element is being dragged.
+ */
+export function useCanvasSnapTargets() {
+ const { project, editorState, latestFrameLayout } = useEditorContext();
+ const time = () => editorState.previewTime ?? editorState.playbackTime ?? 0;
+
+ return (exclude: SnapExclude): SnapTargets => {
+ const rects: NormRect[] = [];
+ const layout = latestFrameLayout();
+
+ if (layout) {
+ if (exclude !== "display") rects.push(normRect(layout.display, layout));
+ if (layout.camera && exclude !== "camera")
+ rects.push(normRect(layout.camera, layout));
+ }
+
+ const t = time();
+ project.timeline?.textSegments?.forEach((segment, index) => {
+ if (
+ typeof exclude === "object" &&
+ exclude !== null &&
+ "text" in exclude &&
+ exclude.text === index
+ )
+ return;
+ if (!(t >= segment.start && t < segment.end)) return;
+ if (segment.enabled === false) return;
+ const center = segment.center ?? { x: 0.5, y: 0.5 };
+ const size = segment.size ?? { x: 0.35, y: 0.2 };
+ rects.push({
+ x: center.x - size.x / 2,
+ y: center.y - size.y / 2,
+ w: size.x,
+ h: size.y,
+ });
+ });
+ project.timeline?.maskSegments?.forEach((segment, index) => {
+ if (
+ typeof exclude === "object" &&
+ exclude !== null &&
+ "mask" in exclude &&
+ exclude.mask === index
+ )
+ return;
+ if (!(t >= segment.start && t < segment.end)) return;
+ rects.push({
+ x: segment.center.x - segment.size.x / 2,
+ y: segment.center.y - segment.size.y / 2,
+ w: segment.size.x,
+ h: segment.size.y,
+ });
+ });
+
+ return buildSnapTargets(rects, {
+ margin: layout ? classicMargin(layout) : undefined,
+ });
+ };
+}
+
+/** Renders the active smart-guide lines. Mounted above all other overlays. */
+export function SnapGuidesOverlay(props: { size: Size }) {
+ const { snapGuides } = useEditorContext();
+ return (
+ <div class="overflow-hidden absolute inset-0 pointer-events-none">
+ <For each={snapGuides()}>
+ {(guide) => (
+ <div
+ class="absolute bg-[#FF3B6B]"
+ style={
+ guide.axis === "v"
+ ? {
+ left: `${guide.pos * props.size.width}px`,
+ top: `${guide.start * props.size.height}px`,
+ width: "1px",
+ height: `${(guide.end - guide.start) * props.size.height}px`,
+ }
+ : {
+ top: `${guide.pos * props.size.height}px`,
+ left: `${guide.start * props.size.width}px`,
+ height: "1px",
+ width: `${(guide.end - guide.start) * props.size.width}px`,
+ }
+ }
+ />
+ )}
+ </For>
+ </div>
+ );
+}
+
+export function CanvasElementsOverlay(props: { size: Size }) {
+ const {
+ project,
+ setProject,
+ editorState,
+ setEditorState,
+ projectHistory,
+ latestFrameLayout,
+ setSnapGuides,
+ } = useEditorContext();
+
+ const snapTargetsFor = useCanvasSnapTargets();
+
+ const time = () => editorState.previewTime ?? editorState.playbackTime ?? 0;
+
+ const sceneModeAt = (t: number) =>
+ project.timeline?.sceneSegments?.find((s) => t >= s.start && t < s.end)
+ ?.mode ?? "default";
+
+ const zoomActive = () => {
+ const t = time();
+ return (project.timeline?.zoomSegments ?? []).some(
+ (s) => t >= s.start && t < s.end,
+ );
+ };
+
+ // Optimistic rects follow the pointer at input rate during a drag; the
+ // video catches up a frame later. Cleared once the next rendered layout
+ // arrives so the box never flashes back to a stale position.
+ const [dragRects, setDragRects] = createSignal<{
+ display?: NormRect;
+ camera?: NormRect;
+ } | null>(null);
+ let dragging = false;
+
+ createEffect(
+ on(
+ latestFrameLayout,
+ () => {
+ if (!dragging) setDragRects(null);
+ },
+ { defer: true },
+ ),
+ );
+
+ const displayRect = createMemo<NormRect | null>(() => {
+ const optimistic = dragRects()?.display;
+ if (optimistic) return optimistic;
+ const layout = latestFrameLayout();
+ return layout ? normRect(layout.display, layout) : null;
+ });
+
+ const cameraRect = createMemo<NormRect | null>(() => {
+ const optimistic = dragRects()?.camera;
+ if (optimistic) return optimistic;
+ const layout = latestFrameLayout();
+ return layout?.camera ? normRect(layout.camera, layout) : null;
+ });
+
+ const overlayVisible = () => !editorState.playing && !!latestFrameLayout();
+ const paneScene = () => {
+ const mode = sceneModeAt(time());
+ return mode === "splitScreen" || mode === "floating";
+ };
+ const showDisplay = () =>
+ overlayVisible() && !paneScene() && sceneModeAt(time()) !== "cameraOnly";
+ const showCamera = () => overlayVisible() && !paneScene();
+ // The rendered display rect is zoom-transformed while a zoom segment is
+ // active, but drags write base-layout config — lock it to avoid a
+ // mismatched pointer feel. Camera placement is not zoom-transformed.
+ const displayDraggable = () => !zoomActive();
+ const cameraResizable = () => !zoomActive();
+
+ const selection = () => editorState.canvasSelection;
+ const select = (type: "display" | "camera") =>
+ batch(() => {
+ setEditorState("canvasSelection", { type });
+ setEditorState("timeline", "selection", null);
+ });
+
+ // Selecting a timeline segment drops the canvas selection (and vice versa
+ // in `select`), so only one selection UI is active at a time.
+ createEffect(
+ on(
+ () => editorState.timeline.selection,
+ (timelineSelection) => {
+ if (timelineSelection) setEditorState("canvasSelection", null);
+ },
+ { defer: true },
+ ),
+ );
+
+ const thresholds = () => ({
+ x: SNAP_PX / Math.max(props.size.width, 1),
+ y: SNAP_PX / Math.max(props.size.height, 1),
+ });
+
+ function createCanvasDrag<T extends { moved: boolean }>(
+ setup: () => T | null,
+ update: (
+ e: MouseEvent,
+ value: T,
+ initialMouse: { x: number; y: number },
+ ) => void,
+ ) {
+ return (downEvent: MouseEvent) => {
+ if (downEvent.button !== 0) return;
+ const initial = setup();
+ if (!initial) return;
+ const state = initial;
+
+ downEvent.preventDefault();
+ downEvent.stopPropagation();
+
+ const initialMouse = { x: downEvent.clientX, y: downEvent.clientY };
+ const resumeHistory = projectHistory.pause();
+ dragging = true;
+
+ function handleUpdate(event: MouseEvent) {
+ // A plain click must not write config (it would e.g. convert a
+ // preset camera position into a manual one).
+ if (
+ !state.moved &&
+ Math.hypot(
+ event.clientX - initialMouse.x,
+ event.clientY - initialMouse.y,
+ ) < 2
+ )
+ return;
+ state.moved = true;
+ update(event, state, initialMouse);
+ }
+
+ const throttledUpdate = throttle(handleUpdate, 1000 / FPS);
+
+ function finish(finalEvent: MouseEvent) {
+ throttledUpdate.clear();
+ handleUpdate(finalEvent);
+ resumeHistory();
+ setSnapGuides([]);
+ dragging = false;
+ dispose();
+ }
+
+ const dispose = createRoot((dispose) => {
+ createEventListenerMap(window, {
+ mousemove: throttledUpdate,
+ mouseup: finish,
+ });
+ return dispose;
+ });
+ };
+ }
+
+ const moveHandler = (element: "display" | "camera") =>
+ createCanvasDrag(
+ () => {
+ const rect = element === "display" ? displayRect() : cameraRect();
+ if (!rect) return null;
+ if (element === "display" && !displayDraggable()) return null;
+ return { rect, targets: snapTargetsFor(element), moved: false };
+ },
+ (e, state, initialMouse) => {
+ const raw = {
+ ...state.rect,
+ x: state.rect.x + (e.clientX - initialMouse.x) / props.size.width,
+ y: state.rect.y + (e.clientY - initialMouse.y) / props.size.height,
+ };
+
+ let dx = 0;
+ let dy = 0;
+ if (e.shiftKey) {
+ setSnapGuides([]);
+ } else {
+ const snap = snapMovingRect(
+ raw,
+ state.targets,
+ thresholds().x,
+ thresholds().y,
+ );
+ dx = snap.dx;
+ dy = snap.dy;
+ setSnapGuides(snap.guides);
+ }
+
+ // The display's center may go anywhere in-frame (it can overhang
+ // edges, matching the renderer); the camera stays fully visible.
+ const isDisplay = element === "display";
+ const x = isDisplay
+ ? clamp(raw.x + dx, -raw.w / 2, 1 - raw.w / 2)
+ : clamp(raw.x + dx, 0, Math.max(0, 1 - raw.w));
+ const y = isDisplay
+ ? clamp(raw.y + dy, -raw.h / 2, 1 - raw.h / 2)
+ : clamp(raw.y + dy, 0, Math.max(0, 1 - raw.h));
+ const center = { x: x + raw.w / 2, y: y + raw.h / 2 };
+
+ setDragRects((prev) => ({ ...prev, [element]: { ...raw, x, y } }));
+ if (element === "display") {
+ setProject("background", "displayPosition", center);
+ } else {
+ setProject("camera", "manualPosition", center);
+ }
+ },
+ );
+
+ const onDisplayMove = moveHandler("display");
+ const onCameraMove = moveHandler("camera");
+
+ // Aspect-locked corner resize: derive a scale factor from the pointer's
+ // outward travel, snap the dragged corner, then map the scale onto the
+ // config field (camera.size / background.padding). The optimistic rect
+ // uses the same anchor the renderer will use, so the box and the video
+ // stay glued.
+ const resolveScale = (
+ e: MouseEvent,
+ state: { rect: NormRect; targets: SnapTargets },
+ initialMouse: { x: number; y: number },
+ dirX: 1 | -1,
+ dirY: 1 | -1,
+ anchor: { x: number; y: number },
+ ) => {
+ const dxN = (e.clientX - initialMouse.x) / props.size.width;
+ const dyN = (e.clientY - initialMouse.y) / props.size.height;
+ const outward = dxN * dirX + dyN * dirY;
+ let scale = 1 + (2 * outward) / (state.rect.w + state.rect.h);
+
+ if (e.shiftKey) {
+ setSnapGuides([]);
+ } else {
+ const corner0 = {
+ x: state.rect.x + (dirX > 0 ? state.rect.w : 0),
+ y: state.rect.y + (dirY > 0 ? state.rect.h : 0),
+ };
+ const rawCorner = {
+ x: anchor.x + (corner0.x - anchor.x) * scale,
+ y: anchor.y + (corner0.y - anchor.y) * scale,
+ };
+ const snap = snapResizeCorner(
+ rawCorner,
+ state.targets,
+ thresholds().x,
+ thresholds().y,
+ );
+ if (snap.axis === "x" && Math.abs(corner0.x - anchor.x) > 1e-6) {
+ scale = (snap.x - anchor.x) / (corner0.x - anchor.x);
+ } else if (snap.axis === "y" && Math.abs(corner0.y - anchor.y) > 1e-6) {
+ scale = (snap.y - anchor.y) / (corner0.y - anchor.y);
+ }
+ setSnapGuides(snap.guides);
+ }
+
+ return Math.max(scale, 0.05);
+ };
+
+ const cameraResizeHandler = (dirX: 1 | -1, dirY: 1 | -1) =>
+ createCanvasDrag(
+ () => {
+ const rect = cameraRect();
+ const layout = latestFrameLayout();
+ if (!rect || !layout || !cameraResizable()) return null;
+ return {
+ rect,
+ layout,
+ manual: project.camera.manualPosition,
+ enumPos: { ...project.camera.position },
+ targets: snapTargetsFor("camera"),
+ moved: false,
+ };
+ },
+ (e, state, initialMouse) => {
+ const anchor = state.manual
+ ? {
+ x: state.rect.x + state.rect.w / 2,
+ y: state.rect.y + state.rect.h / 2,
+ }
+ : {
+ // Without a manual position the renderer anchors the
+ // camera at its preset corner/edge.
+ x:
+ state.enumPos.x === "left"
+ ? state.rect.x
+ : state.enumPos.x === "center"
+ ? state.rect.x + state.rect.w / 2
+ : state.rect.x + state.rect.w,
+ y:
+ state.enumPos.y === "top"
+ ? state.rect.y
+ : state.rect.y + state.rect.h,
+ };
+ const scale = resolveScale(e, state, initialMouse, dirX, dirY, anchor);
+
+ // camera.size is a percentage of the output's min axis, applied
+ // to the camera's min dimension on top of a fixed padding.
+ const { output_width: W, output_height: H } = state.layout;
+ const minAxis = Math.min(W, H);
+ const camPad = 50 * (H / 1080);
+ const minDim0 = Math.min(state.rect.w * W, state.rect.h * H);
+ const newSize = clamp(
+ (((minDim0 * scale - camPad) / minAxis) * 100) as number,
+ 20,
+ 80,
+ );
+ setProject("camera", "size", newSize);
+
+ const applied = ((newSize / 100) * minAxis + camPad) / minDim0;
+ const w = state.rect.w * applied;
+ const h = state.rect.h * applied;
+ const x = clamp(
+ anchor.x - (anchor.x - state.rect.x) * applied,
+ 0,
+ Math.max(0, 1 - w),
+ );
+ const y = clamp(
+ anchor.y - (anchor.y - state.rect.y) * applied,
+ 0,
+ Math.max(0, 1 - h),
+ );
+ setDragRects((prev) => ({ ...prev, camera: { x, y, w, h } }));
+ },
+ );
+
+ const displayResizeHandler = (dirX: 1 | -1, dirY: 1 | -1) =>
+ createCanvasDrag(
+ () => {
+ const rect = displayRect();
+ const layout = latestFrameLayout();
+ if (!rect || !layout || !displayDraggable()) return null;
+
+ const { output_width: W, output_height: H } = layout;
+ const contentAspect = (rect.w * W) / Math.max(rect.h * H, 1e-6);
+ const frameAspect = W / H;
+ // Normalized display width at padding 0 (aspect-fit).
+ const maxWidth = Math.min(1, contentAspect / frameAspect);
+ const padding0 = project.background.padding;
+ // Two-point linear model width(padding); only affects pointer
+ // gain — the box re-anchors to the real rect every frame.
+ const gain =
+ padding0 > 0 && rect.w < maxWidth
+ ? (1 - rect.w / maxWidth) / padding0
+ : 0.0044;
+
+ return {
+ rect,
+ targets: snapTargetsFor("display"),
+ maxWidth,
+ gain,
+ moved: false,
+ };
+ },
+ (e, state, initialMouse) => {
+ const anchor = {
+ x: state.rect.x + state.rect.w / 2,
+ y: state.rect.y + state.rect.h / 2,
+ };
+ const scale = resolveScale(e, state, initialMouse, dirX, dirY, anchor);
+
+ const newWidth = clamp(
+ state.rect.w * scale,
+ state.maxWidth * Math.max(1 - state.gain * 100, 0.05),
+ state.maxWidth,
+ );
+ const newPadding = clamp(
+ (1 - newWidth / state.maxWidth) / state.gain,
+ 0,
+ 100,
+ );
+ setProject("background", "padding", newPadding);
+
+ const applied = newWidth / state.rect.w;
+ const w = state.rect.w * applied;
+ const h = state.rect.h * applied;
+ setDragRects((prev) => ({
+ ...prev,
+ display: {
+ x: clamp(anchor.x - w / 2, 0, Math.max(0, 1 - w)),
+ y: clamp(anchor.y - h / 2, 0, Math.max(0, 1 - h)),
+ w,
+ h,
+ },
+ }));
+ },
+ );
+
+ // Arrow-key nudge for the selected element (1px, Shift = 10px), Escape
+ // deselects. Scoped here rather than useEditorShortcuts so held keys
+ // repeat.
+ createEventListener(document, "keydown", (e: KeyboardEvent) => {
+ const selected = selection();
+ if (!selected || !overlayVisible()) return;
+ const target = e.target as HTMLElement | null;
+ if (
+ target &&
+ (target.tagName === "INPUT" ||
+ target.tagName === "TEXTAREA" ||
+ target.isContentEditable)
+ )
+ return;
+
+ if (e.key === "Escape") {
+ setEditorState("canvasSelection", null);
+ return;
+ }
+
+ const arrows: Record<string, [number, number]> = {
+ ArrowLeft: [-1, 0],
+ ArrowRight: [1, 0],
+ ArrowUp: [0, -1],
+ ArrowDown: [0, 1],
+ };
+ const dir = arrows[e.key];
+ if (!dir) return;
+
+ const isDisplay = selected.type === "display";
+ if (isDisplay && (!showDisplay() || !displayDraggable())) return;
+ if (!isDisplay && !showCamera()) return;
+ const rect = isDisplay ? displayRect() : cameraRect();
+ if (!rect) return;
+
+ e.preventDefault();
+ const px = e.shiftKey ? 10 : 1;
+ const x = clamp(
+ rect.x + (dir[0] * px) / props.size.width,
+ isDisplay ? -rect.w / 2 : 0,
+ isDisplay ? 1 - rect.w / 2 : Math.max(0, 1 - rect.w),
+ );
+ const y = clamp(
+ rect.y + (dir[1] * px) / props.size.height,
+ isDisplay ? -rect.h / 2 : 0,
+ isDisplay ? 1 - rect.h / 2 : Math.max(0, 1 - rect.h),
+ );
+ const center = { x: x + rect.w / 2, y: y + rect.h / 2 };
+ if (isDisplay) setProject("background", "displayPosition", center);
+ else setProject("camera", "manualPosition", center);
+ });
+
+ return (
+ <div class="absolute inset-0 pointer-events-none">
+ <Show when={overlayVisible()}>
+ <Show when={selection()}>
+ <div
+ class="absolute inset-0 pointer-events-auto"
+ onMouseDown={(e) => {
+ if (e.button !== 0) return;
+ setEditorState("canvasSelection", null);
+ }}
+ />
+ </Show>
+ <Show when={showDisplay() && displayRect()}>
+ {(rect) => (
+ <ElementBox
+ size={props.size}
+ rect={rect()}
+ label="Screen"
+ selected={selection()?.type === "display"}
+ draggable={displayDraggable()}
+ resizable={displayDraggable()}
+ lockedHint="Screen position is locked while a zoom is active"
+ onMouseDown={(e) => {
+ if (e.button !== 0) return;
+ if (selection()?.type !== "display") select("display");
+ onDisplayMove(e);
+ }}
+ resizeHandler={displayResizeHandler}
+ />
+ )}
+ </Show>
+ <Show when={showCamera() && cameraRect()}>
+ {(rect) => (
+ <ElementBox
+ size={props.size}
+ rect={rect()}
+ label="Camera"
+ selected={selection()?.type === "camera"}
+ draggable
+ resizable={cameraResizable()}
+ lockedHint="Camera size is controlled by the zoom settings while a zoom is active"
+ onMouseDown={(e) => {
+ if (e.button !== 0) return;
+ if (selection()?.type !== "camera") select("camera");
+ onCameraMove(e);
+ }}
+ resizeHandler={cameraResizeHandler}
+ />
+ )}
+ </Show>
+ </Show>
+ </div>
+ );
+}
+
+function ElementBox(props: {
+ size: Size;
+ rect: NormRect;
+ label: string;
+ selected: boolean;
+ draggable: boolean;
+ resizable: boolean;
+ lockedHint: string;
+ onMouseDown: (e: MouseEvent) => void;
+ resizeHandler: (dirX: 1 | -1, dirY: 1 | -1) => (e: MouseEvent) => void;
+}) {
+ // The letterbox wrapper has `contain: strict`, so anything positioned
+ // outside the canvas is clipped — keep the label and the corner handles
+ // inside the visible area when the box touches (or overhangs) an edge.
+ const labelStyle = () => {
+ const leftPx = props.rect.x * props.size.width;
+ const topPx = props.rect.y * props.size.height;
+ return {
+ left: `${Math.max(6, 6 - leftPx)}px`,
+ top: topPx >= 28 ? "-24px" : `${Math.max(6, 6 - topPx)}px`,
+ };
+ };
+
+ const flush = () => ({
+ left: props.rect.x * props.size.width <= 6,
+ top: props.rect.y * props.size.height <= 6,
+ right:
+ (props.rect.x + props.rect.w) * props.size.width >= props.size.width - 6,
+ bottom:
+ (props.rect.y + props.rect.h) * props.size.height >=
+ props.size.height - 6,
+ });
+
+ const corners = () => {
+ const f = flush();
+ return [
+ {
+ dirX: -1 as const,
+ dirY: -1 as const,
+ cursor: "cursor-nw-resize",
+ class: cx(
+ "top-0 left-0",
+ !f.left && "-translate-x-1/2",
+ !f.top && "-translate-y-1/2",
+ ),
+ },
+ {
+ dirX: 1 as const,
+ dirY: -1 as const,
+ cursor: "cursor-ne-resize",
+ class: cx(
+ "top-0 right-0",
+ !f.right && "translate-x-1/2",
+ !f.top && "-translate-y-1/2",
+ ),
+ },
+ {
+ dirX: -1 as const,
+ dirY: 1 as const,
+ cursor: "cursor-sw-resize",
+ class: cx(
+ "bottom-0 left-0",
+ !f.left && "-translate-x-1/2",
+ !f.bottom && "translate-y-1/2",
+ ),
+ },
+ {
+ dirX: 1 as const,
+ dirY: 1 as const,
+ cursor: "cursor-se-resize",
+ class: cx(
+ "bottom-0 right-0",
+ !f.right && "translate-x-1/2",
+ !f.bottom && "translate-y-1/2",
+ ),
+ },
+ ];
+ };
+
+ // Hover is tracked in state rather than CSS group-hover so the handles
+ // reveal deterministically (and survive partial HMR of provider modules).
+ const [hovered, setHovered] = createSignal(false);
+ const showHandles = () => props.selected || hovered();
+
+ return (
+ <div
+ class="absolute pointer-events-auto"
+ style={{
+ left: `${props.rect.x * props.size.width}px`,
+ top: `${props.rect.y * props.size.height}px`,
+ width: `${props.rect.w * props.size.width}px`,
+ height: `${props.rect.h * props.size.height}px`,
+ }}
+ title={props.draggable && props.resizable ? undefined : props.lockedHint}
+ onMouseDown={props.onMouseDown}
+ onMouseEnter={() => setHovered(true)}
+ onMouseLeave={() => setHovered(false)}
+ >
+ <div
+ class="absolute inset-0 border-2 transition-colors rounded-md"
+ classList={{
+ "border-blue-9": props.selected,
+ "border-blue-6": !props.selected && hovered(),
+ "border-transparent": !props.selected && !hovered(),
+ "cursor-move": props.draggable,
+ "cursor-default": !props.draggable,
+ }}
+ />
+ <Show when={props.selected || hovered()}>
+ <div
+ class="absolute px-1.5 py-0.5 text-[11px] font-medium text-white bg-blue-9 rounded pointer-events-none select-none"
+ style={labelStyle()}
+ >
+ {props.label}
+ {!props.resizable && " (locked during zoom)"}
+ </div>
+ </Show>
+ <Show when={showHandles()}>
+ <For each={corners()}>
+ {(corner) => (
+ <div
+ class={cx(
+ "absolute w-3 h-3 rounded-full border border-white shadow-xs",
+ props.resizable
+ ? cx(
+ "bg-blue-9 transition-transform hover:scale-125",
+ corner.cursor,
+ )
+ : "bg-gray-8 cursor-not-allowed opacity-60",
+ corner.class,
+ )}
+ onMouseDown={(e) => {
+ if (!props.resizable) {
+ e.preventDefault();
+ e.stopPropagation();
+ return;
+ }
+ props.resizeHandler(corner.dirX, corner.dirY)(e);
+ }}
+ />
+ )}
+ </For>
+ </Show>
+ </div>
+ );
+}
diff --git a/apps/desktop/src/routes/editor/snapping.ts b/apps/desktop/src/routes/editor/snapping.ts
new file mode 100644
index 000000000..810942d94
--- /dev/null
+++ b/apps/desktop/src/routes/editor/snapping.ts
@@ -0,0 +1,218 @@
+// Pure geometry for Figma-style smart guides on the preview canvas. All
+// coordinates are normalized [0, 1] against the output frame; callers convert
+// the pixel snap radius per-axis (SNAP_PX / canvasSize) since the axes scale
+// differently.
+
+export type NormRect = { x: number; y: number; w: number; h: number };
+
+export type SnapLineKind =
+ | "frame-edge"
+ | "frame-center"
+ | "margin"
+ | "element-edge"
+ | "element-center";
+
+export type SnapLine = {
+ pos: number;
+ kind: SnapLineKind;
+ // Extent of the source rect along the other axis, used to draw guide
+ // spans between the two aligned elements. Absent = full-frame line.
+ refStart?: number;
+ refEnd?: number;
+};
+
+export type SnapTargets = { v: SnapLine[]; h: SnapLine[] };
+
+export type SnapGuide = {
+ axis: "v" | "h";
+ pos: number;
+ start: number;
+ end: number;
+ kind: SnapLineKind;
+};
+
+/** Magnetic radius in CSS px. */
+export const SNAP_PX = 7;
+
+export function buildSnapTargets(
+ otherRects: NormRect[],
+ opts?: { margin?: { x: number; y: number } },
+): SnapTargets {
+ const v: SnapLine[] = [
+ { pos: 0, kind: "frame-edge" },
+ { pos: 1, kind: "frame-edge" },
+ { pos: 0.5, kind: "frame-center" },
+ ];
+ const h: SnapLine[] = [
+ { pos: 0, kind: "frame-edge" },
+ { pos: 1, kind: "frame-edge" },
+ { pos: 0.5, kind: "frame-center" },
+ ];
+
+ const margin = opts?.margin;
+ if (margin) {
+ v.push(
+ { pos: margin.x, kind: "margin" },
+ { pos: 1 - margin.x, kind: "margin" },
+ );
+ h.push(
+ { pos: margin.y, kind: "margin" },
+ { pos: 1 - margin.y, kind: "margin" },
+ );
+ }
+
+ for (const r of otherRects) {
+ const refV = { refStart: r.y, refEnd: r.y + r.h };
+ const refH = { refStart: r.x, refEnd: r.x + r.w };
+ v.push(
+ { pos: r.x, kind: "element-edge", ...refV },
+ { pos: r.x + r.w / 2, kind: "element-center", ...refV },
+ { pos: r.x + r.w, kind: "element-edge", ...refV },
+ );
+ h.push(
+ { pos: r.y, kind: "element-edge", ...refH },
+ { pos: r.y + r.h / 2, kind: "element-center", ...refH },
+ { pos: r.y + r.h, kind: "element-edge", ...refH },
+ );
+ }
+
+ return { v, h };
+}
+
+type AxisSnap = { delta: number; line: SnapLine } | null;
+
+function snapAxis(
+ anchors: number[],
+ lines: SnapLine[],
+ threshold: number,
+): AxisSnap {
+ let best: AxisSnap = null;
+ let bestCost = Number.POSITIVE_INFINITY;
+ for (const line of lines) {
+ // Center lines win ties against edges for a Figma-like feel.
+ const bias =
+ line.kind === "frame-center" || line.kind === "element-center"
+ ? threshold * 0.15
+ : 0;
+ for (const anchor of anchors) {
+ const delta = line.pos - anchor;
+ if (Math.abs(delta) > threshold) continue;
+ const cost = Math.abs(delta) - bias;
+ if (cost < bestCost) {
+ bestCost = cost;
+ best = { delta, line };
+ }
+ }
+ }
+ return best;
+}
+
+function guideFor(
+ axis: "v" | "h",
+ line: SnapLine,
+ moving: NormRect,
+): SnapGuide {
+ if (line.refStart === undefined || line.refEnd === undefined) {
+ return { axis, pos: line.pos, start: 0, end: 1, kind: line.kind };
+ }
+ const movingStart = axis === "v" ? moving.y : moving.x;
+ const movingEnd = axis === "v" ? moving.y + moving.h : moving.x + moving.w;
+ return {
+ axis,
+ pos: line.pos,
+ kind: line.kind,
+ start: Math.min(line.refStart, movingStart),
+ end: Math.max(line.refEnd, movingEnd),
+ };
+}
+
+/**
+ * Snap a moving rect: its left/center/right x-anchors and top/center/bottom
+ * y-anchors are tested independently against the vertical/horizontal lines.
+ * Returns the correction to add to the rect position plus the active guides.
+ */
+export function snapMovingRect(
+ rect: NormRect,
+ targets: SnapTargets,
+ thresholdX: number,
+ thresholdY: number,
+): { dx: number; dy: number; guides: SnapGuide[] } {
+ const sx = snapAxis(
+ [rect.x, rect.x + rect.w / 2, rect.x + rect.w],
+ targets.v,
+ thresholdX,
+ );
+ const sy = snapAxis(
+ [rect.y, rect.y + rect.h / 2, rect.y + rect.h],
+ targets.h,
+ thresholdY,
+ );
+
+ const dx = sx?.delta ?? 0;
+ const dy = sy?.delta ?? 0;
+ const snapped = { ...rect, x: rect.x + dx, y: rect.y + dy };
+
+ const guides: SnapGuide[] = [];
+ if (sx) guides.push(guideFor("v", sx.line, snapped));
+ if (sy) guides.push(guideFor("h", sy.line, snapped));
+
+ return { dx, dy, guides };
+}
+
+/**
+ * Snap the dragged corner of an aspect-locked resize. Both axes are tested;
+ * the smaller correction wins so the caller can derive a single scale factor
+ * from the snapped axis without breaking the aspect lock.
+ */
+export function snapResizeCorner(
+ corner: { x: number; y: number },
+ targets: SnapTargets,
+ thresholdX: number,
+ thresholdY: number,
+): {
+ axis: "x" | "y" | null;
+ x: number;
+ y: number;
+ guides: SnapGuide[];
+} {
+ const sx = snapAxis([corner.x], targets.v, thresholdX);
+ const sy = snapAxis([corner.y], targets.h, thresholdY);
+
+ const useX =
+ sx && (!sy || Math.abs(sx.delta) <= Math.abs(sy.delta)) ? sx : null;
+ const useY = !useX && sy ? sy : null;
+
+ if (useX) {
+ return {
+ axis: "x",
+ x: corner.x + useX.delta,
+ y: corner.y,
+ guides: [
+ {
+ axis: "v",
+ pos: useX.line.pos,
+ start: 0,
+ end: 1,
+ kind: useX.line.kind,
+ },
+ ],
+ };
+ }
+ if (useY) {
+ return {
+ axis: "y",
+ x: corner.x,
+ y: corner.y + useY.delta,
+ guides: [
+ {
+ axis: "h",
+ pos: useY.line.pos,
+ start: 0,
+ end: 1,
+ kind: useY.line.kind,
+ },
+ ],
+ };
+ }
+ return { axis: null, x: corner.x, y: corner.y, guides: [] };
+}