The VS Code of video editing, built for agents.
TypeScript
Refactor canvas component and remove DrawOverlay
konstantin-paulus3dbdb22953ef
diff --git a/apps/web/src/components/canvas/canvas.tsx b/apps/web/src/components/canvas/canvas.tsxindex bdb2754b..5d2bdce9 100644--- a/apps/web/src/components/canvas/canvas.tsx+++ b/apps/web/src/components/canvas/canvas.tsx@@ -8,7 +8,6 @@ import { CameraController, EngineCanvas } from "@/engine";import { insertAsset } from "@/engine/insert-asset";import { droppedFiles, importFiles } from "@/engine/asset-actions";import { Toolbar } from "./toolbar";-import { DrawOverlay } from "./draw-overlay";import { DesktopAppBanner } from "./desktop-app-banner";import { toast } from "somoto"import { SceneInitOverlay } from "./scene-init-overlay";@@ -25,7 +24,7 @@ export function Canvas() {* imported into the library first, then land the same way.** With no scene under the pointer they land loose on the stage, like an- * element drawn there (see DrawOverlay): the drop says where, so the active+ * element drawn there (see `@/engine/draw-tool`): the drop says where, so the active* scene — which is somewhere else entirely — is not the answer.*/const handleDropEvent = async (event: DragEvent) => {@@ -77,7 +76,6 @@ export function Canvas() {><Toolbar /><DesktopAppBanner />- <DrawOverlay /><SceneInitOverlay /><EngineCanvas /><CameraController />diff --git a/apps/web/src/components/canvas/draw-overlay.tsx b/apps/web/src/components/canvas/draw-overlay.tsxdeleted file mode 100644index dad2fedf..00000000--- a/apps/web/src/components/canvas/draw-overlay.tsx+++ /dev/null@@ -1,284 +0,0 @@-/* This Source Code Form is subject to the terms of the Mozilla Public- * 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 { Show } from "solid-js";-import { useWorld } from "@diffusionstudio/koota-solid";-import { Rect, Scene, SolidPaint, Text } from "@diffusionstudio/reconciler";-import {- Computed,- findSceneAt,- getNextName,- Root,- screenToWorld,- Source,- store,- Tool,- ToolType,- worldToLocal,-} from "@diffusionstudio/runtime";-import { useEditor, useTool } from "@/engine";--import type { Entity } from "koota";-import type { Point } from "@diffusionstudio/runtime";--type ToolConfig = {- isScene?: boolean;- fillColor: string;- previewColor: string;- namePrefix: string;- label: string;- defaultWidth: number;- defaultHeight: number;-};--const TOOL_CONFIG: Partial<Record<ToolType, ToolConfig>> = {- [ToolType.RECT]: {- fillColor: '#E0E0E0',- previewColor: '#E0E0E0',- namePrefix: 'Rect',- label: 'Create shape',- defaultWidth: 300,- defaultHeight: 300,- },- [ToolType.SCENE]: {- isScene: true,- fillColor: '#000000',- previewColor: '#000000',- namePrefix: 'Scene',- label: 'Create scene',- defaultWidth: 1920,- defaultHeight: 1080,- },- [ToolType.TEXT]: {- fillColor: '#FFFFFF',- previewColor: 'transparent',- namePrefix: 'Text',- label: 'Create text',- // Width/height for click insertions are derived from the font size below.- defaultWidth: 0,- defaultHeight: 0,- },-};--const CLICK_THRESHOLD = 10;--export function DrawOverlay() {- const world = useWorld();- const editor = useEditor();- const selectedTool = useTool();-- let overlayRef!: HTMLDivElement;- let previewRef!: HTMLDivElement;- let dimensionsRef!: HTMLDivElement;-- let isDrawing = false;- let startPoint: Point | null = null;- let currentPoint: Point | null = null;- let targetScene: Entity | null = null;-- const config = () => TOOL_CONFIG[selectedTool()];- const isActiveTool = () => !!config();-- const getRect = () => {- if (!startPoint || !currentPoint) return null;- return {- x: Math.min(startPoint.x, currentPoint.x),- y: Math.min(startPoint.y, currentPoint.y),- width: Math.abs(currentPoint.x - startPoint.x),- height: Math.abs(currentPoint.y - startPoint.y),- };- };-- const updatePreview = () => {- const cfg = config();- const rect = getRect();- if (!previewRef || !rect || !cfg) {- if (previewRef) previewRef.style.display = 'none';- if (dimensionsRef) dimensionsRef.style.display = 'none';- return;- }-- previewRef.style.display = 'block';- previewRef.style.left = `${rect.x}px`;- previewRef.style.top = `${rect.y}px`;- previewRef.style.width = `${rect.width}px`;- previewRef.style.height = `${rect.height}px`;- previewRef.style.backgroundColor = cfg.previewColor;- previewRef.style.outline = '2px solid #008CFF';- previewRef.style.outlineOffset = '0px';-- const worldTopLeft = screenToWorld(world, rect.x, rect.y);- const worldBottomRight = screenToWorld(world, rect.x + rect.width, rect.y + rect.height);- const worldWidth = Math.round(worldBottomRight.x - worldTopLeft.x);- const worldHeight = Math.round(worldBottomRight.y - worldTopLeft.y);-- dimensionsRef.style.display = 'block';- dimensionsRef.style.left = `${rect.x + rect.width / 2}px`;- dimensionsRef.style.top = `${rect.y + rect.height + 8}px`;- dimensionsRef.textContent = `${worldWidth}x${worldHeight}`;- };-- const getLocalPoint = (e: PointerEvent): Point => {- const rect = overlayRef.getBoundingClientRect();- return { x: e.clientX - rect.left, y: e.clientY - rect.top };- };-- const handlePointerDown = (e: PointerEvent) => {- if (!isActiveTool()) return;-- const point = getLocalPoint(e);- const worldPt = screenToWorld(world, point.x, point.y);- targetScene = findSceneAt(world, worldPt.x, worldPt.y);-- overlayRef.setPointerCapture(e.pointerId);- isDrawing = true;- startPoint = point;- currentPoint = point;- };-- const handlePointerMove = (e: PointerEvent) => {- if (!isDrawing) return;- currentPoint = getLocalPoint(e);- updatePreview();- };-- const reset = () => {- isDrawing = false;- startPoint = null;- currentPoint = null;- targetScene = null;- updatePreview();- };-- const handlePointerUp = () => {- if (!isDrawing) return;-- const cfg = config();- const rect = getRect();- if (!cfg || !rect) {- reset();- return;- }-- const tool = selectedTool();- const isClick = rect.width < CLICK_THRESHOLD || rect.height < CLICK_THRESHOLD;-- const worldTopLeft = screenToWorld(world, rect.x, rect.y);- const worldBottomRight = screenToWorld(world, rect.x + rect.width, rect.y + rect.height);-- // Approximate text size from the parent scene's height.- let fontSize = 16;- if (tool === ToolType.TEXT && targetScene !== null) {- const sceneHeight = store(world, Computed).height[targetScene.id()] ?? 0;- fontSize = Math.max(8, Math.round(sceneHeight / 22.5));- }-- let width: number;- let height: number;- if (!isClick) {- width = Math.round(worldBottomRight.x - worldTopLeft.x);- height = Math.round(worldBottomRight.y - worldTopLeft.y);- } else if (tool === ToolType.TEXT) {- // Rough text bounds based on "Text" (4 chars) at the chosen font size.- width = Math.round(fontSize * 0.6 * 4);- height = Math.round(fontSize * 1.2);- } else {- width = cfg.defaultWidth;- height = cfg.defaultHeight;- }-- let posX = isClick ? worldTopLeft.x - width / 2 : worldTopLeft.x;- let posY = isClick ? worldTopLeft.y - height / 2 : worldTopLeft.y;-- // Scenes always live at the root; rect/text may parent into a hovered scene.- const parentScene = tool === ToolType.SCENE ? null : targetScene;- if (parentScene !== null) {- const local = worldToLocal(world, parentScene, posX, posY);- posX = local.x;- posY = local.y;- }-- const parent = parentScene ?? world.get(Root)!;- // Nothing to draw into until a project is mounted: the element would have- // no file to be written to.- if (!parent.get(Source)?.value) {- reset();- return;- }-- const name = getNextName(world, cfg.namePrefix);- const x = Math.round(posX);- const y = Math.round(posY);- // A clicked-in text sizes itself to its glyphs, so it takes no size.- const size = tool !== ToolType.TEXT || !isClick ? { width, height } : {};-- const [entity] = editor.insertElement(parent, () => {- if (cfg.isScene) {- return (- <Scene name={name} x={x} y={y} width={width} height={height}>- <SolidPaint color={cfg.fillColor} />- </Scene>- );- }- if (tool === ToolType.TEXT) {- return (- <Text name={name} x={x} y={y} {...size} fontSize={fontSize}>- Text- <SolidPaint color={cfg.fillColor} />- </Text>- );- }- return (- <Rect name={name} x={x} y={y} {...size}>- <SolidPaint color={cfg.fillColor} />- </Rect>- );- });-- if (entity) {- if (tool === ToolType.SCENE) {- editor.activate(entity);- }-- editor.select(entity);- }-- world.set(Tool, { value: tool === ToolType.TEXT ? ToolType.TEXT_EDIT : ToolType.MOVE });- reset();- };-- return (- <Show when={isActiveTool()}>- <div- ref={overlayRef}- class="absolute inset-0 z-5 cursor-crosshair"- on:pointerdown={handlePointerDown}- on:pointermove={handlePointerMove}- on:pointerup={handlePointerUp}- >- <div- ref={previewRef}- class="absolute pointer-events-none"- style={{ display: 'none' }}- />- <div- ref={dimensionsRef}- class="absolute pointer-events-none"- style={{- display: 'none',- transform: 'translateX(-50%)',- 'background-color': '#008CFF',- color: '#FFFFFF',- 'font-family': 'Inter, sans-serif',- 'font-size': '11px',- 'line-height': '1',- padding: '3px 4px',- 'border-radius': '3px',- 'white-space': 'nowrap',- }}- />- </div>- </Show>- );-}diff --git a/apps/web/src/engine/create-engine.ts b/apps/web/src/engine/create-engine.tsindex c77e2aba..65df4a22 100644--- a/apps/web/src/engine/create-engine.ts+++ b/apps/web/src/engine/create-engine.ts@@ -5,7 +5,7 @@import { assetSystem, renderSystem, transformSystem, playbackSystem, motionSystem, AudioEngine, createRuntimeWorld, Geometry, Mode, RenderSurface, Time, ChildOf, syncInteractiveState } from '@diffusionstudio/runtime';import { hudSystem } from './hud';import { createSignal, type Accessor, type Setter } from 'solid-js';-import { AssetSelection, GradientTool, Hud, Keys, MODIFIER_KEYS, ObjectMaskTool, Pointer, PointerEvents, ProjectConfig, SnapLines } from './traits';+import { AssetSelection, DrawTool, GradientTool, Hud, Keys, MODIFIER_KEYS, ObjectMaskTool, Pointer, PointerEvents, ProjectConfig, SnapLines } from './traits';import { inputSystem } from './input/input-system';import { clearClipFrames, clearClipPeaks, clearMedia, clearPeaks, timelineSystem, TimelineSurface } from './timeline';import { clearObjectTrackOf, clearObjectTracks } from './object-mask';@@ -49,7 +49,7 @@ class Engine {public constructor(projectId: string, options: EngineOptions = {}) {this.world = createRuntimeWorld(projectId);- this.world.add(Pointer, Keys, SnapLines, Hud, ObjectMaskTool, GradientTool, PointerEvents, AssetSelection, ProjectConfig, TimelineSurface);+ this.world.add(Pointer, Keys, SnapLines, Hud, ObjectMaskTool, GradientTool, DrawTool, PointerEvents, AssetSelection, ProjectConfig, TimelineSurface);this.unsubscribe.push(this.world.onAdd(ChildOf('*'), () => (this.interactiveDirty = true)),diff --git a/apps/web/src/engine/draw-tool.tsx b/apps/web/src/engine/draw-tool.tsxnew file mode 100644index 00000000..0de972ed--- /dev/null+++ b/apps/web/src/engine/draw-tool.tsx@@ -0,0 +1,245 @@+/* This Source Code Form is subject to the terms of the Mozilla Public+ * 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/. */++/**+ * The tools that put a new node on the stage: rect, scene and text. A drag+ * draws the node's box, a click drops one of a default size centered on the+ * pointer. A rect or text lands in the scene the press was in, a scene always+ * at the root.+ */++import { Rect, Scene, SolidPaint, Text } from '@diffusionstudio/reconciler';+import {+ Computed, HitRegions, RenderSurface, Root, Source, Tool, ToolType,+ findSceneAt, getNextName, identity2D, rectToQuad, screenToWorld, store, worldToLocal,+} from '@diffusionstudio/runtime';++import { getDocumentEditor } from './editor';+import { DrawTool, Pointer } from './traits';++import type { World } from 'koota';+import type { DispatchedPointerEvent, Point } from '@diffusionstudio/runtime';++type Ctx2D = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;++type DrawConfig = {+ namePrefix: string;+ fillColor: string;+ /** What the box is filled with while it is drawn; null for an outline only. */+ previewColor: string | null;+ /** The size a click drops, in document units. */+ defaultWidth: number;+ defaultHeight: number;+};++const DRAW_CONFIG: Partial<Record<ToolType, DrawConfig>> = {+ [ToolType.RECT]: {+ namePrefix: 'Rect',+ fillColor: '#E0E0E0',+ previewColor: '#E0E0E0',+ defaultWidth: 300,+ defaultHeight: 300,+ },+ [ToolType.SCENE]: {+ namePrefix: 'Scene',+ fillColor: '#000000',+ previewColor: '#000000',+ defaultWidth: 1920,+ defaultHeight: 1080,+ },+ [ToolType.TEXT]: {+ namePrefix: 'Text',+ fillColor: '#FFFFFF',+ previewColor: null,+ // A clicked-in text sizes itself to its glyphs.+ defaultWidth: 0,+ defaultHeight: 0,+ },+};++const ACCENT = '#008CFF';+/** Below this many CSS px on either side, a drag is a click. */+const CLICK_THRESHOLD = 10;++export function isDrawTool(world: World): boolean {+ return !!DRAW_CONFIG[world.get(Tool)?.value ?? ToolType.MOVE];+}++/** The box from where the press began to the pointer, in device pixels. */+function getDrawnRect(world: World): { x: number; y: number; width: number; height: number } {+ const pointer = world.get(Pointer)!;+ return {+ x: Math.min(pointer.dragStartX, pointer.clientX),+ y: Math.min(pointer.dragStartY, pointer.clientY),+ width: Math.abs(pointer.clientX - pointer.dragStartX),+ height: Math.abs(pointer.clientY - pointer.dragStartY),+ };+}++/** A point in device pixels, in the document. */+function toDocument(world: World, x: number, y: number, resolution: number): Point {+ return screenToWorld(world, x / resolution, y / resolution);+}++/**+ * While a draw tool is up: the region over the whole stage that takes its+ * presses, pushed last so it sits over everything else, and the box being+ * drawn with its size under it.+ */+export function drawDrawTool(world: World, ctx: Ctx2D, resolution: number): void {+ const config = DRAW_CONFIG[world.get(Tool)?.value ?? ToolType.MOVE];+ if (!config) return;++ world.get(HitRegions)!.list.push({+ target: { kind: 'hud', id: 'draw', quad: rectToQuad(identity2D(), ctx.canvas.width, ctx.canvas.height) },+ callback: handleDrawInteraction,+ });++ // A press that panning took over never sees its release, so the box goes+ // with the button rather than waiting for a dragend that will not come.+ if (!world.get(DrawTool)?.drawing || world.get(Pointer)?.phase !== 'pressed') return;++ const rect = getDrawnRect(world);++ ctx.save();+ ctx.resetTransform();++ ctx.beginPath();+ ctx.rect(rect.x, rect.y, rect.width, rect.height);+ if (config.previewColor) {+ ctx.fillStyle = config.previewColor;+ ctx.fill();+ }+ ctx.strokeStyle = ACCENT;+ ctx.lineWidth = Math.round(2 * resolution);+ ctx.stroke();++ const topLeft = toDocument(world, rect.x, rect.y, resolution);+ const bottomRight = toDocument(world, rect.x + rect.width, rect.y + rect.height, resolution);+ const text = `${Math.round(bottomRight.x - topLeft.x)}x${Math.round(bottomRight.y - topLeft.y)}`;++ ctx.translate(rect.x + rect.width / 2, rect.y + rect.height);+ ctx.scale(resolution, resolution);+ ctx.translate(0, 16);+ ctx.textAlign = 'center';+ ctx.textBaseline = 'middle';+ ctx.font = '11px Inter, sans-serif';++ const boxWidth = ctx.measureText(text).width + 8;++ ctx.beginPath();+ ctx.roundRect(-boxWidth / 2, -9, boxWidth, 16, 3);+ ctx.fillStyle = ACCENT;+ ctx.fill();++ ctx.fillStyle = '#FFFFFF';+ ctx.fillText(text, 0, 0);++ ctx.restore();+}++/**+ * A press starts the box in the scene it landed in, the release puts the+ * node there and hands over to the move tool, or to text editing for a text.+ */+export function handleDrawInteraction(world: World, event: DispatchedPointerEvent): void {+ const resolution = world.get(RenderSurface)?.resolution ?? 1;++ if (event.type === 'dragstart') {+ const point = toDocument(world, event.clientX, event.clientY, resolution);+ world.set(DrawTool, { drawing: true, scene: findSceneAt(world, point.x, point.y) });+ return;+ }++ if (event.type !== 'dragend') return;++ const { drawing, scene } = world.get(DrawTool)!;+ world.set(DrawTool, { drawing: false, scene: null });++ const tool = world.get(Tool)?.value ?? ToolType.MOVE;+ const config = DRAW_CONFIG[tool];+ if (!drawing || !config) return;++ const rect = getDrawnRect(world);+ const threshold = CLICK_THRESHOLD * resolution;+ const isClick = rect.width < threshold || rect.height < threshold;++ const topLeft = toDocument(world, rect.x, rect.y, resolution);+ const bottomRight = toDocument(world, rect.x + rect.width, rect.y + rect.height, resolution);+ const targetScene = scene?.isAlive() ? scene : null;++ // Approximate text size from the parent scene's height.+ let fontSize = 16;+ if (tool === ToolType.TEXT && targetScene !== null) {+ const sceneHeight = store(world, Computed).height[targetScene.id()] ?? 0;+ fontSize = Math.max(8, Math.round(sceneHeight / 22.5));+ }++ let width: number;+ let height: number;+ if (!isClick) {+ width = Math.round(bottomRight.x - topLeft.x);+ height = Math.round(bottomRight.y - topLeft.y);+ } else if (tool === ToolType.TEXT) {+ // Rough text bounds based on "Text" (4 chars) at the chosen font size.+ width = Math.round(fontSize * 0.6 * 4);+ height = Math.round(fontSize * 1.2);+ } else {+ width = config.defaultWidth;+ height = config.defaultHeight;+ }++ let posX = isClick ? topLeft.x - width / 2 : topLeft.x;+ let posY = isClick ? topLeft.y - height / 2 : topLeft.y;++ // Scenes always live at the root; rect/text may parent into a hovered scene.+ const parentScene = tool === ToolType.SCENE ? null : targetScene;+ if (parentScene !== null) {+ const local = worldToLocal(world, parentScene, posX, posY);+ posX = local.x;+ posY = local.y;+ }++ const parent = parentScene ?? world.get(Root)!;+ // Nothing to draw into until a project is mounted: the element would have+ // no file to be written to.+ if (!parent.get(Source)?.value) return;++ const editor = getDocumentEditor(world);+ const name = getNextName(world, config.namePrefix);+ const x = Math.round(posX);+ const y = Math.round(posY);+ // A clicked-in text sizes itself to its glyphs, so it takes no size.+ const size = tool !== ToolType.TEXT || !isClick ? { width, height } : {};++ const [entity] = editor.insertElement(parent, () => {+ if (tool === ToolType.SCENE) {+ return (+ <Scene name={name} x={x} y={y} width={width} height={height}>+ <SolidPaint color={config.fillColor} />+ </Scene>+ );+ }+ if (tool === ToolType.TEXT) {+ return (+ <Text name={name} x={x} y={y} {...size} fontSize={fontSize}>+ Text+ <SolidPaint color={config.fillColor} />+ </Text>+ );+ }+ return (+ <Rect name={name} x={x} y={y} {...size}>+ <SolidPaint color={config.fillColor} />+ </Rect>+ );+ });++ if (entity) {+ if (tool === ToolType.SCENE) editor.activate(entity);+ editor.select(entity);+ }++ world.set(Tool, { value: tool === ToolType.TEXT ? ToolType.TEXT_EDIT : ToolType.MOVE });+}diff --git a/apps/web/src/engine/hud/hud-system.ts b/apps/web/src/engine/hud/hud-system.tsindex 5f8f11e1..0ac44051 100644--- a/apps/web/src/engine/hud/hud-system.ts+++ b/apps/web/src/engine/hud/hud-system.ts@@ -22,6 +22,7 @@ import { getMarqueeQuad } from '../input/snapping';import { getMountedNameInput } from './name-input';import { drawObjectMasks } from '../object-mask';import { drawGradientTool, isGradientTool } from '../gradient-tool';+import { drawDrawTool } from '../draw-tool';import type { Entity, World } from 'koota';import type { Mat2D } from '@diffusionstudio/runtime';@@ -61,6 +62,7 @@ export function hudSystem(world: World): void {drawMarquee(world, ctx, resolution);drawObjectMasks(world, ctx, resolution);drawGradientTool(world, ctx, resolution);+ drawDrawTool(world, ctx, resolution);}function drawSnapLines(world: World, ctx: Ctx2D, resolution: number): void {diff --git a/apps/web/src/engine/traits.ts b/apps/web/src/engine/traits.tsindex 2849ef4a..3a708fa3 100644--- a/apps/web/src/engine/traits.ts+++ b/apps/web/src/engine/traits.ts@@ -101,6 +101,16 @@ export const GradientTool = trait({held: null as GradientHandle | null,});+/**+ * A box being drawn by the rect, scene or text tool: whether a press is+ * drawing one, and the scene it landed in, which the new node goes into.+ * The box itself is the pointer's, from where the press began to where it is.+ */+export const DrawTool = trait({+ drawing: false,+ scene: null as Entity | null,+});+/*** Queue of pointer events to be processed by the input system.*/