Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor camera handling and integrate document reporting

konstantin-paulus61e1e9a0d2c6
diff --git a/apps/web/src/components/sidebar-right/inspector/background.tsx b/apps/web/src/components/sidebar-right/inspector/background.tsx
index 17de88cb..80515e69 100644
--- a/apps/web/src/components/sidebar-right/inspector/background.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/background.tsx
@@ -22,7 +22,7 @@ import { useDocument } from "@/engine/hooks/use-document";
export function BackgroundSettings() {
const world = useWorld();
- const document = useDocument();
+ const doc = useDocument();
const [isPickerOpen, setIsPickerOpen] = createSignal(false);
const background = useTrait(world.get(Root)!, Background);
@@ -34,7 +34,7 @@ export function BackgroundSettings() {
const assignColor = (value: number) => {
const root = world.get(Root)!;
const color = colorToHex(value);
- document().editProperty(root, "background", color);
+ doc().editProperty(root, "background", color);
};
return (
diff --git a/apps/web/src/components/sidebar-right/inspector/inspector-header.tsx b/apps/web/src/components/sidebar-right/inspector/inspector-header.tsx
index 9ab2e900..6154c57b 100644
--- a/apps/web/src/components/sidebar-right/inspector/inspector-header.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/inspector-header.tsx
@@ -12,19 +12,31 @@ import {
} from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";
import { useWorld } from "@diffusionstudio/koota-solid";
-import { focusContent, setCameraZoom, zoomCameraBy } from "@diffusionstudio/runtime";
+import { focusContent, getCameraMatrix, Root, setCameraZoom, zoomCameraBy } from "@diffusionstudio/runtime";
import { useCameraScale } from "@/engine";
+import { useDocument } from "@/engine/hooks/use-document";
export function InspectorHeader() {
const world = useWorld();
+ const doc = useDocument();
// Every operation here is anchored on the stage itself — its center, or its
// content — so the menu needs no canvas geometry of its own.
const scale = useCameraScale();
const zoomLabel = () => `${Math.round(scale() * 100)}%`;
- const zoomBy = (factor: number) => zoomCameraBy(world, factor);
- const zoomTo = (percent: number) => setCameraZoom(world, percent / 100);
- const zoomToFit = () => focusContent(world);
+
+ const zoomBy = (factor: number) => {
+ zoomCameraBy(world, factor);
+ doc()?.reportEdit(world.get(Root)!, 'camera', getCameraMatrix(world));
+ };
+ const zoomTo = (percent: number) => {
+ setCameraZoom(world, percent / 100);
+ doc()?.reportEdit(world.get(Root)!, 'camera', getCameraMatrix(world));
+ };
+ const zoomToFit = () => {
+ focusContent(world);
+ doc()?.reportEdit(world.get(Root)!, 'camera', getCameraMatrix(world));
+ };
return (
<div class="h-12 shrink-0 flex items-center px-4">
diff --git a/apps/web/src/engine/camera-controller.tsx b/apps/web/src/engine/camera-controller.tsx
index 7c923f22..5429dc2b 100644
--- a/apps/web/src/engine/camera-controller.tsx
+++ b/apps/web/src/engine/camera-controller.tsx
@@ -2,16 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-// Input bindings for the stage camera. The camera's semantics live in the
-// runtime (pan/zoom/focus actions); this file only translates DOM events into
-// calls on them, which is why it stays app-side.
-
-import { createEffect, createMemo, onCleanup } from 'solid-js';
+import { createMemo, onCleanup, onMount } from 'solid-js';
import { useTrait, useWorld } from '@diffusionstudio/koota-solid';
-import { panCamera, setCamera, zoomCameraAt, getCamera, RenderSurface } from '@diffusionstudio/runtime';
+import { panCamera, setCamera, zoomCameraAt, getCamera, RenderSurface, getCameraMatrix, Root } from '@diffusionstudio/runtime';
+import { useDocument } from './hooks/use-document';
import type { JSX } from 'solid-js';
-import type { World } from 'koota';
/** deltaY is in lines (deltaMode 1) or pages (deltaMode 2) on some devices. */
const DELTA_MODE_SCALE = [1, 16, 600];
@@ -29,13 +25,17 @@ function isEditable(target: EventTarget | null): boolean {
return target instanceof HTMLElement && target.isContentEditable;
}
-/**
- * Bind camera gestures to `canvas` and return a disposer. Pointer and wheel
- * events are canvas-scoped so the rest of the editor keeps its own scrolling;
- * the space-key modifier is tracked on the document (and window blur) because
- * a held key is global state the canvas never sees.
- */
-function bindCameraInput(world: World, canvas: HTMLCanvasElement): () => void {
+export function CameraController(): JSX.Element {
+ const world = useWorld();
+ const surface = useTrait(world, RenderSurface);
+ const doc = useDocument();
+
+ const canvas = createMemo(() => {
+ const target = surface()?.canvas;
+ if (target instanceof HTMLCanvasElement) return target;
+ return null;
+ });
+
let spaceHeld = false;
let panning = false;
let panPointerId: number | null = null;
@@ -45,21 +45,28 @@ function bindCameraInput(world: World, canvas: HTMLCanvasElement): () => void {
let startF = 0;
const updateCursor = (): void => {
- canvas.style.cursor = panning ? 'grabbing' : (spaceHeld ? 'grab' : '');
+ const target = canvas();
+
+ if (target) {
+ target.style.cursor = panning ? 'grabbing' : (spaceHeld ? 'grab' : '');
+ }
};
/** Pointer position in canvas CSS pixels. */
const localPoint = (event: { clientX: number; clientY: number }): [x: number, y: number] => {
- const rect = canvas.getBoundingClientRect();
+ const rect = canvas()?.getBoundingClientRect();
+ if (!rect) return [0, 0];
return [event.clientX - rect.left, event.clientY - rect.top];
};
const endPan = (): void => {
if (!panning) return;
panning = false;
- if (panPointerId !== null && canvas.hasPointerCapture(panPointerId)) {
- canvas.releasePointerCapture(panPointerId);
+
+ if (panPointerId !== null && canvas()?.hasPointerCapture(panPointerId)) {
+ canvas()?.releasePointerCapture(panPointerId);
}
+
panPointerId = null;
updateCursor();
};
@@ -78,6 +85,8 @@ function bindCameraInput(world: World, canvas: HTMLCanvasElement): () => void {
} else {
panCamera(world, event.deltaX * scale, event.deltaY * scale);
}
+
+ doc()?.reportEdit(world.get(Root)!, 'camera', getCameraMatrix(world));
};
const handlePointerDown = (event: PointerEvent): void => {
@@ -89,7 +98,7 @@ function bindCameraInput(world: World, canvas: HTMLCanvasElement): () => void {
panning = true;
panPointerId = event.pointerId;
- canvas.setPointerCapture(event.pointerId);
+ canvas()?.setPointerCapture(event.pointerId);
[startX, startY] = localPoint(event);
const camera = getCamera(world);
@@ -105,6 +114,7 @@ function bindCameraInput(world: World, canvas: HTMLCanvasElement): () => void {
// a long drag can't accumulate rounding error.
const [x, y] = localPoint(event);
setCamera(world, { e: startE + (x - startX), f: startF + (y - startY) });
+ doc()?.reportEdit(world.get(Root)!, 'camera', getCameraMatrix(world));
};
const handlePointerUp = (event: PointerEvent): void => {
@@ -134,51 +144,28 @@ function bindCameraInput(world: World, canvas: HTMLCanvasElement): () => void {
updateCursor();
};
- canvas.addEventListener('wheel', handleWheel, { passive: false });
- canvas.addEventListener('pointerdown', handlePointerDown);
- canvas.addEventListener('pointermove', handlePointerMove);
- canvas.addEventListener('pointerup', handlePointerUp);
- canvas.addEventListener('pointercancel', handlePointerUp);
- document.addEventListener('keydown', handleKeyDown);
- document.addEventListener('keyup', handleKeyUp);
- window.addEventListener('blur', handleBlur);
-
- return () => {
- canvas.removeEventListener('wheel', handleWheel);
- canvas.removeEventListener('pointerdown', handlePointerDown);
- canvas.removeEventListener('pointermove', handlePointerMove);
- canvas.removeEventListener('pointerup', handlePointerUp);
- canvas.removeEventListener('pointercancel', handlePointerUp);
+ onMount(() => {
+ canvas()?.addEventListener('wheel', handleWheel, { passive: false });
+ canvas()?.addEventListener('pointerdown', handlePointerDown);
+ canvas()?.addEventListener('pointermove', handlePointerMove);
+ canvas()?.addEventListener('pointerup', handlePointerUp);
+ canvas()?.addEventListener('pointercancel', handlePointerUp);
+ document.addEventListener('keydown', handleKeyDown);
+ document.addEventListener('keyup', handleKeyUp);
+ window.addEventListener('blur', handleBlur);
+ })
+
+ onCleanup(() => {
+ canvas()?.removeEventListener('wheel', handleWheel);
+ canvas()?.removeEventListener('pointerdown', handlePointerDown);
+ canvas()?.removeEventListener('pointermove', handlePointerMove);
+ canvas()?.removeEventListener('pointerup', handlePointerUp);
+ canvas()?.removeEventListener('pointercancel', handlePointerUp);
document.removeEventListener('keydown', handleKeyDown);
document.removeEventListener('keyup', handleKeyUp);
window.removeEventListener('blur', handleBlur);
endPan();
- };
-}
-
-/**
- * Gives the stage its camera gestures: wheel to scroll-pan, ctrl/⌘-wheel (and
- * trackpad pinch, which browsers report the same way) to zoom at the cursor,
- * and drag to pan while space is held or with the middle button. Renders
- * nothing — drop it anywhere under an EngineProvider, in any order relative to
- * EngineCanvas: it follows the canvas through the world's RenderSurface, so it
- * binds as soon as one is mounted and rebinds if it is replaced.
- */
-export function CameraController(): JSX.Element {
- const world = useWorld();
- const surface = useTrait(world, RenderSurface);
-
- // Memoized on identity: RenderSurface also changes on every resize, and
- // listeners must not be torn down and rebuilt for that.
- const canvas = createMemo(() => {
- const target = surface()?.canvas;
- return target instanceof HTMLCanvasElement ? target : null;
- });
-
- createEffect(() => {
- const target = canvas();
- if (target) onCleanup(bindCameraInput(world, target));
- });
+ })
return null;
}
diff --git a/packages/jsx/src/index.ts b/packages/jsx/src/index.ts
index a958fa69..820be00b 100644
--- a/packages/jsx/src/index.ts
+++ b/packages/jsx/src/index.ts
@@ -37,6 +37,7 @@ export type {
AnimationSpec,
AnimationType,
AudioProps,
+ CameraMatrix,
CaptionPreset,
CaptionsProps,
ColorStopProps,
diff --git a/packages/jsx/src/types.ts b/packages/jsx/src/types.ts
index b95b0d7d..4dd81d07 100644
--- a/packages/jsx/src/types.ts
+++ b/packages/jsx/src/types.ts
@@ -255,10 +255,24 @@ export type SourceProps = {
id?: string;
};
+/**
+ * A 2D affine transform as its six values, in the order CSS `matrix()` and
+ * canvas `setTransform` take them: `[a, b, c, d, e, f]`, where `a`/`d` scale,
+ * `b`/`c` skew, and `e`/`f` translate. See `StageProps["camera"]`.
+ */
+export type CameraMatrix = [a: number, b: number, c: number, d: number, e: number, f: number];
+
/** The infinite canvas every project renders into; only allowed as the root element. */
export type StageProps = {
/** Canvas color, any CSS color. */
background?: string;
+ /**
+ * The editor's viewport when the project is opened: `[1, 0, 0, 1, 0, 0]` is
+ * the origin at 100%. Not part of the composition — nothing rendered or
+ * exported depends on it — so a project that never says where to look opens
+ * at the origin.
+ */
+ camera?: CameraMatrix;
children?: SolidJSX.Element;
};
diff --git a/packages/reconciler/src/document.ts b/packages/reconciler/src/document.ts
index 28fa1cc9..56aa6bf6 100644
--- a/packages/reconciler/src/document.ts
+++ b/packages/reconciler/src/document.ts
@@ -19,10 +19,12 @@ import {
DocumentRoot,
Root,
Source,
+ setCameraMatrix,
} from '@diffusionstudio/runtime';
import { SOURCE_ATTR } from '@diffusionstudio/jsx';
import type { PropValue } from '@diffusionstudio/jsx';
+import type { CameraMatrix } from '@diffusionstudio/runtime';
import type { Entity, World } from 'koota';
import type { ProjectDocument } from './host';
@@ -77,18 +79,17 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {
}
/**
- * The editor's door into the composition, as opposed to `setProperty`,
- * which is the rendering project's. Both change the same traits the same
- * way; the difference is where the value came from, and so whether the
- * source already says it. A project's own render must never be reported as
- * an edit, or every reactive update would write the file it came from.
- *
- * The value is the one a project would have written — "#161616", not the
- * number the runtime packs a color into — because it is on its way to being
- * spelled out in a JSX attribute.
+ * Writes an edit to the document and saves the changes to the source
*/
public editProperty(entity: Entity, name: string, value: PropValue): void {
this.setProperty({ entity }, name, value);
+ this.reportEdit(entity, name, value);
+ }
+
+ /**
+ * Reports an edit whose change the editor already made itself
+ */
+ public reportEdit(entity: Entity, name: string, value: PropValue): void {
const source = entity.get(Source)?.value;
if (source) {
@@ -166,6 +167,15 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {
this.stage.entity.set(Background, { value: color ?? DEFAULT_BACKGROUND });
return;
}
+ case 'camera': {
+ if (!Array.isArray(value) || value.length !== 6) return;
+
+ const numbers = value.map(toNumber);
+ if (!numbers.includes(undefined)) {
+ setCameraMatrix(this.world, (numbers as CameraMatrix));
+ }
+ return;
+ }
default:
// children/ref and anything from a richer vocabulary: ignored, so
// such a project still renders what this host understands.
diff --git a/packages/runtime/src/actions/camera.ts b/packages/runtime/src/actions/camera.ts
index 3a03f55d..03f46358 100644
--- a/packages/runtime/src/actions/camera.ts
+++ b/packages/runtime/src/actions/camera.ts
@@ -16,6 +16,7 @@ import { getCamera, getCameraInverse, getCameraScale, getContentBounds, getEntit
import type { Entity, World } from 'koota';
import type { Camera2D } from '../traits';
+import type { CameraMatrix } from '../queries/camera';
import type { Rect } from '../math';
/** Default breathing room, in CSS pixels, left around a focused rect. */
@@ -31,6 +32,15 @@ export function resetCamera(world: World): void {
setCamera(world, { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 });
}
+/**
+ * Restores a matrix `getCameraMatrix` reported, verbatim. Unclamped, unlike
+ * the zoom operations: a matrix says exactly what it says, and the gestures
+ * that produce one have already been held to the limits.
+ */
+export function setCameraMatrix(world: World, [a, b, c, d, e, f]: CameraMatrix): void {
+ setCamera(world, { a, b, c, d, e, f });
+}
+
/**
* Scroll the view by a delta in canvas CSS pixels, as a wheel or trackpad
* gesture does: the content moves against the delta. Independent of zoom —
diff --git a/packages/runtime/src/queries/camera.ts b/packages/runtime/src/queries/camera.ts
index d0f60b30..2562851d 100644
--- a/packages/runtime/src/queries/camera.ts
+++ b/packages/runtime/src/queries/camera.ts
@@ -33,6 +33,19 @@ export function getCameraScale(world: World): number {
return Math.hypot(a, b);
}
+/**
+ * The camera as the six values of its affine matrix, in the order CSS
+ * `matrix()` and canvas `setTransform` take them. The form a project writes
+ * (`<stage camera={…}>`): the whole transform, so a view survives the trip to
+ * the file and back with nothing dropped.
+ */
+export type CameraMatrix = [a: number, b: number, c: number, d: number, e: number, f: number];
+
+export function getCameraMatrix(world: World): CameraMatrix {
+ const { a, b, c, d, e, f } = getCamera(world);
+ return [a, b, c, d, e, f];
+}
+
/**
* Camera inverse, for mapping canvas CSS pixels back into the document, or
* null when the camera has been scaled to nothing.