Skip to content

The VS Code of video editing, built for agents.

TypeScript

feat: enhance canvas behavior with dynamic panning on panel visibility changes

konstantin-paulusbbb2fad77ac9
diff --git a/apps/web/src/components/canvas/canvas.tsx b/apps/web/src/components/canvas/canvas.tsx
index 0c713142..970226cc 100644
--- a/apps/web/src/components/canvas/canvas.tsx
+++ b/apps/web/src/components/canvas/canvas.tsx
@@ -2,9 +2,11 @@
* 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/. */
+import { createEffect, on, onCleanup, onMount } from "solid-js";
import { useWorld } from "@diffusionstudio/koota-solid";
import { findSceneAt, screenToWorld, worldToLocal, Library, Root } from "@diffusionstudio/runtime";
-import { CameraController, EngineCanvas, nodesAt } from "@/engine";
+import { CameraController, EngineCanvas, nodesAt, panBy } from "@/engine";
+import { useLayout } from "@/context/layout";
import { insertAsset } from "@/engine/insert-asset";
import { droppedFiles, importFiles } from "@/engine/asset-actions";
import { Toolbar } from "./toolbar";
@@ -18,6 +20,33 @@ import type { Asset } from "@diffusionstudio/assets";
export function Canvas() {
const world = useWorld();
+ const { uiVisible } = useLayout();
+ let container!: HTMLDivElement;
+
+ /**
+ * Where the canvas sits on screen, kept up as the panels around it change
+ * size (the left one animates between the sidebar's tabs).
+ */
+ let origin: DOMRect | null = null;
+ const observer = new ResizeObserver(() => (origin = container.getBoundingClientRect()));
+ onMount(() => observer.observe(container));
+ onCleanup(() => observer.disconnect());
+
+ /**
+ * Showing or hiding the panels moves the canvas's corner across the
+ * screen; the camera moves back by as much, so what is drawn stays where it
+ * was. The grid snaps rather than animates when the panels come and go, so
+ * the new place can be read straight away, ahead of the observer.
+ */
+ createEffect(on(uiVisible, () => {
+ const before = origin;
+ origin = container.getBoundingClientRect();
+ if (!before) return;
+
+ const dx = origin.left - before.left;
+ const dy = origin.top - before.top;
+ if (dx || dy) panBy(world, dx, dy);
+ }, { defer: true }));
/**
* Drops onto the canvas: library assets (dragged from the panel) land where
@@ -69,7 +98,7 @@ export function Canvas() {
}
return (
- <div class="relative size-full bg-background">
+ <div ref={container} class="relative size-full bg-background">
<div
class="absolute inset-0"
on:drop={handleDropEvent}
diff --git a/apps/web/src/engine/camera.ts b/apps/web/src/engine/camera.ts
index 75e23415..5518279e 100644
--- a/apps/web/src/engine/camera.ts
+++ b/apps/web/src/engine/camera.ts
@@ -14,6 +14,7 @@ import {
focusEntities,
getCameraMatrix,
getSelection,
+ panCamera,
setCameraZoom,
zoomCameraBy,
} from '@diffusionstudio/runtime';
@@ -26,6 +27,12 @@ function reportCamera(world: World): void {
getDocumentEditor(world).reportEdit(world.get(Root)!, 'camera', getCameraMatrix(world));
}
+/** Scrolls the view by a delta in canvas CSS pixels: the content moves against it. */
+export function panBy(world: World, dx: number, dy: number): void {
+ panCamera(world, dx, dy);
+ reportCamera(world);
+}
+
/** Zooms around the center of the viewport: above 1 in, below 1 out. */
export function zoomBy(world: World, factor: number): void {
zoomCameraBy(world, factor);