Skip to content

The VS Code of video editing, built for agents.

TypeScript

Remove deprecated `bitecs` dependency and clean up engine components

konstantin-paulus63620be1f8e8
diff --git a/apps/web/package.json b/apps/web/package.json
index 88c073eb..0f62b659 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -31,7 +31,6 @@
"@trpc/client": "^11.10.0",
"@trpc/server": "^11.10.0",
"animejs": "^4.4.1",
- "bitecs": "^0.4.0",
"cmdk-solid": "^1.1.2",
"colord": "^2.9.3",
"cva": "^1.0.0-beta.4",
diff --git a/apps/web/src/components/canvas/canvas.tsx b/apps/web/src/components/canvas/canvas.tsx
index 86a72f7d..bdb2754b 100644
--- a/apps/web/src/components/canvas/canvas.tsx
+++ b/apps/web/src/components/canvas/canvas.tsx
@@ -16,8 +16,6 @@ import { ASSET_DRAG_TYPE } from "@/components/sidebar-left/folder-item";
import type { Asset } from "@diffusionstudio/assets";
-export const MAIN_CANVAS_ID = 'engine-canvas';
-
export function Canvas() {
const world = useWorld();
@@ -83,12 +81,6 @@ export function Canvas() {
<SceneInitOverlay />
<EngineCanvas />
<CameraController />
-
- {/** Deprecated bitecs canvas */}
- <canvas
- id={MAIN_CANVAS_ID}
- class="absolute inset-0 opacity-0 pointer-events-none"
- />
</div>
</div>
);
diff --git a/apps/web/src/components/dashboard/projects-view.tsx b/apps/web/src/components/dashboard/projects-view.tsx
index 0fb79a0a..45272e58 100644
--- a/apps/web/src/components/dashboard/projects-view.tsx
+++ b/apps/web/src/components/dashboard/projects-view.tsx
@@ -2,7 +2,7 @@
* 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 { forgetProjectBundle, generateProjectName } from "@/components/engine/db";
+import { forgetProjectBundle, generateProjectName } from "@/lib/db";
import {
ContextMenu,
ContextMenuContent,
diff --git a/apps/web/src/components/engine/api/assets.ts b/apps/web/src/components/engine/api/assets.ts
deleted file mode 100644
index 599a6718..00000000
--- a/apps/web/src/components/engine/api/assets.ts
+++ /dev/null
@@ -1,957 +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 { createSignal } from "solid-js";
-import type { Accessor, Setter } from "solid-js";
-import { assert } from "@/utils";
-import { ALL_FORMATS, InputTrack, UrlSource } from 'mediabunny';
-import { BlobSource } from 'mediabunny';
-import { Input } from 'mediabunny';
-import { nanoid } from "nanoid";
-import { GeometryType, PaintType } from "../components";
-import { secondsToFrames } from "../utils/time";
-import { toast } from "somoto";
-import { createEntity } from "./entities";
-import { appendChild } from "./hierarchy";
-import { addComponent, setComponent } from "./events";
-import { resizeEntity } from "./resize";
-import { mimeTypeToExtension, showFileDialog } from "@/utils";
-import { trpc } from "@/lib/trpc";
-import {
- ElectronFileHandle,
- isSerializedElectronFileHandle,
- rehydrateElectronFileHandle,
-} from "@/lib/electron-file-handle";
-
-import type { EngineWorld } from "./world";
-import type { Asset } from "../db";
-import type { Transcript } from "@diffusionstudio/api-contract";
-
-const URL_REGEX = /\b(?:https?|ftp|blob):\/\/[^\s]+/;
-const DEFAULT_SEQUENCE_FPS = 30;
-
-type MediaInput =
- | string
- | File
- | FileSystemFileHandle
- | FileSystemDirectoryHandle
- | ElectronFileHandle;
-
-type AssetState = {
- projectId: string;
- counter: number;
- /** Where the browser build writes asset files: the origin private file system. */
- directoryHandle: Promise<FileSystemDirectoryHandle>;
- version: Accessor<number>;
- setVersion: Setter<number>;
- selected: Accessor<Map<string, Asset>>;
- setSelected: Setter<Map<string, Asset>>;
-};
-
-const assetStates = new WeakMap<object, AssetState>();
-const inflightLoads = new WeakMap<EngineWorld, Map<string, Promise<Asset>>>();
-
-/**
- * Initializes the asset subsystem for a world. Call once, right after the
- * world is created. `world.assets` must already be an (empty) Map.
- */
-export function initAssets(world: EngineWorld, projectId: string): void {
- const [version, setVersion] = createSignal(0);
- const [selected, setSelected] = createSignal<Map<string, Asset>>(new Map());
-
- assetStates.set(world, {
- projectId,
- counter: 0,
- directoryHandle: navigator.storage.getDirectory(),
- version,
- setVersion,
- selected,
- setSelected,
- });
-}
-
-function assetState(world: EngineWorld): AssetState {
- const state = assetStates.get(world);
- assert(state, 'Assets not initialized for this world; call initAssets() first');
- return state;
-}
-
-/**
- * Reactive version counter. Read inside a tracking scope (memo/effect) to
- * re-run whenever the asset set changes — e.g. after re-granting filesystem
- * permission so previously-failed getFile() calls retry.
- */
-export function assetsVersion(world: EngineWorld): number {
- return assetState(world).version();
-}
-
-/** Bumps the version signal so reactive consumers (thumbnails, previews) re-run. */
-export function invalidateAssets(world: EngineWorld): void {
- assetState(world).setVersion((v) => v + 1);
-}
-
-/**
- * Allocates the next id in the project's shared asset/folder id space, so an
- * id never refers to both an asset and a folder.
- */
-export function allocateId(world: EngineWorld): string {
- const state = assetState(world);
- state.counter += 1;
- return state.counter.toString(36).padStart(4, "0");
-}
-
-/**
- * Raises the id counter past every counter id in `ids`. Ids that are not
- * base36 numbers (e.g. legacy "fld-…" folder ids) are ignored. Restores may
- * run in parallel, so this only ever raises the counter.
- */
-export function raiseIdCounter(world: EngineWorld, ids: Iterable<string>): void {
- const state = assetState(world);
- for (const id of ids) {
- if (!/^[0-9a-z]+$/i.test(id)) continue;
- const num = parseInt(id, 36);
- if (Number.isSafeInteger(num) && num > state.counter) state.counter = num;
- }
-}
-
-type LoadAssetOptions = {
- name?: string;
- generationId?: string | null;
- generationKey?: string | null;
- folderId?: string | null;
-};
-
-/**
- * Loads an asset from a handle, URL, File, or DataTransferItem. Detects the
- * MIME type, writes to OPFS when needed, persists the metadata, and returns the
- * created (or pre-existing) Asset.
- */
-export async function loadAsset(
- world: EngineWorld,
- handle: MediaInput | DataTransferItem,
- options?: LoadAssetOptions,
-): Promise<Asset> {
- const state = assetState(world);
-
- // If the handle is a data transfer item, get the file from it
- if (handle instanceof DataTransferItem) {
- const file = await getFileFromDataTransferItem(handle);
- assert(file, 'Failed to get file from data transfer item');
- handle = file;
- }
-
- // Step 1: Generate a hash for the asset
- let hash = '';
- if (typeof handle === 'string') {
- assert(URL_REGEX.test(handle), 'Invalid URL');
- hash = handle;
- } else if (handle instanceof File) {
- hash = `${handle.name}-${handle.size}-${handle.lastModified}`;
- } else if (handle instanceof FileSystemFileHandle) {
- const file = await handle.getFile();
- hash = `${file.name}-${file.size}-${file.lastModified}`;
- } else if (handle instanceof ElectronFileHandle) {
- const file = await handle.getFile();
- hash = `${file.name}-${file.size}-${file.lastModified}`;
- } else if (handle instanceof FileSystemDirectoryHandle) {
- let size = 0;
- let count = 0;
- let modified = 0;
- for await (const [, entry] of handle.entries()) {
- if (entry.kind !== 'file') continue;
- const file = await entry.getFile();
- size += file.size;
- modified += file.lastModified;
- count++;
- }
- hash = `seq:${handle.name}-${size}-${modified}-${count}`;
- } else {
- assert(false, 'Could not generate hash for handle');
- }
-
- const existingAsset = Array.from(world.assets.values()).find(asset => asset.hash === hash);
- if (existingAsset) {
- return existingAsset;
- };
-
- // Dedup against a concurrent, still-in-flight load of the same source.
- let pending = inflightLoads.get(world);
- if (!pending) {
- pending = new Map();
- inflightLoads.set(world, pending);
- }
-
- const running = pending.get(hash);
- if (running) return await running;
-
- const promise = createAsset(world, handle, options, hash, state);
- pending.set(hash, promise);
- try {
- return await promise;
- } finally {
- pending.delete(hash);
- }
-}
-
-/**
- * Creates and persists a brand-new asset for a source whose hash was not found
- * among the existing or in-flight assets. Split out of `loadAsset` so a single
- * promise per hash can be memoized around it (see `inflightLoads`).
- */
-async function createAsset(
- world: EngineWorld,
- handle: MediaInput,
- options: LoadAssetOptions | undefined,
- hash: string,
- state: AssetState,
-): Promise<Asset> {
- // Step 2: Generate a unique ID for the asset
- const id = allocateId(world);
- const createdAt = new Date().toISOString();
- const mimeType = await detectMimeType(handle);
- assert(mimeType, 'Could not detect MIME type');
-
- let name = '';
- if (options?.name) {
- name = options.name;
- } else if (typeof handle === 'string') {
- name = (handle.split('/').pop() ?? `Unnamed_${nanoid(8)}`).slice(0, 24);
- } else if (handle instanceof File) {
- name = handle.name;
- } else if (handle instanceof FileSystemFileHandle) {
- const file = await handle.getFile();
- name = file.name;
- } else if (handle instanceof ElectronFileHandle) {
- name = handle.name;
- } else if (handle instanceof FileSystemDirectoryHandle) {
- name = handle.name;
- }
-
- // Step 3: Write the asset to the file system if necessary
- let writable: FileSystemWritableFileStream | null = null;
- let fileHandle: FileSystemFileHandle | ElectronFileHandle | null = null;
-
- try {
- const uniqueName = id + mimeTypeToExtension(mimeType);
- const directory = await state.directoryHandle;
-
- // Get or create the backups folder
- const assetsFolder = await directory.getDirectoryHandle('assets', { create: true });
-
- if (handle instanceof Blob) {
- fileHandle = await assetsFolder.getFileHandle(uniqueName, { create: true });
- writable = await fileHandle.createWritable();
-
- // write blob to file handle
- await writable.write(handle);
- } else if (typeof handle === 'string') {
- fileHandle = await assetsFolder.getFileHandle(uniqueName, { create: true });
- writable = await fileHandle.createWritable();
-
- // stream file to file handle
- const response = await fetch(handle);
- const reader = response.body?.getReader();
- if (!reader) throw new Error('Failed to stream file');
-
- while (true) {
- const { done, value } = await reader.read();
- if (done) break;
- await writable.write(value);
- }
- } else if (handle instanceof FileSystemFileHandle) {
- fileHandle = handle;
- } else if (handle instanceof ElectronFileHandle) {
- fileHandle = handle; // Electron files live on the user's disk; don't copy into OPFS.
- } else if (handle instanceof FileSystemDirectoryHandle) {
- const entry = await handle.entries().next();
- const firstFile = entry.value[1];
- assert(firstFile instanceof FileSystemFileHandle, 'First entry is not a file handle');
- fileHandle = entry.value[1];
- } else {
- assert(false, 'Invalid handle');
- }
- } catch (error) {
- console.error('Failed to write asset to file system:', error);
- } finally {
- await writable?.close();
- }
-
- assert(fileHandle, 'File handle not found');
-
- const file = await fileHandle.getFile();
-
- const baseAsset = {
- id,
- hash,
- createdAt,
- lastModified: file.lastModified,
- name,
- mimeType,
- size: file.size,
- handle: fileHandle,
- generationId: options?.generationId ?? null,
- generationKey: options?.generationKey ?? null,
- folderId: options?.folderId ?? null,
- }
-
- if (handle instanceof FileSystemDirectoryHandle) {
- const entry = await handle.entries().next();
- const firstFile = entry.value[1];
- assert(firstFile instanceof FileSystemFileHandle, 'First entry is not a file handle');
- const { width, height } = await getRasterDimensions(
- await firstFile.getFile()
- );
-
- let count = 0;
- for await (const [, entry] of handle.entries()) {
- if (entry.kind !== 'file') continue;
- count++;
- }
- const duration = count / DEFAULT_SEQUENCE_FPS;
-
- return saveAsset(world, {
- ...baseAsset,
- type: 'SEQUENCE',
- width,
- height,
- frameRate: DEFAULT_SEQUENCE_FPS,
- duration,
- directoryHandle: handle,
- });
- }
-
- const metadata = await probeMediaMetadata(file, mimeType);
- return saveAsset(world, { ...baseAsset, ...metadata });
-}
-
-/**
- * Derives the type-specific asset properties (dimensions, duration, tracks)
- * of a media file.
- */
-async function probeMediaMetadata(file: Blob, mimeType: string) {
- if (mimeType.match(/^image\//)) {
- const { width, height } = mimeType === 'image/svg+xml'
- ? await getSvgDimensions(file)
- : await getRasterDimensions(file);
-
- return { type: 'IMAGE', width, height } as const;
- } else if (mimeType === 'application/json' || mimeType === 'application/x-subrip' || mimeType === 'text/vtt') {
- return { type: 'TRANSCRIPT' } as const;
- } else if (mimeType.match(/^(audio\/|video\/)/)) {
- const input = new Input({
- formats: ALL_FORMATS,
- source: new BlobSource(file),
- });
-
- const audioTrack = await input.getPrimaryAudioTrack();
- const channels = audioTrack?.numberOfChannels;
- const sampleRate = audioTrack?.sampleRate;
-
- if (mimeType.startsWith('audio/')) {
- assert(sampleRate, 'Sample rate not found');
- assert(channels, 'Channels not found');
-
- const duration = await trackContentDuration(audioTrack);
-
- return { type: 'AUDIO', duration, sampleRate, channels } as const;
- }
-
- const videoTrack = await input.getPrimaryVideoTrack();
- assert(videoTrack, 'Video track not found');
- const stats = await videoTrack.computePacketStats();
- const duration = await trackContentDuration(videoTrack);
-
- return {
- type: 'VIDEO',
- width: videoTrack.displayWidth,
- height: videoTrack.displayHeight,
- frameRate: stats.averagePacketRate,
- bitRate: stats.averageBitrate,
- sampleRate,
- duration,
- channels,
- } as const;
- }
-
- assert(false, 'Unsupported file type');
-}
-
-/**
- * Describes a local file as an ephemeral Asset: full metadata, but no id
- * allocation and no persistence — the library is never touched. The path
- * doubles as the id so errors and results name the file.
- */
-export async function describeFileAsset(handle: ElectronFileHandle): Promise<Asset> {
- const file = await handle.getFile();
- const mimeType = await detectMimeType(handle);
- assert(mimeType, 'Could not detect MIME type');
-
- return {
- id: handle.path,
- hash: `${file.name}-${file.size}-${file.lastModified}`,
- createdAt: new Date().toISOString(),
- lastModified: file.lastModified,
- name: handle.name,
- mimeType,
- size: file.size,
- handle,
- generationId: null,
- generationKey: null,
- folderId: null,
- ...(await probeMediaMetadata(file, mimeType)),
- };
-}
-
-/**
- * Reactive view over the asset set, for use *inside* Solid tracking scopes
- * (memos, effects, JSX). Each accessor reads the version signal so the scope
- * re-runs when assets are added, saved, or removed.
- *
- * Outside a reactive scope (decoders, render, async handlers, …) don't use
- * this — read `world.assets` (a plain `Map<string, Asset>`) directly, e.g.
- * `world.assets.get(id)`. These accessors exist only to establish the
- * dependency; the data is the Map.
- */
-export function useAssets(world: EngineWorld) {
- const state = assetState(world);
- return {
- /** All assets, in insertion order. */
- all: (): Asset[] => {
- state.version();
- return Array.from(world.assets.values());
- },
- /** A single asset by id. */
- get: (id: string): Asset | undefined => {
- state.version();
- return world.assets.get(id);
- },
- /** The current selection set. */
- selected: (): Map<string, Asset> => state.selected(),
- };
-}
-
-/**
- * Appends one or more assets to the in-memory store without persisting.
- * New assets are ordered first; existing records win on id conflict.
- */
-export function appendAssets(world: EngineWorld, ...incoming: Asset[]): void {
- const map = world.assets;
- const existing = Array.from(map.values());
- map.clear();
- for (const asset of incoming) map.set(asset.id, asset);
- for (const asset of existing) map.set(asset.id, asset);
- invalidateAssets(world);
-}
-
-/**
- * Saves an asset to the database and the in-memory store.
- */
-export async function saveAsset(world: EngineWorld, asset: Asset): Promise<Asset> {
- const state = assetState(world);
- const map = world.assets;
-
- // Re-insert at the front to preserve the prior signal-replacement ordering.
- const existing = Array.from(map.values()).filter((a) => a.id !== asset.id);
- map.clear();
- map.set(asset.id, asset);
- existing.forEach(a => map.set(a.id, a));
-
- const sel = state.selected();
- if (sel.has(asset.id)) {
- const next = new Map(sel);
- next.set(asset.id, asset);
- state.setSelected(next);
- }
-
- invalidateAssets(world);
-
- return asset;
-}
-
-/**
- * Removes one or more assets from the in-memory store and the database.
- */
-export async function removeAsset(world: EngineWorld, ...ids: string[]): Promise<void> {
- const map = world.assets;
- for (const id of ids) map.delete(id);
- invalidateAssets(world);
-}
-
-/**
- * Restores assets into the in-memory store. Call once during engine init.
- * Placeholder: projects are held purely in memory, so there is nothing to
- * load yet — wire the project (JSX package) loader in here.
- */
-export async function restoreAssets(world: EngineWorld): Promise<void> {
- const records: Asset[] = [];
- if (records.length === 0) return;
-
- const map = world.assets;
- map.clear();
- for (const record of records) {
- map.set(record.id, rehydrateAsset(record));
- }
-
- raiseIdCounter(world, records.map((record) => record.id));
-
- invalidateAssets(world);
-}
-
-/** The file-backing handle shared by every asset variant. */
-type AssetHandle = Asset['handle'];
-
-const assetFileCache = new WeakMap<AssetHandle, Promise<File>>();
-
-/**
- * Returns the File backing an asset, reusing an in-flight or already-resolved
- * read for the same handle.
- */
-export function getAssetFile(asset: Pick<Asset, 'handle'>): Promise<File> {
- const { handle } = asset;
- const cached = assetFileCache.get(handle);
- if (cached) return cached;
-
- const promise = handle.getFile();
- assetFileCache.set(handle, promise);
- promise.catch(() => {
- if (assetFileCache.get(handle) === promise) {
- assetFileCache.delete(handle);
- }
- });
- return promise;
-}
-
-/** Returns the File/Blob backing an asset, or null if the asset is missing. */
-export async function getAssetBlob(world: EngineWorld, id: string): Promise<Blob | null> {
- const asset = world.assets.get(id);
- if (!asset) return null;
-
- return await getAssetFile(asset);
-}
-
-/**
- * Upload a single local asset to the cloud bucket. The bucket key is prefixed
- * with the project id to make it globally unique.
- */
-export async function uploadAsset(world: EngineWorld, id: string) {
- const blob = await getAssetBlob(world, id);
- if (!blob) {
- console.error(`Asset ${id} not found`);
- return null;
- }
- return uploadBlob(blob, `${assetState(world).projectId}-${id}`);
-}
-
-/**
- * Upload multiple local assets in parallel, filtering out any failures.
- */
-export async function uploadAssets(world: EngineWorld, ids?: string[]) {
- if (!ids || ids.length === 0) return undefined;
- const results = await Promise.all(ids.map((id) => uploadAsset(world, id)));
- return results.filter((ref) => ref !== null);
-}
-
-/** Selects a single asset by id (replacing any prior selection). */
-export function selectAsset(world: EngineWorld, id: string): void {
- const asset = world.assets.get(id);
- if (!asset) return;
- assetState(world).setSelected(new Map([[id, asset]]));
-}
-
-/** Clears the selected-asset set. */
-export function clearSelectedAssets(world: EngineWorld): void {
- assetState(world).setSelected(new Map());
-}
-
-/**
- * Upload a Blob to the cloud bucket via a signed URL under the asset id.
- * Returns the FileRef the API can resolve.
- */
-export async function uploadBlob(blob: Blob, id: string) {
- const contentType = blob.type || "application/octet-stream";
- const { uploadUrl, fileRef } = await trpc.getUploadUrl.mutate({ id, contentType });
-
- if (uploadUrl) {
- const res = await fetch(uploadUrl, {
- method: "PUT",
- headers: { "Content-Type": contentType },
- body: blob,
- });
-
- if (!res.ok) {
- console.error(`Failed to upload asset ${id}: ${res.status}`);
- return null;
- }
- }
-
- return fileRef;
-}
-
-/** GCS requires every resumable chunk except the last to be a multiple of 256 KiB. */
-const RESUMABLE_CHUNK_SIZE = 8 * 1024 * 1024;
-
-/**
- * Open a GCS resumable upload session against a signed "resumable" URL and
- * return the session URI that the bytes are streamed to.
- */
-export async function startResumableSession(uploadUrl: string, mimeType: string): Promise<string> {
- const res = await fetch(uploadUrl, {
- method: "POST",
- headers: { "x-goog-resumable": "start", "Content-Type": mimeType },
- });
- if (!res.ok) {
- throw new Error(`Failed to start resumable upload session: ${res.status}`);
- }
- const sessionUrl = res.headers.get("Location");
- if (!sessionUrl) {
- throw new Error("Resumable upload session response is missing the Location header.");
- }
- return sessionUrl;
-}
-
-/**
- * Stream a ReadableStream into an open GCS resumable session, PUTting
- * fixed-size chunks as bytes arrive so the full payload is never buffered.
- */
-export async function uploadResumableStream(
- readable: ReadableStream<Uint8Array<ArrayBuffer>>,
- sessionUrl: string,
-): Promise<void> {
- const reader = readable.getReader();
- let pending: Uint8Array<ArrayBuffer> = new Uint8Array(0);
- let uploaded = 0;
- let done = false;
-
- while (!done) {
- const { value, done: streamDone } = await reader.read();
- if (streamDone) {
- done = true;
- } else if (value) {
- pending = concatBytes(pending, value);
- }
-
- while (pending.length >= RESUMABLE_CHUNK_SIZE) {
- await putResumableChunk(sessionUrl, pending.subarray(0, RESUMABLE_CHUNK_SIZE), uploaded, null);
- uploaded += RESUMABLE_CHUNK_SIZE;
- pending = pending.subarray(RESUMABLE_CHUNK_SIZE);
- }
- }
-
- await putResumableChunk(sessionUrl, pending, uploaded, uploaded + pending.length);
-}
-
-async function putResumableChunk(
- sessionUrl: string,
- chunk: Uint8Array<ArrayBuffer>,
- offset: number,
- total: number | null,
-): Promise<void> {
- const last = offset + chunk.length - 1;
- const contentRange =
- total === null
- ? `bytes ${offset}-${last}/*`
- : chunk.length === 0
- ? `bytes */${total}`
- : `bytes ${offset}-${last}/${total}`;
-
- const res = await fetch(sessionUrl, {
- method: "PUT",
- headers: { "Content-Range": contentRange },
- body: chunk,
- });
-
- // 308 "Resume Incomplete" is expected while more chunks remain; the final
- // chunk finalizes the object and returns 200/201.
- if (total === null) {
- if (res.status !== 308) {
- throw new Error(`Unexpected status during resumable upload: ${res.status}`);
- }
- } else if (!res.ok) {
- throw new Error(`Failed to finalize resumable upload: ${res.status}`);
- }
-}
-
-function concatBytes(a: Uint8Array<ArrayBuffer>, b: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer> {
- const out = new Uint8Array(a.length + b.length);
- out.set(a);
- out.set(b, a.length);
- return out;
-}
-
-function rehydrateAsset(asset: Asset): Asset {
- if (isSerializedElectronFileHandle(asset.handle)) {
- return { ...asset, handle: rehydrateElectronFileHandle(asset.handle) };
- }
- return asset;
-}
-
-export async function insertAssetInTimeline(world: EngineWorld, asset: Asset, mode: 'playhead' | 'start') {
- const sceneEid = world.selection.scene;
- if (sceneEid === null) {
- toast("Create a scene first to insert assets");
- return;
- }
- const c = world.components;
- const delay = mode === 'playhead' ? c.Computed.localTime[sceneEid] : 0;
-
- // Resolve any async asset data up front so all the entity mutations below
- // run in a single synchronous, undoable transaction.
- let transcriptTrim: { start: number; end: number } | null = null;
- if (asset.type === "TRANSCRIPT") {
- // Transcript should be trimmed to the span of recognized words
- const file = await getAssetFile(asset);
- const transcript = JSON.parse(await file.text()) as Transcript;
- const lastWord = transcript.at(-1)?.words.at(-1);
- const firstWord = transcript.at(0)?.words.at(0);
- transcriptTrim = {
- start: secondsToFrames(firstWord?.start),
- end: secondsToFrames(lastWord?.end),
- };
- }
-
- world.history.transaction('Insert asset', () => {
- const eid = createEntity(world);
- setComponent(world, eid, c.Name, asset.name);
- setComponent(world, eid, c.Delay, delay);
-
- if (asset.type === 'IMAGE' || asset.type === 'VIDEO' || asset.type === 'SEQUENCE') {
- const width = Math.round(asset.width);
- const height = Math.round(asset.height);
- resizeEntity(world, eid, { width, height });
- setComponent(world, eid, c.KeepAspectRatio, { width, height });
- }
-
- if (asset.type === "TRANSCRIPT") {
- setComponent(world, eid, c.Geometry, GeometryType.TEXT);
- setComponent(world, eid, c.Caption, {});
- if (transcriptTrim) setComponent(world, eid, c.Trim, transcriptTrim);
- } else {
- setComponent(world, eid, c.Geometry, GeometryType.RECT);
-
- // Other assets need a paint component
- let paint = PaintType.IMAGE;
- if (asset.type === 'SEQUENCE') paint = PaintType.SEQUENCE;
- if (asset.type === 'VIDEO') paint = PaintType.VIDEO;
- if (asset.type === 'AUDIO') paint = PaintType.WAVEFORM;
- const fillEid = createEntity(world);
- setComponent(world, fillEid, c.Paint, paint);
- setComponent(world, fillEid, c.AssetId, asset.id);
- if (asset.type === 'AUDIO') {
- addComponent(world, fillEid, c.Hidden);
- }
- appendChild(world, fillEid, eid);
- }
-
- if (asset.type === "AUDIO") {
- addComponent(world, eid, c.Audio);
- resizeEntity(world, eid, { width: 500, height: 150 });
- }
-
- if (asset.type === "AUDIO" || asset.type === "TRANSCRIPT") {
- setComponent(world, eid, c.AssetId, asset.id);
- }
-
- appendChild(world, eid, sceneEid);
- });
-};
-
-export async function replaceAssetHandle(world: EngineWorld, asset: Asset) {
- const { mimeType } = asset;
- let handle: FileSystemFileHandle | ElectronFileHandle | undefined;
-
- if (window.desktop) {
- const [picked] = await showFileDialog(mimeType, false);
- handle = picked as FileSystemFileHandle | ElectronFileHandle;
- } else {
- try {
- [handle] = await window.showOpenFilePicker({
- multiple: false,
- types: [
- {
- description: `${mimeType} files`,
- accept: { [mimeType]: [mimeTypeToExtension(mimeType)] },
- },
- ],
- });
- } catch {
- return;
- }
- }
- if (!handle) return;
-
- const detected = await detectMimeType(handle);
- if (detected !== mimeType) {
- toast.error("Mime type mismatch", {
- description: `Expected ${mimeType}, got ${detected ?? "unknown"}`,
- });
- return;
- }
-
- try {
- await saveAsset(world, { ...asset, handle });
- } catch (e) {
- toast.error("Failed to replace media", { description: (e as Error).message });
- }
-};
-
-type FileOrHandle = File | FileSystemFileHandle | FileSystemDirectoryHandle | ElectronFileHandle
-
-export async function getFileFromDataTransferItem(item?: DataTransferItem): Promise<FileOrHandle | null> {
- if (!item) return null;
-
- const file = item.getAsFile();
- if (typeof item.getAsFileSystemHandle !== 'function') return file;
- const handle = await item.getAsFileSystemHandle();
-
- if (!handle || (handle.kind !== 'file' && handle.kind !== 'directory')) {
- return null;
- }
-
- if (window.desktop && handle.kind === 'file' && file) {
- const path = window.desktop.getPathForFile(file);
- if (path) return ElectronFileHandle.fromFile(path, file);
- }
-
- return handle as FileOrHandle;
-}
-
-
-export async function detectMimeType(handle: MediaInput): Promise<string | null> {
- let mimeType: string | null = null;
-
- if (typeof handle === 'string') {
- mimeType = await fetchMimeType(handle);
- } else if (handle instanceof Blob) {
- mimeType = handle.type;
- } else if (handle instanceof FileSystemDirectoryHandle) {
- const entry = await handle.entries().next();
- mimeType = await detectMimeType(entry.value[1]);
- } else {
- handle = await handle.getFile();
- mimeType = handle.type;
- }
-
- // Subtitle files: the extension is authoritative — OS registries rarely map .srt.
- const fileName = typeof handle === 'string'
- ? handle.split(/[?#]/)[0]
- : (handle as File).name ?? '';
- if (/\.srt$/i.test(fileName) || mimeType === 'application/x-subrip') {
- return 'application/x-subrip';
- } else if (/\.vtt$/i.test(fileName) || mimeType === 'text/vtt') {
- return 'text/vtt';
- }
-
- if (mimeType?.startsWith('image/')) {
- return mimeType;
- } else if (mimeType?.startsWith('text/html')) {
- return mimeType;
- } else if (mimeType?.startsWith('application/json')) {
- return mimeType;
- } else if (handle instanceof FileSystemDirectoryHandle) {
- return mimeType?.startsWith('image/') ? mimeType : null;
- }
-
- // For audio and video, we will do a more thorough detection using mediabunny
- try {
- const input = new Input({
- formats: ALL_FORMATS,
- source: typeof handle === 'string'
- ? new UrlSource(handle)
- : new BlobSource(handle),
- });
-
- mimeType = await input.getMimeType();
- } catch {
- return null;
- }
-
- if (mimeType?.startsWith('audio/')) {
- return mimeType;
- } else if (mimeType?.startsWith('video/')) {
- return mimeType;
- } else {
- return null;
- }
-}
-
-async function getRasterDimensions(file: Blob): Promise<{ width: number; height: number }> {
- const bitmap = await createImageBitmap(file);
- const { width, height } = bitmap;
- bitmap.close();
- return { width, height };
-}
-
-async function getSvgDimensions(file: Blob): Promise<{ width: number; height: number }> {
- const text = await file.text();
- const root = new DOMParser().parseFromString(text, 'image/svg+xml').documentElement;
-
- const parseLength = (v: string | null) => {
- if (!v) return 0;
- const n = parseFloat(v);
- return Number.isFinite(n) ? n : 0;
- };
-
- let width = parseLength(root.getAttribute('width'));
- let height = parseLength(root.getAttribute('height'));
-
- if ((!width || !height) && root.getAttribute('viewBox')) {
- const parts = root.getAttribute('viewBox')!.split(/[\s,]+/).map(Number);
- if (parts.length === 4 && Number.isFinite(parts[2]) && Number.isFinite(parts[3])) {
- width = width || parts[2]!;
- height = height || parts[3]!;
- }
- }
-
- // SVG spec default when neither width/height nor viewBox is declared.
- return { width: width || 300, height: height || 150 };
-}
-
-export async function fetchMimeType(handle: string): Promise<string | null> {
- if (handle.startsWith('<html>')) {
- return 'text/html';
- }
-
- let response: Response;
- try {
- response = await fetch(handle, { method: 'HEAD' });
- } catch {
- const controller = new AbortController();
- response = await fetch(handle, { signal: controller.signal });
- controller.abort();
- }
-
- if (!response.ok) {
- return null;
- }
-
- const contentType = response.headers.get('Content-Type');
-
- if (!contentType) {
- return null;
- }
-
- return contentType;
-}
-
-/**
- * Playable content length of a media track, in seconds: the end timestamp
- * minus the track's first *presented* timestamp. A track may carry a global
- * time offset (a non-zero first timestamp); using the raw end timestamp as the
- * duration would over-report the length by that offset.
- *
- * A negative first timestamp means the head has been trimmed by an edit list
- * (e.g. a QuickTime `elst` with a non-zero media_time); those pre-roll samples
- * "should not be presented", so the presented content is [0, end]. Clamp the
- * start to 0 — otherwise `end - start` counts the trimmed region and
- * over-reports the duration by the trim amount. Clamped to >= 0.
- */
-async function trackContentDuration(track?: InputTrack | null): Promise<number> {
- if (!track) return 0;
-
- const [start, end] = await Promise.all([
- track.getFirstTimestamp(),
- track.computeDuration(),
- ]);
- return Math.max(0, end - Math.max(0, start));
-}
diff --git a/apps/web/src/components/engine/api/base.ts b/apps/web/src/components/engine/api/base.ts
deleted file mode 100644
index 95cc470e..00000000
--- a/apps/web/src/components/engine/api/base.ts
+++ /dev/null
@@ -1,168 +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 { query, getRelationTargets, Not, getAllEntities } from "bitecs";
-import { getCssVar } from "@/utils/browser";
-import { rgbToColor } from "@/utils/color";
-import { deserializeEntity } from "./serialize";
-import { ChildOf } from "../components/relations";
-import { restoreFonts } from "../font/utils";
-import * as tl from '../timeline';
-import { createEntity, deleteEntity } from "./entities";
-import { assert } from "@/utils";
-import { syncInteractiveState } from "./utils";
-import { isScene } from "./query";
-import { restoreAssets } from "./assets";
-import { restoreFolders } from "./folders";
-
-import type { EngineWorld } from "./world";
-import type { DBEntity, Camera2D } from "../types";
-
-/** Serialized world-level state (camera + background). */
-export type DBWorldState = {
- camera: Camera2D;
- background: number;
-};
-
-export function getParentEntity(world: EngineWorld, eid: number | null | undefined): number | null {
- if (!eid) return null;
-
- const targets = getRelationTargets(world, eid, ChildOf);
- if (targets.length === 0) return null;
- return targets[0];
-}
-
-export function getSiblingEntities(world: EngineWorld, eid: number, component: unknown): number[] {
- const c = world.components;
-
- const parentEid = getParentEntity(world, eid);
- if (parentEid === null) return [];
- const children = [...query(world, [ChildOf(parentEid), Not(c.Deleted), component])];
-
- return children;
-}
-
-/**
- * Placeholder: projects are held purely in memory. Hook project (JSX package)
- * persistence of camera + background in here once it lands.
- */
-export function persistWorldState(_world: EngineWorld): void { }
-
-/** Placeholder loader for entity records; projects are in memory only. */
-async function loadEntityRecords(_world: EngineWorld): Promise<DBEntity[]> {
- return [];
-}
-
-/** Placeholder loader for the world record; projects are in memory only. */
-async function loadWorldRecord(_world: EngineWorld): Promise<DBWorldState | undefined> {
- return undefined;
-}
-
-/**
- * Restores a world from its persisted records. Projects are currently held
- * purely in memory, so this boots an empty world; `records` / `worldRecord`
- * are placeholders for the upcoming project (JSX package) loader.
- */
-export async function restoreWorld(world: EngineWorld) {
- assert(getAllEntities(world).length === 0, 'World is not empty');
-
- const [records, worldRecord] = await Promise.all([
- loadEntityRecords(world),
- loadWorldRecord(world),
- ]);
-
- await Promise.all([
- restoreAssets(world),
- restoreFolders(world),
- restoreFonts(world),
- ]);
-
- const cam = worldRecord?.camera;
- const cameraValid = !!cam
- && [cam.a, cam.b, cam.c, cam.d, cam.e, cam.f].every(Number.isFinite)
- && cam.a > 0 && cam.d > 0;
-
- if (cameraValid) {
- world.camera.a = cam.a;
- world.camera.b = cam.b;
- world.camera.c = cam.c;
- world.camera.d = cam.d;
- world.camera.e = cam.e;
- world.camera.f = cam.f;
- } else {
- world.camera.a = 1;
- world.camera.b = 0;
- world.camera.c = 0;
- world.camera.d = 1;
- world.camera.e = 0;
- world.camera.f = 0;
- }
-
- if (worldRecord) {
- world.background = worldRecord.background;
- } else {
- const { r, g, b } = getCssVar('--color-canvas').toRgb();
- world.background = rgbToColor(r, g, b);
- }
-
- // Bitecs hands out fresh sequential eids after resetWorld, but the record
- // key is the entity's eid — so to keep records stable across reloads we
- // restore each entity at the exact eid it was saved under. Fill the gap
- // eids with placeholder entities up to the max stored eid, then remove
- // them once the real ones are deserialized.
- const liveEids = new Set(records.map(r => r.eid));
- const maxEid = records.length ? Math.max(...records.map(r => r.eid)) : 0;
-
- // Loading a project must never populate the undo stack: deserialize
- // untracked, then clear so the first undo can't unwind the boot.
- world.history.untrack(() => {
- const placeholders: number[] = [];
- for (let i = 1; i <= maxEid; i++) {
- const eid = createEntity(world);
- if (!liveEids.has(eid)) placeholders.push(eid);
- }
-
- for (const record of records) {
- try {
- const copy = { ...record };
- if (copy.ChildOf !== undefined && !liveEids.has(copy.ChildOf)) {
- // Parent entity is missing from the records — make this entity
- // top-level rather than letting the ChildOf dangle into a
- // placeholder slot that's about to be removed.
- delete copy.ChildOf;
- }
- deserializeEntity(world, record.eid, copy);
- } catch (error) {
- deleteEntity(world, record.eid);
- console.error(error);
- }
- }
-
- for (const eid of placeholders) {
- deleteEntity(world, eid);
- }
- });
-
- world.history.clear();
-
- syncInteractiveState(world);
-}
-
-export function switchActiveScene(world: EngineWorld, eid: number | null): void {
- if (eid !== null) {
- assert(isScene(world, eid), "Entity is not a scene");
- }
-
- if (eid === world.selection.scene) {
- return;
- }
-
- world.selection.scene = eid;
-
- tl.clearAudioBuffers();
- tl.clearVideoBuffers();
- tl.clearImageThumbnailCache();
- tl.updateTimelineTransform(world);
- world.rebuildTimelineIndex();
-}
diff --git a/apps/web/src/components/engine/api/clipboard.ts b/apps/web/src/components/engine/api/clipboard.ts
deleted file mode 100644
index 48ab6cda..00000000
--- a/apps/web/src/components/engine/api/clipboard.ts
+++ /dev/null
@@ -1,97 +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 { hasComponent, query, Or } from 'bitecs';
-import { cloneFromRecords, cloneSubtree, serializeEntity, stripMountIdentity } from './serialize';
-import { getEntityTree } from './query';
-import { getParentEntity } from './base';
-import { addComponent, clearComponent, setComponent } from './events';
-import { appendChild, removeChild } from './hierarchy';
-
-import type { DBEntity } from '../types';
-import type { EngineWorld } from './world';
-
-type ClipboardEntry = {
- root: number;
- records: DBEntity[];
-};
-
-let clipboard: ClipboardEntry[] = [];
-
-export function copySelection(world: EngineWorld) {
- const c = world.components;
- const selection = [...query(world, [c.Selected, Or(c.Geometry, c.Group)])];
- if (selection.length === 0) return;
-
- clipboard = selection.map(eid => ({
- root: eid,
- records: getEntityTree(world, eid).map(e => serializeEntity(world, e)),
- }));
-}
-
-export function pasteClipboard(world: EngineWorld) {
- if (clipboard.length === 0) return;
-
- const c = world.components;
- const selection = [...query(world, [c.Selected, Or(c.Scene, c.Group)])];
- const targetParentEid = selection[0] ?? null;
-
- world.history.transaction('Paste', () => {
- const copyRootEids: number[] = [];
-
- for (const snapshot of clipboard) {
- const result = cloneFromRecords(world, stripMountIdentity(snapshot.records));
- const rootCopyEid = result.get(snapshot.root);
- if (!rootCopyEid || (!hasComponent(world, rootCopyEid, c.Geometry) && !hasComponent(world, rootCopyEid, c.Group))) continue;
-
- copyRootEids.push(rootCopyEid);
-
- const currentParentEid = getParentEntity(world, rootCopyEid);
-
- if (currentParentEid !== null) {
- removeChild(world, rootCopyEid, currentParentEid);
- }
- if (targetParentEid !== null) {
- appendChild(world, rootCopyEid, targetParentEid);
- }
- }
-
- clearComponent(world, c.Selected, false);
-
- copyRootEids.forEach(eid => addComponent(world, eid, c.Selected, false));
- });
-}
-
-export function duplicateSelection(world: EngineWorld, { offset = true }: { offset?: boolean } = {}) {
- const c = world.components;
- const selection = [...query(world, [c.Selected, Or(c.Geometry, c.Group)])];
- if (selection.length === 0) return;
-
- const snapshots = selection.map(eid => ({
- root: eid,
- tree: getEntityTree(world, eid),
- }));
-
- world.history.transaction('Duplicate', () => {
- const copyRootEids: number[] = [];
- for (const snapshot of snapshots) {
- const result = cloneSubtree(world, snapshot.tree);
- const rootCopyEid = result.get(snapshot.root);
- if (!rootCopyEid || (!hasComponent(world, rootCopyEid, c.Geometry) && !hasComponent(world, rootCopyEid, c.Group))) continue;
-
- copyRootEids.push(rootCopyEid);
-
- const currentParentEid = getParentEntity(world, rootCopyEid);
- if (offset && currentParentEid === null) {
- // Source's Computed is populated; the copy's isn't refreshed until the next caching pass.
- const nextX = c.Position.x[rootCopyEid] + c.Computed.width[snapshot.root];
- setComponent(world, rootCopyEid, c.Position, { x: nextX });
- }
- }
-
- clearComponent(world, c.Selected, false);
-
- copyRootEids.forEach(eid => addComponent(world, eid, c.Selected, false));
- });
-}
diff --git a/apps/web/src/components/engine/api/components.ts b/apps/web/src/components/engine/api/components.ts
deleted file mode 100644
index d4dc79d4..00000000
--- a/apps/web/src/components/engine/api/components.ts
+++ /dev/null
@@ -1,19 +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 * as ComponentRegistry from "../components/components";
-
-export type { MountData } from "../components/components";
-
-export function createComponents(): Components {
- return Object.fromEntries(
- Object.entries(ComponentRegistry)
- .map(([name, definition]) => [name, structuredClone(definition)]),
- ) as Components;
-}
-
-export type Components = {
- [K in keyof typeof ComponentRegistry]: typeof ComponentRegistry[K];
-}
-export type ComponentName = keyof Components;
diff --git a/apps/web/src/components/engine/api/engine.ts b/apps/web/src/components/engine/api/engine.ts
deleted file mode 100644
index 895936d7..00000000
--- a/apps/web/src/components/engine/api/engine.ts
+++ /dev/null
@@ -1,167 +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 { ChildOf } from '../components';
-import { Not, query } from 'bitecs';
-import { createEngineWorld, restoreWorld, switchActiveScene } from '../api';
-
-import { transformSystem } from '../systems/transform';
-import { renderSystem } from '../systems/render';
-import { playbackSystem } from '../systems/playback';
-import { motionSystem } from '../systems/motion';
-import { syncDomSystem } from '../systems/dom';
-import { inputSystem } from '../systems/input-system';
-import { createCameraController } from '../systems';
-import { hudSystem } from '../systems/hud';
-import { createKeyboardSystem } from '../systems/keyboard';
-import { createSignal } from 'solid-js';
-
-import type Stats from 'stats.js';
-
-export type Engine = ReturnType<typeof createEngine>;
-
-
-export function createEngine(id: string) {
- const audio = new AudioContext({ latencyHint: 'playback' });
- const world = createEngineWorld(id, audio);
- const camera = createCameraController(world);
- const keyboard = createKeyboardSystem(world, camera);
- const [initialized, setInitialized] = createSignal(false);
-
- let running = false;
- let rafId: number | null = null;
- let stats: Stats | null = null;
-
- async function init(el: HTMLCanvasElement) {
- if (world.canvas) return;
-
- world.canvas = el;
- world.ctx = el.getContext('2d');
- camera.init(el);
- keyboard.init();
-
- await restoreWorld(world);
-
- const c = world.components;
- // Auto-select the first scene as the active timeline
- for (const eid of query(world, [c.Geometry, c.Scene, Not(ChildOf('*')), Not(c.Deleted)])) {
- switchActiveScene(world, eid);
- break;
- }
-
- // Mount FPS stats panel if in development mode
- if (import.meta.env.DEV) {
- const { default: Stats } = await import('stats.js');
- stats = new Stats();
- stats.showPanel(0); // 0 = FPS
- stats.dom.style.position = 'absolute';
- stats.dom.style.top = '12px';
- stats.dom.style.left = '12px';
- stats.dom.style.zIndex = '100';
- world.canvas.parentElement!.appendChild(stats.dom);
- }
-
- applyResize();
- start();
- setInitialized(true);
- }
-
- function tick(timestamp: number): void {
- if (!running || !world.ctx || !world.canvas) return;
-
- stats?.begin();
-
- world.timestamp.delta = timestamp - world.timestamp.now
- world.timestamp.now = timestamp
-
- inputSystem(world);
- playbackSystem(world);
- motionSystem(world);
- transformSystem(world);
- renderSystem(world);
- hudSystem(world);
- syncDomSystem(world);
-
- stats?.end();
-
- rafId = requestAnimationFrame(tick);
- }
-
- function start(): void {
- if (running) return;
- running = true;
- rafId = requestAnimationFrame(tick);
- }
-
- function stop(): void {
- running = false;
- if (rafId !== null) {
- cancelAnimationFrame(rafId);
- rafId = null;
- }
- }
-
- function dispose(): void {
- stop();
- world.liveMounts.disposeAll();
- keyboard.dispose();
- camera.dispose();
- }
-
- function applyResize(): void {
- const canvas = world.canvas;
- if (!(canvas instanceof HTMLCanvasElement)) return;
- const parent = canvas.parentElement;
- if (!parent) return;
-
- const rect = parent.getBoundingClientRect();
- const dpr = window.devicePixelRatio;
- const nextWidth = Math.round(rect.width * dpr);
- const nextHeight = Math.round(rect.height * dpr);
-
- canvas.style.width = `${rect.width}px`;
- canvas.style.height = `${rect.height}px`;
-
- // Always update world.canvas — the renderer reads it for the clear
- // rect and the transform system uses it as the top-level parent rect
- // for size/constraint/culling, regardless of bitmap reassignment.
- canvas.width = nextWidth;
- canvas.height = nextHeight;
- world.resolution = dpr;
-
- // Skip the bitmap reassignment when nothing changed — assigning
- // canvas.width/height resets the 2d context and clears the bitmap.
- if (canvas.width === nextWidth && canvas.height === nextHeight) return;
-
- canvas.width = nextWidth;
- canvas.height = nextHeight;
- }
-
- function resize(): void {
- applyResize();
-
- // ResizeObserver fires after layout, before paint. Without a synchronous
- // redraw, the just-cleared canvas (assigning width/height clears the
- // bitmap) paints empty until the next requestAnimationFrame — visible
- // as a flash during continuous resize. Re-run the render pipeline now
- // so the new canvas size paints with fresh content in the same frame.
- if (running) {
- transformSystem(world);
- renderSystem(world);
- syncDomSystem(world);
- }
- }
-
- return {
- init,
- start,
- stop,
- dispose,
- resize,
- initialized,
- get running() { return running; },
- get world() { return world; },
- get camera() { return camera; },
- };
-}
diff --git a/apps/web/src/components/engine/api/entities.ts b/apps/web/src/components/engine/api/entities.ts
deleted file mode 100644
index b272dac9..00000000
--- a/apps/web/src/components/engine/api/entities.ts
+++ /dev/null
@@ -1,83 +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 { addEntity, removeEntity } from "bitecs";
-import { DEFAULT_DURATION_FRAMES } from "../constants";
-import { addComponent } from "./events";
-import { getEntityTree } from "./query";
-
-import type { EngineWorld } from "./world";
-
-
-export function createEntity(world: EngineWorld) {
- const c = world.components;
- let eid!: number;
-
- world.history.push({
- label: 'creating new entity',
- execute: () => (eid = addEntity(world)),
- reverse: () => (removeEntity(world, eid)),
- })
-
- addComponent(world, eid, c.Computed, false);
-
- c.Computed.positionX[eid] = 0;
- c.Computed.positionY[eid] = 0;
- c.Computed.offsetX[eid] = 0;
- c.Computed.offsetY[eid] = 0;
- c.Computed.originX[eid] = 0;
- c.Computed.originY[eid] = 0;
- c.Computed.rotation[eid] = 0;
- c.Computed.scaleX[eid] = 1;
- c.Computed.scaleY[eid] = 1;
- c.Computed.skewX[eid] = 0;
- c.Computed.skewY[eid] = 0;
- c.Computed.opacity[eid] = 1;
- c.Computed.color[eid] = 0;
- c.Computed.blur[eid] = 0;
- c.Computed.volume[eid] = 0;
- c.Computed.value[eid] = 0;
- c.Computed.strokeWidth[eid] = 1;
- c.Computed.cornerRadius[eid] = 0;
- c.Computed.cornerRadiusTopLeft[eid] = 0;
- c.Computed.cornerRadiusTopRight[eid] = 0;
- c.Computed.cornerRadiusBottomRight[eid] = 0;
- c.Computed.cornerRadiusBottomLeft[eid] = 0;
- c.Computed.stopOffset[eid] = 0;
- c.Computed.stopColor[eid] = 0;
- c.Computed.stopOpacity[eid] = 1;
- c.Computed.width[eid] = 300;
- c.Computed.height[eid] = 300;
- c.Computed.start[eid] = 0;
- c.Computed.localTime[eid] = 0;
- c.Computed.localTimeInSeconds[eid] = 0;
- c.Computed.visibility[eid] = 1;
- c.Computed.end[eid] = DEFAULT_DURATION_FRAMES;
- c.Computed.duration[eid] = DEFAULT_DURATION_FRAMES;
- c.Computed.delay[eid] = 0;
-
- addComponent(world, eid, c.Cache, false);
- c.Cache.children[eid] = [];
- c.Cache.fills[eid] = [];
- c.Cache.shadows[eid] = [];
- c.Cache.strokes[eid] = [];
- c.Cache.effects[eid] = [];
- c.Cache.textRanges[eid] = [];
- c.Cache.masks[eid] = [];
- c.Cache.animations[eid] = [];
- c.Cache.keyframeTracks[eid] = [];
-
- return eid;
-}
-
-export function deleteEntity(world: EngineWorld, eid: number) {
- const c = world.components;
- // Tagging the whole subtree Deleted is one logical step. Nests under any
- // outer transaction (keyboard delete, ungroup, keyframe toggle, …).
- world.history.transaction('deleting entity', () => {
- for (const e of getEntityTree(world, eid)) {
- addComponent(world, e, c.Deleted);
- }
- });
-}
diff --git a/apps/web/src/components/engine/api/events.ts b/apps/web/src/components/engine/api/events.ts
deleted file mode 100644
index 15a794e2..00000000
--- a/apps/web/src/components/engine/api/events.ts
+++ /dev/null
@@ -1,184 +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 { addComponent as bitecsAddComponent, removeComponent as bitecsRemoveComponent, hasComponent, query } from "bitecs";
-
-import type { EngineWorld } from "./world";
-import type { HistoryEntry } from "../utils/history";
-
-// bitecs tags pair components (e.g. ChildOf(parent)) with this symbol pointing
-// back to the relation factory. We use it so observers registered against the
-// factory (ChildOf) fire for any target.
-const $relation = Symbol.for("bitecs-relation");
-
-export type ComponentData<C> =
- C extends readonly (infer V)[]
- ? V
- : Partial<{ [K in keyof C]: C[K] extends readonly (infer V)[] ? V : never }>;
-
-type EventName = "add" | "remove" | "set";
-
-type Callback<E extends EventName, C> =
- E extends "set"
- ? (eid: number, data: ComponentData<C>) => void
- : (eid: number) => void;
-
-type Registry = {
- observers: Map<unknown, Map<EventName, Set<Function>>>;
-};
-
-const REGISTRIES = new WeakMap<object, Registry>();
-
-function getRegistry(world: EngineWorld): Registry {
- let reg = REGISTRIES.get(world as object);
- if (!reg) {
- reg = { observers: new Map() };
- REGISTRIES.set(world as object, reg);
- }
- return reg;
-}
-
-/**
- * Push a history entry, or replay it untracked when `track` is false so it
- * never reaches the undo stack. Callers pass `track: false` for ephemeral or
- * derived runtime state (selection, hovering, drag snapshots, decoders, …)
- * that must not be undoable.
- */
-function record(world: EngineWorld, track: boolean, entry: HistoryEntry): void {
- if (track) {
- world.history.push(entry);
- } else {
- world.history.untrack(() => world.history.push(entry));
- }
-}
-
-function getKey(component: unknown): unknown {
- if (component && typeof component === "object" && $relation in (component as object)) {
- return (component as Record<symbol, unknown>)[$relation];
- }
- return component;
-}
-
-function notify(world: EngineWorld, event: EventName, component: unknown, eid: number, data: unknown): void {
- const perComponent = getRegistry(world).observers.get(getKey(component));
- const observers = perComponent?.get(event);
- if (!observers) return;
- for (const cb of observers) {
- (cb as (eid: number, data: unknown) => void)(eid, data);
- }
-}
-
-export function observe<E extends EventName, C>(
- world: EngineWorld,
- event: E,
- component: C,
- callback: Callback<E, C>,
-): () => void {
- const reg = getRegistry(world);
- const key = getKey(component);
-
- let perComponent = reg.observers.get(key);
- if (!perComponent) {
- perComponent = new Map();
- reg.observers.set(key, perComponent);
- }
-
- let observers = perComponent.get(event);
- if (!observers) {
- observers = new Set();
- perComponent.set(event, observers);
- }
-
- observers.add(callback as Function);
- return () => { observers!.delete(callback as Function); };
-}
-
-export function addComponent<C>(world: EngineWorld, eid: number, component: C, track = true): void {
- if (hasComponent(world, eid, component)) return;
-
- record(world, track, {
- label: `adding component ${componentName(world, component)} to entity ${eid}`,
- execute: () => {
- bitecsAddComponent(world, eid, component);
- notify(world, "add", component, eid, undefined);
- },
- reverse: () => {
- bitecsRemoveComponent(world, eid, component);
- notify(world, "remove", component, eid, undefined);
- },
- });
-}
-
-export function removeComponent<C>(world: EngineWorld, eid: number, component: C, track = true): void {
- if (!hasComponent(world, eid, component)) return;
-
- record(world, track, {
- label: `removing component ${componentName(world, component)} from entity ${eid}`,
- execute: () => {
- bitecsRemoveComponent(world, eid, component);
- notify(world, "remove", component, eid, undefined);
- },
- reverse: () => {
- bitecsAddComponent(world, eid, component);
- notify(world, "add", component, eid, undefined);
- },
- });
-}
-
-function hasSetObserver(world: EngineWorld, component: unknown): boolean {
- const observers = getRegistry(world).observers.get(getKey(component))?.get("set");
- return observers !== undefined && observers.size > 0;
-}
-
-function componentName(world: EngineWorld, component: unknown): string {
- for (const [name, value] of Object.entries(world.components)) {
- if (value === component) return name;
- }
- return "<unknown>";
-}
-
-function snapshotComponentData<C>(component: C, eid: number): ComponentData<C> {
- if (Array.isArray(component)) {
- return (component as unknown as unknown[])[eid] as ComponentData<C>;
- }
- const snapshot: Record<string, unknown> = {};
- for (const key in (component as object)) {
- snapshot[key] = (component as Record<string, unknown[]>)[key]?.[eid];
- }
- return snapshot as ComponentData<C>;
-}
-
-export function setComponent<C>(world: EngineWorld, eid: number, component: C, data: ComponentData<C>, track = true): void {
- if (!hasSetObserver(world, component) && typeof component === "object" && component !== null) {
- console.error(`setComponent: no 'set' observer registered for component '${componentName(world, component)}' — data passed to setComponent will be dropped.`);
- }
-
- const wasPresent = hasComponent(world, eid, component);
- const previous = wasPresent ? snapshotComponentData(component, eid) : undefined;
-
- record(world, track, {
- label: `setting component ${componentName(world, component)} on entity ${eid}`,
- execute: () => {
- bitecsAddComponent(world, eid, component);
- if (!wasPresent) notify(world, "add", component, eid, undefined);
- notify(world, "set", component, eid, data);
- },
- reverse: () => {
- if (!wasPresent) {
- bitecsRemoveComponent(world, eid, component);
- notify(world, "remove", component, eid, undefined);
- } else {
- notify(world, "set", component, eid, previous);
- }
- },
- });
-}
-
-export function clearComponent(world: EngineWorld, component: unknown, track = true): void {
- world.history.transaction(`Clearing component ${componentName(world, component)}`, () => {
- for (const eid of query(world, [component])) {
- removeComponent(world, eid, component, track);
- }
- })
-}
diff --git a/apps/web/src/components/engine/api/folders.ts b/apps/web/src/components/engine/api/folders.ts
deleted file mode 100644
index 8d3be493..00000000
--- a/apps/web/src/components/engine/api/folders.ts
+++ /dev/null
@@ -1,327 +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 { createSignal } from "solid-js";
-import { assert } from "@/utils";
-import { allocateId, assetsVersion, invalidateAssets, raiseIdCounter, saveAsset, removeAsset } from "./assets";
-
-import type { Accessor, Setter } from "solid-js";
-import type { EngineWorld } from "./world";
-import type { Asset, Folder } from "../db";
-
-type FolderState = {
- currentId: Accessor<string | null>;
- setCurrentId: Setter<string | null>;
-};
-
-const folderStates = new WeakMap<object, FolderState>();
-
-/**
- * Initializes the folder subsystem for a world. Call once, right after the
- * world is created. `world.folders` must already be an (empty) Map.
- */
-export function initFolders(world: EngineWorld, _projectId: string): void {
- const [currentId, setCurrentId] = createSignal<string | null>(null);
-
- folderStates.set(world, {
- currentId,
- setCurrentId,
- });
-}
-
-function folderState(world: EngineWorld): FolderState {
- const state = folderStates.get(world);
- assert(state, 'Folders not initialized for this world; call initFolders() first');
- return state;
-}
-
-/**
- * A folder's effective parent: its stored parentId, or root when the parent
- * record no longer exists (dangling references degrade to root instead of
- * hiding the folder).
- */
-function effectiveParentId(world: EngineWorld, folder: Folder): string | null {
- return folder.parentId !== null && world.folders.has(folder.parentId)
- ? folder.parentId
- : null;
-}
-
-/**
- * The folder an asset effectively lives in: its stored folderId, or root when
- * that folder no longer exists.
- */
-export function assetFolderId(world: EngineWorld, asset: Asset): string | null {
- const folderId = asset.folderId ?? null;
- return folderId !== null && world.folders.has(folderId) ? folderId : null;
-}
-
-/**
- * True when `id` is a descendant of `ancestorId` in the folder tree.
- */
-export function isDescendantFolder(world: EngineWorld, id: string, ancestorId: string): boolean {
- const visited = new Set<string>();
- let current = world.folders.get(id);
- while (current && current.parentId !== null && !visited.has(current.id)) {
- if (current.parentId === ancestorId) return true;
- visited.add(current.id);
- current = world.folders.get(current.parentId);
- }
- return false;
-}
-
-/**
- * Reactive view over the folder tree, for use inside Solid tracking scopes.
- * Folders share the asset version signal, so any folder or asset mutation
- * re-runs dependents.
- */
-export function useFolders(world: EngineWorld) {
- const state = folderState(world);
- return {
- /**
- * All folders, unordered.
- */
- all(): Folder[] {
- assetsVersion(world);
- return Array.from(world.folders.values());
- },
- /**
- * A single folder by id.
- */
- get(id: string): Folder | undefined {
- assetsVersion(world);
- return world.folders.get(id);
- },
- /**
- * Direct child folders of a parent (null = root), sorted by name.
- */
- childrenOf(parentId: string | null): Folder[] {
- assetsVersion(world);
- return Array.from(world.folders.values())
- .filter((folder) => effectiveParentId(world, folder) === parentId)
- .sort((a, b) => a.name.localeCompare(b.name));
- },
- /**
- * The id of the folder currently open in the asset panel (null = root).
- */
- currentId(): string | null {
- assetsVersion(world);
- const id = state.currentId();
- return id !== null && world.folders.has(id) ? id : null;
- },
- /**
- * Breadcrumb chain from the root down to the current folder.
- */
- path(): Folder[] {
- assetsVersion(world);
- const chain: Folder[] = [];
- const visited = new Set<string>();
- let current = state.currentId() !== null
- ? world.folders.get(state.currentId()!)
- : undefined;
- while (current && !visited.has(current.id)) {
- chain.unshift(current);
- visited.add(current.id);
- current = current.parentId !== null
- ? world.folders.get(current.parentId)
- : undefined;
- }
- return chain;
- },
- };
-}
-
-/**
- * Opens a folder in the asset panel (null = back to root).
- */
-export function openFolder(world: EngineWorld, id: string | null): void {
- folderState(world).setCurrentId(id);
-}
-
-/**
- * The folder currently open in the asset panel, for use outside tracking
- * scopes (event handlers, keyboard shortcuts). Reactive consumers should use
- * `useFolders(world).currentId()` instead.
- */
-export function getCurrentFolderId(world: EngineWorld): string | null {
- const id = folderState(world).currentId();
- return id !== null && world.folders.has(id) ? id : null;
-}
-
-/**
- * The next free `base`/`base N` name among a parent's children.
- */
-export function uniqueFolderName(world: EngineWorld, base: string, parentId: string | null): string {
- const names = new Set(
- Array.from(world.folders.values())
- .filter((folder) => effectiveParentId(world, folder) === parentId)
- .map((folder) => folder.name),
- );
- let name = base;
- for (let i = 2; names.has(name); i++) name = `${base} ${i}`;
- return name;
-}
-
-/**
- * The next free "New folder"/"New folder N" name among a parent's children.
- */
-export function nextFolderName(world: EngineWorld, parentId: string | null): string {
- return uniqueFolderName(world, 'New folder', parentId);
-}
-
-/**
- * Finds a direct child folder of `parentId` by name (case-insensitive),
- * creating it when missing.
- */
-export async function ensureFolder(
- world: EngineWorld,
- name: string,
- parentId: string | null = null,
-): Promise<Folder> {
- const target = name.trim().toLowerCase();
- for (const folder of world.folders.values()) {
- if (
- effectiveParentId(world, folder) === parentId &&
- folder.name.trim().toLowerCase() === target
- ) {
- return folder;
- }
- }
- return createFolder(world, name, parentId);
-}
-
-/**
- * Creates a folder under `parentId` (null = root) and persists it.
- */
-export async function createFolder(
- world: EngineWorld,
- name: string,
- parentId: string | null = null,
-): Promise<Folder> {
- const folder: Folder = {
- id: allocateId(world),
- name,
- parentId: parentId !== null && world.folders.has(parentId) ? parentId : null,
- createdAt: new Date().toISOString(),
- };
-
- world.folders.set(folder.id, folder);
- invalidateAssets(world);
-
- return folder;
-}
-
-/**
- * Renames a folder and persists it.
- */
-export async function renameFolder(world: EngineWorld, id: string, name: string): Promise<void> {
- const folder = world.folders.get(id);
- if (!folder || folder.name === name) return;
-
- const next = { ...folder, name };
- world.folders.set(id, next);
- invalidateAssets(world);
-}
-
-/**
- * Moves assets into a folder (null = root). Ignores unknown asset ids and
- * unknown target folders.
- */
-export async function moveAssetsToFolder(
- world: EngineWorld,
- ids: string[],
- folderId: string | null,
-): Promise<void> {
- if (folderId !== null && !world.folders.has(folderId)) return;
-
- for (const id of ids) {
- const asset = world.assets.get(id);
- if (!asset || (asset.folderId ?? null) === folderId) continue;
- await saveAsset(world, { ...asset, folderId });
- }
-}
-
-/**
- * Re-parents a folder (null = root). Returns false when the move would create
- * a cycle (dropping a folder into itself or one of its descendants).
- */
-export async function moveFolder(
- world: EngineWorld,
- id: string,
- parentId: string | null,
-): Promise<boolean> {
- const folder = world.folders.get(id);
- if (!folder) return false;
- if (parentId === id) return false;
- if (parentId !== null) {
- if (!world.folders.has(parentId)) return false;
- if (isDescendantFolder(world, parentId, id)) return false;
- }
- if (folder.parentId === parentId) return true;
-
- const next = { ...folder, parentId };
- world.folders.set(id, next);
- invalidateAssets(world);
- return true;
-}
-
-/**
- * Deletes a folder with its contents.
- */
-export async function deleteFolder(world: EngineWorld, id: string) {
- const folder = world.folders.get(id);
- if (!folder) return { deletedFolders: 0, deletedAssets: 0 };
-
- const state = folderState(world);
-
- // Navigate out of the doomed subtree before removing it.
- const currentId = state.currentId();
- if (currentId !== null && (currentId === id || isDescendantFolder(world, currentId, id))) {
- state.setCurrentId(folder.parentId);
- }
-
- const subtree = new Set([id]);
- let grew = true;
- while (grew) {
- grew = false;
- for (const candidate of world.folders.values()) {
- if (candidate.parentId !== null && subtree.has(candidate.parentId) && !subtree.has(candidate.id)) {
- subtree.add(candidate.id);
- grew = true;
- }
- }
- }
-
- const assetIds = Array.from(world.assets.values())
- .filter((asset) => asset.folderId != null && subtree.has(asset.folderId))
- .map((asset) => asset.id);
-
- if (assetIds.length > 0) {
- await removeAsset(world, ...assetIds);
- }
-
- for (const folderId of subtree) {
- world.folders.delete(folderId);
- }
-
- invalidateAssets(world);
- return { deletedFolders: subtree.size, deletedAssets: assetIds.length };
-}
-
-/**
- * Restores folders into the in-memory store. Call once during engine init.
- * Placeholder: projects are held purely in memory, so there is nothing to
- * load yet — wire the project (JSX package) loader in here.
- */
-export async function restoreFolders(world: EngineWorld): Promise<void> {
- const records: Folder[] = [];
- if (records.length === 0) return;
-
- world.folders.clear();
- for (const record of records) {
- world.folders.set(record.id, record);
- }
-
- raiseIdCounter(world, records.map((record) => record.id));
- invalidateAssets(world);
-}
diff --git a/apps/web/src/components/engine/api/frame.ts b/apps/web/src/components/engine/api/frame.ts
deleted file mode 100644
index 839a0631..00000000
--- a/apps/web/src/components/engine/api/frame.ts
+++ /dev/null
@@ -1,105 +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 { query, Or } from 'bitecs';
-import { GeometryType, PaintType } from '../components';
-import { aabbFromTransformedRect } from '../math';
-import { getParentEntity, switchActiveScene } from './base';
-import { getNextName, isScene } from './query';
-import { createEntity } from './entities';
-import { addComponent, clearComponent, setComponent } from './events';
-import { appendChild, removeChild } from './hierarchy';
-import { resizeEntity } from './resize';
-import { computeLocalMatrix } from '../systems/transform';
-
-import type { EngineWorld } from './world';
-import type { Mat2D } from '../math';
-
-/**
- * Wrap the current node selection in a new SCENE (a clipped, playable frame).
- * Mirrors `groupSelection`, but the wrapper is a Scene with its own fixed Size
- * derived from the selection's bounding box, plus the clipping/playback/fill a
- * scene needs. Already-existing scenes in the selection are skipped — scenes
- * can't be nested. Children's positions are rewritten into the scene's local
- * space so their on-canvas position is preserved. Returns the new scene's
- * entity id, or null if there was nothing to frame.
- */
-export function frameSelection(world: EngineWorld): number | null {
- const c = world.components;
- const selection = [...query(world, [c.Selected, Or(c.Geometry, c.Group)])]
- .filter(eid => !isScene(world, eid));
- if (selection.length === 0) return null;
-
- const sceneEid = world.history.transaction('Framing selection', () => {
- const targetParent = getParentEntity(world, selection[0]);
- const frameables = selection.filter(eid => getParentEntity(world, eid) === targetParent);
-
- // Bounding box of children in target-parent local space.
- let minX = Infinity;
- let minY = Infinity;
- let maxX = -Infinity;
- let maxY = -Infinity;
- for (const eid of frameables) {
- const mat: Mat2D = {
- a: c.LocalTransform.a[eid] ?? 1, b: c.LocalTransform.b[eid] ?? 0,
- c: c.LocalTransform.c[eid] ?? 0, d: c.LocalTransform.d[eid] ?? 1,
- e: c.LocalTransform.e[eid] ?? 0, f: c.LocalTransform.f[eid] ?? 0,
- };
- const w = c.Computed.width[eid];
- const h = c.Computed.height[eid];
- const bounds = aabbFromTransformedRect(mat, w, h);
- if (bounds.minX < minX) minX = bounds.minX;
- if (bounds.minY < minY) minY = bounds.minY;
- if (bounds.maxX > maxX) maxX = bounds.maxX;
- if (bounds.maxY > maxY) maxY = bounds.maxY;
- }
-
- const x = Math.round(minX);
- const y = Math.round(minY);
- const width = Math.max(1, Math.round(maxX - minX));
- const height = Math.max(1, Math.round(maxY - minY));
-
- // A scene is a RECT geometry carrying the Scene/ClipsContent/Playback
- // tags plus a solid fill — same recipe the scene draw tool uses.
- const eid = createEntity(world);
- setComponent(world, eid, c.Geometry, GeometryType.RECT);
- addComponent(world, eid, c.Scene);
- addComponent(world, eid, c.ClipsContent);
- setComponent(world, eid, c.Playback, {});
- setComponent(world, eid, c.Name, getNextName(world, 'Scene'));
- setComponent(world, eid, c.Position, { x, y });
- if (targetParent) appendChild(world, eid, targetParent);
- resizeEntity(world, eid, { width, height });
-
- const fillEid = createEntity(world);
- setComponent(world, fillEid, c.Paint, PaintType.SOLID);
- setComponent(world, fillEid, c.Color, 0x000000);
- appendChild(world, fillEid, eid);
-
- for (const child of frameables) {
- const prevX = c.Position.x[child] ?? 0;
- const prevY = c.Position.y[child] ?? 0;
- const parentEid = getParentEntity(world, child);
- if (parentEid) removeChild(world, child, parentEid);
- appendChild(world, child, eid);
- setComponent(world, child, c.Position, { x: prevX - x, y: prevY - y });
- // Refresh the child's cached local matrix now; the next transform pass
- // re-derives world transforms from it.
- computeLocalMatrix(world, child);
- }
-
- clearComponent(world, c.Selected, false);
- addComponent(world, eid, c.Selected, false);
- switchActiveScene(world, eid);
-
- return eid;
- });
-
- // Make the freshly created frame the active scene (mirrors the scene tool),
- // so the timeline retargets to it. Done outside history — active-scene state
- // isn't part of the undo stack.
- if (sceneEid !== null) switchActiveScene(world, sceneEid);
-
- return sceneEid;
-}
diff --git a/apps/web/src/components/engine/api/group.ts b/apps/web/src/components/engine/api/group.ts
deleted file mode 100644
index 9e19d897..00000000
--- a/apps/web/src/components/engine/api/group.ts
+++ /dev/null
@@ -1,135 +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 { query, Not, Or } from 'bitecs';
-import { ChildOf } from '../components';
-import { aabbFromTransformedRect } from '../math';
-import { getParentEntity } from './base';
-import { getNextName } from './query';
-import { createEntity, deleteEntity } from './entities';
-import { addComponent, clearComponent, setComponent } from './events';
-import { appendChild, removeChild } from './hierarchy';
-import { resolveNewSequenceOverlaps } from './overlap';
-import { computeLocalMatrix } from '../systems/transform';
-import { sortByItemIndex } from '../utils/sort';
-
-import type { EngineWorld } from './world';
-import type { Mat2D } from '../math';
-
-/**
- * Wrap the current node selection in a new GROUP. All selected nodes must
- * share the same parent; scenes are skipped. Children's positions are
- * rewritten into the group's local space so their on-canvas position is
- * preserved.
- */
-export function groupSelection(world: EngineWorld, sequential: boolean = false): number | null {
- const c = world.components;
- const selection = [...query(world, [c.Selected, Or(c.Geometry, c.Group)])];
- if (selection.length === 0) return null;
-
- return world.history.transaction('Grouping selection', () => {
- const targetParent = getParentEntity(world, selection[0]);
-
- const groupables = selection
- .filter(eid => getParentEntity(world, eid) === targetParent)
- .sort(sortByItemIndex(world));
-
- // Bounding box of children in target-parent local space.
- let minX = Infinity;
- let minY = Infinity;
- let maxX = -Infinity;
- let maxY = -Infinity;
- for (const eid of groupables) {
- const mat: Mat2D = {
- a: c.LocalTransform.a[eid] ?? 1, b: c.LocalTransform.b[eid] ?? 0,
- c: c.LocalTransform.c[eid] ?? 0, d: c.LocalTransform.d[eid] ?? 1,
- e: c.LocalTransform.e[eid] ?? 0, f: c.LocalTransform.f[eid] ?? 0,
- };
- const w = c.Computed.width[eid];
- const h = c.Computed.height[eid];
- const bounds = aabbFromTransformedRect(mat, w, h);
- if (bounds.minX < minX) minX = bounds.minX;
- if (bounds.minY < minY) minY = bounds.minY;
- if (bounds.maxX > maxX) maxX = bounds.maxX;
- if (bounds.maxY > maxY) maxY = bounds.maxY;
- }
-
- // Sequences sit at the parent's origin (they're passthrough); plain
- // groups sit at the children's AABB origin so their own pivot lines up.
- const x = sequential ? 0 : Math.round(minX);
- const y = sequential ? 0 : Math.round(minY);
-
- const topItemIndex = c.ItemIndex[groupables[groupables.length - 1]] ?? 0;
-
- const groupEid = createEntity(world);
- addComponent(world, groupEid, c.Group);
- if (sequential) {
- addComponent(world, groupEid, c.Sequential);
- }
- setComponent(world, groupEid, c.Position, { x, y });
- setComponent(world, groupEid, c.Name, getNextName(world, sequential ? 'Sequence' : 'Group'));
- if (targetParent) appendChild(world, groupEid, targetParent);
- setComponent(world, groupEid, c.ItemIndex, topItemIndex);
-
- for (const [index, eid] of groupables.entries()) {
- const prevX = c.Position.x[eid] ?? 0;
- const prevY = c.Position.y[eid] ?? 0;
- const parentEid = getParentEntity(world, eid);
- if (parentEid) removeChild(world, eid, parentEid);
- appendChild(world, eid, groupEid);
- setComponent(world, eid, c.ItemIndex, index);
- setComponent(world, eid, c.Position, { x: prevX - x, y: prevY - y });
- // Refresh the child's cached local matrix now. The next transform pass
- computeLocalMatrix(world, eid);
- }
-
-
- if (sequential) {
- resolveNewSequenceOverlaps(world, groupEid);
- }
-
- clearComponent(world, c.Selected, false);
- addComponent(world, groupEid, c.Selected, false);
-
- return groupEid;
- });
-}
-
-/**
- * Inverse of `groupSelection`. For every GROUP in the current selection,
- * lift its direct children up to the group's parent and remove the group.
- * Children's positions are rewritten so they stay put on canvas.
- */
-export function ungroupSelection(world: EngineWorld): void {
- const c = world.components;
- const groups = [...query(world, [c.Selected, c.Group])];
- if (groups.length === 0) return;
-
- world.history.transaction('Ungrouping selection', () => {
- const promoted: number[] = [];
-
- for (const groupEid of groups) {
- const targetParent = getParentEntity(world, groupEid);
- // speard is important here
- const children = [...query(world, [ChildOf(groupEid), Not(c.Deleted)])];
- for (const childEid of children) {
- removeChild(world, childEid, groupEid);
- setComponent(world, childEid, c.Position, {
- x: (c.Position.x[childEid] ?? 0) + (c.Position.x[groupEid] ?? 0),
- y: (c.Position.y[childEid] ?? 0) + (c.Position.y[groupEid] ?? 0),
- });
-
- if (targetParent) {
- appendChild(world, childEid, targetParent);
- }
- promoted.push(childEid);
- }
-
- deleteEntity(world, groupEid);
- }
-
- clearComponent(world, c.Selected, false);
- promoted.forEach(eid => addComponent(world, eid, c.Selected, false));
- });
-}
diff --git a/apps/web/src/components/engine/api/hierarchy.ts b/apps/web/src/components/engine/api/hierarchy.ts
deleted file mode 100644
index c19da2a0..00000000
--- a/apps/web/src/components/engine/api/hierarchy.ts
+++ /dev/null
@@ -1,148 +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 { addComponent, removeComponent, query, Not, Or } from "bitecs";
-import { ChildOf } from "../components";
-import * as utils from "./utils";
-import { getParentEntity } from "./base";
-import { setComponent } from "./events";
-import { assert } from "@/utils";
-import { getEntityTree } from "./query";
-import { sortByItemIndex } from "../utils/sort";
-
-import type { EngineWorld } from "./world";
-
-export type ReorderTarget = 'front' | 'back' | number;
-
-export function appendChild(world: EngineWorld, eid: number, pid: number): void {
- const parentEid = getParentEntity(world, eid);
- assert(parentEid === null, "Entity already has a parent");
- assert(pid !== eid, "Cannot parent entity into itself");
- assert(!getEntityTree(world, eid).includes(pid), "Cannot parent entity into its own subtree");
-
- world.history.push({
- label: `appending child ${eid} to parent ${pid}`,
- execute: () => appendChildComponent(world, eid, pid),
- reverse: () => removeChildComponent(world, eid, pid),
- });
-}
-
-export function removeChild(world: EngineWorld, eid: number, pid: number): void {
- const parentEid = getParentEntity(world, eid);
- assert(parentEid === pid, "Entity is not a child of the specified parent");
-
- world.history.push({
- label: `removing child ${eid} from parent ${pid}`,
- execute: () => removeChildComponent(world, eid, pid),
- reverse: () => appendChildComponent(world, eid, pid),
- });
-}
-
-function appendChildComponent(world: EngineWorld, eid: number, pid: number): void {
- addComponent(world, eid, ChildOf(pid));
- utils.rebuildCaches(world, eid, pid);
- utils.propagateSize(world, eid);
- utils.resolveConstraintOffsets(world, eid);
- utils.disconnectAudioBus(world, eid);
- utils.reactToChildAttached(world, eid);
- utils.persistEntity(world, eid);
- utils.syncInteractiveState(world);
- world.rebuildTimelineIndex();
-}
-
-function removeChildComponent(world: EngineWorld, eid: number, pid: number): void {
- removeComponent(world, eid, ChildOf(pid));
- utils.rebuildCaches(world, eid, pid);
- utils.disconnectAudioBus(world, eid);
- utils.persistEntity(world, eid);
- utils.syncInteractiveState(world);
- world.rebuildTimelineIndex();
-}
-
-/**
- * Move a single entity within its geometry siblings by rewriting `ItemIndex`
- * on every sibling. `target` may be:
- * - `'front'` — move to the last index (highest ItemIndex = drawn on top)
- * - `'back'` — move to index 0 (lowest ItemIndex = drawn first)
- * - a number — move to that absolute index (clamped into range)
- */
-export function reorderEntity(world: EngineWorld, eid: number, target: ReorderTarget): void {
- const c = world.components;
- const parent = getParentEntity(world, eid);
- if (parent === null) return;
- const siblings = [...query(world, [ChildOf(parent), Not(c.Deleted), Or(c.Geometry, c.Group, c.AdjustmentLayer)])];
- siblings.sort(sortByItemIndex(world));
- const index = siblings.indexOf(eid);
- if (index === -1) return;
- const newIndex = resolveReorderIndex(target, siblings.length);
- if (index === newIndex) return;
- siblings.splice(newIndex, 0, siblings.splice(index, 1)[0]);
- world.history.transaction('Reorder', () => {
- for (const [idx, sib] of siblings.entries()) {
- setComponent(world, sib, c.ItemIndex, idx);
- }
- });
-}
-
-/**
- * Move the current geometry selection within its parent. `target` may be:
- * - `'front'` — move all selected entities to the front
- * - `'back'` — move all selected entities to the back
- * - a number — insert the selected entities starting at that absolute index
- *
- * Selections that span multiple parents are reordered per-parent; the
- * relative order of the selected entities within each parent is preserved.
- */
-export function reorderSelection(world: EngineWorld, target: ReorderTarget): void {
- const c = world.components;
- const selection = [...query(world, [c.Selected, Or(c.Geometry, c.Group, c.AdjustmentLayer)])];
- if (selection.length === 0) return;
-
- const byParent = new Map<number, Set<number>>();
- for (const eid of selection) {
- const parent = getParentEntity(world, eid);
- if (parent === null) continue;
- if (!byParent.has(parent)) byParent.set(parent, new Set());
- byParent.get(parent)!.add(eid);
- }
-
- world.history.transaction('Reorder', () => {
- for (const [parent, selected] of byParent) {
- const siblings = [...query(world, [ChildOf(parent), Not(c.Deleted), Or(c.Geometry, c.Group, c.AdjustmentLayer)])];
- siblings.sort(sortByItemIndex(world));
- const selectedOrdered = siblings.filter(eid => selected.has(eid));
- const rest = siblings.filter(eid => !selected.has(eid));
-
- let newOrder: number[];
- if (target === 'front') {
- newOrder = [...rest, ...selectedOrdered];
- } else if (target === 'back') {
- newOrder = [...selectedOrdered, ...rest];
- } else {
- // Insert the selected block starting at the target index, clamped so
- // the whole block still fits inside the sibling array.
- const maxStart = Math.max(0, rest.length);
- const insertAt = Math.max(0, Math.min(maxStart, Math.trunc(target)));
- newOrder = [...rest.slice(0, insertAt), ...selectedOrdered, ...rest.slice(insertAt)];
- }
-
- let changed = false;
- for (let i = 0; i < newOrder.length; i++) {
- if (newOrder[i] !== siblings[i]) { changed = true; break; }
- }
- if (!changed) continue;
-
- for (const [idx, sib] of newOrder.entries()) {
- setComponent(world, sib, c.ItemIndex, idx);
- }
- }
- });
-}
-
-function resolveReorderIndex(target: ReorderTarget, length: number): number {
- if (target === 'front') return length - 1;
- if (target === 'back') return 0;
- // Numeric target: clamp into [0, length - 1].
- return Math.max(0, Math.min(length - 1, Math.trunc(target)));
-}
diff --git a/apps/web/src/components/engine/api/index.ts b/apps/web/src/components/engine/api/index.ts
deleted file mode 100644
index 7f58d524..00000000
--- a/apps/web/src/components/engine/api/index.ts
+++ /dev/null
@@ -1,25 +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/. */
-
-export * from "./base";
-export * from "./serialize";
-export * from "./query";
-export * from "./keyframe";
-export * from "./clipboard";
-export * from "./group";
-export * from "./frame";
-export * from "./playback";
-export * from "./overlap";
-export * from "./assets";
-export * from "./folders";
-export * from "./engine";
-export * from "./entities";
-export * from "./components";
-export * from "./world";
-export * from "./observers";
-export * from "./events";
-export * from "./hierarchy";
-export * from "./timeline-index";
-export * from "./interactions";
-export * from "./resize";
diff --git a/apps/web/src/components/engine/api/interactions.ts b/apps/web/src/components/engine/api/interactions.ts
deleted file mode 100644
index 469b55db..00000000
--- a/apps/web/src/components/engine/api/interactions.ts
+++ /dev/null
@@ -1,888 +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 { hasComponent, None, Not, Or, query } from "bitecs";
-import { ChildOf, computeLocalMatrix, decompose2D, duplicateSelection, getParentEntity, identity2D, multiply2D, quadsIntersect, rectToQuad, rotate2D, scale2D, translate2D, type Mat2D, type Point, type Quad, transformPoint, invert2D, getSceneAncestor, isPointerInEntity, appendChild, removeChild, togglePlayback, switchActiveScene, transformSystem, clearComponent, computeGroupBounds } from "..";
-import { addComponent, removeComponent, setComponent, clearSelectedAssets, findKeyframeTrackEntity } from "../api";
-import { resizeEntity } from "./resize";
-import { syncInteractiveState } from "./utils";
-import { buildSnapCandidatesFromCorners, buildSnapCandidatesFromQuad, entityAnchor, entityOffset, entityQuad, entityWorldMat, findSnapTarget, getMarqueeQuad, getSelectionMaskSnapshot, getSelectionTransfromSnapshot, getSnapCandidatesSnapshot, mountNameInput, snapshotSelectionMask, snapshotSelectionTransforms, snapshotSnapCandidates, updateResizeCursor, updateRotationCursor } from "../utils/interaction";
-import { assert } from "@/utils";
-import z from "zod";
-
-import type { DispatchedPointerEvent } from "../types";
-import type { EngineWorld } from "./world";
-
-const SNAP_DISTANCE = 6;
-
-export function handleGeometryInteraction(world: EngineWorld, event: DispatchedPointerEvent) {
- const c = world.components;
-
- // Equivalent to pointerdown on an entity/ click
- if (event.type === 'dragstart' && event.target.kind === 'entity') {
- if (world.keys.has('shift')) {
- addComponent(world, event.target.id, c.Selected, false);
- } else {
- clearComponent(world, c.Selected, false);
- addComponent(world, event.target.id, c.Selected, false);
- }
-
- handleMaskInteraction(world, event);
- }
-
- if (event.type === 'drag') {
- handleMaskInteraction(world, event);
- }
-
- if (event.type === 'dragend') {
- handleMaskInteraction(world, event);
- }
-
- if (event.type === 'dblclick' && event.target.kind === 'entity') {
- const parentEid = event.target.id;
- const childEid = findChildHitTarget(world, parentEid, event.clientX, event.clientY);
- if (childEid !== null) {
- removeComponent(world, parentEid, c.Interactive, false);
- for (const cid of query(world, [Or(c.Geometry, c.Group), ChildOf(parentEid), Not(c.Deleted)])) {
- addComponent(world, cid, c.Interactive, false);
- }
- clearComponent(world, c.Selected, false);
- clearComponent(world, c.Hovering, false);
- addComponent(world, childEid, c.Selected, false);
- addComponent(world, childEid, c.Hovering, false);
- }
- }
-
- if (world.pointer.state === 'lifted') {
- world.cursor.update('default');
- }
-}
-
-function findChildHitTarget(world: EngineWorld, parentEid: number, x: number, y: number): number | null {
- const c = world.components;
- const children = query(world, [Or(c.Geometry, c.Group), ChildOf(parentEid), Not(c.IsMask), Not(c.Culled), Not(c.Deleted)]);
- for (let i = children.length - 1; i >= 0; i--) {
- if (isPointerInEntity(world, children[i], { x, y })) {
- return children[i];
- }
- }
- return null;
-}
-
-export function handleCanvasInteraction(world: EngineWorld, event: DispatchedPointerEvent) {
- const c = world.components;
-
- if (event.type === 'dragstart') {
- world.hudMode = 'marquee';
- }
-
- if (event.type === 'drag' && world.hudMode === 'marquee') {
- const marqueeQuad = getMarqueeQuad(world);
- if (marqueeQuad) {
- clearComponent(world, c.Selected, false);
- for (const eid of query(world, [c.Interactive, Or(c.Geometry, c.Group), Not(c.Deleted), Not(c.Culled)])) {
- if (quadsIntersect(marqueeQuad, entityQuad(world, eid))) {
- addComponent(world, eid, c.Selected, false);
- }
- }
- }
-
- world.cursor.update('default');
- }
-
- if (event.type === 'dragend') {
- world.hudMode = 'idle';
- }
-
- if (event.type === 'pointerup') {
- const x0 = world.pointer.dragStartX;
- const y0 = world.pointer.dragStartY;
- const x1 = event.clientX;
- const y1 = event.clientY;
- const distance = Math.sqrt((x1 - x0) ** 2 + (y1 - y0) ** 2);
- if (distance < 4) {
- clearComponent(world, c.Selected, false);
- syncInteractiveState(world);
- clearComponent(world, c.Hovering, false);
- clearSelectedAssets(world);
- }
- }
-
- if (world.pointer.state === 'lifted') {
- world.cursor.update('default');
- }
-}
-
-export function handlePlayInteraction(world: EngineWorld, event: DispatchedPointerEvent) {
- if (event.target.kind !== 'hud') return;
- const eid = parseHudEntityId(event.target.id, 'play');
- if (eid === null) return;
-
- if (event.type === 'click') {
- togglePlayback(world, eid);
- }
-}
-
-export function handleLabelInteraction(world: EngineWorld, event: DispatchedPointerEvent) {
- if (event.target.kind !== 'hud') return;
- const eid = parseHudEntityId(event.target.id, 'label');
- if (eid === null) return;
- const c = world.components;
-
- if (event.type === 'pointerenter') {
- clearComponent(world, c.Hovering, false);
- addComponent(world, eid, c.Hovering, false);
- }
-
- if (event.type === 'pointerleave') {
- clearComponent(world, c.Hovering, false);
- }
-
- if (event.type === 'dragstart') {
- if (world.keys.has('shift')) {
- addComponent(world, eid, c.Selected, false);
- } else {
- clearComponent(world, c.Selected, false);
- addComponent(world, eid, c.Selected, false);
- }
- }
-
- if (event.type === 'dblclick') {
- mountNameInput(world, eid);
- }
-
- if (event.type === 'click' && hasComponent(world, eid, c.Scene)) {
- switchActiveScene(world, eid);
- }
-
- handleMaskInteraction(world, event);
-}
-
-function parseHudEntityId(id: string, prefix: string): number | null {
- if (!id.startsWith(`${prefix}:`)) return null;
- const eid = Number(id.slice(prefix.length + 1));
- return Number.isFinite(eid) ? eid : null;
-}
-
-export function handleResizeInteraction(world: EngineWorld, event: DispatchedPointerEvent) {
- const c = world.components;
-
- const orientation = anchorSchema.parse(event.target.id);
- const snapshot = getSelectionMaskSnapshot();
-
- if (event.type === 'dragstart') {
- snapshotSelectionMask(world);
- snapshotSelectionTransforms(world);
- snapshotSnapCandidates(world);
- world.snapLines = [];
- world.history.startTransaction('Resize');
- }
-
- if (event.type === 'drag' && snapshot) {
- const oldTr = snapshot.mat;
- const selection = query(world, [c.Selected, Or(c.Geometry, c.Group)]);
- world.snapLines.length = 0;
-
- const currentLocalPoint = getPointerCurrentPoint(world, oldTr);
- const initialLocalPoint = getPointerInitialPoint(world, oldTr);
-
- // Calculate delta in mask local space
- const deltaX = currentLocalPoint.x - initialLocalPoint.x;
- const deltaY = currentLocalPoint.y - initialLocalPoint.y;
-
- // How the drag distance relates to the mask size
- const factor = HANDLE_RESIZE_FACTOR[orientation];
-
- // Delta size
- let deltaWidth = deltaX * factor.x;
- let deltaHeight = deltaY * factor.y;
-
- // Lock aspect ratio when shift is held OR every selected entity has KeepAspectRatio.
- // Both paths run through the same math here so the pivot/position stays consistent;
- // when KAR's stored ratio matches the mask ratio (the observer keeps them in sync),
- // the Size observer's enforceAspectRatio becomes a no-op.
- const lockAR = world.keys.has('shift') || (
- selection.length > 0 && selection.every(eid => hasComponent(world, eid, c.KeepAspectRatio))
- );
-
- if (lockAR) {
- const w0 = snapshot.width;
- const h0 = snapshot.height;
-
- if (w0 > 0 && h0 > 0) {
- if (['tl', 'tr', 'bl', 'br'].includes(orientation)) {
- // Project the pointer delta onto the pivot→corner diagonal: the dragged
- // corner gets placed at the closest point on the AR-locked ray, so the
- // cursor stays as close as possible to the corner without ever leaping
- // past it on either axis.
- const denom = w0 * w0 + h0 * h0;
- const t = (deltaX * factor.x * w0 + deltaY * factor.y * h0) / denom;
- deltaWidth = w0 * t;
- deltaHeight = h0 * t;
- } else if (['l', 'r'].includes(orientation)) {
- const scale = (w0 + deltaWidth) / w0;
- deltaHeight = h0 * (scale - 1);
- } else if (['t', 'b'].includes(orientation)) {
- const scale = (h0 + deltaHeight) / h0;
- deltaWidth = w0 * (scale - 1);
- }
- }
- }
-
- // Pivot point: opposite to the handle being dragged
- // factor -1 → 1 (full), factor 0 → 0.5 (center), factor 1 → 0
- let pivotX = snapshot.width * (1 - factor.x) / 2;
- let pivotY = snapshot.height * (1 - factor.y) / 2;
-
- // When Alt is pressed, we want to scale the selection from the center
- if (world.keys.has('alt')) {
- pivotX = snapshot.width * 0.5;
- pivotY = snapshot.height * 0.5;
- deltaWidth = deltaWidth * 2;
- deltaHeight = deltaHeight * 2;
- }
-
- let newWidth = snapshot.width + deltaWidth;
- let newHeight = snapshot.height + deltaHeight;
-
- // Snap the resize vertices to nearby candidates.
- {
- const proposedScaleX = newWidth / snapshot.width;
- const proposedScaleY = newHeight / snapshot.height;
-
- let proposedLocalScaleTr = translate2D(pivotX, pivotY);
- proposedLocalScaleTr = multiply2D(proposedLocalScaleTr, scale2D(proposedScaleX, proposedScaleY));
- proposedLocalScaleTr = multiply2D(proposedLocalScaleTr, translate2D(-pivotX, -pivotY));
-
- const proposedWorldTr = multiply2D(oldTr, proposedLocalScaleTr);
- const proposedQuad = rectToQuad(
- proposedWorldTr,
- snapshot.width,
- snapshot.height,
- );
-
- // Only snap the vertices of the edge/corner being dragged
- // rectToQuad returns [TL, TR, BR, BL]
- const snapCandidates = getSnapCandidatesSnapshot();
- const draggedVertices = getResizeHandleVertices(orientation, proposedQuad);
- const cornerCandidates = buildSnapCandidatesFromCorners(draggedVertices);
- const snapThreshold = SNAP_DISTANCE * world.resolution;
-
- if (lockAR) {
- // The dragged corner moves linearly from `base` (s=0, the pivot in
- // world space) through `sourcePoint` (s=proposedScale). Each snap
- // line crosses that ray at exactly one scale, so the ratio of new-to-
- // old scale that puts the corner on it is just (span + offset) / span
- // on the snapped axis. Pick the snap whose ratio is closest to 1 and
- // apply it uniformly to both axes.
- const xSnap = findSnapTarget(cornerCandidates.x, snapCandidates.x, snapThreshold, 'x');
- const ySnap = findSnapTarget(cornerCandidates.y, snapCandidates.y, snapThreshold, 'y');
- const base = transformPoint(oldTr, pivotX, pivotY);
-
- const ratioOf = (snap: { sourcePoint: Point; offset: number } | null, axis: 'x' | 'y') => {
- if (!snap) return null;
- const span = snap.sourcePoint[axis] - base[axis];
- return Math.abs(span) < 1e-9 ? null : (span + snap.offset) / span;
- };
-
- const rX = ratioOf(xSnap, 'x');
- const rY = ratioOf(ySnap, 'y');
- const pickX = rX !== null && (rY === null || Math.abs(rX - 1) <= Math.abs(rY - 1));
- const pickY = rY !== null && !pickX;
- const ratio = pickX ? rX! : pickY ? rY! : null;
- const snap = pickX ? xSnap! : pickY ? ySnap! : null;
-
- if (snap && ratio !== null) {
- newWidth *= ratio;
- newHeight *= ratio;
- world.snapLines.push({
- from: {
- x: base.x + (snap.sourcePoint.x - base.x) * ratio,
- y: base.y + (snap.sourcePoint.y - base.y) * ratio,
- },
- to: snap.targetPoint,
- });
- }
- } else {
- const xSnap = factor.x !== 0 ? findSnapTarget(cornerCandidates.x, snapCandidates.x, snapThreshold, 'x') : null;
- const ySnap = factor.y !== 0 ? findSnapTarget(cornerCandidates.y, snapCandidates.y, snapThreshold, 'y') : null;
-
- if (xSnap) world.snapLines.push({
- from: {
- x: xSnap.sourcePoint.x + xSnap.offset,
- y: xSnap.sourcePoint.y + (ySnap?.offset ?? 0),
- },
- to: xSnap.targetPoint
- });
-
- if (ySnap) world.snapLines.push({
- from: {
- x: ySnap.sourcePoint.x + (xSnap?.offset ?? 0),
- y: ySnap.sourcePoint.y + ySnap.offset,
- },
- to: ySnap.targetPoint
- });
-
-
- if (xSnap || ySnap) {
- // Convert the world-space snap offset back into a local-space size correction
- const inverseTr = invert2D(oldTr);
- inverseTr.e = 0;
- inverseTr.f = 0;
- const localCorrection = transformPoint(inverseTr, xSnap?.offset ?? 0, ySnap?.offset ?? 0);
-
- // The correction needs to be scaled by the factor and alt multiplier
- const altMultiplier = world.keys.has('alt') ? 2 : 1;
- if (factor.x !== 0) {
- deltaWidth += localCorrection.x * factor.x * altMultiplier;
- }
- if (factor.y !== 0) {
- deltaHeight += localCorrection.y * factor.y * altMultiplier;
- }
-
- newWidth = snapshot.width + deltaWidth;
- newHeight = snapshot.height + deltaHeight;
- }
- }
- }
-
- // Scale factors
- const scaleX = newWidth / snapshot.width;
- const scaleY = newHeight / snapshot.height;
-
- // Scale transformation in local space around the pivot
- let localScaleTr = translate2D(pivotX, pivotY);
- localScaleTr = multiply2D(localScaleTr, scale2D(scaleX, scaleY));
- localScaleTr = multiply2D(localScaleTr, translate2D(-pivotX, -pivotY));
-
- // Groups have no Size of their own — resize them by transforming their
- // direct children instead. Each child's snapshot was captured at dragstart.
- // The group's own transform is left untouched; transformSystem rebuilds
- // its Computed AABB from the moved/resized children.
- const hasGroupSelected = selection.some(eid => hasComponent(world, eid, c.Group));
- // The single-node wobble guard doesn't apply to group children: they're
- // scaled in the group's space, not their own, so rotation/skew can change.
- const writeRotationSkew = selection.length > 1 || hasGroupSelected;
-
- for (const selectedEid of selection) {
- const targets = hasComponent(world, selectedEid, c.Group)
- ? [...query(world, [ChildOf(selectedEid), Or(c.Geometry, c.Group), Not(c.Deleted)])]
- : [selectedEid];
-
- for (const eid of targets) {
- const initialState = getSelectionTransfromSnapshot(eid);
- if (!initialState) continue;
-
- const parentTransform = initialState.parentTransform;
- const anchor = entityAnchor(world, eid);
-
- // Don't include the pivot point in the old local transform
- const oldLocal = multiply2D(
- initialState.localTransform,
- translate2D(
- initialState.width * anchor.x,
- initialState.height * anchor.y,
- )
- );
-
- // Convert scaleMatrix from mask local space to world space
- let deltaWorld = multiply2D(oldTr, localScaleTr);
- deltaWorld = multiply2D(deltaWorld, invert2D(oldTr));
-
- // Calculate new local transform: [new local] = [parent inverted] * [delta] * [parent] * [old local]
- let newLocal = multiply2D(invert2D(parentTransform), deltaWorld);
- newLocal = multiply2D(newLocal, parentTransform);
- newLocal = multiply2D(newLocal, oldLocal);
-
- // Decompose the new local transform
- const decomposed = decompose2D(newLocal);
-
- // Calculate the new width and height from the decomposed transform
- // Using the decomposed scale accounts for entity rotation relative to the mask
- // NOTE: Claude added these two lines and I have no idea why they are needed (but it works)
- const newWidth = initialState.width * (decomposed.scaleX / initialState.scaleX);
- const newHeight = initialState.height * (decomposed.scaleY / initialState.scaleY);
-
- // Calculate the new pivot point
- const newPivotX = (newWidth * anchor.x);
- const newPivotY = (newHeight * anchor.y);
-
- const newPosX = decomposed.x - newPivotX;
- const newPosY = decomposed.y - newPivotY;
-
- const offset = entityOffset(world, eid);
-
- resizeEntity(world, eid, { width: newWidth, height: newHeight });
- {
- const roundedX = Math.round(newPosX - offset.x);
- const roundedY = Math.round(newPosY - offset.y);
- setComponent(world, eid, c.Position, { x: roundedX, y: roundedY });
- }
-
- if (writeRotationSkew) {
- const roundedRot = Math.round(decomposed.rotation * 100) / 100;
- const roundedSkewX = Math.round(decomposed.skewX * 100) / 100;
- const roundedSkewY = Math.round(decomposed.skewY * 100) / 100;
- setComponent(world, eid, c.Rotation, roundedRot);
- setComponent(world, eid, c.Skew, { x: roundedSkewX, y: roundedSkewY });
- }
-
- // transformSystem walks parent-first, so a group's computeGroupBounds
- // reads its children's LocalTransform from the previous walk — one frame
- // stale. Refresh this child's LocalTransform synchronously so the AABB
- // computed for any selected ancestor in this same tick is current. The
- // lag is invisible for non-rotated groups but shows up as drift in a
- // rotated direction once the group has a rotation.
- if (hasGroupSelected) {
- computeLocalMatrix(world, eid);
- }
- }
-
- // A group's own LocalTransform pivots around anchor * Computed.width/height
- // (see computeLocalMatrix). Resizing its children changes the group's AABB,
- // moving that pivot — and with a nonzero group rotation the (I - R)·pivot
- // term shifts the group's world translation, dragging the whole group
- // (children + selection mask) off the pointer-anchored position. The child
- // math above snapshots the group's world transform at dragstart and assumes
- // it stays fixed, so compensate the group's Position to keep its
- // LocalTransform equal to that snapshot. A no-op when the group isn't
- // rotated/skewed, which is why drift only shows up once it's rotated.
- if (hasComponent(world, selectedEid, c.Group)) {
- const groupSnapshot = getSelectionTransfromSnapshot(selectedEid);
- if (groupSnapshot) {
- // Recompute the group's AABB from the freshly-updated children so the
- // new pivot (anchor * new size) reflects the resize.
- computeGroupBounds(world, selectedEid);
-
- const anchor = entityAnchor(world, selectedEid);
- const newPivotX = anchor.x * c.Computed.width[selectedEid];
- const newPivotY = anchor.y * c.Computed.height[selectedEid];
-
- // Desired local = the dragstart snapshot; its linear part (rotation,
- // skew, scale) is untouched by the resize.
- const desiredLocal = groupSnapshot.localTransform;
- const linear: Mat2D = {
- a: desiredLocal.a, b: desiredLocal.b,
- c: desiredLocal.c, d: desiredLocal.d,
- e: 0, f: 0,
- };
-
- // computeLocalMatrix builds L = T(pos) · K with K = T(pivot)·linear·T(-pivot).
- // Solve T(pos) = L · K⁻¹ for the new pivot so the rebuilt matrix matches.
- let k = multiply2D(translate2D(newPivotX, newPivotY), linear);
- k = multiply2D(k, translate2D(-newPivotX, -newPivotY));
- const posMat = multiply2D(desiredLocal, invert2D(k));
-
- const offset = entityOffset(world, selectedEid);
- setComponent(world, selectedEid, c.Position, {
- x: Math.round(posMat.e - offset.x),
- y: Math.round(posMat.f - offset.y),
- });
- computeLocalMatrix(world, selectedEid);
- }
- }
- }
- updateResizeCursor(world, orientation);
- }
-
- if (event.type === 'dragend') {
- // Clear state when resize ends
- world.snapLines.length = 0;
- world.history.commitTransaction();
- }
-
- if (world.pointer.state === 'lifted') {
- updateResizeCursor(world, orientation);
- }
-}
-
-export function handleRotateInteraction(world: EngineWorld, event: DispatchedPointerEvent) {
- const c = world.components;
-
- const snapshot = getSelectionMaskSnapshot();
-
- if (event.type === 'dragstart') {
- snapshotSelectionTransforms(world);
- snapshotSelectionMask(world);
- world.history.startTransaction('Rotate');
- }
-
- if (event.type === 'drag' && snapshot) {
- const oldTr = snapshot.mat;
-
- // Calculate the pivot point in mask local space
- // For 2+ nodes: use the center of the selection
- // For 1 node: use the anchor point of the node
- let pivotX: number;
- let pivotY: number;
-
- const selection = query(world, [c.Selected, Or(c.Geometry, c.Group)]);
-
- if (selection.length === 1) {
- const eid = selection[0];
-
- const anchor = entityAnchor(world, eid);
- // Use the anchor point (which is at the origin in local space after accounting for anchor offset)
- pivotX = snapshot.width * anchor.x;
- pivotY = snapshot.height * anchor.y;
- } else {
- // Use the center of the selection bounding box
- pivotX = snapshot.width / 2;
- pivotY = snapshot.height / 2;
- }
-
- // Get pointer positions in mask local space
- const currentLocalPoint = getPointerCurrentPoint(world, oldTr);
- const initialLocalPoint = getPointerInitialPoint(world, oldTr);
-
- // Calculate angles from pivot to pointer positions
- const initialAngle = Math.atan2(
- initialLocalPoint.y - pivotY,
- initialLocalPoint.x - pivotX
- );
- const currentAngle = Math.atan2(
- currentLocalPoint.y - pivotY,
- currentLocalPoint.x - pivotX
- );
-
- // Calculate rotation delta in radians
- const deltaAngleRad = currentAngle - initialAngle;
- const deltaAngleDeg = deltaAngleRad * 180 / Math.PI;
-
- // Rotation transformation in local space around the pivot
- let localRotateTr = translate2D(pivotX, pivotY);
- localRotateTr = multiply2D(localRotateTr, rotate2D(deltaAngleDeg));
- localRotateTr = multiply2D(localRotateTr, translate2D(-pivotX, -pivotY));
-
- for (const eid of selection) {
- const initialState = getSelectionTransfromSnapshot(eid);
- if (!initialState) continue;
-
- const parentTransform = initialState.parentTransform;
- const anchor = entityAnchor(world, eid);
-
- // Include the anchor offset in the old local transform
- const oldLocal = multiply2D(
- initialState.localTransform,
- translate2D(
- initialState.width * anchor.x,
- initialState.height * anchor.y,
- )
- );
-
- // Convert rotation from mask local space to world space
- let deltaWorld = multiply2D(oldTr, localRotateTr);
- deltaWorld = multiply2D(deltaWorld, invert2D(oldTr));
-
- // Calculate new local transform: [new local] = [parent inverted] * [delta] * [parent] * [old local]
- let newLocal = multiply2D(invert2D(parentTransform), deltaWorld)
- newLocal = multiply2D(newLocal, parentTransform);
- newLocal = multiply2D(newLocal, oldLocal);
-
- // Decompose the new local transform
- let decomposed = decompose2D(newLocal);
-
- // Snap rotation to global 15 degree multiples when Shift is pressed.
- // The snap must rotate about the same pivot the interactive rotation
- // used (the mask pivot, in world space) and the position must be
- // re-derived from the snapped transform. Reusing the unsnapped position
- // works for a single node — computeLocalMatrix pivots about the same
- // anchor, so its world position is rotation-invariant — but a group's
- // local matrix pivots about its box center (width/2, height/2) while the
- // interaction pivots about the mask/AABB center (offset by the group's
- // AABB origin). Overriding rotation while keeping the unsnapped position
- // then rotates about the wrong point and the mask jumps.
- if (world.keys.has('shift')) {
- const snapIncrement = 15;
- const snapped = Math.round(decomposed.rotation / snapIncrement) * snapIncrement;
- const snapDelta = snapped - decomposed.rotation;
-
- if (snapDelta !== 0) {
- const pivotWorld = transformPoint(oldTr, pivotX, pivotY);
- let snapWorld = translate2D(pivotWorld.x, pivotWorld.y);
- snapWorld = multiply2D(snapWorld, rotate2D(snapDelta));
- snapWorld = multiply2D(snapWorld, translate2D(-pivotWorld.x, -pivotWorld.y));
-
- let snappedLocal = multiply2D(invert2D(parentTransform), snapWorld);
- snappedLocal = multiply2D(snappedLocal, parentTransform);
- snappedLocal = multiply2D(snappedLocal, newLocal);
- newLocal = snappedLocal;
- decomposed = decompose2D(newLocal);
- }
- }
-
- // Calculate the anchor offset
- const anchorOffsetX = initialState.width * anchor.x;
- const anchorOffsetY = initialState.height * anchor.y;
-
- const offset = entityOffset(world, eid);
- const newPosX = decomposed.x - anchorOffsetX - offset.x;
- const newPosY = decomposed.y - anchorOffsetY - offset.y;
-
- const roundedPosX = Math.round(newPosX);
- const roundedPosY = Math.round(newPosY);
- const roundedRot = Math.round(decomposed.rotation * 100) / 100;
- setComponent(world, eid, c.Position, { x: roundedPosX, y: roundedPosY });
- setComponent(world, eid, c.Rotation, roundedRot);
- }
- updateRotationCursor(world);
- }
-
- if (event.type === 'dragend') {
- // Clear state when rotation ends
- world.history.commitTransaction();
- }
-
- if (world.pointer.state === 'lifted') {
- updateRotationCursor(world);
- }
-}
-
-const transformCorrectionFactors: Map<number, Mat2D> = new Map();
-
-export function handleMaskInteraction(world: EngineWorld, event: DispatchedPointerEvent) {
- const c = world.components;
- const snapshot = getSelectionMaskSnapshot();
-
- if (event.type === 'dblclick') {
- const selection = query(world, [c.Selected, Or(c.Geometry, c.Group)]);
- if (selection.length === 1) {
- const parentEid = selection[0];
- const childEid = findChildHitTarget(world, parentEid, event.clientX, event.clientY);
- if (childEid !== null) {
- removeComponent(world, parentEid, c.Interactive, false);
- for (const cid of query(world, [Or(c.Geometry, c.Group), ChildOf(parentEid), Not(c.Deleted)])) {
- addComponent(world, cid, c.Interactive, false);
- }
- clearComponent(world, c.Selected, false);
- clearComponent(world, c.Hovering, false);
- addComponent(world, childEid, c.Selected, false);
- addComponent(world, childEid, c.Hovering, false);
- }
- }
- return;
- }
-
- if (event.type == 'dragstart') {
- // One gesture = one undo step. For an option-drag the clone + the move
- // share this transaction (duplicateSelection's own transaction nests into
- // it); a plain drag just records the move.
- world.history.startTransaction(world.keys.has('alt') ? 'Duplicate' : 'Transform');
-
- if (world.keys.has('alt')) {
- duplicateSelection(world, { offset: false });
- transformSystem(world);
- }
-
- // Get the base matrix that was used to render the backdrop
- snapshotSelectionMask(world);
- snapshotSelectionTransforms(world);
- snapshotSnapCandidates(world);
- world.snapLines = [];
-
- // Initialize correction factors to identity for each selected node
- transformCorrectionFactors.clear();
- for (const eid of query(world, [c.Selected, Or(c.Geometry, c.Group)])) {
- transformCorrectionFactors.set(eid, identity2D());
- }
-
- world.hudMode = 'moving';
- }
-
- if (event.type == 'drag' && snapshot) {
- const oldTr = snapshot.mat;
- world.snapLines.length = 0;
-
- const currentLocalPoint = getPointerCurrentPoint(world, oldTr);
- const initialLocalPoint = getPointerInitialPoint(world, oldTr);
-
- // Calculate delta in mask local space
- let deltaX = currentLocalPoint.x - initialLocalPoint.x;
- let deltaY = currentLocalPoint.y - initialLocalPoint.y;
-
- // Determine if the drag is snapping to a guide
- if (!world.keys.has('mod')) {
- const proposedMaskQuad = rectToQuad(
- multiply2D(oldTr, translate2D(deltaX, deltaY)),
- snapshot.width,
- snapshot.height,
- );
- const snapCandidates = getSnapCandidatesSnapshot();
- const proposedMaskCandidates = buildSnapCandidatesFromQuad(proposedMaskQuad);
- const snapThreshold = SNAP_DISTANCE * world.resolution;
- const xSnap = findSnapTarget(proposedMaskCandidates.x, snapCandidates.x, snapThreshold, 'x');
- const ySnap = findSnapTarget(proposedMaskCandidates.y, snapCandidates.y, snapThreshold, 'y');
-
- if (xSnap) world.snapLines.push({
- from: {
- x: xSnap.sourcePoint.x + xSnap.offset,
- y: xSnap.sourcePoint.y + (ySnap?.offset ?? 0),
- },
- to: xSnap.targetPoint
- });
-
- if (ySnap) world.snapLines.push({
- from: {
- x: ySnap.sourcePoint.x + (xSnap?.offset ?? 0),
- y: ySnap.sourcePoint.y + ySnap.offset,
- },
- to: ySnap.targetPoint
- });
-
- if (xSnap || ySnap) {
- const inverseTr = invert2D(oldTr);
- assert(inverseTr, 'Matrix must be invertible');
- inverseTr.e = 0;
- inverseTr.f = 0;
-
- const localCorrection = transformPoint(inverseTr, xSnap?.offset ?? 0, ySnap?.offset ?? 0);
- deltaX += localCorrection.x;
- deltaY += localCorrection.y;
- }
- }
-
- // Update mask offset by adding delta to the initial offset
- const localOffsetTr = translate2D(deltaX, deltaY);
-
- // Apply the offset to each node
- for (const eid of query(world, [c.Selected, Or(c.Geometry, c.Group)])) {
- const initialState = getSelectionTransfromSnapshot(eid);
- if (!initialState) continue;
-
- const parentEid = getParentEntity(world, eid);
- const oldParentTransform = entityWorldMat(world, parentEid);
-
- let reparented = false;
-
- const inScene = getSceneAncestor(world, eid) !== null;
- const sceneHitTarget = findSceneHitTarget(world, event.clientX, event.clientY);
-
- if (sceneHitTarget && sceneHitTarget !== eid && parentEid === null) {
- try {
- appendChild(world, eid, sceneHitTarget);
- reparented = true;
- } catch {/* ignore */ }
- try {
- switchActiveScene(world, sceneHitTarget);
- } catch { /* ignore */ }
- }
-
- if (!sceneHitTarget && inScene && parentEid !== null && !hasPositionKeyframes(world, eid)) {
- try {
- removeChild(world, eid, parentEid);
- reparented = true;
- } catch { /* ignore */ }
- }
-
- if (reparented) {
- // Calculate correction factor: newParent^-1 * oldParent
- // This compensates for the change in parent transform
- const newParentEid = getParentEntity(world, eid);
- const newParentTransform = entityWorldMat(world, newParentEid);
- const correction = multiply2D(invert2D(newParentTransform), oldParentTransform);
- const existingCorrection = transformCorrectionFactors.get(eid) ?? identity2D();
- transformCorrectionFactors.set(eid, multiply2D(existingCorrection, correction));
- }
-
- const anchor = entityAnchor(world, eid);
- const offsetX = initialState.width * anchor.x;
- const offsetY = initialState.height * anchor.y;
-
- const parentTransform = initialState.parentTransform;
- const oldLocal = multiply2D(initialState.localTransform, translate2D(offsetX, offsetY));
-
- // Convert scaleMatrix from mask local space to world space
- let deltaWorld = multiply2D(oldTr, localOffsetTr);
- deltaWorld = multiply2D(deltaWorld, invert2D(oldTr));
-
- // Get correction factor (identity if no reparenting occurred)
- const correctionFactor = transformCorrectionFactors.get(eid) ?? identity2D();
-
- // Calculate new local transform: [new local] = [correction] * [parent inverted] * [delta] * [parent] * [old local]
- // The correction factor compensates for any parent changes during the drag
- let newLocal = multiply2D(correctionFactor, invert2D(parentTransform));
- newLocal = multiply2D(newLocal, deltaWorld);
- newLocal = multiply2D(newLocal, parentTransform);
- newLocal = multiply2D(newLocal, oldLocal);
-
- const offset = entityOffset(world, eid);
- const decomposed = decompose2D(newLocal);
- const newPosX = Math.round(decomposed.x - offsetX - offset.x);
- const newPosY = Math.round(decomposed.y - offsetY - offset.y);
-
- setComponent(world, eid, c.Position, { x: newPosX, y: newPosY });
- }
- }
-
- if (event.type == 'dragend') {
- // Clear state when drag ends
- world.snapLines.length = 0;
- world.hudMode = 'idle';
- world.history.commitTransaction();
- }
-
- if (world.pointer.state === 'lifted') {
- world.cursor.update('default');
- }
-}
-
-function getPointerCurrentPoint(world: EngineWorld, mat: Mat2D) {
- const currentX = world.pointer.clientX;
- const currentY = world.pointer.clientY;
-
- const inverse = invert2D(mat);
- assert(inverse, 'Matrix must be invertible');
-
- return transformPoint(inverse, currentX, currentY);
-}
-
-function getPointerInitialPoint(world: EngineWorld, mat: Mat2D) {
- const initialX = world.pointer.dragStartX ?? 0;
- const initialY = world.pointer.dragStartY ?? 0;
-
- const inverse = invert2D(mat);
- assert(inverse, 'Matrix must be invertible');
-
- return transformPoint(inverse, initialX, initialY);
-}
-
-function hasPositionKeyframes(world: EngineWorld, eid: number) {
- return findKeyframeTrackEntity(world, eid, 'position.x') !== null
- || findKeyframeTrackEntity(world, eid, 'position.y') !== null;
-}
-
-function findSceneHitTarget(world: EngineWorld, x: number, y: number) {
- const c = world.components;
-
- for (const eid of query(world, [c.Scene, None(ChildOf('*'), c.Selected, c.Dragging, c.Culled, c.Deleted)])) {
- if (isPointerInEntity(world, eid, { x, y })) {
- return eid;
- }
- }
-
- return null;
-}
-
-const anchorSchema = z.enum(['tl', 'tr', 'bl', 'br', 't', 'r', 'b', 'l']);
-type AnchorType = z.infer<typeof anchorSchema>;
-
-const HANDLE_RESIZE_FACTOR: Record<AnchorType, Point> = {
- tl: { x: -1, y: -1 }, tr: { x: 1, y: -1 },
- bl: { x: -1, y: 1 }, br: { x: 1, y: 1 },
- t: { x: 0, y: -1 }, r: { x: 1, y: 0 },
- b: { x: 0, y: 1 }, l: { x: -1, y: 0 },
-};
-
-function getResizeHandleVertices(orientation: AnchorType, q: Quad): Point[] {
- switch (orientation) {
- case 'tl': return [q[0]];
- case 'tr': return [q[1]];
- case 'br': return [q[2]];
- case 'bl': return [q[3]];
- case 't': return [q[0], q[1]];
- case 'r': return [q[1], q[2]];
- case 'b': return [q[2], q[3]];
- case 'l': return [q[3], q[0]];
- }
-}
diff --git a/apps/web/src/components/engine/api/keyframe.ts b/apps/web/src/components/engine/api/keyframe.ts
deleted file mode 100644
index c00937f9..00000000
--- a/apps/web/src/components/engine/api/keyframe.ts
+++ /dev/null
@@ -1,203 +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 { hasComponent, Not, query } from 'bitecs';
-import { ChildOf } from '../components/relations';
-import { getPropertyPaths, type PropertyPath } from '../systems/motion';
-import { getNodeLocalFrame } from './query';
-import { getParentEntity } from './base';
-import { createEntity, deleteEntity } from './entities';
-import { appendChild } from './hierarchy';
-import { setComponent } from './events';
-
-import type { EngineWorld } from './world';
-
-/**
- * Find the KeyframeTrack entity (if any) for a (target, property) pair.
- */
-export function findKeyframeTrackEntity(world: EngineWorld, target: number, property: string): number | null {
- const c = world.components;
- for (const tid of query(world, [c.KeyframeTrack, ChildOf(target), Not(c.Deleted)])) {
- if (c.KeyframeTrack.property[tid] === property) return tid;
- }
- return null;
-}
-
-/**
- * Collect all keyframe entities for a (target, property) pair.
- */
-export function getKeyframeTrack(world: EngineWorld, target: number, property: string): number[] {
- const c = world.components;
- const tid = findKeyframeTrackEntity(world, target, property);
- if (tid === null) return [];
- return [...query(world, [c.Keyframe, ChildOf(tid), Not(c.Deleted)])];
-}
-
-/**
- * Find the target node for a given entity.
- */
-export function findKeyframeTargetNode(world: EngineWorld, eid: number): number | null {
- const c = world.components;
-
- let current: number | null = eid;
- while (current) {
- if (hasComponent(world, current, c.Geometry) || hasComponent(world, current, c.Group) || hasComponent(world, current, c.AdjustmentLayer)) {
- return current;
- }
- current = getParentEntity(world, current);
- }
-
- return null;
-}
-
-/**
- * Get the KeyframeTrack entity for (target, property), creating one if missing.
- */
-function ensureKeyframeTrackEntity(world: EngineWorld, target: number, property: PropertyPath): number {
- const c = world.components;
- const existing = findKeyframeTrackEntity(world, target, property);
- if (existing !== null) return existing;
-
- const tid = createEntity(world);
- setComponent(world, tid, c.KeyframeTrack, { property });
- appendChild(world, tid, target);
- return tid;
-}
-
-/**
- * Add a keyframe at the current local time for (target, property).
- * Reads the current value from the target. If a keyframe at this exact time
- * already exists, its value is overwritten.
- */
-export function syncKeyframeTrack(world: EngineWorld, eid: number, property: PropertyPath) {
- const c = world.components;
-
- const worldProps = getPropertyPaths(world);
- const nodeEid = findKeyframeTargetNode(world, eid);
- if (nodeEid === null) return;
-
- const trackEid = findKeyframeTrackEntity(world, eid, property);
- if (trackEid === null) return;
-
- const localFrame = getNodeLocalFrame(world, nodeEid);
- const existingKfEid = query(world, [c.Keyframe, ChildOf(trackEid), Not(c.Deleted)])
- .find(kid => c.Keyframe.time[kid] === localFrame);
-
- const currentValue = worldProps[property].authored[eid] ?? 0;
- const isNumber = typeof currentValue === 'number';
- if (!isNumber) return;
-
- if (existingKfEid) {
- setComponent(world, existingKfEid, c.Keyframe, { value: currentValue });
- } else {
- const kfid = createEntity(world);
- setComponent(world, kfid, c.Keyframe, {
- time: localFrame,
- value: currentValue,
- easing: 'linear',
- });
- appendChild(world, kfid, trackEid);
- }
-}
-
-/**
- * Toggle a keyframe at the current local time for (target, property).
- * Creates the KeyframeTrack on first use; removes it when its last keyframe
- * is toggled off.
- */
-export function toggleKeyframeTrack(world: EngineWorld, eid: number, property: PropertyPath) {
- const c = world.components;
-
- const worldProps = getPropertyPaths(world);
- const nodeEid = findKeyframeTargetNode(world, eid);
- if (nodeEid === null) return;
-
- const localFrame = getNodeLocalFrame(world, nodeEid);
- const trackEid = findKeyframeTrackEntity(world, eid, property);
-
- let trackKeyframes: number[] = [];
- if (trackEid !== null) {
- trackKeyframes = [...query(world, [c.Keyframe, ChildOf(trackEid), Not(c.Deleted)])];
- }
-
- const existingKfEid = trackKeyframes.find(kid => c.Keyframe.time[kid] === localFrame);
-
- const currentValue = worldProps[property].authored[eid] ?? 0;
- const isNumber = typeof currentValue === 'number';
- if (!isNumber) return;
-
- if (existingKfEid) {
- world.history.transaction('Remove keyframe', () => {
- deleteEntity(world, existingKfEid);
- // If that was the track's last keyframe, drop the track too.
- if (trackEid && trackKeyframes.length === 1) {
- deleteEntity(world, trackEid);
- }
- });
- return;
- }
-
- world.history.transaction('Add keyframe', () => {
- const track = ensureKeyframeTrackEntity(world, eid, property);
- const kfid = createEntity(world);
- setComponent(world, kfid, c.Keyframe, {
- time: localFrame,
- value: currentValue,
- easing: 'linear',
- });
- appendChild(world, kfid, track);
- });
-}
-
-/**
- * Declaratively replace all keyframes for (target, property): creates the
- * track on first use, deletes it when `keyframes` is empty. Times are local
- * frames; values are in the property's authored unit. Runs without a history
- * transaction — callers (mount, patch) provide their own.
- */
-export function setKeyframeTrack(
- world: EngineWorld,
- target: number,
- property: PropertyPath,
- keyframes: ReadonlyArray<{ time: number; value: number; easing?: string }>,
-): void {
- const c = world.components;
- const existing = findKeyframeTrackEntity(world, target, property);
-
- if (existing !== null) {
- for (const kid of query(world, [c.Keyframe, ChildOf(existing), Not(c.Deleted)])) {
- deleteEntity(world, kid);
- }
- if (keyframes.length === 0) {
- deleteEntity(world, existing);
- return;
- }
- }
- if (keyframes.length === 0) return;
-
- const tid = existing ?? ensureKeyframeTrackEntity(world, target, property);
- const sorted = [...keyframes].sort((a, b) => a.time - b.time);
- for (const kf of sorted) {
- const kid = createEntity(world);
- setComponent(world, kid, c.Keyframe, {
- time: kf.time,
- value: kf.value,
- easing: kf.easing ?? '',
- });
- appendChild(world, kid, tid);
- }
-}
-
-export function removeKeyframeTrack(world: EngineWorld, eid: number, property: PropertyPath) {
- const c = world.components;
- const trackEid = findKeyframeTrackEntity(world, eid, property);
- if (trackEid === null) return;
-
- const keyframes = [...query(world, [c.Keyframe, ChildOf(trackEid), Not(c.Deleted)])];
-
- world.history.transaction('Remove keyframe track', () => {
- keyframes.forEach(kfEid => deleteEntity(world, kfEid));
- deleteEntity(world, trackEid);
- });
-}
diff --git a/apps/web/src/components/engine/api/observers.ts b/apps/web/src/components/engine/api/observers.ts
deleted file mode 100644
index dc1fc0ea..00000000
--- a/apps/web/src/components/engine/api/observers.ts
+++ /dev/null
@@ -1,534 +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 { hasComponent, query, Not } from "bitecs";
-import { AnimationPhase, AnimationType, BlendMode, CaptionType, ConstraintType, EffectType, FontStyle, GeometryType, PaintType, ScaleMode, StrokeCap, StrokeJoin, TextAlign, TextBaseline, TextCase, TransitionType } from "../components/constants";
-import { ChildOf } from "../components/relations";
-import { DEFAULT_CLIP_HEIGHT } from "../timeline/config";
-import { getParentEntity } from "./base";
-import { observe, addComponent, removeComponent } from "./events";
-import { syncKeyframeTrack } from "./keyframe";
-import { DEFAULT_DURATION_FRAMES } from "../constants";
-import * as utils from "./utils";
-
-import type { EngineWorld } from "./world";
-
-export function observeWorld(world: EngineWorld) {
- const c = world.components;
-
- observe(world, "set", c.Geometry, (eid, type) => {
- c.Geometry[eid] = type ?? c.Geometry[eid] ?? GeometryType.RECT;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Paint, (eid, type) => {
- c.Paint[eid] = type ?? c.Paint[eid] ?? PaintType.SOLID;
- utils.reactToPaintChange(world, eid);
- utils.bubbleTimeRangeUp(world, eid);
- utils.persistEntity(world, eid);
- });
-
- observe(world, "add", c.Group, (eid) => {
- utils.recomputeEntityTimeRange(world, eid);
- utils.bubbleTimeRangeUp(world, eid);
- utils.persistEntity(world, eid);
- });
-
- observe(world, "remove", c.Group, (eid) => {
- utils.persistEntity(world, eid);
- });
-
- observe(world, "add", c.Scene, (eid) => {
- utils.recomputeEntityTimeRange(world, eid);
- utils.bubbleTimeRangeUp(world, eid);
- utils.persistEntity(world, eid);
- });
-
- observe(world, "remove", c.Scene, (eid) => {
- utils.persistEntity(world, eid);
- });
-
- observe(world, "add", c.Audio, (eid) => {
- utils.recomputeEntityTimeRange(world, eid);
- utils.bubbleTimeRangeUp(world, eid);
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.ExportSettings, (eid, data) => {
- const s = c.ExportSettings;
- s.templateId[eid] = data.templateId ?? s.templateId[eid] ?? null;
- s.format[eid] = data.format ?? s.format[eid] ?? 'mp4';
- s.videoEnabled[eid] = data.videoEnabled ?? s.videoEnabled[eid] ?? true;
- s.videoCodec[eid] = data.videoCodec ?? s.videoCodec[eid] ?? 'avc';
- s.videoBitrate[eid] = data.videoBitrate ?? s.videoBitrate[eid] ?? 10e6;
- s.videoFps[eid] = data.videoFps ?? s.videoFps[eid] ?? 30;
- s.videoResolution[eid] = data.videoResolution ?? s.videoResolution[eid] ?? 1080;
- s.audioEnabled[eid] = data.audioEnabled ?? s.audioEnabled[eid] ?? true;
- s.audioCodec[eid] = data.audioCodec ?? s.audioCodec[eid] ?? 'aac';
- s.audioSampleRate[eid] = data.audioSampleRate ?? s.audioSampleRate[eid] ?? 48000;
- });
-
- observe(world, "add", c.Selected, (eid) => {
- if (!hasComponent(world, eid, c.Audio)) return;
- for (const fid of [...query(world, [c.Paint, ChildOf(eid), Not(c.Deleted)])]) {
- if (c.Paint[fid] === PaintType.WAVEFORM) {
- removeComponent(world, fid, c.Hidden);
- }
- }
- });
-
- observe(world, "remove", c.Selected, (eid) => {
- if (!hasComponent(world, eid, c.Audio) || getParentEntity(world, eid) === null) return;
- for (const fid of [...query(world, [c.Paint, ChildOf(eid), Not(c.Deleted)])]) {
- if (c.Paint[fid] === PaintType.WAVEFORM) {
- addComponent(world, fid, c.Hidden);
- }
- }
- });
-
- observe(world, "set", c.Effect, (eid, { type, value }) => {
- c.Effect.type[eid] = type ?? c.Effect.type[eid] ?? EffectType.BRIGHTNESS;
- c.Effect.value[eid] = value ?? c.Effect.value[eid] ?? 0.5;
- c.Computed.value[eid] = c.Effect.value[eid];
- utils.persistEntity(world, eid);
- if (value !== undefined) syncKeyframeTrack(world, eid, 'effect.value');
- });
-
- observe(world, "add", c.IsMask, (eid) => {
- utils.persistEntity(world, eid);
- });
-
- observe(world, "remove", c.IsMask, (eid) => {
- utils.persistEntity(world, eid);
- });
-
- observe(world, "add", c.Shadow, (eid) => {
- utils.persistEntity(world, eid);
- });
-
- observe(world, "add", c.Stroke, (eid) => {
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Caption, (eid, { type, colors, verticalAlign }) => {
- c.Caption.type[eid] = type ?? c.Caption.type[eid] ?? CaptionType.CLASSIC;
- c.Caption.colors[eid] = colors ?? c.Caption.colors[eid] ?? [0xFFFFFF];
- c.Caption.verticalAlign[eid] = verticalAlign ?? c.Caption.verticalAlign[eid];
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Shader, (eid, { code, uniforms }) => {
- c.Shader.code[eid] = code ?? c.Shader.code[eid] ?? "";
- c.Shader.uniforms[eid] = uniforms ?? c.Shader.uniforms[eid] ?? null;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Name, (eid, name) => {
- c.Name[eid] = name ?? c.Name[eid] ?? "";
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Key, (eid, key) => {
- c.Key[eid] = key ?? c.Key[eid] ?? "";
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.MountScript, (eid, { mountId, scriptAssetId }) => {
- c.MountScript.mountId[eid] = mountId ?? c.MountScript.mountId[eid] ?? "";
- c.MountScript.scriptAssetId[eid] = scriptAssetId ?? c.MountScript.scriptAssetId[eid] ?? "";
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.MountPath, (eid, { mountId, path }) => {
- c.MountPath.mountId[eid] = mountId ?? c.MountPath.mountId[eid] ?? "";
- c.MountPath.path[eid] = path ?? c.MountPath.path[eid] ?? "";
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Position, (eid, { x, y }) => {
- c.Position.x[eid] = x ?? c.Position.x[eid] ?? 0;
- c.Position.y[eid] = y ?? c.Position.y[eid] ?? 0;
- c.Computed.positionX[eid] = c.Position.x[eid];
- c.Computed.positionY[eid] = c.Position.y[eid];
- utils.persistEntity(world, eid);
- if (x !== undefined) syncKeyframeTrack(world, eid, 'position.x');
- if (y !== undefined) syncKeyframeTrack(world, eid, 'position.y');
- });
-
- observe(world, "set", c.Offset, (eid, { x, y }) => {
- c.Offset.x[eid] = x ?? c.Offset.x[eid] ?? 0;
- c.Offset.y[eid] = y ?? c.Offset.y[eid] ?? 0;
- c.Computed.offsetX[eid] = c.Offset.x[eid];
- c.Computed.offsetY[eid] = c.Offset.y[eid];
- utils.persistEntity(world, eid);
- if (x !== undefined) syncKeyframeTrack(world, eid, 'offset.x');
- if (y !== undefined) syncKeyframeTrack(world, eid, 'offset.y');
- });
-
- observe(world, "set", c.Rotation, (eid, rotation) => {
- c.Rotation[eid] = rotation ?? c.Rotation[eid] ?? 0;
- c.Computed.rotation[eid] = c.Rotation[eid];
- utils.persistEntity(world, eid);
- if (rotation !== undefined) syncKeyframeTrack(world, eid, 'rotation');
- });
-
- observe(world, "set", c.Scale, (eid, { x, y }) => {
- c.Scale.x[eid] = x ?? c.Scale.x[eid] ?? 1;
- c.Scale.y[eid] = y ?? c.Scale.y[eid] ?? 1;
- c.Computed.scaleX[eid] = c.Scale.x[eid];
- c.Computed.scaleY[eid] = c.Scale.y[eid];
- utils.persistEntity(world, eid);
- if (x !== undefined) syncKeyframeTrack(world, eid, 'scale.x');
- if (y !== undefined) syncKeyframeTrack(world, eid, 'scale.y');
- });
-
- observe(world, "set", c.UniformScale, (eid, scale) => {
- c.UniformScale[eid] = scale ?? c.UniformScale[eid] ?? 1;
- c.Computed.scaleX[eid] = c.UniformScale[eid];
- c.Computed.scaleY[eid] = c.UniformScale[eid];
- utils.persistEntity(world, eid);
- if (scale !== undefined) syncKeyframeTrack(world, eid, 'scale');
- });
-
- observe(world, "set", c.Anchor, (eid, { x, y }) => {
- c.Anchor.x[eid] = x ?? c.Anchor.x[eid] ?? 0.5;
- c.Anchor.y[eid] = y ?? c.Anchor.y[eid] ?? 0.5;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Skew, (eid, { x, y }) => {
- c.Skew.x[eid] = x ?? c.Skew.x[eid] ?? 0;
- c.Skew.y[eid] = y ?? c.Skew.y[eid] ?? 0;
- c.Computed.skewX[eid] = c.Skew.x[eid];
- c.Computed.skewY[eid] = c.Skew.y[eid];
- utils.persistEntity(world, eid);
- if (x !== undefined) syncKeyframeTrack(world, eid, 'skew.x');
- if (y !== undefined) syncKeyframeTrack(world, eid, 'skew.y');
- });
-
- observe(world, "set", c.Size, (eid, params) => {
- c.Size.width[eid] = params.width ?? c.Size.width[eid] ?? 300;
- c.Size.height[eid] = params.height ?? c.Size.height[eid] ?? 300;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Flip, (eid, { x, y }) => {
- c.Flip.x[eid] = x ?? c.Flip.x[eid] ?? 1;
- c.Flip.y[eid] = y ?? c.Flip.y[eid] ?? 1;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "add", c.Hidden, (eid) => {
- utils.persistEntity(world, eid);
- });
-
- observe(world, "remove", c.Hidden, (eid) => {
- utils.persistEntity(world, eid);
- });
-
- observe(world, "add", c.ClipsContent, (eid) => {
- utils.persistEntity(world, eid);
- });
-
- observe(world, "remove", c.ClipsContent, (eid) => {
- utils.persistEntity(world, eid);
- });
-
- observe(world, "add", c.Sequential, (eid) => {
- addComponent(world, eid, c.Group);
- removeComponent(world, eid, c.Position);
- removeComponent(world, eid, c.Offset);
- removeComponent(world, eid, c.Rotation);
- removeComponent(world, eid, c.Scale);
- removeComponent(world, eid, c.Skew);
- removeComponent(world, eid, c.Anchor);
- removeComponent(world, eid, c.Flip);
- utils.syncInteractiveState(world);
- utils.persistEntity(world, eid);
- });
-
- observe(world, "remove", c.Sequential, (eid) => {
- utils.syncInteractiveState(world);
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Appearance, (eid, { opacity, blendMode }) => {
- c.Appearance.opacity[eid] = opacity ?? c.Appearance.opacity[eid] ?? 1;
- c.Appearance.blendMode[eid] = blendMode ?? c.Appearance.blendMode[eid] ?? BlendMode.SOURCE_OVER;
- c.Computed.opacity[eid] = c.Appearance.opacity[eid];
- utils.persistEntity(world, eid);
- if (opacity !== undefined) syncKeyframeTrack(world, eid, 'opacity');
- });
-
- observe(world, "set", c.CornerRadius, (eid, cornerRadius) => {
- c.CornerRadius[eid] = cornerRadius ?? c.CornerRadius[eid] ?? 0;
- c.Computed.cornerRadius[eid] = c.CornerRadius[eid];
- utils.persistEntity(world, eid);
- if (cornerRadius !== undefined) syncKeyframeTrack(world, eid, 'vertexRadius');
- });
-
- observe(world, "set", c.MixedCornerRadius, (eid, params) => {
- c.MixedCornerRadius.topLeft[eid] = params.topLeft ?? c.MixedCornerRadius.topLeft[eid] ?? 0;
- c.MixedCornerRadius.topRight[eid] = params.topRight ?? c.MixedCornerRadius.topRight[eid] ?? 0;
- c.MixedCornerRadius.bottomRight[eid] = params.bottomRight ?? c.MixedCornerRadius.bottomRight[eid] ?? 0;
- c.MixedCornerRadius.bottomLeft[eid] = params.bottomLeft ?? c.MixedCornerRadius.bottomLeft[eid] ?? 0;
- c.Computed.cornerRadiusTopLeft[eid] = c.MixedCornerRadius.topLeft[eid];
- c.Computed.cornerRadiusTopRight[eid] = c.MixedCornerRadius.topRight[eid];
- c.Computed.cornerRadiusBottomRight[eid] = c.MixedCornerRadius.bottomRight[eid];
- c.Computed.cornerRadiusBottomLeft[eid] = c.MixedCornerRadius.bottomLeft[eid];
- utils.persistEntity(world, eid);
- if (params.topLeft !== undefined) syncKeyframeTrack(world, eid, 'mixedVertexRadius.topLeft');
- if (params.topRight !== undefined) syncKeyframeTrack(world, eid, 'mixedVertexRadius.topRight');
- if (params.bottomRight !== undefined) syncKeyframeTrack(world, eid, 'mixedVertexRadius.bottomRight');
- if (params.bottomLeft !== undefined) syncKeyframeTrack(world, eid, 'mixedVertexRadius.bottomLeft');
- });
-
- observe(world, "set", c.Color, (eid, color) => {
- c.Color[eid] = color ?? c.Color[eid] ?? 0xE0E0E0;
- c.Computed.color[eid] = c.Color[eid];
- utils.persistEntity(world, eid);
- if (color !== undefined) syncKeyframeTrack(world, eid, 'color');
- });
-
- observe(world, "set", c.Blur, (eid, blur) => {
- c.Blur[eid] = blur ?? c.Blur[eid] ?? 0;
- c.Computed.blur[eid] = c.Blur[eid];
- utils.persistEntity(world, eid);
- if (blur !== undefined) syncKeyframeTrack(world, eid, 'blur');
- });
-
- observe(world, "set", c.AssetId, (eid, assetId) => {
- c.AssetId[eid] = assetId ?? c.AssetId[eid] ?? "";
- utils.reactToAssetChange(world, eid);
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.ScaleMode, (eid, scaleMode) => {
- c.ScaleMode[eid] = scaleMode ?? c.ScaleMode[eid] ?? ScaleMode.FIT;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Volume, (eid, volume) => {
- c.Volume[eid] = volume ?? c.Volume[eid] ?? 0;
- c.Computed.volume[eid] = c.Volume[eid];
- utils.persistEntity(world, eid);
- if (volume !== undefined) syncKeyframeTrack(world, eid, 'volume');
- });
-
- observe(world, "add", c.Muted, (eid) => {
- utils.persistEntity(world, eid);
- });
-
- observe(world, "remove", c.Muted, (eid) => {
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Chars, (eid, chars) => {
- c.Chars[eid] = chars ?? c.Chars[eid] ?? "Text";
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.TextStyle, (eid, params) => {
- // A TextStyle on a TextRange entity is a *partial* override of its parent's
- // style — only the fields explicitly provided should be written. Defaulting
- // the rest (as we do for standalone text below) would clobber the parent's
- // font, size, case, etc. with "Arial"/16px and break sub-range captions.
- if (hasComponent(world, eid, c.TextRange)) {
- if (params.leading !== undefined) c.TextStyle.leading[eid] = params.leading;
- if (params.fontSize !== undefined) c.TextStyle.fontSize[eid] = params.fontSize;
- if (params.fontFamily !== undefined) c.TextStyle.fontFamily[eid] = params.fontFamily;
- if (params.fontWeight !== undefined) c.TextStyle.fontWeight[eid] = params.fontWeight;
- if (params.fontStyle !== undefined) c.TextStyle.fontStyle[eid] = params.fontStyle;
- if (params.textAlign !== undefined) c.TextStyle.textAlign[eid] = params.textAlign;
- if (params.textBaseline !== undefined) c.TextStyle.textBaseline[eid] = params.textBaseline;
- if (params.textCase !== undefined) c.TextStyle.textCase[eid] = params.textCase;
- if (params.letterSpacing !== undefined) c.TextStyle.letterSpacing[eid] = params.letterSpacing;
- utils.persistEntity(world, eid);
- return;
- }
-
- c.TextStyle.leading[eid] = params.leading ?? c.TextStyle.leading[eid] ?? 1;
- c.TextStyle.fontSize[eid] = params.fontSize ?? c.TextStyle.fontSize[eid] ?? 16;
- c.TextStyle.fontFamily[eid] = params.fontFamily ?? c.TextStyle.fontFamily[eid] ?? "Arial";
- c.TextStyle.fontWeight[eid] = params.fontWeight ?? c.TextStyle.fontWeight[eid] ?? "400";
- c.TextStyle.fontStyle[eid] = params.fontStyle ?? c.TextStyle.fontStyle[eid] ?? FontStyle.NORMAL;
- c.TextStyle.textAlign[eid] = params.textAlign ?? c.TextStyle.textAlign[eid] ?? TextAlign.LEFT;
- c.TextStyle.textBaseline[eid] = params.textBaseline ?? c.TextStyle.textBaseline[eid] ?? TextBaseline.TOP;
- c.TextStyle.textCase[eid] = params.textCase ?? c.TextStyle.textCase[eid] ?? TextCase.ORIGINAL;
- c.TextStyle.letterSpacing[eid] = params.letterSpacing ?? c.TextStyle.letterSpacing[eid] ?? 0;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.TextRange, (eid, { start, end }) => {
- c.TextRange.start[eid] = start ?? c.TextRange.start[eid] ?? 0;
- c.TextRange.end[eid] = end ?? c.TextRange.end[eid] ?? null;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.ColorStop, (eid, { offset, color, opacity }) => {
- c.ColorStop.offset[eid] = offset ?? c.ColorStop.offset[eid] ?? 0;
- c.ColorStop.color[eid] = color ?? c.ColorStop.color[eid] ?? 0xFFFFFF;
- c.ColorStop.opacity[eid] = opacity ?? c.ColorStop.opacity[eid] ?? 1;
- c.Computed.stopOffset[eid] = c.ColorStop.offset[eid];
- c.Computed.stopColor[eid] = c.ColorStop.color[eid];
- c.Computed.stopOpacity[eid] = c.ColorStop.opacity[eid];
- utils.persistEntity(world, eid);
- if (offset !== undefined) syncKeyframeTrack(world, eid, 'stop.offset');
- if (color !== undefined) syncKeyframeTrack(world, eid, 'stop.color');
- if (opacity !== undefined) syncKeyframeTrack(world, eid, 'stop.opacity');
- });
-
- observe(world, "set", c.StrokeStyle, (eid, params) => {
- c.StrokeStyle.width[eid] = params.width ?? c.StrokeStyle.width[eid] ?? 1;
- c.StrokeStyle.join[eid] = params.join ?? c.StrokeStyle.join[eid] ?? StrokeJoin.MITER;
- c.StrokeStyle.cap[eid] = params.cap ?? c.StrokeStyle.cap[eid] ?? StrokeCap.BUTT;
- c.StrokeStyle.miterLimit[eid] = params.miterLimit ?? c.StrokeStyle.miterLimit[eid] ?? 3;
- c.Computed.strokeWidth[eid] = c.StrokeStyle.width[eid];
- utils.persistEntity(world, eid);
- if (params.width !== undefined) syncKeyframeTrack(world, eid, 'stroke.width');
- });
-
- observe(world, "set", c.Playback, (eid, { playing, loop, speed }) => {
- c.Playback.playing[eid] = playing ?? c.Playback.playing[eid] ?? 0;
- c.Playback.loop[eid] = loop ?? c.Playback.loop[eid] ?? 1;
- c.Playback.speed[eid] = speed ?? c.Playback.speed[eid] ?? 1;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.ClipHeight, (eid, height) => {
- c.ClipHeight[eid] = height ?? c.ClipHeight[eid] ?? DEFAULT_CLIP_HEIGHT;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "add", c.Expanded, (eid) => {
- utils.persistEntity(world, eid);
- world.rebuildTimelineIndex();
- });
-
- observe(world, "remove", c.Expanded, (eid) => {
- utils.persistEntity(world, eid);
- world.rebuildTimelineIndex();
- });
-
- observe(world, "set", c.Delay, (eid, delay) => {
- c.Delay[eid] = delay ?? c.Delay[eid] ?? 0;
- utils.propagateTimeRangeDown(world, eid);
- utils.bubbleTimeRangeUp(world, eid);
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.PlaybackRate, (eid, playbackRate) => {
- c.PlaybackRate[eid] = playbackRate ?? c.PlaybackRate[eid] ?? 1;
- utils.propagateTimeRangeDown(world, eid);
- utils.bubbleTimeRangeUp(world, eid);
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Trim, (eid, { start, end }) => {
- const fallbackDuration = c.Computed.duration[eid] ?? DEFAULT_DURATION_FRAMES;
- c.Trim.start[eid] = start ?? c.Trim.start[eid] ?? 0;
- c.Trim.end[eid] = end ?? c.Trim.end[eid] ?? fallbackDuration;
- utils.clampTrimToAssetDuration(world, eid);
- utils.recomputeEntityTimeRange(world, eid);
- utils.bubbleTimeRangeUp(world, eid);
- utils.persistEntity(world, eid);
- });
-
- observe(world, "remove", c.Trim, (eid) => {
- utils.recomputeEntityTimeRange(world, eid);
- utils.bubbleTimeRangeUp(world, eid);
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Workarea, (eid, { start, end }) => {
- const fallbackEnd = c.Computed.duration[eid] ?? DEFAULT_DURATION_FRAMES;
- c.Workarea.start[eid] = start ?? c.Workarea.start[eid] ?? 0;
- c.Workarea.end[eid] = end ?? c.Workarea.end[eid] ?? fallbackEnd;
- });
-
- observe(world, "set", c.Constraint, (eid, { horizontal, vertical }) => {
- c.Constraint.horizontal[eid] = horizontal ?? c.Constraint.horizontal[eid] ?? ConstraintType.MIN;
- c.Constraint.vertical[eid] = vertical ?? c.Constraint.vertical[eid] ?? ConstraintType.MIN;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.KeepAspectRatio, (eid, { width, height }) => {
- c.KeepAspectRatio.width[eid] = width ?? c.KeepAspectRatio.width[eid] ?? 1;
- c.KeepAspectRatio.height[eid] = height ?? c.KeepAspectRatio.height[eid] ?? 1;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.KeyframeTrack, (eid, { property }) => {
- c.KeyframeTrack.property[eid] = property ?? c.KeyframeTrack.property[eid] ?? "";
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Keyframe, (eid, { time, value, easing }) => {
- c.Keyframe.time[eid] = time ?? c.Keyframe.time[eid] ?? 0;
- c.Keyframe.value[eid] = value ?? c.Keyframe.value[eid] ?? 0;
- c.Keyframe.easing[eid] = easing ?? c.Keyframe.easing[eid] ?? "linear";
- // Ensure keyframes are sorted by frame
- utils.rebuildCaches(world, eid, getParentEntity(world, eid));
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Animation, (eid, { duration, delay, type, phase }) => {
- c.Animation.duration[eid] = duration ?? c.Animation.duration[eid] ?? 0;
- c.Animation.delay[eid] = delay ?? c.Animation.delay[eid] ?? 0;
- c.Animation.type[eid] = type ?? c.Animation.type[eid] ?? AnimationType.FADE;
- c.Animation.phase[eid] = phase ?? c.Animation.phase[eid] ?? AnimationPhase.IN;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.ItemIndex, (eid, index) => {
- c.ItemIndex[eid] = index ?? c.ItemIndex[eid] ?? 0;
- utils.rebuildCaches(world, eid, getParentEntity(world, eid));
- world.rebuildTimelineIndex();
- utils.persistEntity(world, eid);
- });
-
- observe(world, "set", c.Transition, (eid, { type, duration }) => {
- c.Transition.type[eid] = type ?? c.Transition.type[eid] ?? TransitionType.DISSOLVE;
- c.Transition.duration[eid] = duration ?? c.Transition.duration[eid] ?? 0;
- utils.persistEntity(world, eid);
- });
-
- observe(world, "add", c.Deleted, (eid) => {
- utils.rebuildCaches(world, eid, getParentEntity(world, eid));
- utils.reactToChildDetached(world, eid);
- utils.disposeDecoders(world, eid);
- utils.disposeHtmlHosts(world, eid);
- utils.disposeSurfaceHosts(world, eid);
- utils.disposeShaderHosts(world, eid);
- utils.disconnectAudioBus(world, eid);
- utils.unpersistEntity(world, eid);
- world.rebuildTimelineIndex();
- });
-
- observe(world, "remove", c.Deleted, (eid) => {
- utils.rebuildCaches(world, eid, getParentEntity(world, eid));
- utils.persistEntity(world, eid);
- world.rebuildTimelineIndex();
- });
-
- observe(world, "add", c.Culled, (eid) => {
- utils.disposeDecoders(world, eid);
- });
-
- // Non persistent components
- observe(world, "set", c.ClipDragOrigin, (eid, { delay, start, end }) => {
- c.ClipDragOrigin.delay[eid] = delay ?? c.ClipDragOrigin.delay[eid] ?? 0;
- c.ClipDragOrigin.start[eid] = start ?? c.ClipDragOrigin.start[eid] ?? 0;
- c.ClipDragOrigin.end[eid] = end ?? c.ClipDragOrigin.end[eid] ?? 0;
- });
-
- observe(world, "set", c.KeyframeDragOrigin, (eid, { time }) => {
- c.KeyframeDragOrigin.time[eid] = time ?? c.KeyframeDragOrigin.time[eid] ?? 0;
- });
-
- observe(world, "set", c.TrimDragOrigin, (eid, { start, end }) => {
- c.TrimDragOrigin.start[eid] = start ?? c.TrimDragOrigin.start[eid] ?? 0;
- c.TrimDragOrigin.end[eid] = end ?? c.TrimDragOrigin.end[eid] ?? 0;
- });
-}
diff --git a/apps/web/src/components/engine/api/overlap.ts b/apps/web/src/components/engine/api/overlap.ts
deleted file mode 100644
index 322929a7..00000000
--- a/apps/web/src/components/engine/api/overlap.ts
+++ /dev/null
@@ -1,173 +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 { hasComponent, Not, Or, query } from 'bitecs';
-import { computeTrimStart, computeTrimEnd } from '../utils';
-import { cloneSubtree } from './serialize';
-import { getParentEntity } from './base';
-import { getEntityTree, isGroup } from './query';
-import { ChildOf } from '../components';
-import { setComponent } from './events';
-import { deleteEntity } from './entities';
-
-import type { EngineWorld } from './world';
-
-/**
- * Enforce the "no two direct children overlap in time" invariant of a
- * Sequential container after a clip has been moved (dragged + released).
- *
- * For every dragged clip whose direct parent is Sequential, each conflicting
- * sibling is resolved against the dragged clip's [start, end) span using an
- * overwrite model:
- * - fully covered → removed
- * - overlaps left edge → trimmed (out-point pulled back to the drop start)
- * - overlaps right edge→ trimmed (in-point pushed to the drop end)
- * - fully engulfs → split into two halves around the dropped clip
- *
- * Group siblings are resolved recursively at the leaf level; their bounds
- * recompute from their remaining children, and an emptied group is removed.
- *
- * Call this while the move's history transaction is still open so the whole
- * resolution lands in the same undo step as the move.
- */
-export function resolveSequentialOverlaps(world: EngineWorld, draggedEids: number[]): void {
- const c = world.components;
- if (draggedEids.length === 0) return;
-
- // Dragged clips never trim or remove each other.
- const ignore = new Set(draggedEids);
-
- for (const draggedEid of draggedEids) {
- const parentEid = getParentEntity(world, draggedEid);
- if (parentEid === null || !hasComponent(world, parentEid, c.Sequential)) continue;
-
- const occStart = c.Computed.start[draggedEid];
- const occEnd = c.Computed.end[draggedEid];
- if (occStart === undefined || occEnd === undefined || occEnd <= occStart) continue;
-
- // Snapshot the sibling list before mutating — resolution deletes, trims,
- // and clones, all of which change the live query result.
- const siblings = [...query(world, [Or(c.Geometry, c.Group), ChildOf(parentEid), Not(c.Deleted)])]
- .filter(sib => !ignore.has(sib));
-
- for (const sib of siblings) {
- resolveEntityOverlap(world, sib, occStart, occEnd, ignore);
- }
- }
-}
-
-/**
- * Enforce the no-overlap invariant on a *freshly created* Sequential container.
- *
- * resolveSequentialOverlaps has an authoritative set — the clips the user just
- * dragged. On creation there is none: every child is a peer that merely happened
- * to overlap on the canvas. We pick a deterministic precedence instead — earlier
- * clips win and keep their full extent; later clips yield, trimmed back to start
- * after the clip preceding them (fully-covered clips are removed).
- *
- * Children are processed in start-frame order with each one authoritative over
- * all later siblings. Because every authoritative pass trims *all* later
- * siblings uniformly, each clip's start stays monotonic by rank, so the "dropped
- * clip lands inside a sibling" split case from resolveEntityOverlap cannot arise
- * here — only edge trims and removals.
- *
- * Call inside the creation transaction so the resolution shares the group's
- * undo step.
- */
-export function resolveNewSequenceOverlaps(world: EngineWorld, sequenceEid: number): void {
- const c = world.components;
- if (!hasComponent(world, sequenceEid, c.Sequential)) return;
-
- const children = [...query(world, [Or(c.Geometry, c.Group), ChildOf(sequenceEid), Not(c.Deleted)])]
- .sort((a, b) => (c.Computed.start[a] ?? 0) - (c.Computed.start[b] ?? 0));
-
- // No dragged clips to protect; group recursion has no leaves to skip.
- const ignore = new Set<number>();
-
- for (let i = 0; i < children.length; i++) {
- const eid = children[i];
- // An earlier authoritative clip may have removed or trimmed this one.
- if (hasComponent(world, eid, c.Deleted)) continue;
- const occStart = c.Computed.start[eid];
- const occEnd = c.Computed.end[eid];
- if (occStart === undefined || occEnd === undefined || occEnd <= occStart) continue;
-
- for (let j = i + 1; j < children.length; j++) {
- const sib = children[j];
- if (hasComponent(world, sib, c.Deleted)) continue;
- resolveEntityOverlap(world, sib, occStart, occEnd, ignore);
- }
- }
-}
-
-/**
- * Resolve a single entity against the occupied [occStart, occEnd) span.
- * Groups recurse to their children; leaf clips are trimmed, removed, or split.
- */
-function resolveEntityOverlap(
- world: EngineWorld,
- eid: number,
- occStart: number,
- occEnd: number,
- ignore: Set<number>,
-): void {
- const c = world.components;
-
- const start = c.Computed.start[eid];
- const end = c.Computed.end[eid];
- if (start === undefined || end === undefined) return;
-
- // No temporal overlap — nothing to do. Touching edges (end === occStart or
- // start === occEnd) are abutting, not overlapping.
- if (end <= occStart || start >= occEnd) return;
-
- // Plain groups (incl. nested Sequential containers) have no intrinsic trim;
- // resolve their leaves and let the group's bounds recompute from what's left.
- if (isGroup(world, eid)) {
- if (start >= occStart && end <= occEnd) {
- // Whole group sits inside the dropped span — remove it outright.
- deleteEntity(world, eid);
- return;
- }
-
- const children = [...query(world, [Or(c.Geometry, c.Group), ChildOf(eid), Not(c.Deleted)])]
- .filter(child => !ignore.has(child));
- for (const child of children) {
- resolveEntityOverlap(world, child, occStart, occEnd, ignore);
- }
-
- // If every child was removed, the group is now empty — drop it too.
- const remaining = query(world, [Or(c.Geometry, c.Group), ChildOf(eid), Not(c.Deleted)]);
- if (remaining.length === 0) deleteEntity(world, eid);
- return;
- }
-
- const startCovered = start >= occStart;
- const endCovered = end <= occEnd;
-
- if (startCovered && endCovered) {
- // Fully covered — remove it.
- deleteEntity(world, eid);
- } else if (!startCovered && endCovered) {
- // Overlaps the dropped clip's left edge: keep the left remainder by
- // pulling the out-point back to the drop start.
- setComponent(world, eid, c.Trim, { end: computeTrimEnd(world, eid, occStart) });
- } else if (startCovered && !endCovered) {
- // Overlaps the dropped clip's right edge: keep the right remainder by
- // pushing the in-point forward to the drop end.
- setComponent(world, eid, c.Trim, { start: computeTrimStart(world, eid, occEnd) });
- } else {
- // The dropped clip lands inside this one — split it into two halves around
- // the span. Clone first so the copy inherits the original (un-shrunk) trim,
- // then shrink the original to the left half and the copy to the right half.
- const copyTrimStart = computeTrimStart(world, eid, occEnd);
- const result = cloneSubtree(world, getEntityTree(world, eid));
- const copyEid = result.get(eid);
-
- setComponent(world, eid, c.Trim, { end: computeTrimEnd(world, eid, occStart) });
- if (copyEid !== undefined) {
- setComponent(world, copyEid, c.Trim, { start: copyTrimStart });
- }
- }
-}
diff --git a/apps/web/src/components/engine/api/playback.ts b/apps/web/src/components/engine/api/playback.ts
deleted file mode 100644
index cdd3430d..00000000
--- a/apps/web/src/components/engine/api/playback.ts
+++ /dev/null
@@ -1,199 +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 { hasComponent, Not, Or, query } from 'bitecs';
-import { computeTrimStart, computeTrimEnd } from '../utils';
-import { cloneAudioCacheForSplit } from '../timeline/render/audio';
-import { cloneThumbnailCacheForSplit } from '../timeline/render/video';
-import { cloneSubtree } from './serialize';
-import { getParentEntity } from './base';
-import { getEntityTree, getNextName, isGroup, isSequence } from './query';
-import { ChildOf, PaintType } from '../components';
-import { addComponent, clearComponent, removeComponent, setComponent } from './events';
-import { createEntity } from './entities';
-import { appendChild, removeChild } from './hierarchy';
-
-import type { EngineWorld } from './world';
-
-export function togglePlayback(world: EngineWorld, eid: number | null = world.selection.scene) {
- const c = world.components;
-
- if (eid === null) return;
- // Playback is an ephemeral component — never recorded in history.
- setComponent(world, eid, c.Playback, {
- playing: Number(!c.Playback.playing[eid]),
- speed: 1,
- }, false);
- world.audio.resume();
-}
-
-const MAX_SHUTTLE_SPEED = 3;
-
-export function shuttleForward(world: EngineWorld) {
- const c = world.components;
- const sid = world.selection.scene;
- if (sid === null) return;
-
- const speed = c.Playback.speed[sid] || 1;
-
- if (c.Playback.playing[sid] !== 1 || speed < 0) {
- // Start forward playback at 1x
- setComponent(world, sid, c.Playback, { playing: 1, speed: 1 }, false);
- } else {
- // Double the forward speed, capped at max
- setComponent(world, sid, c.Playback, { speed: Math.min(speed * 2, MAX_SHUTTLE_SPEED) }, false);
- }
-
- world.audio.resume();
-}
-
-export function shuttleReverse(world: EngineWorld) {
- const c = world.components;
- const sid = world.selection.scene;
- if (sid === null) return;
-
- const speed = c.Playback.speed[sid] || 1;
-
- if (c.Playback.playing[sid] !== 1 || speed > 0) {
- // Start reverse playback at 1x
- setComponent(world, sid, c.Playback, { playing: 1, speed: -1 }, false);
- } else {
- // Double the reverse speed, capped at max
- setComponent(world, sid, c.Playback, { speed: Math.max(speed * 2, -MAX_SHUTTLE_SPEED) }, false);
- }
-
- world.audio.resume();
-}
-
-export function stopPlayback(world: EngineWorld) {
- const c = world.components;
- const sid = world.selection.scene;
- if (sid === null) return;
- setComponent(world, sid, c.Playback, { playing: 0, speed: 1 }, false);
-}
-
-export function splitAtPlayhead(world: EngineWorld) {
- const c = world.components;
- const sceneEid = world.selection.scene;
- if (sceneEid === null) return;
-
- world.history.transaction('Split', () => {
- const splitFrame = c.Computed.localTime[sceneEid];
-
- // (original, copy) root pairs produced by splitting clips as a unit.
- const pairs: { original: number; copy: number }[] = [];
-
- // Split a non-sequence clip (a leaf clip or a plain group) as a unit:
- // clone its whole subtree — including all children — then shrink the
- // original to end at the playhead and the copy to start there.
- const splitUnit = (eid: number) => {
- const tree = getEntityTree(world, eid);
- const nextTrimEnd = computeTrimEnd(world, eid, splitFrame);
- const copyTrimStart = computeTrimStart(world, eid, splitFrame);
-
- // Clone first so the copy inherits the original (un-shrunk) trimEnd.
- const result = cloneSubtree(world, tree);
- const copyEid = result.get(eid);
- if (!copyEid) return;
-
- // Apply trim to both split roots.
- setComponent(world, eid, c.Trim, { end: nextTrimEnd });
- setComponent(world, copyEid, c.Trim, { start: copyTrimStart });
-
- cloneAudioCacheForSplit(eid, copyEid);
- cloneThumbnailCacheForSplit(eid, copyEid);
-
- // Hand the primed video decoder to the copy. The original clip's range
- // now ends at the playhead (no longer in range), while the copy starts
- // there — so the copy is what's about to render. Both share delay and
- // playbackRate, so the decoder's relativeTime is already correct and
- // the copy renders its first frame without a blank/seek flash.
- const fills = query(world, [c.Paint, ChildOf(eid), Not(c.Deleted), Not(c.Hidden)]);
- const origVideoFill = fills.find(fid => c.Paint[fid] === PaintType.VIDEO);
- if (origVideoFill) {
- const decoder = c.VideoDecoder[origVideoFill];
- const copyVideoFill = result.get(origVideoFill);
- if (decoder && copyVideoFill) {
- // Decoder components are flat arrays with no `set` observer, so
- // setComponent would drop the value. Move it by direct assignment.
- addComponent(world, copyVideoFill, c.VideoDecoder, false);
- c.VideoDecoder[copyVideoFill] = decoder;
- removeComponent(world, origVideoFill, c.VideoDecoder, false);
- c.VideoDecoder[origVideoFill] = null;
- }
- }
-
- const origSequenceFill = fills.find(fid => c.Paint[fid] === PaintType.SEQUENCE);
- if (origSequenceFill) {
- const decoder = c.SequenceDecoder[origSequenceFill];
- const copySequenceFill = result.get(origSequenceFill);
- if (decoder && copySequenceFill) {
- addComponent(world, copySequenceFill, c.SequenceDecoder, false);
- c.SequenceDecoder[copySequenceFill] = decoder;
- removeComponent(world, origSequenceFill, c.SequenceDecoder, false);
- c.SequenceDecoder[origSequenceFill] = null;
- }
- }
-
- pairs.push({ original: eid, copy: copyEid });
- };
-
- // Split an entity at the playhead. A sequence can't be trimmed as a unit —
- // its time range is derived from its children — so we descend and split
- // each child the playhead intersects. Everything else (leaf clips, plain
- // groups, scenes) is split as a unit.
- const splitEntity = (eid: number) => {
- if (isSequence(world, eid)) {
- const children = [...query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), ChildOf(eid), Not(c.Deleted)])]
- .filter(child => c.Computed.visibility[child] === 1);
- for (const child of children) splitEntity(child);
- return;
- }
-
- splitUnit(eid);
- };
-
- // Split the current selection, or — when nothing is selected — every
- // visible clip directly under the active scene. The active scene itself is
- // the editing context, never a split target.
- const selection = [...query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), c.Selected])]
- .filter(eid => eid !== sceneEid)
- .filter(eid => c.Computed.visibility[eid] === 1);
-
- const roots = selection.length > 0
- ? selection
- : [...query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), ChildOf(sceneEid), Not(c.Deleted)])]
- .filter(eid => c.Computed.visibility[eid] === 1);
-
- for (const eid of roots) splitEntity(eid);
-
- // When the split happens outside a group/sequence (e.g. directly on the
- // scene), wrap each (original, copy) pair in a fresh Sequential group so
- // the two halves stay tied together in time.
- for (const { original, copy } of pairs) {
- const parentEid = getParentEntity(world, original);
- // If the parent already wraps children (a group or sequence) the
- // pair is implicitly tied together; only naked clips under a scene
- // need a fresh Sequential wrapper.
- if (parentEid === null || isGroup(world, parentEid) || hasComponent(world, parentEid, c.Sequential)) continue;
-
- const groupEid = createEntity(world);
- addComponent(world, groupEid, c.Group);
- addComponent(world, groupEid, c.Sequential);
- setComponent(world, groupEid, c.Name, getNextName(world, 'Sequence'));
- if (hasComponent(world, original, c.ClipHeight)) {
- setComponent(world, groupEid, c.ClipHeight, c.ClipHeight[original]);
- }
- appendChild(world, groupEid, parentEid);
-
- removeChild(world, original, parentEid);
- appendChild(world, original, groupEid);
- removeChild(world, copy, parentEid);
- appendChild(world, copy, groupEid);
- }
-
- clearComponent(world, c.Selected, false);
- pairs.forEach(({ copy }) => addComponent(world, copy, c.Selected, false));
- });
-}
diff --git a/apps/web/src/components/engine/api/query.ts b/apps/web/src/components/engine/api/query.ts
deleted file mode 100644
index 9b113fcb..00000000
--- a/apps/web/src/components/engine/api/query.ts
+++ /dev/null
@@ -1,150 +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 { query, hasComponent, Not } from 'bitecs';
-import { ChildOf, GeometryType, PaintType } from '../components';
-import { getParentEntity } from './base';
-
-import type { EngineWorld } from './world';
-
-export function isScene(world: EngineWorld, eid: number): boolean {
- return hasComponent(world, eid, world.components.Scene);
-}
-
-/**
- * Nearest ancestor of `eid` (excluding `eid` itself) that is a scene, or null.
- */
-export function getSceneAncestor(world: EngineWorld, eid: number): number | null {
- let current = getParentEntity(world, eid);
- while (current !== null) {
- if (isScene(world, current)) return current;
- current = getParentEntity(world, current);
- }
- return null;
-}
-
-export function isGroup(world: EngineWorld, eid: number): boolean {
- return hasComponent(world, eid, world.components.Group);
-}
-
-export function isGroupLike(world: EngineWorld, eid: number): boolean {
- const c = world.components;
- return hasComponent(world, eid, c.Group) || hasComponent(world, eid, c.Scene);
-}
-
-export function isSequence(world: EngineWorld, eid: number): boolean {
- return hasComponent(world, eid, world.components.Sequential);
-}
-
-export function isAudio(world: EngineWorld, eid: number): boolean {
- return hasComponent(world, eid, world.components.Audio);
-}
-
-export function isAdjustmentLayer(world: EngineWorld, eid: number): boolean {
- return hasComponent(world, eid, world.components.AdjustmentLayer);
-}
-
-export function isCaption(world: EngineWorld, eid: number): boolean {
- return hasComponent(world, eid, world.components.Caption);
-}
-
-export function isText(world: EngineWorld, eid: number): boolean {
- const c = world.components;
- return hasComponent(world, eid, c.Geometry) && c.Geometry[eid] === GeometryType.TEXT;
-}
-
-export function isRect(world: EngineWorld, eid: number): boolean {
- const c = world.components;
- return hasComponent(world, eid, c.Geometry) && c.Geometry[eid] === GeometryType.RECT;
-}
-
-/** A vanilla rect — not a container (group/scene/sequence) or audio clip.
- * (Captions live on TEXT geometry, so they're naturally excluded.) */
-export function isShape(world: EngineWorld, eid: number): boolean {
- const c = world.components;
- return hasComponent(world, eid, c.Geometry)
- && c.Geometry[eid] === GeometryType.RECT
- && !hasComponent(world, eid, c.Group)
- && !hasComponent(world, eid, c.Scene)
- && !hasComponent(world, eid, c.Audio);
-}
-
-export function hasHtmlPaint(world: EngineWorld, eid: number): boolean {
- const c = world.components;
- return (c.Cache.fills[eid] ?? []).some(fid => c.Paint[fid] === PaintType.HTML);
-}
-
-export function hasSurfacePaint(world: EngineWorld, eid: number): boolean {
- const c = world.components;
- return (c.Cache.fills[eid] ?? []).some(fid => c.Paint[fid] === PaintType.SURFACE);
-}
-
-export function isMask(world: EngineWorld, eid: number): boolean {
- return hasComponent(world, eid, world.components.IsMask);
-}
-
-/**
- * Returns the local frame for a node in its own source/content time — the
- * timeline frame relative to the node's delay, scaled by its playback rate.
- * This matches `Computed.localTime` (the space keyframes are sampled
- * in), so keyframes authored at any playback rate land on the right content
- * frame.
- */
-export function getNodeLocalFrame(world: EngineWorld, nodeEid: number): number {
- const c = world.components;
-
- const delay = c.Computed.delay[nodeEid];
- const playbackRate = c.PlaybackRate[nodeEid] ?? 1;
-
- let current: number | null = nodeEid;
- while (current) {
- const parent = getParentEntity(world, current);
-
- // must be a root playback entity
- if (hasComponent(world, current, c.Playback) && parent === null) {
- const currentTime = c.Computed.localTime[current];
- return Math.round((currentTime - delay) * playbackRate);
- }
-
- current = parent;
- }
-
- return 0;
-}
-
-export function getNextName(world: EngineWorld, prefix: string): string {
- const c = world.components;
-
- let max = 0;
- const pattern = new RegExp(`^${prefix} (\\d+)$`);
-
- for (let i = 0; i < c.Name.length; i++) {
- if (c.Name[i] === undefined) continue;
- const match = c.Name[i]?.match(pattern);
- if (match) {
- const n = parseInt(match[1] ?? '0');
- if (n > max) max = n;
- }
- }
-
- return `${prefix} ${max + 1}`;
-}
-
-export function getEntityTree(world: EngineWorld, rootEid: number): number[] {
- const c = world.components;
-
- const tree: number[] = [];
-
- const walk = (eid: number): number[] => {
- tree.push(eid);
-
- for (const cid of query(world, [ChildOf(eid), Not(c.Deleted)])) {
- walk(cid);
- }
-
- return tree;
- }
-
- return walk(rootEid);
-}
diff --git a/apps/web/src/components/engine/api/resize.ts b/apps/web/src/components/engine/api/resize.ts
deleted file mode 100644
index 97805441..00000000
--- a/apps/web/src/components/engine/api/resize.ts
+++ /dev/null
@@ -1,210 +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 { hasComponent, query } from "bitecs";
-import { syncKeyframeTrack } from "./keyframe";
-import { removeComponent, setComponent } from "./events";
-import { getParentEntity } from "./base";
-import { ChildOf, ConstraintType } from "../components";
-
-import type { EngineWorld } from "./world";
-
-type ResizeParams = { width?: number; height?: number };
-
-/**
- * Resize an entity. Enforces aspect ratio, writes the final Size, propagates
- * Computed size down the subtree, reapplies sibling constraints, and syncs
- * the width/height keyframe tracks. Group/Sequential entities don't own a Size
- * — calling this on one strips the component instead.
- */
-export function resizeEntity(world: EngineWorld, eid: number, params: ResizeParams): void {
- const c = world.components;
-
- if (hasComponent(world, eid, c.Group)) {
- removeComponent(world, eid, c.Size);
- return;
- }
-
- world.history.transaction('Resize', () => {
- // Enforce aspect ratio
- const arWidth = c.KeepAspectRatio.width[eid] ?? 0;
- const arHeight = c.KeepAspectRatio.height[eid] ?? 0;
-
- if (hasComponent(world, eid, c.KeepAspectRatio) && arWidth > 0 && arHeight > 0) {
- const ratio = arWidth / arHeight;
-
- if (params.width !== undefined && params.height === undefined) {
- params.height = Math.round(params.width / ratio);
- } else if (params.height !== undefined && params.width === undefined) {
- params.width = Math.round(params.height * ratio);
- } else if (params.width !== undefined && params.height !== undefined) {
- params.height = Math.round(params.width / ratio);
- }
- }
-
- if (typeof params.width === 'number') {
- params.width = Math.round(params.width);
- }
- if (typeof params.height === 'number') {
- params.height = Math.round(params.height);
- }
-
- setComponent(world, eid, c.Size, params);
- propagateSize(world, eid);
- resolveConstraintOffsets(world, eid);
-
- if (params.width !== undefined) {
- syncKeyframeTrack(world, eid, "width");
- }
- if (params.height !== undefined) {
- syncKeyframeTrack(world, eid, "height");
- }
- });
-}
-
-export function propagateSize(world: EngineWorld, eid: number) {
- const c = world.components;
- if (!hasComponent(world, eid, c.Size)) return;
-
- c.Computed.width[eid] = Math.round(c.Size.width[eid]);
- c.Computed.height[eid] = Math.round(c.Size.height[eid]);
-
- for (const child of query(world, [ChildOf(eid), c.Size])) {
- propagateSize(world, child);
- }
-}
-
-export function resolveConstraintOffsets(world: EngineWorld, eid: number): void {
- const c = world.components;
-
- let dimsEid: number | null = eid;
- // Sequences aren't spatial constructs — they mirror their parent's frame.
- while (dimsEid !== null && hasComponent(world, dimsEid, c.Sequential)) {
- dimsEid = getParentEntity(world, dimsEid);
- }
- if (dimsEid === null) return;
-
- resolveConstraintOffsetsAgainst(world, eid, dimsEid);
-}
-
-function resolveConstraintOffsetsAgainst(world: EngineWorld, eid: number, dimsEid: number): void {
- const c = world.components;
-
- // Sequential children are transparent: resolve their children against the
- // same spatial parent.
- for (const childEid of query(world, [ChildOf(eid), c.Sequential])) {
- resolveConstraintOffsetsAgainst(world, childEid, dimsEid);
- }
-
- for (const childEid of query(world, [ChildOf(eid), c.Constraint])) {
- if (hasComponent(world, childEid, c.Sequential)) continue;
-
- const curParentW = c.Computed.width[dimsEid];
- const curParentH = c.Computed.height[dimsEid];
-
- const prevParentW = c.ConstraintCache.parentWidth[childEid];
- const prevParentH = c.ConstraintCache.parentHeight[childEid];
-
- const horizontalConstraint = c.Constraint.horizontal[childEid] ?? 0;
- const verticalConstraint = c.Constraint.vertical[childEid] ?? 0;
-
- const curChildW = c.Computed.width[childEid];
- const curChildH = c.Computed.height[childEid];
-
- const prevChildW = c.ConstraintCache.childWidth[childEid];
- const prevChildH = c.ConstraintCache.childHeight[childEid];
-
- // Update caches
- c.ConstraintCache.parentWidth[childEid] = curParentW;
- c.ConstraintCache.parentHeight[childEid] = curParentH;
- c.ConstraintCache.childWidth[childEid] = curChildW;
- c.ConstraintCache.childHeight[childEid] = curChildH;
-
- // First time seeing this child (e.g. constraint just assigned): seed the cache
- // without adjusting position — diffing against `undefined` would yield NaN.
- if (prevParentW === undefined || prevParentH === undefined) {
- resolveConstraintOffsets(world, childEid);
- continue;
- }
-
- // No change
- if (prevParentW === curParentW && prevParentH === curParentH) continue;
-
- const dpW = curParentW - prevParentW;
- const dpH = curParentH - prevParentH;
- const dcW = curChildW - (prevChildW ?? curChildW);
- const dcH = curChildH - (prevChildH ?? curChildH);
- const positionX = c.Computed.positionX[childEid];
- const positionY = c.Computed.positionY[childEid];
-
- // Resizing constraints (STRETCH/SCALE) record their new dimension here
- // rather than writing it straight away, so a locked aspect ratio can couple
- // the two axes before the Size is committed.
- let newChildW: number | undefined;
- let newChildH: number | undefined;
-
- if (horizontalConstraint === ConstraintType.MAX) {
- setComponent(world, childEid, c.Position, { x: positionX + dpW - dcW });
- } else if (horizontalConstraint === ConstraintType.CENTER) {
- setComponent(world, childEid, c.Position, { x: positionX + dpW / 2 - dcW / 2 });
- } else if (horizontalConstraint === ConstraintType.STRETCH) {
- // Pin both left and right edges: hold the left edge in place and grow the
- // child by the parent's width delta so the right margin stays constant.
- newChildW = curChildW + dpW;
- } else if (horizontalConstraint === ConstraintType.SCALE && prevParentW !== 0) {
- // Scale offset and width by the parent's horizontal ratio so the child
- // keeps its relative position and proportion.
- const scale = curParentW / prevParentW;
- newChildW = Math.round(curChildW * scale);
- setComponent(world, childEid, c.Position, { x: positionX * scale });
- }
-
- if (verticalConstraint === ConstraintType.MAX) {
- setComponent(world, childEid, c.Position, { y: positionY + dpH - dcH });
- } else if (verticalConstraint === ConstraintType.CENTER) {
- setComponent(world, childEid, c.Position, { y: positionY + dpH / 2 - dcH / 2 });
- } else if (verticalConstraint === ConstraintType.STRETCH) {
- // Pin both top and bottom edges: hold the top edge in place and grow the
- // child by the parent's height delta so the bottom margin stays constant.
- newChildH = curChildH + dpH;
- } else if (verticalConstraint === ConstraintType.SCALE && prevParentH !== 0) {
- // Scale offset and height by the parent's vertical ratio so the child
- // keeps its relative position and proportion.
- const scale = curParentH / prevParentH;
- newChildH = Math.round(curChildH * scale);
- setComponent(world, childEid, c.Position, { y: positionY * scale });
- }
-
- // When the child's aspect ratio is locked, drive the other dimension from
- // whichever axis resized so the proportion is preserved. Width wins when
- // both axes resize, mirroring resizeEntity's precedence.
- const arWidth = c.KeepAspectRatio.width[childEid] ?? 0;
- const arHeight = c.KeepAspectRatio.height[childEid] ?? 0;
- if (
- hasComponent(world, childEid, c.KeepAspectRatio) &&
- arWidth > 0 && arHeight > 0 &&
- (newChildW !== undefined || newChildH !== undefined)
- ) {
- const ratio = arWidth / arHeight;
- if (newChildW !== undefined) {
- newChildH = Math.round(newChildW / ratio);
- } else if (newChildH !== undefined) {
- newChildW = Math.round(newChildH * ratio);
- }
- }
-
- if (newChildW !== undefined) {
- setComponent(world, childEid, c.Size, { width: newChildW });
- c.Computed.width[childEid] = newChildW;
- c.ConstraintCache.childWidth[childEid] = newChildW;
- }
- if (newChildH !== undefined) {
- setComponent(world, childEid, c.Size, { height: newChildH });
- c.Computed.height[childEid] = newChildH;
- c.ConstraintCache.childHeight[childEid] = newChildH;
- }
-
- resolveConstraintOffsets(world, childEid);
- }
-}
diff --git a/apps/web/src/components/engine/api/serialize.ts b/apps/web/src/components/engine/api/serialize.ts
deleted file mode 100644
index 0aefe3d0..00000000
--- a/apps/web/src/components/engine/api/serialize.ts
+++ /dev/null
@@ -1,470 +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 { hasComponent, getRelationTargets } from "bitecs";
-import { ChildOf } from "../components/relations";
-import { assert } from "@/utils";
-import { createEntity, deleteEntity } from "./entities";
-import { appendChild } from "./hierarchy";
-import { addComponent, setComponent } from "./events";
-import { resizeEntity } from "./resize";
-
-import type { DBEntity } from "../types";
-import type { EngineWorld } from "./world";
-
-export function serializeEntity(world: EngineWorld, eid: number): DBEntity {
- const record: DBEntity = { id: String(eid), eid };
- const c = world.components;
-
- if (hasComponent(world, eid, c.Geometry)) {
- record.Geometry = c.Geometry[eid];
- }
- if (hasComponent(world, eid, c.Paint)) {
- record.Paint = c.Paint[eid];
- }
- if (hasComponent(world, eid, c.Group)) {
- record.Group = {};
- }
- if (hasComponent(world, eid, c.Scene)) {
- record.Scene = {};
- }
- if (hasComponent(world, eid, c.Audio)) {
- record.Audio = {};
- }
- if (hasComponent(world, eid, c.AdjustmentLayer)) {
- record.AdjustmentLayer = {};
- }
- if (hasComponent(world, eid, c.Effect)) {
- record.Effect = { type: c.Effect.type[eid], value: c.Effect.value[eid] };
- }
- if (hasComponent(world, eid, c.Shadow)) {
- record.Shadow = {};
- }
- if (hasComponent(world, eid, c.Stroke)) {
- record.Stroke = {};
- }
- if (hasComponent(world, eid, c.Name)) {
- record.Name = c.Name[eid];
- }
- if (hasComponent(world, eid, c.Key)) {
- record.Key = c.Key[eid];
- }
- if (hasComponent(world, eid, c.MountScript)) {
- record.MountScript = {
- mountId: c.MountScript.mountId[eid],
- scriptAssetId: c.MountScript.scriptAssetId[eid],
- };
- }
- if (hasComponent(world, eid, c.MountPath)) {
- record.MountPath = {
- mountId: c.MountPath.mountId[eid],
- path: c.MountPath.path[eid],
- };
- }
- if (hasComponent(world, eid, c.ItemIndex)) {
- record.ItemIndex = c.ItemIndex[eid] ?? 0;
- }
- if (hasComponent(world, eid, c.ClipHeight)) {
- record.ClipHeight = c.ClipHeight[eid];
- }
- if (hasComponent(world, eid, c.Expanded)) {
- record.Expanded = {};
- }
- if (hasComponent(world, eid, ChildOf('*'))) {
- record.ChildOf = getRelationTargets(world, eid, ChildOf)[0] ?? 0;
- }
- if (hasComponent(world, eid, c.IsMask)) {
- record.IsMask = {};
- }
- if (hasComponent(world, eid, c.Position)) {
- record.Position = { x: c.Position.x[eid], y: c.Position.y[eid] };
- }
- if (hasComponent(world, eid, c.Offset)) {
- record.Offset = { x: c.Offset.x[eid], y: c.Offset.y[eid] };
- }
- if (hasComponent(world, eid, c.Rotation)) {
- record.Rotation = c.Rotation[eid];
- }
- if (hasComponent(world, eid, c.Scale)) {
- record.Scale = { x: c.Scale.x[eid], y: c.Scale.y[eid] };
- }
- if (hasComponent(world, eid, c.UniformScale)) {
- record.UniformScale = c.UniformScale[eid];
- }
- if (hasComponent(world, eid, c.Anchor)) {
- record.Anchor = { x: c.Anchor.x[eid], y: c.Anchor.y[eid] };
- }
- if (hasComponent(world, eid, c.Skew)) {
- record.Skew = { x: c.Skew.x[eid], y: c.Skew.y[eid] };
- }
- if (hasComponent(world, eid, c.Size)) {
- record.Size = { width: c.Size.width[eid], height: c.Size.height[eid] };
- }
- if (hasComponent(world, eid, c.Flip)) {
- record.Flip = { x: c.Flip.x[eid], y: c.Flip.y[eid] };
- }
- if (hasComponent(world, eid, c.Appearance)) {
- record.Appearance = {
- opacity: c.Appearance.opacity[eid],
- blendMode: c.Appearance.blendMode[eid],
- };
- }
- if (hasComponent(world, eid, c.CornerRadius)) {
- record.CornerRadius = c.CornerRadius[eid];
- }
- if (hasComponent(world, eid, c.MixedCornerRadius)) {
- record.MixedCornerRadius = {
- topLeft: c.MixedCornerRadius.topLeft[eid],
- topRight: c.MixedCornerRadius.topRight[eid],
- bottomRight: c.MixedCornerRadius.bottomRight[eid],
- bottomLeft: c.MixedCornerRadius.bottomLeft[eid],
- };
- }
- if (hasComponent(world, eid, c.Color)) {
- record.Color = c.Color[eid];
- }
- if (hasComponent(world, eid, c.Blur)) {
- record.Blur = c.Blur[eid];
- }
- if (hasComponent(world, eid, c.AssetId)) {
- record.AssetId = c.AssetId[eid];
- }
- if (hasComponent(world, eid, c.ScaleMode)) {
- record.ScaleMode = c.ScaleMode[eid];
- }
- if (hasComponent(world, eid, c.Shader)) {
- record.Shader = {
- code: c.Shader.code[eid],
- uniforms: c.Shader.uniforms[eid] ?? undefined,
- };
- }
- if (hasComponent(world, eid, c.Volume)) {
- record.Volume = c.Volume[eid];
- }
- if (hasComponent(world, eid, c.Muted)) {
- record.Muted = {};
- }
- if (hasComponent(world, eid, c.ColorStop)) {
- record.ColorStop = {
- offset: c.ColorStop.offset[eid],
- color: c.ColorStop.color[eid],
- opacity: c.ColorStop.opacity[eid],
- };
- }
- if (hasComponent(world, eid, c.StrokeStyle)) {
- record.StrokeStyle = {
- width: c.StrokeStyle.width[eid],
- join: c.StrokeStyle.join[eid],
- cap: c.StrokeStyle.cap[eid],
- miterLimit: c.StrokeStyle.miterLimit[eid],
- };
- }
- if (hasComponent(world, eid, c.Hidden)) {
- record.Hidden = true;
- }
- if (hasComponent(world, eid, c.ClipsContent)) {
- record.ClipsContent = true;
- }
- if (hasComponent(world, eid, c.Sequential)) {
- record.Sequential = {};
- }
- if (hasComponent(world, eid, c.Playback)) {
- record.Playback = {
- loop: c.Playback.loop[eid],
- };
- }
- if (hasComponent(world, eid, c.Delay)) {
- record.Delay = c.Delay[eid];
- }
- if (hasComponent(world, eid, c.PlaybackRate)) {
- record.PlaybackRate = c.PlaybackRate[eid];
- }
- if (hasComponent(world, eid, c.Trim)) {
- record.Trim = {
- start: c.Trim.start[eid],
- end: c.Trim.end[eid],
- };
- }
- if (hasComponent(world, eid, c.Transition)) {
- record.Transition = {
- type: c.Transition.type[eid],
- duration: c.Transition.duration[eid],
- };
- }
- if (hasComponent(world, eid, c.Constraint)) {
- record.Constraint = {
- horizontal: c.Constraint.horizontal[eid],
- vertical: c.Constraint.vertical[eid],
- };
- }
- if (hasComponent(world, eid, c.KeepAspectRatio)) {
- record.KeepAspectRatio = {
- width: c.KeepAspectRatio.width[eid],
- height: c.KeepAspectRatio.height[eid],
- };
- }
- if (hasComponent(world, eid, c.KeyframeTrack)) {
- record.KeyframeTrack = {
- property: c.KeyframeTrack.property[eid],
- };
- }
- if (hasComponent(world, eid, c.Keyframe)) {
- record.Keyframe = {
- time: c.Keyframe.time[eid],
- value: c.Keyframe.value[eid],
- easing: c.Keyframe.easing[eid],
- };
- }
- if (hasComponent(world, eid, c.Animation)) {
- record.Animation = {
- duration: c.Animation.duration[eid],
- delay: c.Animation.delay[eid],
- type: c.Animation.type[eid],
- phase: c.Animation.phase[eid],
- };
- }
- if (hasComponent(world, eid, c.Caption)) {
- record.Caption = {
- type: c.Caption.type[eid],
- colors: c.Caption.colors[eid]?.map(v => v),
- verticalAlign: c.Caption.verticalAlign[eid],
- };
- }
- if (hasComponent(world, eid, c.Chars)) {
- record.Chars = c.Chars[eid];
- }
- if (hasComponent(world, eid, c.TextStyle)) {
- record.TextStyle = {
- leading: c.TextStyle.leading[eid],
- fontSize: c.TextStyle.fontSize[eid],
- fontFamily: c.TextStyle.fontFamily[eid],
- fontWeight: c.TextStyle.fontWeight[eid],
- fontStyle: c.TextStyle.fontStyle[eid],
- textAlign: c.TextStyle.textAlign[eid],
- textBaseline: c.TextStyle.textBaseline[eid],
- textCase: c.TextStyle.textCase[eid],
- letterSpacing: c.TextStyle.letterSpacing[eid],
- };
- }
-
- return record;
-}
-
-export function deserializeEntity(world: EngineWorld, eid: number, e: Partial<DBEntity>): void {
- const c = world.components;
-
- if (e.Geometry !== undefined) {
- setComponent(world, eid, c.Geometry, e.Geometry);
- }
- if (e.Paint !== undefined) {
- setComponent(world, eid, c.Paint, e.Paint);
- }
- if (e.Group !== undefined) {
- addComponent(world, eid, c.Group);
- }
- if (e.Scene !== undefined) {
- addComponent(world, eid, c.Scene);
- }
- if (e.Audio !== undefined) {
- addComponent(world, eid, c.Audio);
- }
- if (e.AdjustmentLayer !== undefined) {
- addComponent(world, eid, c.AdjustmentLayer);
- }
- if (e.Effect !== undefined) {
- setComponent(world, eid, c.Effect, e.Effect);
- }
- if (e.Shadow !== undefined) {
- addComponent(world, eid, c.Shadow);
- }
- if (e.Stroke !== undefined) {
- addComponent(world, eid, c.Stroke);
- }
- if (e.Name !== undefined) {
- setComponent(world, eid, c.Name, e.Name);
- }
- if (e.Key !== undefined) {
- setComponent(world, eid, c.Key, e.Key);
- }
- if (e.MountScript !== undefined) {
- setComponent(world, eid, c.MountScript, e.MountScript);
- }
- if (e.MountPath !== undefined) {
- setComponent(world, eid, c.MountPath, e.MountPath);
- }
- if (e.ItemIndex !== undefined) {
- setComponent(world, eid, c.ItemIndex, e.ItemIndex);
- }
- if (e.ClipHeight !== undefined) {
- setComponent(world, eid, c.ClipHeight, e.ClipHeight);
- }
- if (e.Expanded !== undefined) {
- addComponent(world, eid, c.Expanded);
- }
- if (e.IsMask !== undefined) {
- addComponent(world, eid, c.IsMask);
- }
- if (e.Position !== undefined) {
- setComponent(world, eid, c.Position, e.Position);
- }
- if (e.Offset !== undefined) {
- setComponent(world, eid, c.Offset, e.Offset);
- }
- if (e.Rotation !== undefined) {
- setComponent(world, eid, c.Rotation, e.Rotation);
- }
- if (e.Scale !== undefined) {
- setComponent(world, eid, c.Scale, e.Scale);
- }
- if (e.UniformScale !== undefined) {
- setComponent(world, eid, c.UniformScale, e.UniformScale);
- }
- if (e.Anchor !== undefined) {
- setComponent(world, eid, c.Anchor, e.Anchor);
- }
- if (e.Skew !== undefined) {
- setComponent(world, eid, c.Skew, e.Skew);
- }
- if (e.Size !== undefined) {
- resizeEntity(world, eid, e.Size);
- }
- if (e.Flip !== undefined) {
- setComponent(world, eid, c.Flip, e.Flip);
- }
- if (e.Appearance !== undefined) {
- setComponent(world, eid, c.Appearance, e.Appearance);
- }
- if (e.CornerRadius !== undefined) {
- setComponent(world, eid, c.CornerRadius, e.CornerRadius);
- }
- if (e.MixedCornerRadius !== undefined) {
- setComponent(world, eid, c.MixedCornerRadius, e.MixedCornerRadius);
- }
- if (e.Color !== undefined) {
- setComponent(world, eid, c.Color, e.Color);
- }
- if (e.Blur !== undefined) {
- setComponent(world, eid, c.Blur, e.Blur);
- }
- if (e.AssetId !== undefined) {
- setComponent(world, eid, c.AssetId, e.AssetId);
- }
- if (e.ScaleMode !== undefined) {
- setComponent(world, eid, c.ScaleMode, e.ScaleMode);
- }
- if (e.Shader !== undefined) {
- setComponent(world, eid, c.Shader, e.Shader);
- }
- if (e.Volume !== undefined) {
- setComponent(world, eid, c.Volume, e.Volume);
- }
- if (e.Muted !== undefined) {
- addComponent(world, eid, c.Muted);
- }
- if (e.ColorStop !== undefined) {
- setComponent(world, eid, c.ColorStop, e.ColorStop);
- }
- if (e.StrokeStyle !== undefined) {
- setComponent(world, eid, c.StrokeStyle, e.StrokeStyle);
- }
- if (e.Hidden !== undefined) {
- addComponent(world, eid, c.Hidden);
- }
- if (e.ClipsContent !== undefined) {
- addComponent(world, eid, c.ClipsContent);
- }
- if (e.Sequential !== undefined) {
- addComponent(world, eid, c.Sequential);
- }
- if (e.Playback !== undefined) {
- setComponent(world, eid, c.Playback, e.Playback);
- }
- if (e.Delay !== undefined) {
- setComponent(world, eid, c.Delay, e.Delay);
- }
- if (e.PlaybackRate !== undefined) {
- setComponent(world, eid, c.PlaybackRate, e.PlaybackRate);
- }
- if (e.Trim !== undefined) {
- setComponent(world, eid, c.Trim, e.Trim);
- }
- if (e.Transition !== undefined) {
- setComponent(world, eid, c.Transition, e.Transition);
- }
- if (e.Constraint !== undefined) {
- setComponent(world, eid, c.Constraint, e.Constraint);
- }
- if (e.KeepAspectRatio !== undefined) {
- setComponent(world, eid, c.KeepAspectRatio, e.KeepAspectRatio);
- }
- if (e.KeyframeTrack !== undefined) {
- setComponent(world, eid, c.KeyframeTrack, e.KeyframeTrack);
- }
- if (e.Keyframe !== undefined) {
- setComponent(world, eid, c.Keyframe, e.Keyframe);
- }
- if (e.Animation !== undefined) {
- setComponent(world, eid, c.Animation, e.Animation);
- }
- if (e.Caption !== undefined) {
- setComponent(world, eid, c.Caption, e.Caption);
- }
- if (e.Chars !== undefined) {
- setComponent(world, eid, c.Chars, e.Chars);
- }
- if (e.TextStyle !== undefined) {
- setComponent(world, eid, c.TextStyle, e.TextStyle);
- }
- // Attach to parent last so rebuildCaches sees every component this entity
- // has (KeyframeTrack, Keyframe, Animation, IsMask, …). Otherwise their
- // caches stay empty and the UI/motion system can't find them after reload.
- if (e.ChildOf !== undefined) {
- appendChild(world, eid, e.ChildOf);
- }
-}
-
-export function cloneFromRecords(world: EngineWorld, records: DBEntity[]) {
- const eidMap = new Map<number, number>();
-
- for (const record of records) {
- eidMap.set(record.eid, createEntity(world));
- }
-
- for (const record of records) {
- const eid = eidMap.get(record.eid);
- const copy = { ...record };
-
- try {
- assert(eid, `Entity ${copy.eid} not found`);
-
- if (copy.ChildOf !== undefined) {
- copy.ChildOf = eidMap.get(copy.ChildOf) ?? copy.ChildOf;
- }
-
- deserializeEntity(world, eid, copy);
- } catch (error) {
- if (eid) deleteEntity(world, eid);
- console.error(error);
- }
- }
-
- return eidMap;
-}
-
-/**
- * Strips mount identity from records so a *same-world* copy doesn't collide
- * with the original mount's id — adopt would otherwise bind two entities to one
- * `MountPath`. A duplicated/pasted mount becomes plain static entities: its
- * runtime hosts won't re-run (a documented limitation; re-mount to re-animate).
- * The export/capture clone (`cloneFromRecords` directly) keeps identity so it
- * can adopt.
- */
-export function stripMountIdentity(records: DBEntity[]): DBEntity[] {
- return records.map(({ MountScript: _script, MountPath: _path, ...rest }) => rest);
-}
-
-export function cloneSubtree(world: EngineWorld, tree: number[]) {
- const records = tree.map(e => serializeEntity(world, e));
- return cloneFromRecords(world, stripMountIdentity(records));
-}
diff --git a/apps/web/src/components/engine/api/timeline-index.ts b/apps/web/src/components/engine/api/timeline-index.ts
deleted file mode 100644
index c95d9012..00000000
--- a/apps/web/src/components/engine/api/timeline-index.ts
+++ /dev/null
@@ -1,152 +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 { hasComponent, Not, query } from "bitecs";
-import { ChildOf } from "../components/relations";
-import { sortByItemIndex } from "../utils/sort";
-import { isSequence } from "./query";
-
-import type { EngineWorld } from "./world";
-
-export type TimelineNodeKind = 'geometry' | 'sub-item' | 'keyframe-track';
-
-export type TimelineNode = {
- eid: number;
- kind: TimelineNodeKind;
- expanded: boolean;
- expandable: boolean;
- children: TimelineNode[];
-};
-
-export type TimelineIndexValue = {
- root: number | null;
- layers: TimelineNode[];
-};
-
-export function buildTimelineLayers(world: EngineWorld, parent: number): TimelineNode[] {
- const c = world.components;
- const sequence = isSequence(world, parent);
-
- const tracks: number[] = [];
- const geoms: number[] = [];
- const masks: number[] = [];
- const subitems: number[] = [];
-
- for (const cid of query(world, [ChildOf(parent), Not(c.Deleted)])) {
- if (hasComponent(world, cid, c.Keyframe)) continue;
- if (sequence && !hasKeyframes(world, cid)) continue;
-
- if (hasComponent(world, cid, c.KeyframeTrack)) {
- tracks.push(cid);
- } else if (hasComponent(world, cid, c.IsMask)) {
- masks.push(cid);
- } else if (hasComponent(world, cid, c.Geometry) || hasComponent(world, cid, c.Group) || hasComponent(world, cid, c.AdjustmentLayer)) {
- geoms.push(cid);
- } else {
- subitems.push(cid);
- }
- }
-
- const sort = sortByItemIndex(world);
- tracks.sort(sort).reverse();
- geoms.sort(sort).reverse();
- masks.sort(sort).reverse();
- subitems.sort(sort).reverse();
-
- const nodes: TimelineNode[] = [];
-
- for (const cid of tracks) {
- nodes.push({
- eid: cid,
- kind: 'keyframe-track',
- expanded: false,
- expandable: false,
- children: [],
- });
- }
-
- for (const cid of subitems) {
- const node = buildNode(world, cid, 'sub-item');
- if (node.expandable) {
- nodes.push(node);
- }
- }
-
- for (const cid of geoms) {
- nodes.push(buildNode(world, cid, 'geometry'));
- }
-
- for (const cid of masks) {
- nodes.push(buildNode(world, cid, 'geometry'));
- }
-
- return nodes;
-}
-
-/**
- * Build one row node. Expanded nodes get their subtree; collapsed ones stay
- * empty and only probe whether a subtree exists.
- */
-function buildNode(world: EngineWorld, eid: number, kind: TimelineNodeKind): TimelineNode {
- const c = world.components;
-
- if (!hasComponent(world, eid, c.Expanded)) {
- return {
- eid,
- kind,
- expanded: false,
- expandable: isExpandable(world, eid),
- children: [],
- };
- }
-
- const children = buildTimelineLayers(world, eid);
- return {
- eid,
- kind,
- expanded: true,
- expandable: children.length > 0,
- children,
- };
-}
-
-/**
- * Early-exit probe: check if a layer could be expanded.
- */
-function isExpandable(world: EngineWorld, parent: number): boolean {
- const c = world.components;
- const sequence = isSequence(world, parent);
-
- for (const cid of query(world, [ChildOf(parent), Not(c.Deleted)])) {
- if (hasComponent(world, cid, c.Keyframe)) continue;
- if (sequence && !hasKeyframes(world, cid)) continue;
- if (
- hasComponent(world, cid, c.IsMask) ||
- hasComponent(world, cid, c.Geometry) ||
- hasComponent(world, cid, c.Group) ||
- hasComponent(world, cid, c.AdjustmentLayer) ||
- hasComponent(world, cid, c.KeyframeTrack) ||
- isExpandable(world, cid)
- ) {
- return true;
- }
- }
-
- return false;
-}
-
-/**
- * Deep probe: does `eid`'s subtree contain a live KeyframeTrack? Tracks are
- * deleted with their last keyframe, so track presence implies keyframes.
- */
-function hasKeyframes(world: EngineWorld, eid: number): boolean {
- const c = world.components;
-
- for (const cid of query(world, [ChildOf(eid), Not(c.Deleted)])) {
- if (hasComponent(world, cid, c.KeyframeTrack)) return true;
- if (hasKeyframes(world, cid)) return true;
- }
-
- return false;
-}
diff --git a/apps/web/src/components/engine/api/utils.ts b/apps/web/src/components/engine/api/utils.ts
deleted file mode 100644
index d5c89b0e..00000000
--- a/apps/web/src/components/engine/api/utils.ts
+++ /dev/null
@@ -1,467 +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 { entityExists, hasComponent, query, Not, Or } from "bitecs";
-import { ChildOf } from "../components/relations";
-import { sortByFrame, sortByItemIndex } from "../utils/sort";
-import { DEFAULT_DURATION_FRAMES } from "../constants";
-import { findAssetDuration } from "../utils/time";
-import { getParentEntity } from "./base";
-import { isGroupLike, isScene } from "./query";
-import { removeComponent, setComponent, addComponent, clearComponent } from "./events";
-
-import type { EngineWorld } from "./world";
-
-export function rebuildCaches(world: EngineWorld, eid: number, pid: number | null) {
- const c = world.components;
- if (pid === null) return;
-
- if (hasComponent(world, eid, c.Geometry) || hasComponent(world, eid, c.Group) || hasComponent(world, eid, c.AdjustmentLayer)) {
- c.Cache.children[pid] = [...query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), ChildOf(pid), Not(c.IsMask), Not(c.Deleted)])];
- c.Cache.children[pid].sort(sortByItemIndex(world));
- }
-
- if (hasComponent(world, eid, c.IsMask)) {
- c.Cache.masks[pid] = [...query(world, [c.Geometry, c.IsMask, ChildOf(pid), Not(c.Deleted)])];
- c.Cache.masks[pid].sort(sortByItemIndex(world));
- }
-
- if (hasComponent(world, eid, c.KeyframeTrack)) {
- const geomEid = findClosestParentGeometry(world, pid);
- aggregateKeyframeTracks(world, geomEid);
- resetAnimatedValues(world, geomEid);
- }
-
- if (hasComponent(world, eid, c.Keyframe)) {
- c.Cache.keyframes[pid] = [...query(world, [c.Keyframe, ChildOf(pid), Not(c.Deleted)])];
- c.Cache.keyframes[pid].sort(sortByFrame(world));
- }
-
- if (hasComponent(world, eid, c.Animation)) {
- c.Cache.animations[pid] = [...query(world, [c.Animation, ChildOf(pid), Not(c.Deleted)])];
- c.Cache.animations[pid].sort(sortByItemIndex(world));
- resetAnimatedValues(world, pid);
- }
-
- if (hasComponent(world, eid, c.Paint)) {
- c.Cache.fills[pid] = [...query(world, [c.Paint, ChildOf(pid), Not(c.Deleted)])];
- c.Cache.fills[pid].sort(sortByItemIndex(world));
- }
-
- if (hasComponent(world, eid, c.Stroke)) {
- c.Cache.strokes[pid] = [...query(world, [c.Stroke, ChildOf(pid), Not(c.Deleted)])];
- c.Cache.strokes[pid].sort(sortByItemIndex(world));
- }
-
- if (hasComponent(world, eid, c.Effect)) {
- c.Cache.effects[pid] = [...query(world, [c.Effect, ChildOf(pid), Not(c.Shadow), Not(c.Deleted)])];
- c.Cache.effects[pid].sort(sortByItemIndex(world));
- }
-
- if (hasComponent(world, eid, c.Shadow)) {
- c.Cache.shadows[pid] = [...query(world, [c.Shadow, ChildOf(pid), Not(c.Effect), Not(c.Deleted)])];
- c.Cache.shadows[pid].sort(sortByItemIndex(world));
- }
-
- if (hasComponent(world, eid, c.TextRange)) {
- c.Cache.textRanges[pid] = [...query(world, [c.TextRange, ChildOf(pid), Not(c.Deleted)])];
- c.Cache.textRanges[pid].sort(sortByItemIndex(world));
- }
-}
-
-/**
- * Placeholder: projects are held purely in memory. Hook project (JSX package)
- * persistence in here once it lands.
- */
-export function persistEntity(world: EngineWorld, eid: number) {
- const c = world.components;
-
- if (world.mode !== 'realtime' || !entityExists(world, eid) || hasComponent(world, eid, c.Deleted)) return;
-}
-
-/**
- * Placeholder counterpart to `persistEntity`.
- */
-export function unpersistEntity(world: EngineWorld, eid: number) {
- const c = world.components;
-
- if (world.mode !== 'realtime' || !entityExists(world, eid) || !hasComponent(world, eid, c.Deleted)) return;
-
- removeComponent(world, eid, c.Selected, false);
-}
-
-export function findClosestParentGeometry(world: EngineWorld, eid: number) {
- const c = world.components;
- let current: number | null = eid;
- while (current) {
- if (hasComponent(world, current, c.Geometry) || hasComponent(world, current, c.Group) || hasComponent(world, current, c.AdjustmentLayer)) {
- return current;
- }
- current = getParentEntity(world, current);
- }
- return null;
-}
-
-/**
- * Collect every KeyframeTrack under a geometry, stopping at nested geometries
- * (each enclosing geometry owns its own cache entry). Refreshes the denormalised
- * `target` field on each track so consumers iterating the cache can look up the
- * animated entity without walking ChildOf.
- */
-export function aggregateKeyframeTracks(world: EngineWorld, nodeEid: number | null): void {
- if (nodeEid === null) return;
-
- const c = world.components;
- const tracks: number[] = [];
-
- const walk = (eid: number) => {
- for (const cid of query(world, [ChildOf(eid), Not(c.Deleted)])) {
- if (hasComponent(world, cid, c.KeyframeTrack)) {
- c.KeyframeTrack.target[cid] = getParentEntity(world, cid) ?? 0;
- tracks.push(cid);
- continue;
- }
- // Stop at nested nodes — each geometry, group, or adjustment layer owns
- // its own cache entry.
- if (hasComponent(world, cid, c.Geometry) || hasComponent(world, cid, c.Group) || hasComponent(world, cid, c.AdjustmentLayer)) continue;
- walk(cid);
- }
- };
-
- walk(nodeEid);
- c.Cache.keyframeTracks[nodeEid] = tracks;
-}
-
-export function disposeDecoders(world: EngineWorld, eid: number): void {
- const ImageDecoder = world.components.ImageDecoder;
- const VideoDecoder = world.components.VideoDecoder;
- const SequenceDecoder = world.components.SequenceDecoder;
- const AudioDecoder = world.components.AudioDecoder;
-
- ImageDecoder[eid]?.dispose();
- ImageDecoder[eid] = null;
- VideoDecoder[eid]?.dispose();
- VideoDecoder[eid] = null;
- SequenceDecoder[eid]?.dispose();
- SequenceDecoder[eid] = null;
- AudioDecoder[eid]?.reset();
- AudioDecoder[eid] = null;
-
- for (const child of query(world, [ChildOf(eid)])) {
- disposeDecoders(world, child);
- }
-}
-
-export function disposeHtmlHosts(world: EngineWorld, eid: number): void {
- const HtmlHost = world.components.HtmlHost;
-
- HtmlHost[eid]?.dispose();
- HtmlHost[eid] = null;
-
- for (const child of query(world, [ChildOf(eid)])) {
- disposeHtmlHosts(world, child);
- }
-}
-
-export function disposeSurfaceHosts(world: EngineWorld, eid: number): void {
- const SurfaceHost = world.components.SurfaceHost;
-
- SurfaceHost[eid]?.dispose();
- SurfaceHost[eid] = null;
-
- for (const child of query(world, [ChildOf(eid)])) {
- disposeSurfaceHosts(world, child);
- }
-}
-
-export function disposeShaderHosts(world: EngineWorld, eid: number): void {
- const ShaderHost = world.components.ShaderHost;
-
- ShaderHost[eid]?.dispose();
- ShaderHost[eid] = null;
-
- for (const child of query(world, [ChildOf(eid)])) {
- disposeShaderHosts(world, child);
- }
-}
-
-export function disconnectAudioBus(world: EngineWorld, eid: number): void {
- const AudioBus = world.components.AudioBus;
- AudioBus[eid]?.disconnect();
- AudioBus[eid] = null;
-
- for (const child of query(world, [ChildOf(eid)])) {
- disconnectAudioBus(world, child);
- }
-}
-
-/**
- * Time utilty functions.
- */
-
-/**
- * Recompute trim when asset id of an entity changes.
- */
-export function reactToAssetChange(world: EngineWorld, eid: number) {
- const c = world.components;
-
- if (hasComponent(world, eid, c.Paint)) {
- reactToPaintChange(world, eid);
- } else if (hasComponent(world, eid, c.Audio)) {
- reactToGeometryDurationChange(world, eid);
- } else if (hasComponent(world, eid, c.Caption)) {
- // Re-pointed transcript: drop the decoder so it re-resolves with the new asset.
- c.CaptionDecoder[eid]?.dispose();
- c.CaptionDecoder[eid] = null;
- }
-}
-
-/**
- * Re-clamp the parent geometry's trim and bubble the new bounds up.
- */
-export function reactToPaintChange(world: EngineWorld, paintEid: number) {
- const c = world.components;
- const eid = getParentEntity(world, paintEid);
- if (eid === null || !hasComponent(world, eid, c.Geometry)) return;
-
- clampTrimToAssetDuration(world, eid);
- recomputeEntityTimeRange(world, eid);
- bubbleTimeRangeUp(world, eid);
-}
-
-/**
- * Recompute a geometry's bounds and bubble. Used when something other than
- * its own Trim/Delay shifts its duration (e.g. an attached asset id on an
- * Audio entity).
- */
-export function reactToGeometryDurationChange(world: EngineWorld, eid: number) {
- const c = world.components;
- if (!hasComponent(world, eid, c.Geometry)) return;
-
- clampTrimToAssetDuration(world, eid);
- recomputeEntityTimeRange(world, eid);
- bubbleTimeRangeUp(world, eid);
-}
-
-/**
- * Deleted onAdd hook (covers the user-facing "remove" path). Mirror of
- * reactToChildAttached: if the dying entity is a geometry the parent group's
- * bounds may shrink; if it's a paint the parent geometry may lose its asset
- * source and needs a Trim pinned at its current duration.
- */
-export function reactToChildDetached(world: EngineWorld, childEid: number) {
- const c = world.components;
-
- if (hasComponent(world, childEid, c.Geometry) || hasComponent(world, childEid, c.Group)) {
- bubbleTimeRangeUp(world, childEid);
- return;
- }
-
- if (hasComponent(world, childEid, c.Paint)) {
- const parentEid = getParentEntity(world, childEid);
- if (parentEid === null || !hasComponent(world, parentEid, c.Geometry)) return;
- // Pin the current duration before recomputing — otherwise the geometry
- // would silently fall back to the 16s default after losing its paint.
- pinTrimToCurrentDuration(world, parentEid);
- }
-}
-
-export function recomputeEntityTimeRange(world: EngineWorld, eid: number): void {
- const c = world.components;
-
- const parentEid = getParentEntity(world, eid);
-
- const parentDelay = parentEid !== null ? (c.Computed.delay[parentEid] ?? 0) : 0;
- const ownDelay = c.Delay[eid] ?? 0;
- const accumulatedDelay = parentDelay + ownDelay;
-
- c.Computed.delay[eid] = accumulatedDelay;
-
- const playbackRate = c.PlaybackRate[eid] ?? 1;
-
- let start: number;
- let end: number;
-
- if (hasComponent(world, eid, c.Trim)) {
- const trimStart = c.Trim.start[eid] ?? 0;
- const trimEnd = c.Trim.end[eid] ?? 0;
- start = Math.round(accumulatedDelay + trimStart / playbackRate);
- end = Math.round(accumulatedDelay + trimEnd / playbackRate);
- } else if (isGroupLike(world, eid)) {
- const children = c.Cache.children[eid] ?? [];
- if (children.length === 0) {
- start = accumulatedDelay;
- end = accumulatedDelay + DEFAULT_DURATION_FRAMES;
- } else {
- let minStart = Infinity;
- let maxEnd = -Infinity;
- for (const child of children) {
- const cs = c.Computed.start[child] ?? accumulatedDelay;
- const ce = c.Computed.end[child] ?? accumulatedDelay;
- if (cs < minStart) minStart = cs;
- if (ce > maxEnd) maxEnd = ce;
- }
- start = minStart;
- end = maxEnd;
- }
- } else {
- const assetDuration = findAssetDuration(world, eid);
- const sourceDuration = assetDuration ?? DEFAULT_DURATION_FRAMES;
- start = accumulatedDelay;
- end = Math.round(accumulatedDelay + sourceDuration / playbackRate);
- }
-
- c.Computed.start[eid] = start;
- c.Computed.end[eid] = end;
- c.Computed.duration[eid] = end - start;
-}
-
-/**
- * Recompute eid and every descendant (geometry children). Use when something
- * that affects accumulated delay or playback rate changes — every child below
- * needs its delay refreshed.
- */
-export function propagateTimeRangeDown(world: EngineWorld, eid: number): void {
- const c = world.components;
- recomputeEntityTimeRange(world, eid);
- for (const child of c.Cache.children[eid] ?? []) {
- propagateTimeRangeDown(world, child);
- }
- for (const mask of c.Cache.masks[eid] ?? []) {
- propagateTimeRangeDown(world, mask);
- }
- recomputeEntityTimeRange(world, eid);
-}
-
-/**
- * Walk upward recomputing any ancestor whose bounds depend on its children
- * (group-like without an explicit Trim). Call with the PARENT of the entity
- * that changed.
- */
-export function bubbleTimeRangeUp(world: EngineWorld, childEid: number): void {
- const eid = getParentEntity(world, childEid);
- if (eid === null) return;
-
- const c = world.components;
-
- const dependsOnChildren = isGroupLike(world, eid) && !hasComponent(world, eid, c.Trim);
- if (!dependsOnChildren) return;
-
- recomputeEntityTimeRange(world, eid);
- bubbleTimeRangeUp(world, eid);
-}
-
-/**
- * Clamp a Trim's end so it never exceeds the entity's available asset
- * duration. Mutates the Trim component directly — caller is responsible for
- * triggering downstream recomputes.
- */
-export function clampTrimToAssetDuration(world: EngineWorld, eid: number): void {
- const c = world.components;
- if (!hasComponent(world, eid, c.Trim)) return;
-
- const assetDuration = findAssetDuration(world, eid);
- if (!assetDuration) return;
-
- c.Trim.end[eid] = Math.min(c.Trim.end[eid] ?? 0, assetDuration);
-}
-
-/**
- * "Paint is removed" rule: if the geometry has no Trim, pin its current
- * computed duration as a fresh Trim so the entity does not silently fall
- * back to the 16s default after losing its asset source.
- */
-export function pinTrimToCurrentDuration(world: EngineWorld, eid: number): void {
- const c = world.components;
- if (hasComponent(world, eid, c.Trim)) return;
-
- const playbackRate = c.PlaybackRate[eid] ?? 1;
- const globalDuration = c.Computed.duration[eid] ?? 0;
- const localDuration = Math.round(globalDuration * playbackRate);
-
- setComponent(world, eid, c.Trim, {
- start: 0,
- end: localDuration,
- });
-}
-
-export function reactToChildAttached(world: EngineWorld, childEid: number) {
- const c = world.components;
-
- if (hasComponent(world, childEid, c.Geometry) || hasComponent(world, childEid, c.Group)) {
- propagateTimeRangeDown(world, childEid);
- bubbleTimeRangeUp(world, childEid);
- return;
- }
-
- if (hasComponent(world, childEid, c.Paint)) {
- reactToPaintChange(world, childEid);
- }
-}
-
-export function syncInteractiveState(world: EngineWorld) {
- const c = world.components;
-
- clearComponent(world, c.Interactive, false);
-
- const walk = (eid: number, isRoot: boolean) => {
- const children = query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), ChildOf(eid), Not(c.Deleted)]);
- const isSequential = hasComponent(world, eid, c.Sequential);
- // Only a root (top-level) scene passes through to its children. A nested
- // scene is a clickable unit like a group: it becomes Interactive itself
- // and does not expose its children until it is entered (double-click).
- const isRootSceneWithChildren = isRoot && isScene(world, eid) && children.length > 0;
-
- if (isSequential || isRootSceneWithChildren) {
- for (const child of children) {
- walk(child, false);
- }
- return;
- }
-
- addComponent(world, eid, c.Interactive, false);
- }
-
- for (const eid of query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), Not(ChildOf('*')), Not(c.Deleted)])) {
- walk(eid, true);
- }
-}
-
-export function resetAnimatedValues(world: EngineWorld, eid: number | null) {
- if (eid === null) return;
-
- const c = world.components;
-
- c.Computed.positionX[eid] = c.Position.x[eid] ?? 0;
- c.Computed.positionY[eid] = c.Position.y[eid] ?? 0;
- c.Computed.offsetX[eid] = c.Offset.x[eid] ?? 0;
- c.Computed.offsetY[eid] = c.Offset.y[eid] ?? 0;
- c.Computed.rotation[eid] = c.Rotation[eid] ?? 0;
- c.Computed.skewX[eid] = c.Skew.x[eid] ?? 0;
- c.Computed.skewY[eid] = c.Skew.y[eid] ?? 0;
- c.Computed.opacity[eid] = c.Appearance.opacity[eid] ?? 1;
- c.Computed.color[eid] = c.Color[eid] ?? 0;
- c.Computed.blur[eid] = c.Blur[eid] ?? 0;
- c.Computed.volume[eid] = c.Volume[eid] ?? 0;
- c.Computed.strokeWidth[eid] = c.StrokeStyle.width[eid] ?? 1;
- c.Computed.cornerRadius[eid] = c.CornerRadius[eid] ?? 0;
- c.Computed.cornerRadiusTopLeft[eid] = c.MixedCornerRadius.topLeft[eid] ?? 0;
- c.Computed.cornerRadiusTopRight[eid] = c.MixedCornerRadius.topRight[eid] ?? 0;
- c.Computed.cornerRadiusBottomRight[eid] = c.MixedCornerRadius.bottomRight[eid] ?? 0;
- c.Computed.cornerRadiusBottomLeft[eid] = c.MixedCornerRadius.bottomLeft[eid] ?? 0;
- c.Computed.stopOffset[eid] = c.ColorStop.offset[eid] ?? 0;
- c.Computed.stopColor[eid] = c.ColorStop.color[eid] ?? 0;
- c.Computed.stopOpacity[eid] = c.ColorStop.opacity[eid] ?? 1;
- c.Computed.chars[eid] = c.Chars[eid] ?? '';
-
- if (hasComponent(world, eid, c.UniformScale)) {
- c.Computed.scaleX[eid] = c.UniformScale[eid] ?? 1;
- c.Computed.scaleY[eid] = c.UniformScale[eid] ?? 1;
- } else {
- c.Computed.scaleX[eid] = c.Scale.x[eid] ?? 1;
- c.Computed.scaleY[eid] = c.Scale.y[eid] ?? 1;
- }
-}
-
-export { propagateSize, resolveConstraintOffsets } from "./resize";
diff --git a/apps/web/src/components/engine/api/world.ts b/apps/web/src/components/engine/api/world.ts
deleted file mode 100644
index dab0354d..00000000
--- a/apps/web/src/components/engine/api/world.ts
+++ /dev/null
@@ -1,87 +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 { createWorld } from "bitecs";
-import { createSignal } from "solid-js";
-
-import { observeWorld } from "./observers";
-import { createComponents } from "./components";
-import { createHistory } from "../utils/history";
-import { createLiveMounts } from "../utils/live-mounts";
-import { initAssets } from "./assets";
-import { initFolders } from "./folders";
-import { buildTimelineLayers, type TimelineIndexValue } from "./timeline-index";
-import { updateCursor } from "../utils";
-
-import type { FontSource } from "../font";
-import type { Asset, Folder } from "../db";
-import type { RuntimeMode, HitRegion, Camera2D, PointerState, SnapLine, HudMode } from "../types";
-
-export const DEFAULT_BACKGROUND = 0x161616;
-
-export function createEngineWorld(projectId: string, audioContext: AudioContext | OfflineAudioContext) {
- const [timelineIndex, setTimelineIndex] = createSignal<TimelineIndexValue>({ root: null, layers: [] });
-
- const world = createWorld({
- projectId,
- components: createComponents(),
- history: createHistory(),
- liveMounts: createLiveMounts(),
- selection: {
- scene: null as number | null,
- tool: 0,
- },
- mode: 'realtime' as RuntimeMode,
- camera: { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 } as Camera2D,
- background: DEFAULT_BACKGROUND,
- canvas: null as HTMLCanvasElement | OffscreenCanvas | null,
- resolution: window.devicePixelRatio,
- ctx: null as CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D | null,
- keys: new Set<string>(),
- hitRegions: [] as HitRegion[],
- snapLines: [] as SnapLine[],
- hudMode: 'idle' as HudMode,
- pointer: {
- state: 'lifted',
- button: 0,
- clientX: 0,
- clientY: 0,
- dragStartX: 0,
- dragStartY: 0,
- } as PointerState,
- timestamp: {
- now: 0,
- delta: 0,
- },
- frameRate: 30,
- fonts: [] as FontSource[],
- audio: audioContext,
- assets: new Map<string, Asset>(),
- folders: new Map<string, Folder>(),
- promises: null as (Promise<unknown> | null)[] | null,
- timelineIndex,
- rebuildTimelineIndex: () => { },
- cursor: { update: updateCursor }
- });
-
- world.rebuildTimelineIndex = () => {
- const root = world.selection.scene;
-
- if (root == null) {
- setTimelineIndex({ root: null, layers: [] });
- return;
- }
-
- setTimelineIndex({ root, layers: buildTimelineLayers(world, root) });
- };
-
- initAssets(world, projectId);
- initFolders(world, projectId);
-
- observeWorld(world);
-
- return world;
-}
-
-export type EngineWorld = ReturnType<typeof createEngineWorld>;
diff --git a/apps/web/src/components/engine/components/components.ts b/apps/web/src/components/engine/components/components.ts
deleted file mode 100644
index 9c3c77df..00000000
--- a/apps/web/src/components/engine/components/components.ts
+++ /dev/null
@@ -1,463 +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 type { VideoCodec, AudioCodec } from "mediabunny";
-import type { ContainerFormat } from "../encode/types";
-import type { Token } from "../utils/text";
-import type { AudioBus as AudioBusInstance } from "../services/audio-bus";
-import type { HtmlHost as HtmlHostInstance } from "../decoders/html";
-import type { SurfaceHost as SurfaceHostInstance } from "../decoders/surface";
-import type { ShaderHost as ShaderHostInstance } from "../decoders/shader";
-import type { ImageDecoder as ImageDecoderInstance } from "../decoders/image";
-import type { VideoDecoderInstance } from "../decoders/video";
-import type { SequenceDecoder as SequenceDecoderInstance } from "../decoders/sequence";
-import type { AudioDecoder as AudioDecoderInstance } from "../decoders/audio";
-import type { CaptionDecoder as CaptionDecoderInstance } from "../decoders/caption";
-// Geometric primitive — RECT or TEXT (see GeometryType). Other node-like
-// roles (group, audio, scene, caption) are layered on top via tag components.
-export const Geometry = [] as number[];
-
-// Paint applied to a geometry (or stroke). The value is a PaintType enum index.
-export const Paint = [] as number[];
-
-// Tag marking a geometry as a group container. Groups have no Size of
-// their own — their Computed.width/height are derived from the local AABB
-// of their direct children (see transformSystem). They carry a transform
-// state (Position/Rotation/Scale/...) like any other entity.
-export const Group = {};
-
-// Tag marking a geometry as a scene — a clipped, playable frame. Scenes
-// have a fixed Size like any frame; the tag layers playback + clipping on
-// top. Scenes are NOT Groups.
-export const Scene = {};
-
-// Tag marking a geometry as an audio clip (no visual rendering).
-export const Audio = {};
-
-export const AdjustmentLayer = {};
-
-export const ExportSettings = {
- templateId: [] as (string | null)[],
- format: [] as (ContainerFormat | undefined)[],
- videoEnabled: [] as (boolean | undefined)[],
- videoCodec: [] as (VideoCodec | undefined)[],
- videoBitrate: [] as (number | undefined)[],
- videoFps: [] as (number | undefined)[],
- videoResolution: [] as (number | undefined)[],
- audioEnabled: [] as (boolean | undefined)[],
- audioCodec: [] as (AudioCodec | undefined)[],
- audioSampleRate: [] as (number | undefined)[],
-};
-
-export const Effect = {
- type: [] as number[],
- value: [] as number[],
-};
-
-// Tag marking an entity as a mask. Masks are ChildOf their target.
-// MaskCache[targetEid] is derived by childCacheSystem from IsMask + ChildOf queries.
-export const IsMask = {};
-
-// Tag for shadow sub-entities (distinguishes from other Effect sub-entities in ChildOf queries).
-export const Shadow = {};
-
-// Tag for stroke sub-entities (distinguishes from parent Stroke component in ChildOf queries).
-export const Stroke = {};
-
-// Caption preset configuration — only on CAPTION node entities.
-// The transcript is either a standalone TRANSCRIPT asset or embedded in an AUDIO/VIDEO asset, referenced via AssetId.
-export const Caption = {
- type: [] as number[], // CaptionType enum index,
- colors: [] as number[][],
- verticalAlign: [] as (number | undefined)[], // CaptionAlign enum index; unset = the preset's default
-};
-
-export const Name = [] as string[];
-
-// Stable identity for entities.
-export const Key = [] as string[];
-
-// On a mount's root entity: the compiled module (a SCRIPT asset) that a world
-// re-executes to rebuild this mount's reactive graph and runtime hosts.
-export const MountScript = {
- mountId: [] as string[],
- scriptAssetId: [] as string[],
-};
-
-// On every entity a mount materializes: its stable structural key (an index
-// path from the mount root), so a re-run in adopt mode can bind to the existing
-// entity instead of minting a new one.
-export const MountPath = {
- mountId: [] as string[],
- path: [] as string[],
-};
-
-export const Position = {
- x: [] as number[],
- y: [] as number[],
-};
-
-export const Offset = {
- x: [] as number[],
- y: [] as number[],
-};
-
-export const Rotation = [] as number[];
-
-export const Scale = {
- x: [] as number[],
- y: [] as number[],
-};
-
-export const UniformScale = [] as number[];
-
-export const Anchor = {
- x: [] as number[],
- y: [] as number[],
-};
-
-export const Skew = {
- x: [] as number[],
- y: [] as number[],
-};
-
-export const Size = {
- width: [] as number[],
- height: [] as number[],
-};
-
-export const Flip = {
- x: [] as (-1 | 1)[],
- y: [] as (-1 | 1)[],
-};
-
-export const LocalTransform = {
- a: [] as number[],
- b: [] as number[],
- c: [] as number[],
- d: [] as number[],
- e: [] as number[],
- f: [] as number[],
-};
-
-export const WorldTransform = {
- a: [] as number[],
- b: [] as number[],
- c: [] as number[],
- d: [] as number[],
- e: [] as number[],
- f: [] as number[],
-};
-
-export const WorldBounds = {
- minX: [] as number[],
- minY: [] as number[],
- maxX: [] as number[],
- maxY: [] as number[],
-};
-
-export const Culled = {};
-
-export const Hidden = {};
-
-export const ClipsContent = {};
-
-// Tag marking a container whose direct children cannot overlap in time.
-// Drag/trim of any direct child clamps at the neighbour's edge.
-export const Sequential = {};
-
-export const Generating = {};
-
-export const Appearance = {
- opacity: [] as number[],
- blendMode: [] as number[],
-};
-
-export const CornerRadius = [] as number[];
-
-// Per-corner radii (CSS order: TL, TR, BR, BL).
-export const MixedCornerRadius = {
- topLeft: [] as number[],
- topRight: [] as number[],
- bottomRight: [] as number[],
- bottomLeft: [] as number[],
-};
-
-export const Blur = [] as number[];
-
-export const Color = [] as number[];
-
-export const AssetId = [] as string[];
-
-// Scale mode — index into SCALE_MODES. Used by image fills and any future
-// entity with a scaled asset display.
-export const ScaleMode = [] as number[];
-
-// Audio volume in decibels (0 dB = unity, -Infinity = silence).
-export const Volume = [] as number[];
-
-// Audio mute tag — presence means muted.
-export const Muted = {};
-
-// Runtime-only solo tag (not serialized) — presence means soloed.
-export const Soloed = {};
-
-// Text content — stores text-specific data for TextNode entities.
-// Text characters — only on the parent TextNode, not shared with ranges.
-export const Chars = [] as string[];
-
-// Text style properties — shared between TextNode and TextRange sub-entities.
-// On a TextNode this is the default style; on a range sub-entity it overrides.
-export const TextStyle = {
- leading: [] as number[],
- fontSize: [] as number[],
- fontFamily: [] as string[],
- fontWeight: [] as string[],
- fontStyle: [] as number[], // index into FONT_STYLES
- textAlign: [] as number[], // index into TEXT_ALIGNS
- textBaseline: [] as number[], // index into TEXT_BASELINES
- textCase: [] as number[], // -1 = none, index into TEXT_CASES
- letterSpacing: [] as number[], // extra spacing between characters (px)
-};
-
-// Runtime-only: cached text layout data (not serialized).
-export const TextCache = {
- tokens: [] as Token[][][],
-};
-
-export const TextRange = {
- start: [] as number[],
- end: [] as (number | null)[],
-};
-
-// Single gradient stop. Each stop is its own entity, ChildOf the gradient fill.
-// Stop count is fixed for the lifetime of the fill — to animate "between
-// gradients with different stop counts" use a separate fill and cross-fade.
-export const ColorStop = {
- offset: [] as number[],
- color: [] as number[],
- opacity: [] as number[],
-};
-
-export const StrokeStyle = {
- width: [] as number[],
- join: [] as number[], // 0=miter, 1=round, 2=bevel
- cap: [] as number[], // 0=butt, 1=round, 2=square
- miterLimit: [] as number[],
-};
-
-// Playback state of an entity.
-export const Playback = {
- playing: [] as number[], // 0 = paused, 1 = playing
- loop: [] as number[], // 0 = no loop, 1 = loop
- speed: [] as number[], // playback speed multiplier (negative = reverse, default 1)
-};
-
-// Per-clip user-customized row height.
-export const ClipHeight = [] as number[];
-
-// Tag: this clip's keyframe rows are expanded below the clip body.
-export const Expanded = {};
-
-// Offset where the node starts inside its parent's timeline (frames).
-export const Delay = [] as number[];
-
-// Speed multiplier for the node's local time (1 = normal).
-export const PlaybackRate = [] as number[];
-
-// Explicit duration window in the clip's own local time. When present, the
-// node has fixed bounds (workarea on scenes, trimmed source on media clips).
-// When absent: scenes auto-fit to their children, other nodes have no
-// inherent duration (runtime adds Trim immediately on creation).
-export const Trim = {
- start: [] as number[],
- end: [] as number[],
-};
-
-// Explicit playback/export window on a scene
-export const Workarea = {
- start: [] as number[],
- end: [] as number[],
-};
-
-// Constraint anchoring — stores CONSTRAINT_TYPES values (0=MIN, 1=MAX, 2=CENTER).
-export const Constraint = {
- horizontal: [] as number[],
- vertical: [] as number[],
-};
-
-// Aspect ratio lock — stores the target aspect ratio snapshot
-// When the component is set, the current dimensions are captured.
-export const KeepAspectRatio = {
- width: [] as number[],
- height: [] as number[],
-};
-
-// KeyframeTrack entity — one per (target, property) pair. ChildOf its target
-// (geometry, paint, color stop, …) and owns the Keyframe entities as children.
-// `target` is denormalised: ChildOf is authoritative, but the keyed lookup is
-// refreshed in `aggregateKeyframeTracks` so motion/render don't have to walk
-// up the relation on every frame.
-export const KeyframeTrack = {
- property: [] as string[],
- target: [] as number[],
-};
-
-// Keyframe entity component — each keyframe is its own entity, ChildOf its KeyframeTrack.
-// Easing applies to the segment from this keyframe to the next-in-time on the same track.
-export const Keyframe = {
- time: [] as number[],
- value: [] as number[],
- easing: [] as string[],
-};
-
-// Animation component — each animation is its own entity.
-export const Animation = {
- type: [] as number[],
- duration: [] as number[],
- delay: [] as number[],
- phase: [] as number[],
-};
-
-export const ConstraintCache = {
- parentWidth: [] as number[],
- parentHeight: [] as number[],
- childWidth: [] as number[],
- childHeight: [] as number[],
-};
-
-export const Computed = {
- positionX: [] as number[],
- positionY: [] as number[],
- offsetX: [] as number[],
- offsetY: [] as number[],
- // Local-space origin of the entity's bounding rect. Non-zero only for
- // Group containers whose children's AABB extends past (0, 0)
- originX: [] as number[],
- originY: [] as number[],
- rotation: [] as number[],
- scaleX: [] as number[],
- scaleY: [] as number[],
- skewX: [] as number[],
- skewY: [] as number[],
- opacity: [] as number[],
- color: [] as number[],
- blur: [] as number[],
- volume: [] as number[],
- value: [] as number[],
- strokeWidth: [] as number[],
- cornerRadius: [] as number[],
- cornerRadiusTopLeft: [] as number[],
- cornerRadiusTopRight: [] as number[],
- cornerRadiusBottomRight: [] as number[],
- cornerRadiusBottomLeft: [] as number[],
- stopOffset: [] as number[],
- stopColor: [] as number[],
- stopOpacity: [] as number[],
- width: [] as number[],
- height: [] as number[],
- chars: [] as (string | undefined)[],
- localTimeInSeconds: [] as number[], // playhead position in seconds
- localTime: [] as number[], // playhead position in frames (mirrors localTimeInSeconds)
- duration: [] as number[], // duration in frames
- start: [] as number[],
- end: [] as number[],
- delay: [] as number[],
- visibility: [] as number[],
-};
-
-export const ItemIndex = [] as number[];
-
-// Per-entity owned-sub-entity lists. Populated by observers from ChildOf
-// queries; consumed by render, motion, and UI code.
-export const Cache = {
- children: [] as number[][],
- fills: [] as number[][],
- shadows: [] as number[][],
- strokes: [] as number[][],
- effects: [] as number[][],
- textRanges: [] as number[][],
- masks: [] as number[][],
- keyframeTracks: [] as number[][],
- keyframes: [] as number[][],
- animations: [] as number[][],
-};
-
-export const HtmlHost = [] as (HtmlHostInstance | null)[];
-
-export const SurfaceHost = [] as (SurfaceHostInstance | null)[];
-
-export const Shader = {
- code: [] as string[],
- uniforms: [] as (Record<string, number | number[] | string> | null)[],
-};
-
-export const ShaderHost = [] as (ShaderHostInstance | null)[];
-
-// Renderer-runtime state on mounted entities (never serialized)
-export type MountData = {
- props: Record<string, unknown>;
- textBox?: DocumentFragment;
-};
-
-export const Data = [] as (MountData | null)[];
-
-export const ImageDecoder = [] as (ImageDecoderInstance | null)[];
-
-export const VideoDecoder = [] as (VideoDecoderInstance | null)[];
-
-export const SequenceDecoder = [] as (SequenceDecoderInstance | null)[];
-
-export const AudioDecoder = [] as (AudioDecoderInstance | null)[];
-
-export const CaptionDecoder = [] as (CaptionDecoderInstance | null)[];
-
-export const AudioBus = [] as (AudioBusInstance | null)[];
-
-// Transition between adjacent clips in a track.
-// Stored on clip entities — describes the transition INTO this clip from the previous one.
-export const Transition = {
- type: [] as number[], // TransitionType enum index
- duration: [] as number[], // duration in frames
-};
-
-export const AudioPlayback = {
- wasPlaying: [] as number[],
- contextOffsetInSeconds: [] as number[],
- timelineOffsetInSeconds: [] as number[],
-};
-
-export const Selected = {};
-
-export const Deleted = {};
-
-export const Interactive = {};
-
-export const Hovering = {};
-
-export const Dragging = {};
-
-export const Timeline = {
- scrollX: [] as number[],
- scrollY: [] as number[],
- resolution: [] as number[],
- transform: [] as DOMMatrix[],
-};
-
-export const ClipDragOrigin = {
- delay: [] as number[],
- start: [] as number[],
- end: [] as number[],
-};
-
-export const KeyframeDragOrigin = {
- time: [] as number[],
-};
-
-// Snapshot of the clip's start/end frames at the moment a trim interaction
-// began. Tagged on the single clip being trimmed; removed on release.
-export const TrimDragOrigin = {
- start: [] as number[],
- end: [] as number[],
-};
\ No newline at end of file
diff --git a/apps/web/src/components/engine/components/constants.ts b/apps/web/src/components/engine/components/constants.ts
deleted file mode 100644
index 659094b3..00000000
--- a/apps/web/src/components/engine/components/constants.ts
+++ /dev/null
@@ -1,169 +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/. */
-
-export enum BlendMode {
- SOURCE_OVER,
- MULTIPLY,
- SCREEN,
- OVERLAY,
- DARKEN,
- LIGHTEN,
- COLOR_DODGE,
- COLOR_BURN,
- HARD_LIGHT,
- SOFT_LIGHT,
- DIFFERENCE,
- EXCLUSION,
- HUE,
- SATURATION,
- COLOR,
- LUMINOSITY,
-};
-
-export const COMPOSITE_OPERATIONS = Object.keys(BlendMode)
- .filter(key => isNaN(Number(key)))
- .map(key => key.toLowerCase().replace(/_/g, '-')) as GlobalCompositeOperation[];
-
-export enum StrokeJoin {
- MITER,
- ROUND,
- BEVEL,
-}
-
-export enum StrokeCap {
- BUTT,
- ROUND,
- SQUARE,
-}
-
-export enum ScaleMode {
- COVER,
- FILL,
- FIT,
- NONE,
-}
-
-export enum ConstraintType {
- MIN,
- MAX,
- CENTER,
- STRETCH,
- SCALE,
-}
-
-export enum GeometryType {
- RECT,
- TEXT,
-}
-
-export enum PaintType {
- SOLID,
- IMAGE,
- VIDEO,
- LINEAR_GRADIENT,
- RADIAL_GRADIENT,
- SEQUENCE,
- WAVEFORM,
- HTML,
- SURFACE,
- SHADER,
-}
-
-export enum EffectType {
- DROP_SHADOW,
- LAYER_BLUR,
- BRIGHTNESS,
- CONTRAST,
- GRAYSCALE,
- HUE_ROTATION,
- INVERT,
- SATURATE,
- SEPIA,
-}
-
-export enum MotionType {
- ANIMATION,
- KEYFRAME,
-}
-
-export enum AnimationType {
- FADE,
- GAIN,
- GROW,
- SHRINK,
- BLUR,
- SLIDE_LEFT,
- SLIDE_RIGHT,
- SLIDE_UP,
- SLIDE_DOWN,
- SPIN,
- TWIST,
- APPEAR_WORD,
- APPEAR_CHAR,
- SCRAMBLE,
-}
-
-export enum AnimationPhase {
- IN,
- OUT,
-}
-
-export enum TextAlign {
- LEFT,
- CENTER,
- RIGHT,
-}
-
-export enum TextBaseline {
- TOP,
- MIDDLE,
- BOTTOM,
- ALPHABETIC,
-}
-
-export enum TextCase {
- ORIGINAL,
- UPPER,
- LOWER,
-}
-
-export enum FontStyle {
- NORMAL,
- ITALIC,
- OBLIQUE,
-}
-
-export enum TransitionType {
- DISSOLVE,
- SLIDE_FROM_RIGHT,
- SLIDE_FROM_LEFT,
- FADE_TO_BLACK,
- FADE_TO_WHITE,
-}
-
-export enum CaptionType {
- CLASSIC,
- CASCADE,
- SPOTLIGHT,
- WHISPER,
- PAPER,
- GUINEA,
- STARK,
-}
-
-export enum CaptionAlign {
- TOP,
- CENTER,
- BOTTOM,
-}
-
-export enum ToolType {
- MOVE,
- HAND,
- BLADE,
- SCENE,
- RECT,
- TEXT,
- TEXT_EDIT,
-}
diff --git a/apps/web/src/components/engine/components/index.ts b/apps/web/src/components/engine/components/index.ts
deleted file mode 100644
index 11df8dbc..00000000
--- a/apps/web/src/components/engine/components/index.ts
+++ /dev/null
@@ -1,6 +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/. */
-
-export * from './constants';
-export * from './relations';
diff --git a/apps/web/src/components/engine/components/relations.ts b/apps/web/src/components/engine/components/relations.ts
deleted file mode 100644
index 8baf6ec5..00000000
--- a/apps/web/src/components/engine/components/relations.ts
+++ /dev/null
@@ -1,16 +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 { createRelation, withAutoRemoveSubject } from 'bitecs';
-
-/**
- * Universal parent-child relation. Replaces all array-based ownership:
- * Hierarchy.parent/children, Fills, Shadows, Strokes, Effects,
- * TextRanges, Timeline.tracks, Track.clips, Track.animations.
- *
- * Uses withAutoRemoveSubject so removing a parent auto-removes its children.
- * Children are distinguished by their type tags (Fill, Node, Track, etc.).
- * Ordering is encoded via the Index component on each child.
- */
-export const ChildOf = createRelation(withAutoRemoveSubject);
diff --git a/apps/web/src/components/engine/constants.ts b/apps/web/src/components/engine/constants.ts
deleted file mode 100644
index f76594fb..00000000
--- a/apps/web/src/components/engine/constants.ts
+++ /dev/null
@@ -1,7 +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/. */
-
-export const CONONICAL_TIME_BASE = 1e6;
-export const DEFAULT_DURATION_FRAMES = 16 * 30;
-export const SPACE_HOLD_DELAY_MS = 200;
diff --git a/apps/web/src/components/engine/db/index.ts b/apps/web/src/components/engine/db/index.ts
deleted file mode 100644
index 73c36df6..00000000
--- a/apps/web/src/components/engine/db/index.ts
+++ /dev/null
@@ -1,6 +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/. */
-
-export * from './global-db';
-export * from './schemas';
diff --git a/apps/web/src/components/engine/db/schemas.ts b/apps/web/src/components/engine/db/schemas.ts
deleted file mode 100644
index 1631d4ec..00000000
--- a/apps/web/src/components/engine/db/schemas.ts
+++ /dev/null
@@ -1,149 +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 { z } from "zod";
-import { transcriptionOutput } from "@diffusionstudio/api-contract";
-import { ElectronFileHandle } from "@/lib/electron-file-handle";
-
-export const aspectRatioSchema = z.enum(["1:1", "4:3", "3:4", "16:9", "9:16"]);
-export type AspectRatio = z.infer<typeof aspectRatioSchema>;
-
-export const imageGenerationConfigSchema = z.object({
- mode: z.literal("IMAGE"),
- model: z.string(),
- prompt: z.string(),
- aspectRatio: aspectRatioSchema,
- count: z.number().int().min(1).max(4),
- imageRefIds: z.array(z.string()).optional(),
-});
-
-export const videoGenerationConfigSchema = z.object({
- mode: z.literal("VIDEO"),
- model: z.string(),
- prompt: z.string(),
- aspectRatio: aspectRatioSchema,
- duration: z.number().int().min(1),
- generateAudio: z.boolean().optional(),
- startFrameImageId: z.string().optional(),
- endFrameImageId: z.string().optional(),
-});
-
-export const voiceGenerationConfigSchema = z.object({
- mode: z.literal("VOICE"),
- model: z.string(),
- prompt: z.string(),
- voice: z.string(),
-});
-
-export const audioGenerationConfigSchema = z.object({
- mode: z.literal("AUDIO"),
- model: z.string(),
- prompt: z.string(),
-});
-
-export const generationConfigSchema = z.discriminatedUnion("mode", [
- imageGenerationConfigSchema,
- videoGenerationConfigSchema,
- voiceGenerationConfigSchema,
- audioGenerationConfigSchema,
-]);
-
-export type ImageGenerationConfig = z.infer<typeof imageGenerationConfigSchema>;
-export type VideoGenerationConfig = z.infer<typeof videoGenerationConfigSchema>;
-export type VoiceGenerationConfig = z.infer<typeof voiceGenerationConfigSchema>;
-export type AudioGenerationConfig = z.infer<typeof audioGenerationConfigSchema>;
-export type GenerationConfig = z.infer<typeof generationConfigSchema>;
-
-export const folderSchema = z.object({
- id: z.string(),
- name: z.string(),
- parentId: z.string().nullable(),
- createdAt: z.iso.datetime(),
-});
-
-export type Folder = z.infer<typeof folderSchema>;
-
-export const assetBaseSchema = z.object({
- id: z.string(),
- hash: z.string(),
- createdAt: z.iso.datetime(),
- lastModified: z.number().optional(),
- name: z.string(),
- mimeType: z.string(),
- size: z.number().optional(),
- handle: z.union([
- z.instanceof(FileSystemFileHandle),
- z.instanceof(ElectronFileHandle),
- ]),
- generationId: z.string().nullable().optional(),
- generationKey: z.string().nullable().optional(),
- folderId: z.string().nullable().optional(),
-});
-
-export const imageAssetSchema = assetBaseSchema.extend({
- type: z.literal('IMAGE'),
- width: z.number(),
- height: z.number(),
-});
-
-export type ImageAsset = z.infer<typeof imageAssetSchema>;
-
-export const audioAssetSchema = assetBaseSchema.extend({
- type: z.literal('AUDIO'),
- duration: z.number(),
- sampleRate: z.number(),
- channels: z.number(),
- transcript: transcriptionOutput.optional(),
-});
-
-export type AudioAsset = z.infer<typeof audioAssetSchema>;
-
-export const videoAssetSchema = assetBaseSchema.extend({
- type: z.literal('VIDEO'),
- duration: z.number(),
- width: z.number(),
- height: z.number(),
- frameRate: z.number(),
- bitRate: z.number(),
- sampleRate: z.number().optional(),
- channels: z.number().optional(),
- transcript: transcriptionOutput.optional(),
-});
-
-export type VideoAsset = z.infer<typeof videoAssetSchema>;
-
-export const transcriptAssetSchema = assetBaseSchema.extend({
- type: z.literal('TRANSCRIPT'),
-});
-
-export type TranscriptAsset = z.infer<typeof transcriptAssetSchema>;
-
-export const scriptAssetSchema = assetBaseSchema.extend({
- type: z.literal('SCRIPT'),
-});
-
-export type ScriptAsset = z.infer<typeof scriptAssetSchema>;
-
-// `handle` points to the first frame's file (so generic preview/thumbnail
-export const sequenceAssetSchema = assetBaseSchema.extend({
- type: z.literal('SEQUENCE'),
- width: z.number(),
- height: z.number(),
- frameRate: z.number(),
- duration: z.number(),
- directoryHandle: z.instanceof(FileSystemDirectoryHandle),
-});
-
-export type SequenceAsset = z.infer<typeof sequenceAssetSchema>;
-
-export const assetSchema = z.discriminatedUnion('type', [
- imageAssetSchema,
- audioAssetSchema,
- videoAssetSchema,
- transcriptAssetSchema,
- scriptAssetSchema,
- sequenceAssetSchema,
-]);
-
-export type Asset = z.infer<typeof assetSchema>;
diff --git a/apps/web/src/components/engine/decoders/audio-peaks.ts b/apps/web/src/components/engine/decoders/audio-peaks.ts
deleted file mode 100644
index b9d4ef1a..00000000
--- a/apps/web/src/components/engine/decoders/audio-peaks.ts
+++ /dev/null
@@ -1,128 +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 { AudioSampleSink } from 'mediabunny';
-import { getAudioTrack } from './audio';
-
-import type { AudioAsset, VideoAsset } from '../db';
-
-const peakCache = new Map<string, Uint8ClampedArray>();
-const inflight = new Map<string, Promise<Uint8ClampedArray | null>>();
-
-const TARGET_BARS = 128;
-
-export function clearAudioPeaksCache() {
- peakCache.clear();
- inflight.clear();
-}
-
-/**
- * Synchronous, allocation-free peak lookup for hot paths (render loop).
- * Returns cached peaks or `null`. Kicks off a build on first call.
- */
-export function getAudioPeaks(
- asset: AudioAsset | VideoAsset,
-): Uint8ClampedArray | null {
- const key = asset.id;
-
- const cached = peakCache.get(key);
- if (cached) return cached;
-
- if (!inflight.has(key)) {
- inflight.set(key, buildPeaks(key, asset));
- }
-
- return null;
-}
-
-/**
- * Async peak lookup for UI components that can await.
- * Returns peaks from cache or waits for the build to finish.
- */
-export async function getAudioPeaksAsync(
- asset: AudioAsset | VideoAsset,
-): Promise<Uint8ClampedArray | null> {
- const key = asset.id;
-
- const cached = peakCache.get(key);
- if (cached) return cached;
-
- let promise = inflight.get(key);
- if (!promise) {
- promise = buildPeaks(key, asset);
- inflight.set(key, promise);
- }
-
- return promise;
-}
-
-async function buildPeaks(
- key: string,
- asset: AudioAsset | VideoAsset,
-): Promise<Uint8ClampedArray | null> {
- try {
- const track = await getAudioTrack(asset);
- if (!track) return null;
-
- const sink = new AudioSampleSink(track);
- const chunks: Uint8ClampedArray[] = [];
-
- for await (const sample of sink.samples()) {
- let floats: Float32Array;
- let numChannels: number;
- let numFrames: number;
- try {
- const size = sample.allocationSize({ format: 'f32', planeIndex: 0 });
- floats = new Float32Array(size / Float32Array.BYTES_PER_ELEMENT);
- sample.copyTo(floats, { format: 'f32', planeIndex: 0 });
-
- numChannels = sample.numberOfChannels;
- numFrames = floats.length / numChannels;
- } finally {
- sample.close();
- }
-
- const framePeaks = new Uint8ClampedArray(numFrames);
- for (let f = 0; f < numFrames; f++) {
- let max = 0;
- for (let ch = 0; ch < numChannels; ch++) {
- max = Math.max(max, Math.pow(Math.abs(floats[f * numChannels + ch]), 0.8));
- }
- framePeaks[f] = Math.floor(max * 255);
- }
- chunks.push(framePeaks);
- }
-
- if (chunks.length === 0) return null;
-
- const totalFrames = chunks.reduce((s, c) => s + c.length, 0);
- const merged = new Uint8ClampedArray(totalFrames);
- let offset = 0;
- for (const chunk of chunks) {
- merged.set(chunk, offset);
- offset += chunk.length;
- }
-
- const peaks = new Uint8ClampedArray(TARGET_BARS);
- const ratio = totalFrames / TARGET_BARS;
-
- for (let i = 0; i < TARGET_BARS; i++) {
- const start = Math.floor(i * ratio);
- const end = Math.floor((i + 1) * ratio);
- let max = 0;
- for (let j = start; j < end && j < totalFrames; j++) {
- max = Math.max(max, merged[j]);
- }
- peaks[i] = max;
- }
-
- peakCache.set(key, peaks);
- return peaks;
- } catch (e) {
- console.error('Failed to build audio peaks', e);
- return null;
- } finally {
- inflight.delete(key);
- }
-}
diff --git a/apps/web/src/components/engine/decoders/audio.ts b/apps/web/src/components/engine/decoders/audio.ts
deleted file mode 100644
index cfe787c1..00000000
--- a/apps/web/src/components/engine/decoders/audio.ts
+++ /dev/null
@@ -1,278 +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 { Input, ALL_FORMATS, BlobSource, AudioBufferSink, type WrappedAudioBuffer, type InputAudioTrack } from 'mediabunny';
-import { AsyncMutex } from '@/utils';
-import { assert } from '@/utils';
-import { TimeStretcher } from './time-stretcher';
-import { addComponent } from '../api/events';
-import { getAssetFile } from '../api/assets';
-
-import type { EngineWorld } from '../api/world';
-import type { AudioAsset, VideoAsset } from '../db';
-import type { AudioBus } from '../services/audio-bus';
-
-const audioTrackCache = new Map<string, Promise<InputAudioTrack | null>>();
-
-export function clearAudioTrackCache() {
- audioTrackCache.clear();
-}
-
-/**
- * Keyed by asset ID so multiple decoders for the same source
- * audio reuse a single Input + InputAudioTrack.
- */
-export function getAudioTrack(source: AudioAsset | VideoAsset) {
- let promise = audioTrackCache.get(source.id);
- if (promise) return promise;
-
- promise = (async () => {
- try {
- const blob = await getAssetFile(source);
- const input = new Input({
- formats: ALL_FORMATS,
- source: new BlobSource(blob),
- });
- return await input.getPrimaryAudioTrack();
- } catch {
- audioTrackCache.delete(source.id);
- return null;
- }
- })();
-
- audioTrackCache.set(source.id, promise);
- return promise;
-}
-
-type PlayOptions = {
- relativeFrom: number;
- relativeTo: number;
- trimStart: number;
- trimEnd: number;
- playbackRate: number;
- currentTime: number;
- relativeDelay: number;
-}
-
-export class AudioDecoder {
- readonly assetId: string;
-
- private readonly asset: AudioAsset | VideoAsset;
- private readonly mutex = new AsyncMutex();
- private readonly audioNodes = new Set<AudioBufferSourceNode>();
-
- private iterator: AsyncGenerator<WrappedAudioBuffer, void, unknown> | null = null;
- private firstBuffer: WrappedAudioBuffer | null = null;
- private lastBuffer: WrappedAudioBuffer | null = null;
- private stretcher: TimeStretcher | null = null;
- private nextTimestamp = 0;
- private audioTrack: InputAudioTrack | null = null;
- private sink!: AudioBufferSink;
-
- public ready = false;
-
- public constructor(asset: AudioAsset | VideoAsset) {
- this.asset = asset;
- this.assetId = asset.id;
- }
-
- public async init() {
- this.audioTrack = await getAudioTrack(this.asset);
- assert(this.audioTrack, 'Audio track not found');
- this.sink = new AudioBufferSink(this.audioTrack);
- this.ready = true;
- }
-
- private renderData(bus: AudioBus, options: PlayOptions, data: Float32Array[] | null, sampleRate: number) {
- if (!data) {
- return;
- }
-
- const { relativeDelay, playbackRate } = options;
-
- const trimStart = options.trimStart / playbackRate;
- const trimEnd = options.trimEnd / playbackRate;
- const context = bus.context;
- const input = bus.input;
-
- const newBuffer = new AudioBuffer({
- length: data[0].length,
- sampleRate,
- numberOfChannels: data.length,
- });
- for (let c = 0; c < data.length; c++) {
- newBuffer.copyToChannel(data[c] as Float32Array<ArrayBuffer>, c);
- }
-
- const newBufferTimestamp = this.nextTimestamp;
- this.nextTimestamp += newBuffer.duration;
-
- let start = relativeDelay + newBufferTimestamp;
- let offset = 0;
- let duration = newBuffer.duration;
-
- // Check if the buffer goes beyond the clip's end, and if so, truncate it
- if (newBufferTimestamp + duration > trimEnd) {
- duration = trimEnd - newBufferTimestamp;
- }
-
- // Check if the buffer starts before the clip's start, and if so, trim the start
- if (newBufferTimestamp < trimStart) {
- start += trimStart - newBufferTimestamp;
- offset += trimStart - newBufferTimestamp;
- }
-
- // Check if the start is before the context's current time (which is illegal)
- if (start < context.currentTime) {
- offset += context.currentTime - start;
- start = context.currentTime;
- }
-
- // Dumb, DIRTY hack. For some reason (probably due to some bug in the Web Audio API), sometimes a clicking noise
- // can be heard on the boundary between two audio clips. By shifting all audio buffers by half a sample, this is
- // typically avoided. My guess is that it forces the renderer into a sub-sample rendering algorithm which doesn't
- // have the buggy behavior.
- start += 0.5 / context.sampleRate;
-
- duration -= offset;
- duration = Math.max(0, duration);
-
- const bufferSource = context.createBufferSource();
- bufferSource.buffer = newBuffer;
- bufferSource.connect(input);
- bufferSource.start(start, offset, duration);
-
- this.audioNodes.add(bufferSource);
- bufferSource.onended = () => this.audioNodes.delete(bufferSource);
- }
-
- public async playTo(bus: AudioBus | null, options: PlayOptions) {
- if (!this.asset.channels || !this.asset.sampleRate || !bus) {
- // Asset has no audio data
- return;
- }
-
- const { relativeFrom, relativeTo } = options;
-
- const release = await this.mutex.acquire();
-
- const firstTs = this.firstBuffer?.timestamp ?? Number.POSITIVE_INFINITY;
- const lastTs = this.lastBuffer?.timestamp ?? Number.NEGATIVE_INFINITY;
- const isBufferOutOfRange = relativeFrom < firstTs || lastTs < relativeFrom - 1;
-
- try {
- if (this.iterator == null || this.stretcher == null || isBufferOutOfRange) {
- // Close the previous iterator so its pre-decoded AudioSamples get released.
- await this.iterator?.return();
- this.iterator = this.sink.buffers(relativeFrom);
- this.firstBuffer = null;
- this.lastBuffer = null;
- this.stretcher = null;
- }
-
- while (true) {
- const nextBuffer = (await this.iterator.next()).value;
- // Use the actual decoded sample rate if available
- const sampleRate = nextBuffer?.buffer.sampleRate ?? this.asset.sampleRate;
- const numberOfChannels = nextBuffer?.buffer.numberOfChannels ?? this.asset.channels;
- const playbackRate = 1 / options.playbackRate;
-
- this.stretcher ??= new TimeStretcher(numberOfChannels, sampleRate, playbackRate);
-
- if (!nextBuffer) {
- this.renderData(bus, options, this.stretcher.finalize(), sampleRate);
- break;
- }
-
- if (!this.firstBuffer) {
- this.nextTimestamp = nextBuffer.timestamp / options.playbackRate;
- }
-
- if (this.lastBuffer) {
- const sampleGap = Math.round(
- nextBuffer.timestamp - (this.lastBuffer.timestamp + this.lastBuffer.duration)
- );
-
- if (sampleGap > 0) {
- this.renderData(
- bus,
- options,
- Array(numberOfChannels).fill(new Float32Array(sampleGap)),
- sampleRate
- );
- }
- }
-
- this.firstBuffer ??= nextBuffer;
- this.lastBuffer = nextBuffer;
-
- const buffers: Float32Array[] = [];
- for (let c = 0; c < nextBuffer.buffer.numberOfChannels; c++) {
- const channelData = nextBuffer.buffer.getChannelData(c);
- buffers.push(channelData);
- }
-
- // TODO: Handle gap in timestamps case
-
- this.renderData(bus, options, this.stretcher.append(buffers), sampleRate);
-
- if (nextBuffer.timestamp >= relativeTo) {
- break;
- }
- }
- } finally {
- release();
- }
- }
-
- public reset() {
- if (!this.iterator) return;
-
- this.iterator?.return();
- this.iterator = null;
- this.firstBuffer = null;
- this.lastBuffer = null;
-
- for (const node of this.audioNodes) {
- node.stop();
- }
- this.audioNodes.clear();
- }
-}
-
-
-type ResolvedAudioDecoder = {
- decoder: AudioDecoder;
- initPromise: Promise<void> | null;
-};
-
-export function resolveAudioDecoder(world: EngineWorld, eid: number): ResolvedAudioDecoder | null {
- const AssetId = world.components.AssetId;
- const assetId = AssetId[eid];
- if (!assetId) return null;
-
- const existing = world.components.AudioDecoder[eid];
- if (existing && existing.assetId === assetId) {
- return {
- decoder: existing,
- initPromise: null,
- };
- };
-
- // Asset changed — reset old decoder and create a new one.
- if (existing) existing.reset();
-
- const asset = world.assets.get(assetId);
- if (!asset || (asset.type !== 'AUDIO' && asset.type !== 'VIDEO')) return null;
- if (asset.type === 'VIDEO' && !asset.channels) return null;
-
- addComponent(world, eid, world.components.AudioDecoder, false);
-
- const decoder = new AudioDecoder(asset);
- world.components.AudioDecoder[eid] = decoder;
- return {
- decoder,
- initPromise: decoder.init(),
- };
-}
diff --git a/apps/web/src/components/engine/decoders/caption/cascade.ts b/apps/web/src/components/engine/decoders/caption/cascade.ts
deleted file mode 100644
index d4d86122..00000000
--- a/apps/web/src/components/engine/decoders/caption/cascade.ts
+++ /dev/null
@@ -1,99 +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 { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../components';
-import { renderText } from '../../utils/text';
-import { loadWebFont } from '../../font/utils';
-import { groupBy, findActiveGroup, resolveTranscript } from './utils';
-import { placeCaption } from './position';
-import { createEntity } from '../../api';
-import { appendChild } from '../../api/hierarchy';
-import { setComponent } from '../../api/events';
-
-import type { EngineWorld } from '../../api/world';
-import type { Asset } from '../../db';
-import type { CaptionDecoder } from './types';
-
-const WIDTH = 800;
-const HEIGHT = 200;
-
-export class CascadeCaptionDecoder implements CaptionDecoder {
- public readonly type = CaptionType.CASCADE;
- public groups: ReturnType<typeof groupBy> = [];
- public ready = false;
-
- private readonly asset: Asset;
- private currentText = '';
-
- constructor(asset: Asset) {
- this.asset = asset;
- this.init();
- }
-
- private async init() {
- if (this.ready) return;
- const transcript = await resolveTranscript(this.asset);
- this.groups = groupBy(transcript, { length: 50 });
- this.ready = true;
- }
-
- public reposition(world: EngineWorld, eid: number): boolean {
- return placeCaption(world, eid, { width: WIDTH, height: HEIGHT, x: 100, defaultAlign: CaptionAlign.BOTTOM });
- }
-
- public applyStyles(world: EngineWorld, eid: number): void {
- const c = world.components;
-
- if (!this.reposition(world, eid)) return;
- setComponent(world, eid, c.TextStyle, {
- fontFamily: 'Inter',
- fontWeight: '300',
- fontSize: 50,
- textAlign: TextAlign.LEFT,
- textBaseline: TextBaseline.TOP,
- fontStyle: FontStyle.NORMAL,
- textCase: TextCase.ORIGINAL,
- leading: 1.2,
- });
-
- const fillEid = createEntity(world);
- setComponent(world, fillEid, c.Paint, PaintType.SOLID);
- setComponent(world, fillEid, c.Color, 0xFFFFFF);
- appendChild(world, fillEid, eid);
-
- loadWebFont(world, 'Inter');
- }
-
- public seekTo(world: EngineWorld, eid: number, relativeTime: number): void {
- const groupIndex = findActiveGroup(this.groups, relativeTime);
-
- if (groupIndex === -1) {
- world.components.Chars[eid] = '';
- this.currentText = '';
- return;
- }
-
- const group = this.groups[groupIndex];
-
- // Progressive reveal: only show words that have started
- const text = group
- .filter(word => word.start <= relativeTime)
- .map(w => w.text)
- .join(' ');
-
- if (text !== this.currentText) {
- this.currentText = text;
- world.components.Chars[eid] = text;
- }
- }
-
- public draw(world: EngineWorld, eid: number): void {
- renderText(world, eid);
- }
-
- public dispose(): void {
- this.groups = [];
- this.currentText = '';
- }
-}
diff --git a/apps/web/src/components/engine/decoders/caption/classic.ts b/apps/web/src/components/engine/decoders/caption/classic.ts
deleted file mode 100644
index 8cdc833e..00000000
--- a/apps/web/src/components/engine/decoders/caption/classic.ts
+++ /dev/null
@@ -1,106 +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 { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../components';
-import { renderText } from '../../utils/text';
-import { loadWebFont } from '../../font/utils';
-import { groupBy, findActiveGroup, resolveTranscript } from './utils';
-import { placeCaption } from './position';
-import { createEntity } from '../../api';
-import { appendChild } from '../../api/hierarchy';
-import { addComponent, setComponent } from '../../api/events';
-
-import type { EngineWorld } from '../../api/world';
-import type { Asset } from '../../db';
-import type { CaptionDecoder } from './types';
-
-export const CLASSIC_PRESET_WIDTH = 600;
-export const CLASSIC_PRESET_HEIGHT = 100;
-
-export class ClassicCaptionDecoder implements CaptionDecoder {
- public readonly type = CaptionType.CLASSIC;
- public groups: ReturnType<typeof groupBy> = [];
- public ready = false;
-
- private readonly asset: Asset;
- private currentGroupIndex = -1;
-
- constructor(asset: Asset) {
- this.asset = asset;
- this.init();
- }
-
- private async init() {
- if (this.ready) return;
- const transcript = await resolveTranscript(this.asset);
- this.groups = groupBy(transcript, { duration: 0.2 });
- this.ready = true;
- }
-
- public reposition(world: EngineWorld, eid: number): boolean {
- return placeCaption(world, eid, {
- width: CLASSIC_PRESET_WIDTH,
- height: CLASSIC_PRESET_HEIGHT,
- defaultAlign: CaptionAlign.CENTER,
- });
- }
-
- public applyStyles(world: EngineWorld, eid: number): void {
- const c = world.components;
-
- if (!this.reposition(world, eid)) return;
-
- setComponent(world, eid, c.TextStyle, {
- fontFamily: 'Urbanist',
- fontWeight: '600',
- fontSize: 62,
- textAlign: TextAlign.CENTER,
- textBaseline: TextBaseline.MIDDLE,
- textCase: TextCase.LOWER,
- fontStyle: FontStyle.NORMAL,
- leading: 1,
- });
-
- const fillEid = createEntity(world);
- setComponent(world, fillEid, c.Paint, PaintType.SOLID);
- setComponent(world, fillEid, c.Color, 0xFFFFFF);
- appendChild(world, fillEid, eid);
-
- const shadowEid = createEntity(world);
- addComponent(world, shadowEid, c.Shadow);
- setComponent(world, shadowEid, c.Color, 0x000000);
- setComponent(world, shadowEid, c.Appearance, { opacity: 1 });
- setComponent(world, shadowEid, c.Blur, 28);
- setComponent(world, shadowEid, c.Offset, { x: 0, y: 5 });
- appendChild(world, shadowEid, eid);
-
- loadWebFont(world, 'Urbanist');
- }
-
- public seekTo(world: EngineWorld, eid: number, relativeTime: number): void {
- const groupIndex = findActiveGroup(this.groups, relativeTime);
-
- if (groupIndex === -1) {
- world.components.Chars[eid] = '';
- this.currentGroupIndex = -1;
- return;
- }
-
- const group = this.groups[groupIndex];
-
- if (groupIndex !== this.currentGroupIndex) {
- this.currentGroupIndex = groupIndex;
- world.components.Chars[eid] = group.map(w => w.text).join(' ');
- }
- }
-
- public draw(world: EngineWorld, eid: number): void {
- renderText(world, eid);
- }
-
- public dispose(): void {
- this.groups = [];
- this.currentGroupIndex = -1;
- }
-}
diff --git a/apps/web/src/components/engine/decoders/caption/guinea.ts b/apps/web/src/components/engine/decoders/caption/guinea.ts
deleted file mode 100644
index 27fe682d..00000000
--- a/apps/web/src/components/engine/decoders/caption/guinea.ts
+++ /dev/null
@@ -1,148 +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 { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../components';
-import { renderText } from '../../utils/text';
-import { loadWebFont } from '../../font/utils';
-import { groupBy, findActiveGroup, splitSequence, clearTextRanges, resolveTranscript } from './utils';
-import { placeCaption } from './position';
-import { createEntity } from '../../api';
-import { appendChild } from '../../api/hierarchy';
-import { setComponent } from '../../api/events';
-
-import type { EngineWorld } from '../../api/world';
-import type { Asset } from '../../db';
-import type { CaptionDecoder } from './types';
-
-const WIDTH = 700;
-const HEIGHT = 200;
-
-const HIGHLIGHT_COLOR_0 = 0xF55353;
-const HIGHLIGHT_COLOR_1 = 0xFEB139;
-const HIGHLIGHT_COLOR_2 = 0xF6F54D;
-
-const PSEUDO_RANDOM_SEQUENCE = [0, 1, 0, 2, 1, 1, 0, 0, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1];
-
-export class GuineaCaptionDecoder implements CaptionDecoder {
- public readonly type = CaptionType.GUINEA;
- public groups: ReturnType<typeof groupBy> = [];
- public ready = false;
-
- private readonly asset: Asset;
- private currentGroupIndex = -1;
- private activeSplit: 'left' | 'right' | null = null;
- private fillEid: number | null = null;
- private colorIndex = 0;
-
- constructor(asset: Asset) {
- this.asset = asset;
- this.init();
- }
-
- private async init() {
- if (this.ready) return;
- const transcript = await resolveTranscript(this.asset);
- this.groups = groupBy(transcript, { length: 18 });
- this.ready = true;
- }
-
- public reposition(world: EngineWorld, eid: number): boolean {
- return placeCaption(world, eid, { width: WIDTH, height: HEIGHT, defaultAlign: CaptionAlign.CENTER });
- }
-
- public applyStyles(world: EngineWorld, eid: number): void {
- const c = world.components;
-
- if (!this.reposition(world, eid)) return;
- setComponent(world, eid, c.TextStyle, {
- fontFamily: 'The Bold Font',
- fontWeight: '500',
- fontSize: 62,
- textAlign: TextAlign.CENTER,
- textBaseline: TextBaseline.MIDDLE,
- textCase: TextCase.UPPER,
- fontStyle: FontStyle.NORMAL,
- leading: 1,
- });
-
- const fillEid = createEntity(world);
- setComponent(world, fillEid, c.Paint, PaintType.SOLID);
- setComponent(world, fillEid, c.Color, 0xFFFFFF);
- appendChild(world, fillEid, eid);
-
- loadWebFont(world, 'The Bold Font');
- }
-
- public seekTo(world: EngineWorld, eid: number, relativeTime: number): void {
- const c = world.components;
- const groupIndex = findActiveGroup(this.groups, relativeTime);
-
- if (groupIndex === -1) {
- setComponent(world, eid, c.Chars, '');
- clearTextRanges(world, eid);
- this.currentGroupIndex = -1;
- this.activeSplit = null;
- return;
- }
-
- const group = this.groups[groupIndex];
- const [left, right] = splitSequence(group);
- const leftText = left.map(w => w.text).join(' ');
- const rightText = right.map(w => w.text).join(' ');
- const text = rightText ? `${leftText}\n${rightText}` : leftText;
- const activeSplit = right.length > 0 && relativeTime >= right[0].start ? 'right' : 'left';
-
- if (groupIndex !== this.currentGroupIndex || activeSplit !== this.activeSplit) {
- this.currentGroupIndex = groupIndex;
- this.activeSplit = activeSplit;
- c.Chars[eid] = text;
-
- // Active line gets a random highlight color and a slightly larger font (1.1x).
- clearTextRanges(world, eid);
- const start = activeSplit === 'left' ? 0 : leftText.length + 1;
- const end = activeSplit === 'left' ? leftText.length : text.length;
- if (end > start) {
- const baseSize = c.TextStyle.fontSize[eid] ?? 62;
- const fontSize = Math.round(baseSize * 1.1);
-
- const rid = createEntity(world);
- setComponent(world, rid, c.TextRange, { start, end });
- setComponent(world, rid, c.TextStyle, { fontSize });
- appendChild(world, rid, eid);
-
- this.fillEid = createEntity(world);
- setComponent(world, this.fillEid, c.Paint, PaintType.SOLID);
- setComponent(world, this.fillEid, c.Color, HIGHLIGHT_COLOR_0);
- setComponent(world, this.fillEid, c.ItemIndex, 0);
- appendChild(world, this.fillEid, rid);
-
- this.colorIndex++;
- }
- }
- }
-
- public draw(world: EngineWorld, eid: number): void {
- const c = world.components;
-
- const colors = [
- c.Caption.colors[eid]?.[0] ?? HIGHLIGHT_COLOR_0,
- c.Caption.colors[eid]?.[1] ?? HIGHLIGHT_COLOR_1,
- c.Caption.colors[eid]?.[2] ?? HIGHLIGHT_COLOR_2,
- ] as const;
-
- const index = PSEUDO_RANDOM_SEQUENCE[this.colorIndex % PSEUDO_RANDOM_SEQUENCE.length];
-
- if (this.fillEid) {
- c.Color[this.fillEid] = colors[index];
- }
-
- renderText(world, eid);
- }
-
- public dispose(): void {
- this.groups = [];
- this.currentGroupIndex = -1;
- this.activeSplit = null;
- }
-}
diff --git a/apps/web/src/components/engine/decoders/caption/index.ts b/apps/web/src/components/engine/decoders/caption/index.ts
deleted file mode 100644
index b2ed5724..00000000
--- a/apps/web/src/components/engine/decoders/caption/index.ts
+++ /dev/null
@@ -1,82 +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 { hasComponent } from 'bitecs';
-import { CaptionType } from '../../components';
-import { addComponent } from '../../api/events';
-import { ClassicCaptionDecoder } from './classic';
-import { CascadeCaptionDecoder } from './cascade';
-import { SpotlightCaptionDecoder } from './spotlight';
-import { WhisperCaptionDecoder } from './whisper';
-import { PaperCaptionDecoder } from './paper';
-import { GuineaCaptionDecoder } from './guinea';
-import { StarkCaptionDecoder } from './stark';
-
-import type { EngineWorld } from '../../api/world';
-import type { Asset } from '../../db';
-import type { CaptionDecoder } from './types';
-
-export type { CaptionDecoder } from './types';
-export { ClassicCaptionDecoder } from './classic';
-export { CascadeCaptionDecoder } from './cascade';
-export { SpotlightCaptionDecoder } from './spotlight';
-export { WhisperCaptionDecoder } from './whisper';
-export { PaperCaptionDecoder } from './paper';
-export { GuineaCaptionDecoder } from './guinea';
-export { StarkCaptionDecoder } from './stark';
-
-function createCaptionDecoder(type: CaptionType, asset: Asset): CaptionDecoder {
- switch (type) {
- case CaptionType.CLASSIC:
- return new ClassicCaptionDecoder(asset);
- case CaptionType.CASCADE:
- return new CascadeCaptionDecoder(asset);
- case CaptionType.SPOTLIGHT:
- return new SpotlightCaptionDecoder(asset);
- case CaptionType.WHISPER:
- return new WhisperCaptionDecoder(asset);
- case CaptionType.PAPER:
- return new PaperCaptionDecoder(asset);
- case CaptionType.GUINEA:
- return new GuineaCaptionDecoder(asset);
- case CaptionType.STARK:
- return new StarkCaptionDecoder(asset);
- default:
- return new ClassicCaptionDecoder(asset);
- }
-}
-
-/**
- * Lazily resolve (or create) a caption decoder for a caption entity.
- * Recreates the decoder when the caption type changes.
- * Returns null if the transcript asset isn't available yet.
- */
-export function resolveCaptionDecoder(world: EngineWorld, eid: number): CaptionDecoder | null {
- const AssetId = world.components.AssetId;
- const assetId = AssetId[eid];
- if (!assetId) return null;
-
- const captionType = world.components.Caption.type[eid];
- const existing = world.components.CaptionDecoder[eid];
-
- if (existing && captionType === existing.type) return existing;
-
- const typeChanged = existing != null;
- existing?.dispose();
-
- const asset = world.assets.get(assetId);
- if (!asset) return null;
-
- addComponent(world, eid, world.components.CaptionDecoder);
-
- const decoder = createCaptionDecoder(captionType, asset);
- world.components.CaptionDecoder[eid] = decoder;
-
-
- if (typeChanged || !hasComponent(world, eid, world.components.TextStyle)) {
- decoder.applyStyles(world, eid);
- }
-
- return decoder;
-}
diff --git a/apps/web/src/components/engine/decoders/caption/paper.ts b/apps/web/src/components/engine/decoders/caption/paper.ts
deleted file mode 100644
index 7d050f92..00000000
--- a/apps/web/src/components/engine/decoders/caption/paper.ts
+++ /dev/null
@@ -1,117 +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 { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../components';
-import { renderText } from '../../utils/text';
-import { loadWebFont } from '../../font/utils';
-import { groupBy, findActiveGroup, splitSequence, clearTextRanges, resolveTranscript } from './utils';
-import { placeCaption } from './position';
-import { createEntity } from '../../api';
-import { appendChild } from '../../api/hierarchy';
-import { setComponent } from '../../api/events';
-
-import type { EngineWorld } from '../../api/world';
-import type { Asset } from '../../db';
-import type { CaptionDecoder } from './types';
-
-const WIDTH = 700;
-const HEIGHT = 200;
-
-export class PaperCaptionDecoder implements CaptionDecoder {
- public readonly type = CaptionType.PAPER;
- public groups: ReturnType<typeof groupBy> = [];
- public ready = false;
-
- private readonly asset: Asset;
- private currentGroupIndex = -1;
- private activeSplit: 'left' | 'right' | null = null;
-
- constructor(asset: Asset) {
- this.asset = asset;
- this.init();
- }
-
- private async init() {
- if (this.ready) return;
- const transcript = await resolveTranscript(this.asset);
- this.groups = groupBy(transcript, { length: 18 });
- this.ready = true;
- }
-
- public reposition(world: EngineWorld, eid: number): boolean {
- return placeCaption(world, eid, { width: WIDTH, height: HEIGHT, defaultAlign: CaptionAlign.CENTER });
- }
-
- public applyStyles(world: EngineWorld, eid: number): void {
- const c = world.components;
-
- if (!this.reposition(world, eid)) return;
-
- setComponent(world, eid, c.TextStyle, {
- fontFamily: 'Montserrat',
- fontWeight: '300',
- fontSize: 50,
- textAlign: TextAlign.CENTER,
- textBaseline: TextBaseline.MIDDLE,
- textCase: TextCase.ORIGINAL,
- leading: 0.9,
- fontStyle: FontStyle.NORMAL,
- });
-
- const fillEid = createEntity(world);
- setComponent(world, fillEid, c.Paint, PaintType.SOLID);
- setComponent(world, fillEid, c.Color, 0xFFFFFF);
- appendChild(world, fillEid, eid);
-
- loadWebFont(world, 'Montserrat');
- }
-
- public seekTo(world: EngineWorld, eid: number, relativeTime: number): void {
- const c = world.components;
- const groupIndex = findActiveGroup(this.groups, relativeTime);
-
- if (groupIndex === -1) {
- c.Chars[eid] = '';
- clearTextRanges(world, eid);
- this.currentGroupIndex = -1;
- this.activeSplit = null;
- return;
- }
-
- const group = this.groups[groupIndex];
- const [left, right] = splitSequence(group);
- const leftText = left.map(w => w.text).join(' ');
- const rightText = right.map(w => w.text).join(' ');
- const text = rightText ? `${leftText}\n${rightText}` : leftText;
- const activeSplit = right.length > 0 && relativeTime >= right[0].start ? 'right' : 'left';
-
- if (groupIndex !== this.currentGroupIndex || activeSplit !== this.activeSplit) {
- this.currentGroupIndex = groupIndex;
- this.activeSplit = activeSplit;
- c.Chars[eid] = text;
-
- // Two-line display with newline separator.
- // The active line gets weight '500', inactive stays at the node's '300'.
- clearTextRanges(world, eid);
- const start = activeSplit === 'left' ? 0 : leftText.length + 1;
- const end = activeSplit === 'left' ? leftText.length : text.length;
- if (end > start) {
- const rid = createEntity(world);
- setComponent(world, rid, c.TextRange, { start, end });
- setComponent(world, rid, c.TextStyle, { fontWeight: '500' });
- appendChild(world, rid, eid);
- }
- }
- }
-
- public draw(world: EngineWorld, eid: number): void {
- renderText(world, eid);
- }
-
- public dispose(): void {
- this.groups = [];
- this.currentGroupIndex = -1;
- this.activeSplit = null;
- }
-}
diff --git a/apps/web/src/components/engine/decoders/caption/position.ts b/apps/web/src/components/engine/decoders/caption/position.ts
deleted file mode 100644
index 40a3214b..00000000
--- a/apps/web/src/components/engine/decoders/caption/position.ts
+++ /dev/null
@@ -1,48 +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 { CaptionAlign } from '../../components';
-import { getParentEntity } from '../../api';
-import { setComponent } from '../../api/events';
-import { resizeEntity } from '../../api/resize';
-
-import type { EngineWorld } from '../../api/world';
-
-// Margin between the caption box and the parent edge for top/bottom alignment.
-export const CAPTION_MARGIN = 100;
-
-/**
- * Sizes the caption box and places it within its parent. Horizontal placement
- * belongs to the preset (centered unless a fixed `x` is given); vertical
- * placement follows the entity's `Caption.verticalAlign`, falling back to the
- * preset's default. Returns false when the entity has no parent to place
- * against.
- */
-export function placeCaption(
- world: EngineWorld,
- eid: number,
- preset: { width: number; height: number; x?: number; defaultAlign: CaptionAlign },
-): boolean {
- const c = world.components;
-
- const parentEid = getParentEntity(world, eid);
- if (parentEid === null) return false;
- const parentWidth = c.Computed.width[parentEid];
- const parentHeight = c.Computed.height[parentEid];
-
- resizeEntity(world, eid, { width: preset.width, height: preset.height });
-
- const align = c.Caption.verticalAlign[eid] ?? preset.defaultAlign;
- const y = align === CaptionAlign.TOP
- ? CAPTION_MARGIN
- : align === CaptionAlign.BOTTOM
- ? parentHeight - preset.height - CAPTION_MARGIN
- : (parentHeight - preset.height) / 2;
-
- setComponent(world, eid, c.Position, {
- x: preset.x ?? (parentWidth - preset.width) / 2,
- y,
- });
- return true;
-}
diff --git a/apps/web/src/components/engine/decoders/caption/spotlight.ts b/apps/web/src/components/engine/decoders/caption/spotlight.ts
deleted file mode 100644
index 5bb5d5d0..00000000
--- a/apps/web/src/components/engine/decoders/caption/spotlight.ts
+++ /dev/null
@@ -1,134 +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 { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../components';
-import { renderText } from '../../utils/text';
-import { loadWebFont } from '../../font/utils';
-import { groupBy, findActiveGroup, clearTextRanges, resolveTranscript } from './utils';
-import { placeCaption } from './position';
-import { createEntity } from '../../api';
-import { appendChild } from '../../api/hierarchy';
-import { setComponent } from '../../api/events';
-
-import type { EngineWorld } from '../../api/world';
-import type { Asset } from '../../db';
-import type { CaptionDecoder } from './types';
-
-
-const WIDTH = 700;
-const HEIGHT = 100;
-const HIGHLIGHT_COLOR = 0x24D5FF;
-
-export class SpotlightCaptionDecoder implements CaptionDecoder {
- public readonly type = CaptionType.SPOTLIGHT;
- public groups: ReturnType<typeof groupBy> = [];
- public ready = false;
-
- private readonly asset: Asset;
- private currentGroupIndex = -1;
- private currentWordIndex = -1;
- private fillEid: number | null = null;
-
- constructor(asset: Asset) {
- this.asset = asset;
- this.init();
- }
-
- private async init() {
- if (this.ready) return;
- const transcript = await resolveTranscript(this.asset);
- this.groups = groupBy(transcript, { length: 10 });
- this.ready = true;
- }
-
- public reposition(world: EngineWorld, eid: number): boolean {
- return placeCaption(world, eid, { width: WIDTH, height: HEIGHT, defaultAlign: CaptionAlign.CENTER });
- }
-
- public applyStyles(world: EngineWorld, eid: number): void {
- const c = world.components;
-
- if (!this.reposition(world, eid)) return;
-
- setComponent(world, eid, c.TextStyle, {
- fontFamily: 'The Bold Font',
- fontWeight: '500',
- fontStyle: FontStyle.ITALIC,
- fontSize: 70,
- textAlign: TextAlign.CENTER,
- textBaseline: TextBaseline.MIDDLE,
- textCase: TextCase.ORIGINAL,
- leading: 1,
- });
-
- const fillEid = createEntity(world);
- setComponent(world, fillEid, c.Paint, PaintType.SOLID);
- setComponent(world, fillEid, c.Color, 0xFFFFFF);
- appendChild(world, fillEid, eid);
-
- loadWebFont(world, 'The Bold Font');
- }
-
- public seekTo(world: EngineWorld, eid: number, relativeTime: number): void {
- const c = world.components;
- const groupIndex = findActiveGroup(this.groups, relativeTime);
-
- if (groupIndex === -1) {
- c.Chars[eid] = '';
- clearTextRanges(world, eid);
- this.fillEid = null;
- this.currentGroupIndex = -1;
- this.currentWordIndex = -1;
- return;
- }
-
- const group = this.groups[groupIndex];
-
- const wordIndex = group.findIndex(word =>
- relativeTime >= word.start && relativeTime <= word.end
- );
-
- const text = group.map(w => w.text).join(' ');
-
- if (groupIndex !== this.currentGroupIndex || wordIndex !== this.currentWordIndex) {
- this.currentGroupIndex = groupIndex;
- this.currentWordIndex = wordIndex;
- c.Chars[eid] = text;
-
- clearTextRanges(world, eid);
- this.fillEid = null;
-
- if (group.length > 1 && wordIndex !== -1) {
- const start = group.slice(0, wordIndex).map(w => w.text).join(' ').length;
- const end = group.slice(0, wordIndex + 1).map(w => w.text).join(' ').length;
- const rid = createEntity(world);
- setComponent(world, rid, c.TextRange, { start, end });
- appendChild(world, rid, eid);
-
- this.fillEid = createEntity(world);
- setComponent(world, this.fillEid, c.Paint, PaintType.SOLID);
- setComponent(world, this.fillEid, c.Color, HIGHLIGHT_COLOR);
- appendChild(world, this.fillEid, rid);
- }
- }
- }
-
- public draw(world: EngineWorld, eid: number): void {
- const c = world.components;
-
- const highlightColor = c.Caption.colors[eid]?.[0] ?? HIGHLIGHT_COLOR;
-
- if (this.fillEid) {
- c.Color[this.fillEid] = highlightColor;
- }
-
- renderText(world, eid);
- }
-
- public dispose(): void {
- this.groups = [];
- this.currentGroupIndex = -1;
- this.currentWordIndex = -1;
- }
-}
diff --git a/apps/web/src/components/engine/decoders/caption/stark.ts b/apps/web/src/components/engine/decoders/caption/stark.ts
deleted file mode 100644
index 43a427cc..00000000
--- a/apps/web/src/components/engine/decoders/caption/stark.ts
+++ /dev/null
@@ -1,93 +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 { BlendMode, CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../components';
-import { renderText } from '../../utils/text';
-import { loadWebFont } from '../../font/utils';
-import { groupBy, findActiveGroup, resolveTranscript } from './utils';
-import { placeCaption } from './position';
-import { createEntity } from '../../api';
-import { appendChild } from '../../api/hierarchy';
-import { setComponent } from '../../api/events';
-
-import type { EngineWorld } from '../../api/world';
-import type { Asset } from '../../db';
-import type { CaptionDecoder } from './types';
-
-const WIDTH = 700;
-const HEIGHT = 100;
-
-export class StarkCaptionDecoder implements CaptionDecoder {
- public readonly type = CaptionType.STARK;
- public groups: ReturnType<typeof groupBy> = [];
- public ready = false;
-
- private readonly asset: Asset;
- private currentGroupIndex = -1;
-
- constructor(asset: Asset) {
- this.asset = asset;
- this.init();
- }
-
- private async init() {
- if (this.ready) return;
- const transcript = await resolveTranscript(this.asset);
- this.groups = groupBy(transcript, { duration: 0.2 });
- this.ready = true;
- }
-
- public reposition(world: EngineWorld, eid: number): boolean {
- return placeCaption(world, eid, { width: WIDTH, height: HEIGHT, defaultAlign: CaptionAlign.CENTER });
- }
-
- public applyStyles(world: EngineWorld, eid: number): void {
- const c = world.components;
-
- if (!this.reposition(world, eid)) return;
-
- setComponent(world, eid, c.TextStyle, {
- fontFamily: 'Figtree',
- fontWeight: '800',
- fontSize: 70,
- textAlign: TextAlign.CENTER,
- textBaseline: TextBaseline.MIDDLE,
- textCase: TextCase.UPPER,
- fontStyle: FontStyle.NORMAL,
- leading: 1,
- });
-
- const fillEid = createEntity(world);
- setComponent(world, fillEid, c.Paint, PaintType.SOLID);
- setComponent(world, fillEid, c.Color, 0xFFFFFF);
- setComponent(world, fillEid, c.Appearance, { blendMode: BlendMode.DIFFERENCE });
- appendChild(world, fillEid, eid);
-
- loadWebFont(world, 'Figtree');
- }
-
- public seekTo(world: EngineWorld, eid: number, relativeTime: number): void {
- const groupIndex = findActiveGroup(this.groups, relativeTime);
-
- if (groupIndex === -1) {
- world.components.Chars[eid] = '';
- this.currentGroupIndex = -1;
- return;
- }
-
- if (groupIndex !== this.currentGroupIndex) {
- this.currentGroupIndex = groupIndex;
- world.components.Chars[eid] = this.groups[groupIndex].map(w => w.text).join(' ');
- }
- }
-
- public draw(world: EngineWorld, eid: number): void {
- renderText(world, eid);
- }
-
- public dispose(): void {
- this.groups = [];
- this.currentGroupIndex = -1;
- }
-}
diff --git a/apps/web/src/components/engine/decoders/caption/subtitles.ts b/apps/web/src/components/engine/decoders/caption/subtitles.ts
deleted file mode 100644
index 7e5e9a8a..00000000
--- a/apps/web/src/components/engine/decoders/caption/subtitles.ts
+++ /dev/null
@@ -1,60 +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 type { Transcript } from '@diffusionstudio/api-contract';
-
-const TIMESTAMP_RE = /(?:\d{1,2}:)?\d{1,2}:\d{2}[.,]\d{1,3}/g;
-
-function parseTimestamp(raw: string): number {
- return raw
- .replace(',', '.')
- .split(':')
- .reduce((acc, part) => acc * 60 + Number(part), 0);
-}
-
-/**
- * Parses SRT or WebVTT text into a `Transcript`. Each cue becomes one segment
- * (a sentence boundary for grouping). Cues carry no word timings, so each
- * word's window is synthesized proportional to its length within the cue.
- */
-export function parseSubtitles(text: string): Transcript {
- const transcript: Transcript = [];
- const blocks = text.replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n').split(/\n{2,}/);
-
- for (const block of blocks) {
- const lines = block.split('\n');
- const timingIndex = lines.findIndex((line) => line.includes('-->'));
- if (timingIndex === -1) continue; // WEBVTT header, NOTE/STYLE/REGION blocks, stray indices
-
- const timestamps = lines[timingIndex].match(TIMESTAMP_RE);
- if (!timestamps || timestamps.length < 2) continue;
- const start = parseTimestamp(timestamps[0]);
- const end = parseTimestamp(timestamps[1]);
- if (!(end > start)) continue;
-
- // Strip inline markup: HTML/VTT tags (<i>, <c.class>, <00:01.000>) and
- // SSA-style overrides ({\an8}).
- const content = lines
- .slice(timingIndex + 1)
- .join(' ')
- .replace(/<[^>]*>/g, '')
- .replace(/\{[^}]*\}/g, '');
-
- const tokens = content.split(/\s+/).filter(Boolean);
- if (tokens.length === 0) continue;
-
- const total = tokens.reduce((acc, token) => acc + token.length, 0);
- const duration = end - start;
- let elapsed = 0;
- const words = tokens.map((token) => {
- const wordStart = start + (elapsed / total) * duration;
- elapsed += token.length;
- return { text: token, start: wordStart, end: start + (elapsed / total) * duration };
- });
-
- transcript.push({ text: tokens.join(' '), words });
- }
-
- return transcript.sort((a, b) => a.words[0].start - b.words[0].start);
-}
diff --git a/apps/web/src/components/engine/decoders/caption/types.ts b/apps/web/src/components/engine/decoders/caption/types.ts
deleted file mode 100644
index 25a3467d..00000000
--- a/apps/web/src/components/engine/decoders/caption/types.ts
+++ /dev/null
@@ -1,18 +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 type { CaptionType } from '../../components';
-import type { EngineWorld } from '../../api/world';
-import type { WordGroup } from '@diffusionstudio/api-contract';
-
-export interface CaptionDecoder {
- readonly type: CaptionType;
- groups: WordGroup[];
- ready: boolean;
- applyStyles(world: EngineWorld, eid: number): void;
- reposition(world: EngineWorld, eid: number): boolean;
- seekTo(world: EngineWorld, eid: number, relativeTime: number): void;
- draw(world: EngineWorld, eid: number): void;
- dispose(): void;
-}
diff --git a/apps/web/src/components/engine/decoders/caption/utils.ts b/apps/web/src/components/engine/decoders/caption/utils.ts
deleted file mode 100644
index bc121a05..00000000
--- a/apps/web/src/components/engine/decoders/caption/utils.ts
+++ /dev/null
@@ -1,125 +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 { query } from 'bitecs';
-import { ChildOf } from '../../components/relations';
-import { deleteEntity } from '../../api/entities';
-import { getAssetFile } from '../../api/assets';
-import { parseSubtitles } from './subtitles';
-
-import type { Transcript, WordGroup } from '@diffusionstudio/api-contract';
-import type { EngineWorld } from '../../api/world';
-import type { Asset } from '../../db';
-
-export type TextRangeOverride = {
- start: number;
- end: number;
-};
-
-
-export type GroupByOptions = {
- duration: number;
-} | {
- length: number;
-};
-
-const ENDING_PUNCTUATION = ['.', '!', '?', ',', ';'];
-
-export function groupBy(transcript: Transcript, options: GroupByOptions): WordGroup[] {
- const groups: WordGroup[] = [[]];
-
- for (const segment of transcript) {
- // Segment boundaries are sentence boundaries — never merge across them.
- if (groups[groups.length - 1].length > 0) {
- groups.push([]);
- }
-
- for (const word of segment.words) {
- const last = groups[groups.length - 1];
- const charCount = last.reduce((acc, w) => acc + w.text.length, 0);
- const duration = last.reduce((acc, w) => acc + (w.end - w.start), 0);
-
- if ('duration' in options && duration + (word.end - word.start) > options.duration) {
- groups.push([]);
- } else if ('length' in options && charCount + word.text.length > options.length) {
- groups.push([]);
- }
-
- groups.at(-1)?.push(word);
-
- if (ENDING_PUNCTUATION.some(p => word.text.endsWith(p))) {
- groups.push([]);
- }
- }
- }
-
- return groups.filter(g => g.length > 0);
-}
-
-/**
- * Find the active group index for a given time.
- * Returns -1 if no group is active.
- */
-export function findActiveGroup(groups: WordGroup[], relativeTime: number): number {
- return groups.findIndex(group =>
- relativeTime >= group[0].start && relativeTime <= (group.at(-1)?.end ?? 0)
- );
-}
-
-export function clearTextRanges(world: EngineWorld, parentEid: number) {
- const c = world.components;
-
- for (const rid of query(world, [ChildOf(parentEid), c.TextRange])) {
- deleteEntity(world, rid);
- }
-
- c.Cache.textRanges[parentEid] = [];
-}
-
-/**
- * Split a word group into two halves at the nearest word boundary
- * to the midpoint of the joined text. Used by paper and guinea presets.
- */
-export function splitSequence(sequence: WordGroup): [WordGroup, WordGroup] {
- const text = sequence.map(w => w.text).join(' ');
- const midPoint = Math.ceil(text.length / 2);
-
- let index = text.length;
- for (let i = midPoint, j = midPoint; i > 0 && j < text.length - 1; i--, j++) {
- if (text[i] === ' ') {
- index = i;
- break;
- }
- if (text[j] === ' ') {
- index = j;
- break;
- }
- }
-
- const leftText = text.slice(0, index).trim();
- const splitAt = leftText.split(/ /).length;
-
- return [sequence.slice(0, splitAt), sequence.slice(splitAt)];
-}
-
-
-/**
- * Resolve a transcript from any asset type that can carry one.
- */
-export async function resolveTranscript(asset: Asset): Promise<Transcript> {
- if (asset.type === 'TRANSCRIPT') {
- const file = await getAssetFile(asset);
- const text = await file.text();
- if (asset.mimeType === 'application/x-subrip' || asset.mimeType === 'text/vtt') {
- return parseSubtitles(text);
- }
- return JSON.parse(text);
- }
-
- if ((asset.type === 'AUDIO' || asset.type === 'VIDEO') && asset.transcript) {
- return asset.transcript;
- }
-
- return [];
-}
diff --git a/apps/web/src/components/engine/decoders/caption/whisper.ts b/apps/web/src/components/engine/decoders/caption/whisper.ts
deleted file mode 100644
index 6165d0ca..00000000
--- a/apps/web/src/components/engine/decoders/caption/whisper.ts
+++ /dev/null
@@ -1,94 +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 { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../components';
-import { renderText } from '../../utils/text';
-import { loadWebFont } from '../../font/utils';
-import { groupBy, findActiveGroup, resolveTranscript } from './utils';
-import { placeCaption } from './position';
-import { createEntity } from '../../api';
-import { appendChild } from '../../api/hierarchy';
-import { setComponent } from '../../api/events';
-
-import type { EngineWorld } from '../../api/world';
-import type { Asset } from '../../db';
-import type { CaptionDecoder } from './types';
-
-const WIDTH = 1000;
-const HEIGHT = 100;
-
-export class WhisperCaptionDecoder implements CaptionDecoder {
- public readonly type = CaptionType.WHISPER;
- public groups: ReturnType<typeof groupBy> = [];
- public ready = false;
-
- private readonly asset: Asset;
- private currentGroupIndex = -1;
-
- constructor(asset: Asset) {
- this.asset = asset;
- this.init();
- }
-
- private async init() {
- if (this.ready) return;
- const transcript = await resolveTranscript(this.asset);
- this.groups = groupBy(transcript, { duration: 2 });
-
- this.ready = true;
- }
-
- public reposition(world: EngineWorld, eid: number): boolean {
- return placeCaption(world, eid, { width: WIDTH, height: HEIGHT, defaultAlign: CaptionAlign.BOTTOM });
- }
-
- public applyStyles(world: EngineWorld, eid: number): void {
- const c = world.components;
-
- if (!this.reposition(world, eid)) return;
-
- setComponent(world, eid, c.TextStyle, {
- fontFamily: 'Montserrat',
- fontWeight: '400',
- fontSize: 40,
- textAlign: TextAlign.CENTER,
- textBaseline: TextBaseline.MIDDLE,
- textCase: TextCase.ORIGINAL,
- leading: 1.4,
- fontStyle: FontStyle.NORMAL,
- });
-
- const fid = createEntity(world);
- setComponent(world, fid, c.Paint, PaintType.SOLID);
- setComponent(world, fid, c.Color, 0xFFFFFF);
- appendChild(world, fid, eid);
-
- loadWebFont(world, 'Montserrat');
- }
-
- public seekTo(world: EngineWorld, eid: number, relativeTime: number): void {
- const c = world.components;
- const groupIndex = findActiveGroup(this.groups, relativeTime);
-
- if (groupIndex === -1) {
- c.Chars[eid] = '';
- this.currentGroupIndex = -1;
- return;
- }
-
- if (groupIndex !== this.currentGroupIndex) {
- this.currentGroupIndex = groupIndex;
- c.Chars[eid] = this.groups[groupIndex].map(w => w.text).join(' ');
- }
- }
-
- public draw(world: EngineWorld, eid: number): void {
- renderText(world, eid);
- }
-
- public dispose(): void {
- this.groups = [];
- this.currentGroupIndex = -1;
- }
-}
diff --git a/apps/web/src/components/engine/decoders/frame-cache.ts b/apps/web/src/components/engine/decoders/frame-cache.ts
deleted file mode 100644
index 7630b459..00000000
--- a/apps/web/src/components/engine/decoders/frame-cache.ts
+++ /dev/null
@@ -1,186 +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/. */
-
-type FrameCacheConfig = {
- pixels: number;
- count: number;
-}
-
-type FrameTile = {
- tileIndex: number;
- frameIndex: number;
-}
-
-type Frame = VideoFrame | ImageBitmap;
-
-function getDisplaySize(frame: Frame) {
- if (frame instanceof VideoFrame) {
- return [frame.displayWidth, frame.displayHeight];
- }
- return [frame.width, frame.height]
-}
-
-export class FrameCache {
- public readonly config: FrameCacheConfig;
- public readonly atlas = new OffscreenCanvas(0, 0);
- public readonly atlasCtx = this.atlas.getContext('2d')!;
- public readonly source = new OffscreenCanvas(0, 0);
- public readonly sourceCtx = this.source.getContext('2d')!;
-
- private tileWidth: number = 0;
- private tileHeight: number = 0;
- private columns: number = 0;
- private tiles: FrameTile[];
-
- public rotation: number = 0;
- public leftFrameIndex: number;
- public rightFrameIndex: number;
- public lastInserted: number = -1;
-
- public constructor(config: FrameCacheConfig) {
- this.config = config;
- this.leftFrameIndex = 0;
- this.rightFrameIndex = config.count - 1;
- this.tiles = [];
- }
-
- private resizeCaches(frame: Frame) {
- const rotation = this.rotation;
- const [width, height] = getDisplaySize(frame);
- const pixelCount = width * height;
-
- const factor = Math.min(1, Math.sqrt(this.config.pixels / pixelCount));
-
- const sourceWidth = rotation === 90 || rotation === 270
- ? height
- : width;
-
- const sourceHeight = rotation === 90 || rotation === 270
- ? width
- : height;
-
- this.tileWidth = Math.floor(sourceWidth * factor);
- this.tileHeight = Math.floor(sourceHeight * factor);
-
- // Lay tiles in a 2D grid
- this.columns = Math.ceil(Math.sqrt(this.config.count));
- const rows = Math.ceil(this.config.count / this.columns);
-
- const absoluteWidth = this.tileWidth * this.columns;
- const absoluteHeight = this.tileHeight * rows;
-
- if (this.atlas.width !== absoluteWidth || this.atlas.height !== absoluteHeight) {
- this.atlas.width = absoluteWidth;
- this.atlas.height = absoluteHeight;
- this.atlasCtx.imageSmoothingQuality = 'high';
- this.tiles = []; // setting the canvas size resets the tiles
- }
- }
-
- private drawRotated(frame: Frame) {
- const ctx = this.sourceCtx;
- const rotation = this.rotation;
-
- const [displayWidth, displayHeight] = getDisplaySize(frame);
-
- const width = rotation === 90 || rotation === 270
- ? displayHeight
- : displayWidth;
-
- const height = rotation === 90 || rotation === 270
- ? displayWidth
- : displayHeight;
-
- if (this.source.width !== width || this.source.height !== height) {
- this.source.width = width;
- this.source.height = height;
- ctx.imageSmoothingEnabled = false;
- }
-
- ctx.resetTransform();
- ctx.clearRect(0, 0, width, height);
- ctx.translate(width / 2, height / 2);
- ctx.rotate((rotation * Math.PI) / 180);
- ctx.drawImage(frame, -displayWidth / 2, -displayHeight / 2, displayWidth, displayHeight);
- }
-
- private evictTiles() {
- if (this.tiles.length < this.config.count) return;
-
- this.tiles = this.tiles.filter(t => t.frameIndex >= this.leftFrameIndex && t.frameIndex <= this.rightFrameIndex);
- }
-
- private insertTile(frameIndex: number) {
- let tileIndex = 0;
- while (this.tiles.some(t => t.tileIndex === tileIndex)) {
- tileIndex++;
- }
-
- this.tiles.push({ tileIndex, frameIndex });
-
- const ctx = this.atlasCtx;
- const x = (tileIndex % this.columns) * this.tileWidth;
- const y = Math.floor(tileIndex / this.columns) * this.tileHeight;
-
- ctx.clearRect(x, y, this.tileWidth, this.tileHeight);
- ctx.drawImage(this.source, x, y, this.tileWidth, this.tileHeight);
- }
-
- public has(frameIndex: number) {
- return this.tiles.some(t => t.frameIndex === frameIndex);
- }
-
- /**
- * Frame index of the cached frame closest to `frameIndex`, or `undefined` when
- * nothing is cached within `tolerance`. Ties resolve to the earlier frame, so a
- * playhead sitting between two cached frames never shows one that hasn't played yet.
- */
- public findNearest(frameIndex: number, tolerance: number): number | undefined {
- let best: number | undefined;
- let bestDistance = tolerance + 1;
-
- for (const tile of this.tiles) {
- const distance = Math.abs(tile.frameIndex - frameIndex);
- if (distance < bestDistance || (best !== undefined && distance === bestDistance && tile.frameIndex < best)) {
- bestDistance = distance;
- best = tile.frameIndex;
- }
- }
-
- return best;
- }
-
- public insert(frame: VideoFrame | ImageBitmap, index: number) {
- if (this.has(index)) return;
-
- this.evictTiles();
- this.resizeCaches(frame);
- this.drawRotated(frame);
- this.insertTile(index);
- this.lastInserted = index;
- }
-
- public findTile(frameIndex: number) {
- const tile = this.tiles.find(t => t.frameIndex === frameIndex);
-
- if (!tile) return;
-
- const x = (tile.tileIndex % this.columns) * this.tileWidth;
- const y = Math.floor(tile.tileIndex / this.columns) * this.tileHeight;
-
- return { x, y, width: this.tileWidth, height: this.tileHeight };
- }
-
- public dispose() {
- this.atlas.width = 0;
- this.atlas.height = 0;
- this.source.width = 0;
- this.source.height = 0;
- this.tileWidth = 0;
- this.tileHeight = 0;
- this.columns = 0;
- this.lastInserted = -1;
- this.tiles = [];
- }
-}
diff --git a/apps/web/src/components/engine/decoders/html.ts b/apps/web/src/components/engine/decoders/html.ts
deleted file mode 100644
index ec4de380..00000000
--- a/apps/web/src/components/engine/decoders/html.ts
+++ /dev/null
@@ -1,152 +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/. */
-
-type Ctx2D = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
-
-// html-in-canvas (https://github.com/WICG/html-in-canvas). Chromium only,
-// behind chrome://flags/#canvas-draw-element; the API surface is still
-// moving, so every touchpoint is typed and isolated here.
-type DrawElementContext = Ctx2D & {
- drawElementImage(source: Element | unknown, dx: number, dy: number, dw: number, dh: number): DOMMatrix;
-};
-
-export function isHtmlInCanvasSupported(): boolean {
- return typeof CanvasRenderingContext2D !== 'undefined'
- && 'drawElementImage' in CanvasRenderingContext2D.prototype;
-}
-
-/**
- * Resolves after the browser's next rendering update (style/layout/paint) has completed.
- */
-export function nextRenderingUpdate(): Promise<void> {
- return new Promise(resolve => {
- // timeout in case requestAnimationFrame is not called
- const timeout = setTimeout(resolve, 250);
-
- requestAnimationFrame(() => {
- clearTimeout(timeout);
- setTimeout(resolve, 0);
- });
- });
-}
-
-let htmlCanvas: HTMLCanvasElement | null = null;
-let htmlCtx: CanvasRenderingContext2D | null = null;
-let htmlHosts = 0;
-
-function acquireLayoutCanvas(): HTMLCanvasElement {
- if (!htmlCanvas) {
- htmlCanvas = document.createElement('canvas');
- htmlCanvas.width = 1;
- htmlCanvas.height = 1;
- htmlCanvas.toggleAttribute('layoutsubtree', true);
- htmlCanvas.style.cssText = 'position:fixed;left:0;top:0;width:1px;height:1px;pointer-events:none;';
- document.body.appendChild(htmlCanvas);
- htmlCtx = htmlCanvas.getContext('2d')!;
- }
- htmlHosts++;
- return htmlCanvas;
-}
-
-function releaseLayoutCanvas(): void {
- if (--htmlHosts === 0) {
- htmlCanvas?.remove();
- htmlCanvas = null;
- htmlCtx = null;
- }
-}
-
-/**
- * DOM host for one HTML paint: a div inside the shared layoutsubtree canvas.
- * The mount document inserts the paint's JSX children into `element` as real,
- * reactive DOM; the browser lays them out at the parent geometry's box size
- * and `draw` rasterizes the result at the destination's device scale and
- * blits it into the rendering context (stage or offscreen alike).
- */
-export class HtmlHost {
- public readonly element: HTMLDivElement;
- private disposed = false;
- private warned = false;
- private width = -1;
- private height = -1;
-
- public constructor() {
- const el = document.createElement('div');
- // pointer-events: none — layoutsubtree children participate in hit
- // testing and would otherwise swallow page interactions.
- el.style.cssText = 'position:absolute;left:0;top:0;overflow:hidden;pointer-events:none;';
- acquireLayoutCanvas().appendChild(el);
- this.element = el;
- }
-
- /**
- * Resolves asynchronously loaded html resources.
- */
- public whenReady(timeInSeconds: number): Promise<void> {
- // sync animations
- for (const animation of this.element.getAnimations({ subtree: true })) {
- if (animation.playState !== 'paused') animation.pause();
- animation.currentTime = timeInSeconds * 1000;
- }
-
- // wait for fonts to be ready
- const pending: Promise<unknown>[] = [document.fonts.ready];
-
- // wait for images to be ready
- for (const image of this.element.querySelectorAll('img')) {
- // `complete` covers loaded, failed, and srcless images alike; the
- // rest are still in flight, and a failed decode just paints nothing.
- if (image.complete) continue;
- pending.push(image.decode().catch(() => undefined));
- }
-
- return Promise.all(pending).then(() => nextRenderingUpdate());
- }
-
- public setSize(width: number, height: number): void {
- if (width === this.width && height === this.height) return;
- this.width = width;
- this.height = height;
- this.element.style.width = `${width}px`;
- this.element.style.height = `${height}px`;
- }
-
- public draw(ctx: Ctx2D, width: number, height: number): void {
- if (this.disposed || !htmlCanvas || !htmlCtx) return;
-
- this.setSize(width, height);
-
- try {
- const m = ctx.getTransform();
- const sx = Math.hypot(m.a, m.b) || 1;
- const sy = Math.hypot(m.c, m.d) || 1;
- const pw = Math.max(1, Math.ceil(width * sx));
- const ph = Math.max(1, Math.ceil(height * sy));
-
- // Grow-only: shrinking would clear and reallocate every time hosts
- // of different sizes share the canvas within one frame.
- if (htmlCanvas.width < pw) htmlCanvas.width = pw;
- if (htmlCanvas.height < ph) htmlCanvas.height = ph;
-
- htmlCtx.setTransform(sx, 0, 0, sy, 0, 0);
- htmlCtx.clearRect(0, 0, width, height);
- (htmlCtx as DrawElementContext).drawElementImage(this.element, 0, 0, width, height);
- ctx.drawImage(htmlCanvas, 0, 0, pw, ph, 0, 0, width, height);
- } catch (e) {
- // A transient failure (subtree not yet laid out or painted) just
- // skips the frame; log once so a permanent one doesn't spam.
- if (!this.warned) {
- this.warned = true;
- console.error(`Error drawing <HtmlPaint> content: ${e instanceof Error ? `${e.name}: ${e.message}` : e}`);
- }
- }
- }
-
- public dispose(): void {
- if (this.disposed) return;
- this.disposed = true;
- this.element.remove();
- releaseLayoutCanvas();
- }
-}
diff --git a/apps/web/src/components/engine/decoders/image.ts b/apps/web/src/components/engine/decoders/image.ts
deleted file mode 100644
index d729a4c2..00000000
--- a/apps/web/src/components/engine/decoders/image.ts
+++ /dev/null
@@ -1,259 +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 { addComponent } from '../api/events';
-import { getAssetFile } from '../api/assets';
-import type { EngineWorld } from '../api/world';
-import type { ImageAsset } from '../db';
-
-export type DecodedImage = ImageBitmap | HTMLImageElement;
-
-export interface ImageDecoder {
- readonly assetId: string;
- readonly failed: boolean;
- readonly ready: boolean;
- init(): Promise<void>;
- getBitmap(targetWidth: number, targetHeight: number): DecodedImage | null;
- dispose(): void;
-}
-
-/**
- * Image decoder backed by `createImageBitmap`. Used for raster formats
- * (PNG, JPEG, WebP, …) where we want a GPU-friendly bitmap.
- *
- * Fetching + decoding starts immediately on construction.
- */
-export class BitmapImageDecoder implements ImageDecoder {
- readonly assetId: string;
- public failed = false;
- private bitmap: ImageBitmap | null = null;
- private disposed = false;
-
- public constructor(private readonly asset: ImageAsset) {
- this.assetId = asset.id;
- }
-
- public async init() {
- if (this.disposed) return;
-
- try {
- const file = await getAssetFile(this.asset);
- const result = await createImageBitmap(file);
- if (this.disposed) {
- result.close();
- return;
- }
- this.bitmap = result;
- } catch (e) {
- console.error('Error decoding image', e);
- this.failed = true;
- }
- }
-
-
- public get ready() {
- return this.bitmap !== null;
- }
-
- public getBitmap(_targetWidth: number, _targetHeight: number): ImageBitmap | null {
- // TODO: mipmap — pick the LOD closest to targetWidth × targetHeight
- return this.bitmap;
- }
-
- public dispose() {
- if (this.disposed) return;
- if (this.bitmap) {
- this.bitmap.close();
- this.bitmap = null;
- }
- this.disposed = true;
- }
-}
-
-/**
- * Image decoder backed by `HTMLImageElement`. Used for formats that
- * `createImageBitmap` doesn't handle reliably across browsers — most
- * notably SVG, where we want the browser's native vector renderer.
- */
-export class ElementImageDecoder implements ImageDecoder {
- readonly assetId: string;
- public failed = false;
- private element: HTMLImageElement | null = null;
- private url: string | null = null;
- private disposed = false;
- private readonly width: number;
- private readonly height: number;
-
- public constructor(private readonly asset: ImageAsset) {
- this.assetId = asset.id;
- this.width = asset.width;
- this.height = asset.height;
- }
-
- public get ready() {
- return this.element !== null;
- }
-
- public async init() {
- if (this.disposed) return;
-
- try {
- const file = await getAssetFile(this.asset);
- const url = URL.createObjectURL(file);
-
- await new Promise<void>((resolve) => {
- const img = new Image();
- img.onload = () => {
- if (this.disposed) {
- URL.revokeObjectURL(url);
- return resolve();
- }
- // SVGs without explicit width/height attributes report
- // naturalWidth/Height = 0. Pin to the asset's recorded
- // dimensions so getScaledImageProps gets a real aspect.
- img.width = this.width;
- img.height = this.height;
- this.element = img;
- this.url = url;
- resolve();
- };
- img.onerror = () => {
- URL.revokeObjectURL(url);
- this.failed = true;
- resolve();
- };
- img.src = url;
- })
- } catch (e) {
- console.error('Error decoding image', e);
- this.failed = true;
- }
- }
-
- public getBitmap(_targetWidth: number, _targetHeight: number): HTMLImageElement | null {
- return this.element;
- }
-
- public dispose() {
- if (this.disposed) return;
- if (this.url) {
- URL.revokeObjectURL(this.url);
- this.url = null;
- }
- this.element = null;
- this.disposed = true;
- }
-}
-
-function createImageDecoder(asset: ImageAsset): ImageDecoder {
- if (asset.mimeType === 'image/svg+xml') {
- return new ElementImageDecoder(asset);
- }
- return new BitmapImageDecoder(asset);
-}
-
-type DecoderCacheEntry = {
- decoder: ImageDecoder;
- initPromise: Promise<void>;
- refs: number;
-};
-
-/** One decoded bitmap per asset id, scoped per world. */
-type ImageDecoderCache = Map<string, DecoderCacheEntry>;
-
-const decoderCaches = new WeakMap<EngineWorld, ImageDecoderCache>();
-
-function getDecoderCache(world: EngineWorld): ImageDecoderCache {
- let cache = decoderCaches.get(world);
- if (!cache) {
- cache = new Map();
- decoderCaches.set(world, cache);
- }
- return cache;
-}
-
-/**
- * Per-entity handle onto a shared, refcounted decoder. Duplicated entities
- * pointing at the same asset share one decode; disposing a handle releases
- * one reference and the underlying decoder is disposed with the last one.
- */
-class SharedImageDecoder implements ImageDecoder {
- private released = false;
-
- public constructor(
- private readonly entry: DecoderCacheEntry,
- private readonly cache: ImageDecoderCache,
- ) { }
-
- public get assetId() {
- return this.entry.decoder.assetId;
- }
- public get failed() {
- return this.entry.decoder.failed;
- }
- public get ready() {
- return this.entry.decoder.ready;
- }
-
- public init(): Promise<void> {
- return this.entry.initPromise;
- }
-
- public getBitmap(targetWidth: number, targetHeight: number): DecodedImage | null {
- return this.entry.decoder.getBitmap(targetWidth, targetHeight);
- }
-
- public dispose() {
- if (this.released) return;
- this.released = true;
- this.entry.refs--;
- if (this.entry.refs === 0) {
- this.cache.delete(this.entry.decoder.assetId);
- this.entry.decoder.dispose();
- }
- }
-}
-
-type ResolvedImageDecoder = {
- decoder: ImageDecoder;
- initPromise: Promise<void> | null;
-}
-
-export function resolveImageDecoder(world: EngineWorld, eid: number): ResolvedImageDecoder | null {
- const AssetId = world.components.AssetId;
- const assetId = AssetId[eid];
- if (!assetId) return null;
-
- const existing = world.components.ImageDecoder[eid];
- if (existing && existing.assetId === assetId) {
- return {
- decoder: existing,
- initPromise: null,
- };
- };
-
- // Asset changed: release the old decoder and acquire one for the new asset.
- if (existing) existing.dispose();
-
- const asset = world.assets.get(assetId);
- if (!asset || asset.type !== 'IMAGE') return null;
-
- addComponent(world, eid, world.components.ImageDecoder, false);
-
- const cache = getDecoderCache(world);
- let entry = cache.get(assetId);
- if (!entry) {
- const decoder = createImageDecoder(asset);
- entry = { decoder, initPromise: decoder.init(), refs: 0 };
- cache.set(assetId, entry);
- }
- entry.refs++;
-
- const handle = new SharedImageDecoder(entry, cache);
- world.components.ImageDecoder[eid] = handle;
- return {
- decoder: handle,
- initPromise: entry.initPromise,
- };
-}
diff --git a/apps/web/src/components/engine/decoders/index.ts b/apps/web/src/components/engine/decoders/index.ts
deleted file mode 100644
index a21ec116..00000000
--- a/apps/web/src/components/engine/decoders/index.ts
+++ /dev/null
@@ -1,14 +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/. */
-
-export * from './audio';
-export * from './audio-peaks';
-export * from './caption';
-export * from './html';
-export * from './image';
-export * from './keyframe-index';
-export * from './sequence';
-export * from './shader';
-export * from './time-stretcher';
-export * from './video';
diff --git a/apps/web/src/components/engine/decoders/keyframe-index.ts b/apps/web/src/components/engine/decoders/keyframe-index.ts
deleted file mode 100644
index d046fa5f..00000000
--- a/apps/web/src/components/engine/decoders/keyframe-index.ts
+++ /dev/null
@@ -1,109 +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 { EncodedPacketSink } from 'mediabunny';
-
-import type { InputVideoTrack } from 'mediabunny';
-
-/**
- * Key packets walked between two yields to the event loop. Keeps an all-intra
- * track (every packet is a keyframe) from starving the render loop.
- */
-const YIELD_INTERVAL = 512;
-
-const indexCache = new Map<string, KeyframeIndex>();
-
-export function clearKeyframeIndexCache() {
- indexCache.clear();
-}
-
-/**
- * Returns the shared keyframe index for an asset, starting the walk on first
- * access. Cheap to call repeatedly — one index is built per asset, not per decoder.
- */
-export function getKeyframeIndex(assetId: string, track: InputVideoTrack): KeyframeIndex {
- let index = indexCache.get(assetId);
-
- if (!index) {
- index = new KeyframeIndex(track);
- indexCache.set(assetId, index);
- }
-
- return index;
-}
-
-/**
- * Ascending presentation timestamps of every key packet in a video track, walked
- * ahead of time so seeking resolves its seed keyframe with a binary search
- * instead of a container lookup.
- */
-export class KeyframeIndex {
- private timestamps: number[] = [];
- private complete = false;
-
- public constructor(track: InputVideoTrack) {
- this.build(track);
- }
-
- private async build(track: InputVideoTrack) {
- try {
- const sink = new EncodedPacketSink(track);
-
- let packet = await sink.getFirstKeyPacket({ metadataOnly: true });
- let walked = 0;
-
- while (packet) {
- this.timestamps.push(packet.timestamp);
-
- // `getNextKeyPacket` resolves without I/O once the sample table is parsed,
- // so awaiting it only queues a microtask and never lets the browser paint.
- // A timer is a macrotask, which does.
- if (++walked % YIELD_INTERVAL === 0) {
- await new Promise((resolve) => setTimeout(resolve));
- }
-
- packet = await sink.getNextKeyPacket(packet, { metadataOnly: true });
- }
-
- this.complete = true;
- } catch (e) {
- // A disposed input (project closed, permission revoked) lands here; the
- // index simply stays incomplete and callers fall back to a container lookup.
- console.error('Error building keyframe index', e);
- }
- }
-
- /**
- * Timestamp of the last keyframe at or before `seconds`, or `null` when the
- * index cannot answer: `seconds` precedes the first keyframe, or the walk has
- * not reached that far yet and a closer keyframe may still show up.
- */
- public floor(seconds: number): number | null {
- const timestamps = this.timestamps;
- const length = timestamps.length;
-
- if (length === 0 || seconds < timestamps[0]) {
- return null;
- }
- if (!this.complete && seconds > timestamps[length - 1]) {
- return null;
- }
-
- let low = 0;
- let high = length - 1;
-
- while (low < high) {
- const mid = (low + high + 1) >> 1;
- if (timestamps[mid] <= seconds) {
- low = mid;
- } else {
- high = mid - 1;
- }
- }
-
- return timestamps[low];
- }
-}
diff --git a/apps/web/src/components/engine/decoders/sequence.ts b/apps/web/src/components/engine/decoders/sequence.ts
deleted file mode 100644
index b77c87d8..00000000
--- a/apps/web/src/components/engine/decoders/sequence.ts
+++ /dev/null
@@ -1,268 +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 { assert } from '@/utils';
-import { addComponent } from '../api/events';
-import { FrameCache } from './frame-cache';
-
-import type { EngineWorld } from '../api/world';
-import type { SequenceAsset } from '../db';
-
-const FRAME_EXTENSIONS = /\.(png|jpe?g|webp|avif|bmp|gif)$/i;
-
-export type SequenceBufferMode = 'discarded' | 'idle' | 'alive';
-const IDLE_TIMEOUT_MS = 10_000;
-
-export class SequenceDecoder {
- public currentFrame: number = 0;
- public errored = false;
- public disposed = false;
- public hasCache = true;
- public asset: SequenceAsset;
- public mode: SequenceBufferMode = 'alive';
- public initialized: Promise<void>;
-
- private frames: File[] = [];
- private lastRenderedFrame: number = -1;
- private seekGeneration = 0;
- private idleTimer: ReturnType<typeof setTimeout> | null = null;
- private seekLock: Promise<void> = Promise.resolve();
-
- public readonly cache = new FrameCache({ pixels: 720 * 720, count: 49 });
- public readonly canvas = new OffscreenCanvas(0, 0);
- public readonly ctx = this.canvas.getContext('2d')!;
-
- public constructor(asset: SequenceAsset, hasCache: boolean) {
- this.hasCache = hasCache;
- this.asset = asset;
- this.initialized = this.initialize();
- }
-
- private async initialize() {
- try {
- const entries: { name: string; handle: FileSystemFileHandle }[] = [];
-
- for await (const [name, entry] of this.asset.directoryHandle.entries()) {
- if (entry.kind !== 'file') continue;
- if (!FRAME_EXTENSIONS.test(name)) continue;
- entries.push({ name, handle: entry as FileSystemFileHandle });
- }
-
- // Natural-order sort so frame_2 comes before frame_10.
- entries.sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true }));
- this.frames = await Promise.all(entries.map(e => e.handle.getFile()));
- assert(this.frames.length > 0, 'No frames found');
- } catch {
- this.errored = true;
- }
- }
-
- private updateBitmap(frameIndex: number) {
- if (frameIndex === this.lastRenderedFrame) return;
- const tile = this.cache.findTile(frameIndex);
- if (!tile) return;
- const ctx = this.ctx;
-
- if (this.canvas.width !== tile.width || this.canvas.height !== tile.height) {
- this.canvas.width = tile.width;
- this.canvas.height = tile.height;
- ctx.imageSmoothingEnabled = false;
- }
-
- ctx.clearRect(0, 0, tile.width, tile.height);
- ctx.drawImage(
- this.cache.atlas,
- tile.x,
- tile.y,
- tile.width,
- tile.height,
- 0,
- 0,
- tile.width,
- tile.height,
- );
-
- this.lastRenderedFrame = frameIndex;
- }
-
- public toBitmap() {
- if (this.canvas.width === 0 || this.canvas.height === 0) {
- return null;
- }
-
- return this.canvas;
- }
-
- private computeWindow(targetFrame: number, forward: boolean): [number, number] {
- const span = this.cache.config.count - 2;
- const lastFrameIndex = this.frames.length - 1;
-
- // Whole video fits in the cache — keep all of it.
- if (lastFrameIndex <= span) {
- return [0, lastFrameIndex];
- }
-
- const ahead = Math.round((span * 2) / 3);
- const behind = span - ahead;
-
- let left = targetFrame - (forward ? behind : ahead);
- let right = targetFrame + (forward ? ahead : behind);
-
- // Push budget that falls outside the boundaries onto the other side.
- if (left < 0) {
- right -= left;
- left = 0;
- }
- if (right > lastFrameIndex) {
- left -= right - lastFrameIndex;
- right = lastFrameIndex;
- }
-
- return [Math.max(0, left), Math.min(lastFrameIndex, right)];
- }
-
- private async updateBitmapAsync(frame: number) {
- try {
- await this.initialized;
- const file = this.frames[frame];
- if (file === undefined) return;
- const bitmap = await createImageBitmap(file);
-
- if (this.canvas.width !== bitmap.width || this.canvas.height !== bitmap.height) {
- this.canvas.width = bitmap.width;
- this.canvas.height = bitmap.height;
- this.ctx.imageSmoothingEnabled = false;
- }
-
- this.ctx.clearRect(0, 0, bitmap.width, bitmap.height);
- this.ctx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height);
- bitmap.close();
- } catch { /** ignore */ }
- }
-
- public async seekTo(frame: number, frameRate: number) {
- let targetFrame = Math.round((frame / frameRate) * this.asset.frameRate);
-
- // Path without cache
- if (!this.hasCache) {
- return this.updateBitmapAsync(targetFrame);
- }
-
- this.updateBitmap(targetFrame);
-
- const isCurrentFrame = targetFrame === this.currentFrame;
- const isEmpty = this.frames.length === 0;
- const isIdle = this.mode === 'idle' && isCurrentFrame;
- const isCached = this.cache.has(targetFrame) && isCurrentFrame;
- const errored = this.errored;
-
- if (isEmpty || isCached || isIdle || errored) {
- return;
- }
-
- this.touch();
-
- const generation = ++this.seekGeneration;
-
- // When the cache does not have the target frame, forward must be true (latency optimization)
- const forward = this.cache.has(targetFrame) ? targetFrame >= this.currentFrame : true;
- this.currentFrame = targetFrame;
-
- const [left, right] = this.computeWindow(targetFrame, forward);
- this.cache.leftFrameIndex = left;
- this.cache.rightFrameIndex = right;
-
- let run: Promise<void>
- if (forward) {
- run = this.seekLock.then(() => this.fillCache([targetFrame, this.cache.rightFrameIndex], generation));
- } else {
- // +1 to include the target frame
- run = this.seekLock.then(() => this.fillCache([this.cache.leftFrameIndex, targetFrame], generation));
- }
-
- // Run after the previous seek finishes — never concurrently.
- this.seekLock = run.catch(() => { });
- }
-
- private async fillCache(range: [number, number], generation: number): Promise<void> {
- if (generation !== this.seekGeneration || range[0] > range[1]) return;
-
- for (let index = range[0]; index <= range[1]; index++) {
- const file = this.frames[index];
- if (file === undefined || this.cache.has(index)) continue;
-
- try {
- const bitmap = await createImageBitmap(file);
- this.cache.insert(bitmap, index);
- bitmap.close();
- this.updateBitmap(this.currentFrame);
- } catch { /** ignore */ }
-
- if (generation !== this.seekGeneration) break;
- }
- }
-
- private touch() {
- this.mode = 'alive';
- if (this.idleTimer !== null) {
- clearTimeout(this.idleTimer);
- }
- this.idleTimer = setTimeout(() => this.idle(), IDLE_TIMEOUT_MS);
- }
-
- public idle() {
- if (this.mode !== 'alive') return;
- this.mode = 'idle';
-
- if (this.idleTimer !== null) {
- clearTimeout(this.idleTimer);
- this.idleTimer = null;
- }
-
- this.cache.dispose();
- }
-
-
- public dispose() {
- if (this.mode === 'discarded') return;
- this.mode = 'discarded';
-
- if (this.idleTimer !== null) {
- clearTimeout(this.idleTimer);
- this.idleTimer = null;
- }
-
- this.cache.dispose();
-
- // Release the display canvas backing store.
- this.canvas.width = 0;
- this.canvas.height = 0;
- this.lastRenderedFrame = -1;
- }
-}
-
-export function resolveSequenceDecoder(world: EngineWorld, fid: number, hasCache = true): SequenceDecoder | null {
- const AssetId = world.components.AssetId;
- const assetId = AssetId[fid];
- if (!assetId) return null;
-
- const asset = world.assets.get(assetId);
- if (!asset || asset.type !== 'SEQUENCE') return null;
-
- // Identity check (not just id) so decoders rebuild when asset metadata —
- // e.g. frameRate — changes via `assets.save({ ...asset, frameRate: ... })`.
- const existing = world.components.SequenceDecoder[fid];
- if (existing && existing.asset === asset) {
- existing.hasCache = hasCache;
- return existing;
- }
-
- existing?.dispose();
-
- addComponent(world, fid, world.components.SequenceDecoder, false);
-
- const decoder = new SequenceDecoder(asset, hasCache);
- world.components.SequenceDecoder[fid] = decoder;
- return decoder;
-}
diff --git a/apps/web/src/components/engine/decoders/shader.ts b/apps/web/src/components/engine/decoders/shader.ts
deleted file mode 100644
index 8f6afff3..00000000
--- a/apps/web/src/components/engine/decoders/shader.ts
+++ /dev/null
@@ -1,374 +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 { addComponent } from "../api/events";
-import { parseColor } from "@/utils/color";
-
-import type { EngineWorld } from "../api/world";
-
-type Ctx2D = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
-type UniformValue = number | number[] | string;
-
-/**
- * Runtime pipeline for one shader paint: compiles the paint's fragment-stage
- * WGSL against the engine prelude, samples the media paint directly below the
- * shader in the fill stack into a texture, runs the pipeline into an
- * offscreen WebGPU canvas, and hands the result back to the Canvas2D
- * compositor via `drawImage`. Created lazily by `resolveShaderHost`, exactly
- * like the media decoders.
- *
- * The authored contract (see reference/jsx/shader-paint.md): the user writes
- * `@fragment fn main(@location(0) uv: vec2f) -> @location(0) vec4f` with `uv`
- * in box space, samples the media through `sampleSource(uv)` (which honors
- * the media paint's `objectFit` placement), and declares parameters as
- * `@group(1)` uniforms whose values arrive by name from the `uniforms` prop.
- */
-
-// One device for every shader paint in the page; pipelines are per-host.
-let devicePromise: Promise<GPUDevice | null> | null = null;
-
-function sharedDevice(): Promise<GPUDevice | null> {
- devicePromise ??= (async () => {
- const adapter = await navigator.gpu?.requestAdapter();
- if (!adapter) {
- console.error("[shader-paint] WebGPU is not available; shader paints render as passthrough");
- return null;
- }
- return await adapter.requestDevice();
- })();
- return devicePromise;
-}
-
-const PRELUDE = /* wgsl */ `
-struct Globals {
- resolution: vec2f,
- time: f32,
- _pad: f32,
- fitOffset: vec2f,
- fitSize: vec2f,
-}
-
-@group(0) @binding(0) var source: texture_2d<f32>;
-@group(0) @binding(1) var sourceSampler: sampler;
-@group(0) @binding(2) var<uniform> globals: Globals;
-
-struct VSOut {
- @builtin(position) position: vec4f,
- @location(0) uv: vec2f,
-}
-
-@vertex
-fn vs(@builtin(vertex_index) i: u32) -> VSOut {
- var corners = array<vec2f, 3>(vec2f(-1.0, -1.0), vec2f(3.0, -1.0), vec2f(-1.0, 3.0));
- var out: VSOut;
- out.position = vec4f(corners[i], 0.0, 1.0);
- out.uv = vec2f((corners[i].x + 1.0) * 0.5, 1.0 - (corners[i].y + 1.0) * 0.5);
- return out;
-}
-
-// Samples the media in box space: uv spans the parent's box, the media sits
-// within it per objectFit, and letterbox areas return transparent black.
-fn sampleSource(uv: vec2f) -> vec4f {
- let suv = (uv - globals.fitOffset) / globals.fitSize;
- let inside = all(suv >= vec2f(0.0)) && all(suv <= vec2f(1.0));
- let color = textureSampleLevel(source, sourceSampler, clamp(suv, vec2f(0.0), vec2f(1.0)), 0.0);
- return select(vec4f(0.0), color, inside);
-}
-`;
-
-const UNIFORM_COMPONENTS: Record<string, number> = {
- f32: 1,
- vec2f: 2, "vec2<f32>": 2,
- vec3f: 3, "vec3<f32>": 3,
- vec4f: 4, "vec4<f32>": 4,
-};
-
-const UNIFORM_RE = /@group\(1\)\s*@binding\((\d+)\)\s*var\s*<\s*uniform\s*>\s*([A-Za-z_][A-Za-z0-9_]*)\s*:\s*([A-Za-z0-9_]+(?:<[A-Za-z0-9_]+>)?)/g;
-
-type UniformSlot = {
- name: string;
- binding: number;
- components: number;
- buffer: GPUBuffer;
-};
-
-type Pipeline = {
- device: GPUDevice;
- pipeline: GPURenderPipeline;
- sampler: GPUSampler;
- globals: GPUBuffer;
- slots: UniformSlot[];
- group1: GPUBindGroup;
- group0Layout: GPUBindGroupLayout;
-};
-
-export class ShaderHost {
- private canvas = new OffscreenCanvas(1, 1);
- private context: GPUCanvasContext | null = null;
- private gpu: Pipeline | null = null;
- private sourceTexture: GPUTexture | null = null;
- private group0: GPUBindGroup | null = null;
- private code = "";
- private generation = 0;
- private compilePromise: Promise<void> | null = null;
- private colorCache = new Map<string, number | null>();
- private disposed = false;
-
- /** Compiles when the code differs from the current pipeline's. */
- public setCode(code: string): void {
- if (this.disposed || code === this.code) return;
- this.code = code;
- const gen = ++this.generation;
- this.compilePromise = this.compile(code, gen).catch((error) => {
- if (gen === this.generation) this.gpu = null;
- console.error("[shader-paint]", error);
- });
- }
-
- /** Resolves once the current code has compiled (or failed). */
- public whenReady(): Promise<void> | null {
- return this.compilePromise;
- }
-
- /** True when a pipeline exists — callers fall back to drawing the media directly otherwise. */
- public get ready(): boolean {
- return this.gpu !== null && !this.disposed;
- }
-
- private async compile(code: string, gen: number): Promise<void> {
- const device = await sharedDevice();
- if (!device || this.disposed) return;
-
- const module = device.createShaderModule({ code: PRELUDE + code });
- const info = await module.getCompilationInfo();
- if (gen !== this.generation) return;
-
- const preludeLines = PRELUDE.split("\n").length;
- const errors = info.messages.filter((m) => m.type === "error");
- if (errors.length > 0) {
- this.gpu = null;
- const details = errors
- .map((m) => `${Math.max(1, m.lineNum - preludeLines + 1)}:${m.linePos} ${m.message}`)
- .join("\n");
- throw new Error(`WGSL compile failed:\n${details}`);
- }
-
- const group0Layout = device.createBindGroupLayout({
- entries: [
- { binding: 0, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float" } },
- { binding: 1, visibility: GPUShaderStage.FRAGMENT, sampler: { type: "filtering" } },
- { binding: 2, visibility: GPUShaderStage.FRAGMENT, buffer: { type: "uniform" } },
- ],
- });
-
- // Declared @group(1) uniforms get one small buffer each, matched to the
- // `uniforms` record by name. An explicit layout keeps declared-but-unused
- // bindings valid (an "auto" layout would strip them).
- const slots: UniformSlot[] = [];
- for (const match of code.matchAll(UNIFORM_RE)) {
- const components = UNIFORM_COMPONENTS[match[3]];
- if (components === undefined) {
- console.warn(`[shader-paint] uniform \`${match[2]}\` has unsupported type ${match[3]} (use f32/vec2f/vec3f/vec4f)`);
- continue;
- }
- slots.push({
- name: match[2],
- binding: Number(match[1]),
- components,
- buffer: device.createBuffer({
- size: components * 4,
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
- }),
- });
- }
-
- const group1Layout = device.createBindGroupLayout({
- entries: slots.map((slot) => ({
- binding: slot.binding,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" as const },
- })),
- });
-
- const pipeline = device.createRenderPipeline({
- layout: device.createPipelineLayout({ bindGroupLayouts: [group0Layout, group1Layout] }),
- vertex: { module, entryPoint: "vs" },
- fragment: {
- module,
- entryPoint: "main",
- targets: [{ format: navigator.gpu.getPreferredCanvasFormat() }],
- },
- });
- if (gen !== this.generation || this.disposed) return;
-
- this.gpu = {
- device,
- pipeline,
- sampler: device.createSampler({ magFilter: "linear", minFilter: "linear" }),
- globals: device.createBuffer({ size: 32, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST }),
- slots,
- group1: device.createBindGroup({
- layout: group1Layout,
- entries: slots.map((slot) => ({ binding: slot.binding, resource: { buffer: slot.buffer } })),
- }),
- group0Layout,
- };
- // The source texture and its bind group belong to the old pipeline's layout.
- this.sourceTexture?.destroy();
- this.sourceTexture = null;
- this.group0 = null;
- }
-
- private writeUniforms(uniforms: Record<string, UniformValue> | null): void {
- const gpu = this.gpu!;
- for (const slot of gpu.slots) {
- const value = uniforms?.[slot.name];
- if (value === undefined) continue;
- const data = new Float32Array(slot.components);
- if (typeof value === "number") {
- data[0] = value;
- } else if (Array.isArray(value)) {
- for (let i = 0; i < Math.min(slot.components, value.length); i++) data[i] = value[i];
- } else {
- if (!this.colorCache.has(value)) this.colorCache.set(value, parseColor(value));
- const color = this.colorCache.get(value);
- if (color == null) continue;
- data[0] = ((color >> 16) & 0xff) / 255;
- if (slot.components >= 2) data[1] = ((color >> 8) & 0xff) / 255;
- if (slot.components >= 3) data[2] = (color & 0xff) / 255;
- if (slot.components >= 4) data[3] = 1;
- }
- gpu.device.queue.writeBuffer(slot.buffer, 0, data);
- }
- }
-
- /**
- * Runs the shader over `source` and draws the result into the parent's
- * box. `fit` is the media's drawImage placement within the box (from
- * getScaledImageProps); `time` is the parent's local time in seconds.
- * A null `source` runs the shader procedurally: the bound texture stays
- * transparent black, so `sampleSource` returns vec4f(0) everywhere.
- * Returns false when no pipeline is ready — the caller then draws the
- * media directly instead.
- */
- public draw(
- ctx: Ctx2D,
- width: number,
- height: number,
- source: GPUCopyExternalImageSource | null,
- sourceWidth: number,
- sourceHeight: number,
- fit: [dx: number, dy: number, sw: number, sh: number],
- time: number,
- uniforms: Record<string, UniformValue> | null,
- ): boolean {
- const gpu = this.gpu;
- if (!gpu || this.disposed) return false;
-
- const w = Math.max(1, Math.round(width));
- const h = Math.max(1, Math.round(height));
- if (this.canvas.width !== w || this.canvas.height !== h) {
- this.canvas.width = w;
- this.canvas.height = h;
- }
- if (this.context === null) {
- this.context = this.canvas.getContext("webgpu");
- if (!this.context) return false;
- this.context.configure({
- device: gpu.device,
- format: navigator.gpu.getPreferredCanvasFormat(),
- alphaMode: "premultiplied",
- });
- }
-
- const sw = source ? Math.max(1, Math.round(sourceWidth)) : 1;
- const sh = source ? Math.max(1, Math.round(sourceHeight)) : 1;
- if (!this.sourceTexture || this.sourceTexture.width !== sw || this.sourceTexture.height !== sh || !this.group0) {
- this.sourceTexture?.destroy();
- this.sourceTexture = gpu.device.createTexture({
- size: [sw, sh],
- format: "rgba8unorm",
- usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
- });
- this.group0 = gpu.device.createBindGroup({
- layout: gpu.group0Layout,
- entries: [
- { binding: 0, resource: this.sourceTexture.createView() },
- { binding: 1, resource: gpu.sampler },
- { binding: 2, resource: { buffer: gpu.globals } },
- ],
- });
- }
-
- if (source) {
- gpu.device.queue.copyExternalImageToTexture(
- { source },
- { texture: this.sourceTexture, premultipliedAlpha: true },
- [sw, sh],
- );
- }
-
- const [dx, dy, dw, dh] = fit;
- gpu.device.queue.writeBuffer(gpu.globals, 0, new Float32Array([
- w, h, time, 0,
- dx / w, dy / h, dw / w, dh / h,
- ]));
- this.writeUniforms(uniforms);
-
- const encoder = gpu.device.createCommandEncoder();
- const pass = encoder.beginRenderPass({
- colorAttachments: [{
- view: this.context.getCurrentTexture().createView(),
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- loadOp: "clear",
- storeOp: "store",
- }],
- });
- pass.setPipeline(gpu.pipeline);
- pass.setBindGroup(0, this.group0);
- pass.setBindGroup(1, gpu.group1);
- pass.draw(3);
- pass.end();
- gpu.device.queue.submit([encoder.finish()]);
-
- ctx.drawImage(this.canvas, 0, 0, width, height);
- return true;
- }
-
- public dispose(): void {
- if (this.disposed) return;
- this.disposed = true;
- this.generation++;
- this.sourceTexture?.destroy();
- this.sourceTexture = null;
- if (this.gpu) {
- this.gpu.globals.destroy();
- for (const slot of this.gpu.slots) slot.buffer.destroy();
- this.gpu = null;
- }
- this.context?.unconfigure();
- this.canvas.width = 0;
- this.canvas.height = 0;
- }
-}
-
-/**
- * The shader paint's host, created lazily like the media decoders and kept in
- * sync with the persisted `Shader.code` component. Returns null until the
- * paint carries code.
- */
-export function resolveShaderHost(world: EngineWorld, fid: number): ShaderHost | null {
- const c = world.components;
- const code = c.Shader.code[fid];
- if (!code) return null;
-
- let host = c.ShaderHost[fid];
- if (!host) {
- addComponent(world, fid, c.ShaderHost, false);
- host = new ShaderHost();
- c.ShaderHost[fid] = host;
- }
- host.setCode(code);
- return host;
-}
diff --git a/apps/web/src/components/engine/decoders/surface.ts b/apps/web/src/components/engine/decoders/surface.ts
deleted file mode 100644
index c4d5a803..00000000
--- a/apps/web/src/components/engine/decoders/surface.ts
+++ /dev/null
@@ -1,39 +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/. */
-
-type Ctx2D = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
-
-/**
- * Backing store for one surface paint: a detached canvas handed to the
- * mount's `ref` callback, which draws into it with any context type (2d,
- * webgl, webgpu). The engine samples the bitmap every frame and stretches it
- * into the parent geometry's box. After the initial box-sized allocation the
- * bitmap belongs to user code — external renderers (e.g. three.js via
- * `renderer.setSize`) resize it through their own APIs, so the engine never
- * touches it again.
- */
-export class SurfaceHost {
- public readonly canvas = document.createElement('canvas');
- private disposed = false;
-
- /** Initial bitmap allocation — called once at materialization, before the ref runs. */
- public setSize(width: number, height: number): void {
- this.canvas.width = Math.max(1, Math.round(width));
- this.canvas.height = Math.max(1, Math.round(height));
- }
-
- public draw(ctx: Ctx2D, width: number, height: number): void {
- if (this.disposed || this.canvas.width === 0 || this.canvas.height === 0) return;
- ctx.drawImage(this.canvas, 0, 0, width, height);
- }
-
- public dispose(): void {
- if (this.disposed) return;
- this.disposed = true;
- // Release the backing store; context teardown (e.g. a WebGL renderer)
- // is the mount's own onCleanup concern.
- this.canvas.width = 0;
- this.canvas.height = 0;
- }
-}
diff --git a/apps/web/src/components/engine/decoders/time-stretcher.ts b/apps/web/src/components/engine/decoders/time-stretcher.ts
deleted file mode 100644
index 4af25cf5..00000000
--- a/apps/web/src/components/engine/decoders/time-stretcher.ts
+++ /dev/null
@@ -1,260 +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/. */
-
-
-/**
- * Utility class for performing time stretching on a multi-channel audio signal. The input audio will be stretched by
- * a configurable factor without changing its pitch.
- *
- * Internally, this uses a WSOLA-like algorithm.
- */
-export class TimeStretcher {
- factor: number; // This value can be changed at runtime
-
- private numberOfChannels: number;
- private synthesisHopSize: number;
- private windowSize: number;
- private overlapSize: number;
- private tolerance: number;
- private buffers: Float32Array[];
- private bufferEndIndex: number;
- private outputBuffers: Float32Array[];
- private nextOutputBufferShiftAmount: number;
- private outputBufferLength: number;
- private hasDoneOutput: boolean;
- private finalized: boolean;
- private blendValues: number[];
-
- public constructor(numberOfChannels: number, sampleRate: number, factor: number) {
- this.numberOfChannels = numberOfChannels;
- this.synthesisHopSize = Math.floor((512 * sampleRate) / 48000);
- this.windowSize = 2 * this.synthesisHopSize;
- this.overlapSize = this.synthesisHopSize;
- this.tolerance = this.synthesisHopSize;
- this.buffers = [];
- this.bufferEndIndex = this.tolerance;
- this.factor = factor;
- this.outputBuffers = [];
- this.nextOutputBufferShiftAmount = 0;
- this.outputBufferLength = 2 ** 16;
- this.hasDoneOutput = false;
- this.finalized = false;
-
- for (let i = 0; i < numberOfChannels; i++) {
- this.buffers.push(new Float32Array(2 ** 16));
- this.outputBuffers.push(new Float32Array(this.outputBufferLength));
- }
-
- this.blendValues = [];
- for (let i = 0; i < this.overlapSize; i++) {
- this.blendValues[i] = 0.5 * (1 - Math.cos((Math.PI * i) / this.overlapSize));
- }
- }
-
- private ensureOutputBufferLength(requiredLength: number): void {
- if (requiredLength > this.outputBufferLength) {
- this.outputBufferLength = requiredLength;
- for (let i = 0; i < this.numberOfChannels; i++) {
- const largerBuffer = new Float32Array(this.outputBufferLength);
- largerBuffer.set(this.outputBuffers[i], 0);
- this.outputBuffers[i] = largerBuffer;
- }
- }
- }
-
- private clearOutputBuffers(): void {
- if (this.nextOutputBufferShiftAmount > 0) {
- for (let i = 0; i < this.numberOfChannels; i++) {
- const buffer = this.outputBuffers[i];
- buffer.set(buffer.subarray(this.nextOutputBufferShiftAmount), 0);
- }
-
- this.nextOutputBufferShiftAmount = 0;
- }
- }
-
- private synthesizeSegment(
- i: number,
- windowSize: number,
- inputStartPos: number,
- maxPositiveOffset: number = this.tolerance
- ): void {
- const crossCorrelateAllChannels = (k: number): number => {
- let dot = 0;
- let normOldTotal = 0;
- let normNewTotal = 0;
-
- for (let chan = 0; chan < this.numberOfChannels; chan++) {
- const inputBuffer = this.buffers[chan];
- const outputBuffer = this.outputBuffers[chan];
-
- for (let j = 0; j < this.overlapSize; j++) {
- const oldValue = outputBuffer[i + j];
- const newValue = inputBuffer[inputStartPos + k + j];
-
- dot += oldValue * newValue;
- normOldTotal += oldValue ** 2;
- normNewTotal += newValue ** 2;
- }
- }
-
- const ncc = dot / (Math.sqrt(normOldTotal * normNewTotal) || 1e-10);
- return ncc;
- };
-
- let bestCorr = -Infinity;
- let bestOffset = 0;
-
- if (!this.hasDoneOutput || this.factor === 1) {
- bestOffset = 0;
- } else {
- let k = -this.tolerance;
- bestCorr = crossCorrelateAllChannels(k);
- bestOffset = k;
-
- const minStepSize = 1;
- const maxStepSize = 16;
- let prevCorr = bestCorr;
-
- while (k < maxPositiveOffset) {
- const nextK = Math.min(k + 1, maxPositiveOffset);
- const corr = crossCorrelateAllChannels(nextK);
-
- const gradient = Math.abs(corr - prevCorr);
- const adaptiveStep = Math.max(
- minStepSize,
- Math.min(maxStepSize, Math.floor(maxStepSize * Math.exp(-gradient * 3)))
- );
-
- if (corr > bestCorr) {
- bestCorr = corr;
- bestOffset = nextK;
- }
-
- prevCorr = corr;
- k += adaptiveStep;
- }
-
- // Fine-tuning phase
- const fineRange = 8;
- for (k = bestOffset - fineRange; k <= bestOffset + fineRange; k++) {
- if (k >= -this.tolerance && k <= maxPositiveOffset) {
- const corr = crossCorrelateAllChannels(k);
- if (corr > bestCorr) {
- bestCorr = corr;
- bestOffset = k;
- }
- }
- }
- }
-
- for (let chan = 0; chan < this.numberOfChannels; chan++) {
- const inputBuffer = this.buffers[chan];
- const outputBuffer = this.outputBuffers[chan];
-
- for (let j = 0; j < windowSize; j++) {
- const blendValue = j < this.overlapSize ? this.blendValues[j] : 1;
- outputBuffer[i + j] *= 1 - blendValue;
- outputBuffer[i + j] += blendValue * inputBuffer[inputStartPos + bestOffset + j];
- }
- }
-
- this.hasDoneOutput = true;
- }
-
- public append(newBuffers: Float32Array[]): Float32Array[] | null {
- if (this.finalized) {
- throw new Error('Cannot append more buffers after calling finalize.');
- }
-
- // Fast path: when factor is 1, pass through directly without any processing
- if (this.factor === 1) {
- this.hasDoneOutput = true;
- return newBuffers;
- }
-
- for (let i = 0; i < this.numberOfChannels; i++) {
- const newBuffer = newBuffers[i];
- const requiredLength = this.bufferEndIndex + newBuffer.length;
- let currentLength = this.buffers[i].length;
-
- while (currentLength < requiredLength) {
- currentLength *= 2;
- }
-
- if (currentLength !== this.buffers[i].length) {
- const largerBuffer = new Float32Array(currentLength);
- largerBuffer.set(this.buffers[i], 0);
- this.buffers[i] = largerBuffer;
- }
-
- this.buffers[i].set(newBuffer, this.bufferEndIndex);
- }
-
- this.bufferEndIndex += newBuffers[0].length;
-
- return this.process();
- }
-
- private process(): Float32Array[] | null {
- let synthesisLength = 0;
- // eslint-disable-next-line no-constant-condition
- for (synthesisLength; true; synthesisLength += this.synthesisHopSize) {
- const inputShiftAmount = Math.floor(synthesisLength / this.factor);
-
- if (inputShiftAmount + this.windowSize + 2 * this.tolerance > this.bufferEndIndex) {
- synthesisLength -= this.synthesisHopSize;
- break;
- }
- }
-
- if (synthesisLength <= 0) {
- return null;
- }
-
- const inputShiftAmount = Math.floor(synthesisLength / this.factor);
- const outputLength = synthesisLength - this.synthesisHopSize + this.windowSize;
-
- this.clearOutputBuffers();
- this.ensureOutputBufferLength(outputLength);
-
- for (let i = 0; i < synthesisLength; i += this.synthesisHopSize) {
- const inputStartPos = this.tolerance + Math.floor(i / this.factor);
- this.synthesizeSegment(i, this.windowSize, inputStartPos);
- }
-
- // Shift all input buffers
- for (let chan = 0; chan < this.numberOfChannels; chan++) {
- const inputBuffer = this.buffers[chan];
- inputBuffer.set(inputBuffer.subarray(inputShiftAmount, this.bufferEndIndex), 0);
- }
-
- this.bufferEndIndex -= inputShiftAmount;
- this.nextOutputBufferShiftAmount = synthesisLength;
-
- return this.outputBuffers.map(x => x.subarray(0, synthesisLength));
- }
-
- public finalize(): Float32Array[] | null {
- this.finalized = true;
-
- // Fast path: when factor is 1, nothing is buffered
- if (this.factor === 1) {
- return null;
- }
-
- if (this.bufferEndIndex <= this.tolerance) {
- return null;
- }
-
- this.clearOutputBuffers();
-
- const synthesisLength = this.bufferEndIndex - this.tolerance;
- this.ensureOutputBufferLength(synthesisLength);
-
- this.synthesizeSegment(0, synthesisLength, this.tolerance, 0);
-
- return this.outputBuffers.map(x => x.subarray(0, synthesisLength));
- }
-}
diff --git a/apps/web/src/components/engine/decoders/video.ts b/apps/web/src/components/engine/decoders/video.ts
deleted file mode 100644
index 0db4d0b0..00000000
--- a/apps/web/src/components/engine/decoders/video.ts
+++ /dev/null
@@ -1,732 +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 { BlobSource, ALL_FORMATS, Input, InputVideoTrack, EncodedPacketSink, EncodedPacket, CanvasSink, type WrappedCanvas } from 'mediabunny';
-import { addComponent } from '../api/events';
-import { getAssetFile } from '../api/assets';
-import { assert } from '@/utils/common';
-import { FrameCache } from './frame-cache';
-import { getKeyframeIndex } from './keyframe-index';
-
-import type { KeyframeIndex } from './keyframe-index';
-import type { EngineWorld } from '../api/world';
-import type { VideoAsset } from '../db';
-
-
-export type VideoBufferMode = 'discarded' | 'idle' | 'alive';
-
-/**
- * Inactivity window after which an `alive` buffer automatically drops to
- * `idle`, freeing its decoder and frame cache while keeping the display canvas.
- */
-const IDLE_TIMEOUT_MS = 10_000;
-
-/**
- * Max extra frames we are willing to decode forward from the live cursor in
- * order to avoid a keyframe lookup + decoder reseed. Higher = stronger bias.
- */
-const FORWARD_BIAS_FRAMES = 24;
-
-/**
- * Frames used to drain the decoder in case of a backward seek to
- * ensure the target frame is surfaced.
- */
-const DRAIN_FRAMES = 8;
-
-/**
- * How far the preview may drift from the requested frame. A neighbour this close
- * beats holding the last drawn frame while the exact one is still decoding.
- */
-const DISPLAY_TOLERANCE_FRAMES = 4;
-
-/**
- * Total pixel budget of the preview frame cache
- */
-const CACHE_PIXEL_BUDGET = 768 * 432 * 81; // 81 tiles at 768x432
-
-/**
- * Per-tile pixel cap (~720p) — enough detail for the preview canvas.
- */
-const MAX_TILE_PIXELS = 1280 * 720;
-
-/**
- * Two seeks arriving closer together than this are treated as one continuous drag.
- */
-const SCRUB_EVENT_WINDOW_MS = 250;
-
-/**
- * Quiet period after the last scrub seek before the exact frame is resolved.
- */
-const SCRUB_SETTLE_MS = 120;
-
-const MIN_CACHE_COUNT = 30;
-const MAX_CACHE_COUNT = 81;
-
-export class VideoBuffer {
- public errored = false;
- public asset: VideoAsset;
- public firstPacketTimestamp = 0;
- public packetSink: EncodedPacketSink | null = null;
- public mode: VideoBufferMode = 'alive';
- public initialized: Promise<void>;
-
- public readonly cache: FrameCache;
- public readonly queue = new VideoDecoderQueue(this.frameCallback.bind(this));
-
- // user facing display canvas
- public readonly canvas = new OffscreenCanvas(0, 0);
- public readonly ctx = this.canvas.getContext('2d')!;
-
- private currentFrame: number = -1;
- private isDirty: boolean = true;
- private keyframes: KeyframeIndex | null = null;
-
- /**
- * Frame the preview is aiming to show. Tracks `currentFrame` except while scrubbing,
- * where it points at the covering keyframe instead of the requested frame.
- */
- private displayFrame: number = -1;
-
- /**
- * Keyframes a scrub is waiting on. The preview keeps showing the last one that landed
- * until the next arrives, so a drag never blanks out for the length of a decode.
- * Holds several at once: decoder output trails submission by a handful of packets.
- */
- private readonly pendingScrub = new Set<number>();
-
- private lastFrameIndex: number = 0;
- private seekGeneration = 0;
- private seekLock: Promise<void> = Promise.resolve();
- private iterator: AsyncGenerator<EncodedPacket, void, unknown> | null = null;
- private idleTimer: ReturnType<typeof setTimeout> | null = null;
- private settleTimer: ReturnType<typeof setTimeout> | null = null;
- private lastSeekAt: number = -Infinity;
-
- public constructor(asset: VideoAsset) {
- this.asset = asset;
-
- const pixels = Math.min(asset.width * asset.height, MAX_TILE_PIXELS);
- const count = Math.min(MAX_CACHE_COUNT, Math.max(MIN_CACHE_COUNT, Math.floor(CACHE_PIXEL_BUDGET / pixels)));
- this.cache = new FrameCache({ pixels, count });
-
- this.initialized = this.initialize();
- }
-
- private async initialize() {
- try {
- const videoTrack = await getVideoTrack(this.asset);
-
- assert(videoTrack, 'Video track not found');
-
- this.keyframes = getKeyframeIndex(this.asset.id, videoTrack);
-
- await this.queue.init(videoTrack);
-
- this.cache.rotation = videoTrack.rotation;
- this.packetSink = new EncodedPacketSink(videoTrack);
- this.firstPacketTimestamp = Math.max(0, await videoTrack.getFirstTimestamp() ?? 0);
- this.lastFrameIndex = Math.max(0, Math.round(this.asset.duration * this.asset.frameRate) - 1);
- this.isDirty = true;
- } catch (e) {
- console.error('Error initializing video decoder', e);
- this.errored = true;
- }
- }
-
- private frameCallback(frame: VideoFrame) {
- const timestampSeconds = frame.timestamp / 1e6;
- const frameIndex = this.secondsToFrames(timestampSeconds);
- this.cache.insert(frame, frameIndex);
- this.isDirty = true;
-
- // A scrub keyframe takes over the preview the moment it lands, and only then:
- // matching on the index keeps frames from an abandoned fill out of the display.
- if (this.pendingScrub.delete(frameIndex)) {
- this.displayFrame = frameIndex;
- }
- }
-
- public seekTo(frame: number, frameRate: number): undefined {
- const targetFrame = Math.round((frame / frameRate) * this.asset.frameRate);
-
- const isEmpty = !this.packetSink;
- const isCurrentFrame = targetFrame === this.currentFrame;
- const isCached = this.cache.has(targetFrame) && isCurrentFrame;
- const isIdle = this.mode === 'idle' && isCurrentFrame;
- const errored = this.errored;
-
- if (isEmpty || isCurrentFrame || isCached || isIdle || errored) {
- return;
- }
-
- const previousFrame = this.currentFrame;
- const consecutive = performance.now() - this.lastSeekAt < SCRUB_EVENT_WINDOW_MS;
- this.lastSeekAt = performance.now();
-
- this.currentFrame = targetFrame;
- this.isDirty = true;
-
- this.touch();
-
- // A run of large jumps is a drag, not a playback step. Walking each GOP to the
- // exact frame costs more than the gap between pointer events, so every walk gets
- // cancelled by the next one and nothing ever paints.
- const jumped = Math.abs(targetFrame - previousFrame) > FORWARD_BIAS_FRAMES;
-
- if (consecutive && jumped && this.scrubTo(targetFrame)) {
- return;
- }
-
- this.exactSeekTo(targetFrame, previousFrame);
- }
-
- /**
- * Paints the keyframe covering `targetFrame` rather than the frame itself: one packet,
- * no walk through the GOP. Returns false when the keyframe index cannot place the
- * target yet, leaving the caller to fall back to an exact seek.
- */
- private scrubTo(targetFrame: number): boolean {
- const keyTimestamp = this.keyframes?.floor(this.framesToSeconds(targetFrame)) ?? null;
-
- if (keyTimestamp === null) {
- return false;
- }
-
- const keyFrame = this.secondsToFrames(keyTimestamp);
- this.scheduleSettle();
-
- // Supersedes any fill still running for a position we have already left.
- const generation = ++this.seekGeneration;
-
- // Consecutive positions inside one GOP resolve to a keyframe we already hold.
- if (this.cache.has(keyFrame)) {
- this.displayFrame = keyFrame;
- return true;
- }
-
- this.pendingScrub.add(keyFrame);
-
- this.seekLock = this.seekLock
- .then(() => this.decodeKeyframe(keyTimestamp, generation))
- .catch(() => { });
-
- return true;
- }
-
- private async decodeKeyframe(keyTimestamp: number, generation: number): Promise<void> {
- if (generation !== this.seekGeneration) return;
-
- const keyPacket = await this.packetSink?.getKeyPacket(keyTimestamp);
- if (!keyPacket || generation !== this.seekGeneration) return;
-
- // Seed the iterator here as well, so the settle pass continues forward from this
- // keyframe instead of reseeding the decoder a second time.
- await this.iterator?.return();
- const iterator = this.packetSink?.packets(keyPacket) ?? null;
- this.iterator = iterator;
- if (!iterator) return;
-
- if (!this.queue.isAlive) {
- this.queue.reseed();
- }
-
- const { value: packet } = await iterator.next();
- if (packet) {
- await this.queue.decode(packet);
- }
- }
-
- /**
- * Re-runs the seek for real once the drag stops.
- */
- private scheduleSettle() {
- if (this.settleTimer !== null) {
- clearTimeout(this.settleTimer);
- }
-
- this.settleTimer = setTimeout(() => {
- this.settleTimer = null;
- if (this.mode !== 'alive' || this.errored) return;
- this.exactSeekTo(this.currentFrame, this.currentFrame);
- }, SCRUB_SETTLE_MS);
- }
-
- private exactSeekTo(targetFrame: number, previousFrame: number) {
- this.displayFrame = targetFrame;
- this.pendingScrub.clear();
-
- const forward = this.isBlockedFrame(targetFrame)
- ? targetFrame >= previousFrame
- : true;
-
- const generation = ++this.seekGeneration;
-
- const [left, right] = this.computeWindow(targetFrame, forward);
- this.cache.leftFrameIndex = left;
- this.cache.rightFrameIndex = right;
-
- let seed = targetFrame;
- let run: Promise<void>
- if (forward) {
- while (this.isBlockedFrame(seed) && seed <= this.cache.rightFrameIndex) {
- seed++;
- }
-
- run = this.seekLock.then(() => this.fillCache([seed, this.cache.rightFrameIndex], generation));
- } else {
- while (this.cache.has(seed) && seed >= this.cache.leftFrameIndex) {
- seed--;
- }
-
- // TODO: Find a better solution for DRAIN_FRAMES
- run = this.seekLock.then(() => this.fillCache([this.cache.leftFrameIndex, seed + DRAIN_FRAMES], generation));
- }
-
- // Run after the previous seek finishes — never concurrently.
- this.seekLock = run.catch(() => { });
- }
-
- private framesToSeconds(frames: number) {
- return Math.max(this.firstPacketTimestamp, (frames / this.asset.frameRate) + this.firstPacketTimestamp);
- }
-
- private secondsToFrames(seconds: number) {
- return Math.max(0, Math.round((seconds - this.firstPacketTimestamp) * this.asset.frameRate));
- }
-
- /**
- * Whether `frame` is already taken care of — decoded into the cache, or still in-flight.
- */
- private isBlockedFrame(frame: number) {
- if (this.cache.has(frame)) return true;
-
- for (const micros of this.queue.inFlight) {
- if (this.secondsToFrames(micros / 1e6) === frame) {
- return true;
- }
- }
-
- return false;
- }
-
- private computeWindow(targetFrame: number, forward: boolean): [number, number] {
- const span = this.cache.config.count - 2;
-
- // Whole video fits in the cache — keep all of it.
- if (this.lastFrameIndex <= span) {
- return [0, this.lastFrameIndex];
- }
-
- const ahead = Math.round((span * 2) / 3);
- const behind = span - ahead;
-
- let left = targetFrame - (forward ? behind : ahead);
- let right = targetFrame + (forward ? ahead : behind);
-
- // Push budget that falls outside the boundaries onto the other side.
- if (left < 0) {
- right -= left;
- left = 0;
- }
- if (right > this.lastFrameIndex) {
- left -= right - this.lastFrameIndex;
- right = this.lastFrameIndex;
- }
-
- return [Math.max(0, left), Math.min(this.lastFrameIndex, right)];
- }
-
- private async fillCache(range: [number, number], generation: number): Promise<void> {
- if (generation !== this.seekGeneration || Math.abs(range[1] - range[0]) <= 3) return;
- // Note: only decode for three or more frames at a time to avoid unnecessary reseeding
-
- const fromSecs = this.framesToSeconds(range[0]);
- const untilSecs = this.framesToSeconds(range[1]);
- const cursor = this.queue.lastSubmitted;
- const live = !!(this.iterator && this.queue.isAlive && cursor);
-
- let reuse = live && cursor!.timestamp < untilSecs;
- let keyTimestamp = this.keyframes?.floor(fromSecs) ?? null;
-
- // If the cursor lags far behind the range start, skip forward to the nearest
- // keyframe rather than decoding through the whole gap.
- const biasSecs = FORWARD_BIAS_FRAMES / this.asset.frameRate;
- if (reuse && cursor!.timestamp < fromSecs - biasSecs) {
- keyTimestamp ??= (await this.packetSink?.getKeyPacket(fromSecs))?.timestamp ?? null;
- if (keyTimestamp !== null && keyTimestamp - cursor!.timestamp > biasSecs) {
- reuse = false; // jumping to the keyframe
- }
- }
-
- // we cant reuse the iterator, so we need to reseed the decoder
- // and get create a new iterator
- if (!reuse) {
- const keyPacket = (await this.packetSink?.getKeyPacket(keyTimestamp ?? fromSecs)) ?? null;
- if (!keyPacket) return;
- await this.iterator?.return();
- this.iterator = this.packetSink?.packets(keyPacket) ?? null;
- }
-
- if (!this.queue.isAlive) {
- this.queue.reseed();
- }
-
- if (generation !== this.seekGeneration || !this.iterator) return;
-
- while (true) {
- const { value: packet, done } = await this.iterator.next();
- if (done || !packet) break;
-
- await this.queue.decode(packet);
-
- if (generation !== this.seekGeneration || packet.timestamp >= untilSecs) break;
- }
- }
-
- public toBitmap() {
- if (this.isDirty && this.displayFrame >= 0) {
-
- const nearest = this.cache.findNearest(this.displayFrame, DISPLAY_TOLERANCE_FRAMES);
- const tile = nearest === undefined ? undefined : this.cache.findTile(nearest);
- const ctx = this.ctx;
-
- if (tile && (this.canvas.width !== tile.width || this.canvas.height !== tile.height)) {
- this.canvas.width = tile.width;
- this.canvas.height = tile.height;
- this.ctx.imageSmoothingEnabled = false;
- }
-
- if (tile) {
- ctx.drawImage(
- this.cache.atlas,
- tile.x,
- tile.y,
- tile.width,
- tile.height,
- 0,
- 0,
- tile.width,
- tile.height,
- );
- this.isDirty = false;
- }
- }
-
- if (this.canvas.width === 0 || this.canvas.height === 0) {
- return null;
- }
-
- return this.canvas;
- }
-
- private touch() {
- this.mode = 'alive';
- if (this.idleTimer !== null) {
- clearTimeout(this.idleTimer);
- }
- this.idleTimer = setTimeout(() => this.idle(), IDLE_TIMEOUT_MS);
- }
-
- public idle() {
- if (this.mode !== 'alive') return;
- this.mode = 'idle';
-
- if (this.idleTimer !== null) {
- clearTimeout(this.idleTimer);
- this.idleTimer = null;
- }
-
- if (this.settleTimer !== null) {
- clearTimeout(this.settleTimer);
- this.settleTimer = null;
- }
-
- this.pendingScrub.clear();
- this.cache.dispose();
- this.queue.dispose();
- this.iterator?.return();
- this.iterator = null;
- }
-
- public dispose() {
- if (this.mode === 'discarded') return;
- this.mode = 'discarded';
-
- if (this.idleTimer !== null) {
- clearTimeout(this.idleTimer);
- this.idleTimer = null;
- }
-
- if (this.settleTimer !== null) {
- clearTimeout(this.settleTimer);
- this.settleTimer = null;
- }
-
- this.pendingScrub.clear();
- this.cache.dispose();
- this.queue.dispose();
- this.iterator?.return();
- this.iterator = null;
-
- // Release the display canvas backing store.
- this.canvas.width = 0;
- this.canvas.height = 0;
- }
-}
-
-class VideoDecoderQueue {
- private config: VideoDecoderConfig | null = null;
- private decoder: VideoDecoder | null = null;
- private resolver: ReturnType<typeof Promise.withResolvers> | null = null;
- private callback: (frame: VideoFrame) => void;
- public lastSubmitted: EncodedPacket | null = null;
- public readonly inFlight = new Set<number>();
-
- public constructor(callback: (frame: VideoFrame) => void) {
- this.callback = callback;
- }
-
- public get isAlive() {
- return this.decoder?.state === 'configured';
- }
-
- /**
- * Synchronously discard all in-flight work and re-arm the decoder for a fresh keyframe
- */
- public reseed() {
- if (!this.decoder) return;
- this.decoder.reset();
- assert(this.config, 'Decoder config not available');
- this.decoder.configure(this.config);
- this.resolver?.resolve(null);
- this.resolver = null;
- this.lastSubmitted = null;
- this.inFlight.clear();
- }
-
- private handleOutput(frame: VideoFrame) {
- this.resolver?.resolve(null);
- this.inFlight.delete(frame.timestamp);
-
- for (const micros of this.inFlight) {
- if (micros < frame.timestamp) {
- this.inFlight.delete(micros);
- }
- }
-
- this.callback(frame);
- frame.close();
- }
-
- private handleDequeue() {
- this.resolver?.resolve(null);
- this.resolver = null;
- };
-
- private handleError(e?: DOMException) {
- console.error(e?.message);
- this.dispose();
- }
-
- public async init(track: InputVideoTrack) {
- this.config = await track.getDecoderConfig();
- assert(this.config, 'Failed to get decoder config from track');
- const support = await VideoDecoder.isConfigSupported(this.config);
- assert(support.supported, 'Decoder config not supported');
- }
-
- private ensureDecoder(packet: EncodedPacket) {
- if (this.decoder || packet.type === 'delta') return;
-
- assert(this.config, 'Decoder config not available');
-
- this.decoder = new VideoDecoder({
- error: this.handleError.bind(this),
- output: this.handleOutput.bind(this),
- });
-
- this.decoder.addEventListener('dequeue', this.handleDequeue.bind(this));
- this.decoder.configure(this.config);
- }
-
- public async decode(packet: EncodedPacket) {
- this.ensureDecoder(packet);
-
- if (this.decoder?.state !== 'configured') {
- this.lastSubmitted = null;
- this.resolver?.resolve(null);
- this.resolver = null;
- this.inFlight.clear();
- return;
- }
-
- if (this.decoder.decodeQueueSize > 2) {
- this.resolver = Promise.withResolvers();
- }
-
- this.decoder.decode(packet.toEncodedVideoChunk());
- this.lastSubmitted = packet;
- this.inFlight.add(packet.microsecondTimestamp);
-
- await this.resolver?.promise;
- }
-
- public dispose() {
- try {
- this.decoder?.close();
- } catch { /* ignore */ }
- this.resolver?.resolve(null);
- this.decoder = null;
- this.lastSubmitted = null;
- this.resolver = null;
- this.inFlight.clear();
- }
-}
-
-/**
- * Dedicated, full-resolution video decoder for export.
- */
-export class VideoExporter {
- public errored = false;
- public asset: VideoAsset;
- public initialized: Promise<void>;
-
- private input: Input | null = null;
- private canvasSink: CanvasSink | null = null;
- private iterator: AsyncGenerator<WrappedCanvas, void, unknown> | null = null;
- private currentCanvas: WrappedCanvas | null = null;
- private firstTimestamp: number = 0;
-
- public constructor(asset: VideoAsset) {
- this.asset = asset;
- this.initialized = this.initialize();
- }
-
- private async initialize() {
- try {
- const blob = await getAssetFile(this.asset);
- this.input = new Input({ formats: ALL_FORMATS, source: new BlobSource(blob) });
- const track = await this.input.getPrimaryVideoTrack();
- assert(track, 'Video track not found');
- // See VideoBuffer.initialize: clamp so an edit-list head trim (negative first
- // timestamp) doesn't offset every exported frame relative to the audio.
- this.firstTimestamp = Math.max(0, await track.getFirstTimestamp() ?? 0);
- this.canvasSink = new CanvasSink(track, { poolSize: 2 });
- } catch (e) {
- console.error('Error initializing video exporter', e);
- this.errored = true;
- }
- }
-
- public async seekTo(frame: number, frameRate: number): Promise<void> {
- await this.initialized;
-
- if (this.errored || !this.canvasSink) return;
-
- const targetFrame = Math.round((frame / frameRate) * this.asset.frameRate);
- const lastFrame = this.currentCanvas
- ? this.secondsToFrames(this.currentCanvas.timestamp)
- : -1;
-
- if (targetFrame === lastFrame) return;
-
- if (!this.iterator || targetFrame < lastFrame) {
- this.iterator = this.canvasSink?.canvases(this.framesToSeconds(targetFrame)) ?? null;
- }
- if (!this.iterator) return;
-
- // Walk forward while the lookahead frame still starts at/before target.
- while (true) {
- const { value, done } = await this.iterator.next();
- if (done || !value) break;
-
- this.currentCanvas = value;
-
- if (this.secondsToFrames(value.timestamp) >= targetFrame) {
- break;
- }
- }
- }
-
- private framesToSeconds(frames: number) {
- return Math.max(this.firstTimestamp, (frames / this.asset.frameRate) + this.firstTimestamp);
- }
-
- private secondsToFrames(seconds: number) {
- return Math.max(0, Math.round((seconds - this.firstTimestamp) * this.asset.frameRate));
- }
-
- public toBitmap(): HTMLCanvasElement | OffscreenCanvas | null {
- if (!this.currentCanvas) {
- return null;
- }
-
- return this.currentCanvas.canvas;
- }
-
- public idle(): void { }
-
- public dispose(): void {
- this.iterator?.return();
- this.iterator = null;
- this.input?.dispose();
- }
-}
-
-export type VideoDecoderInstance = VideoBuffer | VideoExporter;
-
-const videoTrackCache = new Map<string, Promise<InputVideoTrack | null>>();
-
-export function clearVideoTrackCache() {
- videoTrackCache.clear();
-}
-
-export function getVideoTrack(source: VideoAsset) {
- let promise = videoTrackCache.get(source.id);
- if (promise) {
- return promise;
- }
-
- promise = (async () => {
- try {
- const blob = await getAssetFile(source);
- const input = new Input({
- formats: ALL_FORMATS,
- source: new BlobSource(blob)
- });
- return await input.getPrimaryVideoTrack();
- } catch {
- videoTrackCache.delete(source.id);
- return null;
- }
- })();
-
- videoTrackCache.set(source.id, promise);
- return promise;
-}
-
-export function resolveVideoDecoder(world: EngineWorld, fid: number): VideoDecoderInstance | null {
- const AssetId = world.components.AssetId;
- const assetId = AssetId[fid];
- if (!assetId) return null;
-
- const existing = world.components.VideoDecoder[fid];
- if (existing && existing.asset.id === assetId) {
- return existing;
- }
-
- // Asset changed — dispose old decoder and create a new one.
- existing?.dispose();
-
- const asset = world.assets.get(assetId);
- if (!asset || asset.type !== 'VIDEO') return null;
-
- addComponent(world, fid, world.components.VideoDecoder, false);
-
- const decoder = world.mode === 'realtime'
- ? new VideoBuffer(asset)
- : new VideoExporter(asset);
- world.components.VideoDecoder[fid] = decoder;
- return decoder;
-}
diff --git a/apps/web/src/components/engine/encode/buffer.ts b/apps/web/src/components/engine/encode/buffer.ts
deleted file mode 100644
index 72705ae7..00000000
--- a/apps/web/src/components/engine/encode/buffer.ts
+++ /dev/null
@@ -1,50 +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 { BufferTarget, StreamTarget, Target } from 'mediabunny';
-
-import type { ContainerFormat, WritableFileTarget } from './types';
-
-export class TargetBuffer {
- public target: Target;
- public fastStart: false | 'in-memory';
- public handle: FileSystemFileHandle | WritableFileTarget | undefined;
-
- public constructor(
- target: StreamTarget | BufferTarget,
- fastStart: false | 'in-memory',
- handle: FileSystemFileHandle | WritableFileTarget | undefined
- ) {
- this.target = target;
- this.fastStart = fastStart;
- this.handle = handle;
- }
-
- public static async create(
- handle?: FileSystemFileHandle | WritableFileTarget
- ): Promise<TargetBuffer> {
- if (handle) {
- const fileStream = await handle.createWritable();
- return new TargetBuffer(new StreamTarget(fileStream, { chunked: true }), false, handle);
- }
-
- return new TargetBuffer(new BufferTarget(), 'in-memory', undefined);
- }
-
- public async close(format: ContainerFormat = 'mp4'): Promise<Blob | undefined> {
- if (this.fastStart === 'in-memory' && this.target instanceof BufferTarget) {
- if (!this.target.buffer) return;
- return new Blob([this.target.buffer], { type: mimeTypes[format] });
- }
-
- return undefined;
- }
-}
-
-const mimeTypes: Record<ContainerFormat, string> = {
- mp4: 'video/mp4',
- webm: 'video/webm',
- ogg: 'audio/ogg',
- mov: 'video/quicktime',
-};
diff --git a/apps/web/src/components/engine/encode/encoder.ts b/apps/web/src/components/engine/encode/encoder.ts
deleted file mode 100644
index aafb10bb..00000000
--- a/apps/web/src/components/engine/encode/encoder.ts
+++ /dev/null
@@ -1,486 +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 { framesToSeconds } from '../utils';
-import {
- CanvasSource,
- Output,
- AudioSample,
- AudioSampleSource,
-} from 'mediabunny';
-import { TargetBuffer } from './buffer';
-import { createOutputFormat } from './format';
-import { assert } from '@/utils';
-import { createRenderEventDetail } from './utils';
-import { version } from '../../../../package.json';
-import { hasComponent, Hierarchy, Not, Or, query } from 'bitecs';
-
-import { ChildOf } from '../components';
-import { createEngineWorld, switchActiveScene } from '../api';
-import { recomputeEntityTimeRange } from '../api/utils';
-import { addComponent } from '../api/events';
-import { playbackSystem } from '../systems/playback';
-import { motionSystem } from '../systems/motion';
-import { transformSystem } from '../systems/transform';
-import { renderSystem } from '../systems/render';
-import { cloneFromRecords, serializeEntity } from '../api/serialize';
-import { getEntityTree } from '../api/query';
-import { AudioBus } from '../services/audio-bus';
-
-import type { EngineWorld } from '../api/world';
-import type { EncoderConfig } from './interfaces';
-import type { ExportResult } from './types';
-
-export async function createEncoder(sourceWorld: EngineWorld, config: EncoderConfig) {
- const sourceComponents = sourceWorld.components;
- const sourceSceneEid = config.scene;
- const frameRate = config.video?.fps ?? sourceWorld.frameRate;
- const numberOfChannels = config.audio?.numberOfChannels ?? 2;
- const sampleRate = config.audio?.sampleRate ?? 48000;
- const sceneWidth = sourceComponents.Computed.width[sourceSceneEid];
- const sceneHeight = sourceComponents.Computed.height[sourceSceneEid];
- const sceneEnd = sourceComponents.Computed.end[sourceSceneEid];
-
- // Honor the scene's Workarea component: render only the frames between
- // Workarea.start and Workarea.end instead of the full scene duration.
- const hasWorkarea = hasComponent(sourceWorld, sourceSceneEid, sourceComponents.Workarea);
- const workareaStart = hasWorkarea
- ? Math.max(0, Math.min(sceneEnd, sourceComponents.Workarea.start[sourceSceneEid] ?? 0))
- : 0;
- const workareaEnd = hasWorkarea
- ? Math.max(workareaStart, Math.min(sceneEnd, sourceComponents.Workarea.end[sourceSceneEid] ?? sceneEnd))
- : sceneEnd;
- const workareaFrames = workareaEnd - workareaStart;
-
- const playheadStartSeconds = framesToSeconds(workareaStart, sourceWorld.frameRate);
- const duration = framesToSeconds(workareaFrames, sourceWorld.frameRate);
- const audioBitrate = config.audio?.bitrate ?? 128e3;
- const videoBitrate = config.video?.bitrate ?? 10e6;
- const audioCodec = config.audio?.codec ?? 'aac';
- const resolution = config.video?.resolution ?? 1080;
- const scale = Math.round(resolution * 1e6 / sceneHeight) / 1e6;
- const width = Math.round(sceneWidth * scale / 2) * 2;
- const height = Math.round(sceneHeight * scale / 2) * 2;
- const videoCodec = config.video?.codec ?? 'avc';
- const containerFormat = config.format ?? 'mp4';
- const audioEnabled = config.audio?.enabled ?? true;
- const videoEnabled = config.video?.enabled ?? true;
- const frameDuration = 1 / frameRate;
-
- if (config.format === 'ogg') {
- config.audio = { ...config.audio, enabled: true };
- config.video = { ...config.video, enabled: false };
- }
-
- const sceneSubtree = getEntityTree(sourceWorld, sourceSceneEid);
- const subtreeRecords = sceneSubtree.map(eid => serializeEntity(sourceWorld, eid));
-
- // Offscreen canvas
- const offscreenCanvas = new OffscreenCanvas(width, height);
- const offscreenCtx = offscreenCanvas.getContext('2d')!;
-
- // Offline audio context
- const offlineAudioCtx = new OfflineAudioContext(
- numberOfChannels,
- Math.ceil(duration * sampleRate),
- sampleRate,
- );
-
- // Build a fresh offline world that shares assets with the source. Fonts are
- // already loaded into the document; copying the array just tracks them as
- // loaded so the new world doesn't try to re-add them.
- const world = createEngineWorld(sourceWorld.projectId, offlineAudioCtx);
- world.mode = videoEnabled ? 'offline-video' : 'offline-audio';
- world.assets = sourceWorld.assets;
- world.fonts = [...sourceWorld.fonts];
- world.ctx = offscreenCtx;
- world.canvas = offscreenCanvas;
- world.resolution = scale;
- world.camera = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 };
- world.frameRate = frameRate;
- world.promises = [];
-
- const eidMap = cloneFromRecords(world, subtreeRecords);
- const sceneEid = eidMap.get(sourceSceneEid);
- assert(sceneEid !== undefined, 'Failed to clone scene subtree');
-
- rescaleFrameTimestamps(world, sourceWorld.frameRate, frameRate);
- rescaleAllTimeRanges(world);
- normalizeSceneTransform(world, sceneEid);
-
- switchActiveScene(world, sceneEid);
-
- const c = world.components;
- // The workarea (Workarea component) is runtime-only and never serialized, so
- // it isn't cloned into the offline world — nothing to strip here. The encoder
- // drives the playhead explicitly within the [workareaStart, workareaEnd]
- // window computed from the source scene above.
- c.Computed.localTimeInSeconds[sceneEid] = playheadStartSeconds;
- c.Computed.localTime[sceneEid] = Math.round(playheadStartSeconds * frameRate);
- c.Playback.playing[sceneEid] = 1;
- c.Playback.loop[sceneEid] = 0;
- c.Playback.speed[sceneEid] = 1;
- // Anchor audio scheduling: context starts at t=0 but the playhead starts at
- // playheadStartSeconds, so audioDelay = -playheadStartSeconds shifts each
- // entity's scheduled audio back into the encoded window.
- c.AudioPlayback.contextOffsetInSeconds[sceneEid] = 0;
- c.AudioPlayback.timelineOffsetInSeconds[sceneEid] = playheadStartSeconds;
-
- // Set up mediabunny output
- const buffer = await TargetBuffer.create(config.target);
- const format = await createOutputFormat(buffer, config.format);
- const output = new Output({ format, target: buffer.target });
-
- output.setMetadataTags({
- comment: `Made with Diffusion v${version}`,
- });
-
- const audioSource = new AudioSampleSource({
- codec: audioCodec,
- bitrate: audioBitrate,
- });
-
- const sceneFills = [...query(world, [c.Paint, ChildOf(sceneEid), Not(c.Deleted)])];
-
- const videoSource = new CanvasSource(offscreenCanvas, {
- codec: videoCodec,
- bitrate: videoBitrate,
- latencyMode: 'quality',
- alpha: containerFormat === 'webm' && !sceneFills.length
- ? 'keep'
- : 'discard',
- });
-
- if (audioEnabled) {
- output.addAudioTrack(audioSource);
- }
-
- if (videoEnabled) {
- output.addVideoTrack(videoSource);
- }
-
- // Audio worklet streams render-quantum chunks back to the main thread so
- // we can hand them to mediabunny in step with the visual frame loop.
- const audioWorkletUrl = createAudioWorkletUrl();
- await offlineAudioCtx.audioWorklet.addModule(audioWorkletUrl);
-
- const sharedBuffer = new SharedArrayBuffer(4);
- const sharedUint32Array = new Uint32Array(sharedBuffer);
- const mixNode = offlineAudioCtx.createGain();
-
- const sinkNode = new AudioWorkletNode(offlineAudioCtx, 'sink', {
- channelCount: numberOfChannels,
- channelCountMode: 'explicit',
- numberOfInputs: 1,
- numberOfOutputs: 1,
- outputChannelCount: [numberOfChannels],
- processorOptions: {
- buffer: sharedUint32Array,
- },
- });
-
- mixNode.connect(sinkNode);
- sinkNode.connect(offlineAudioCtx.destination);
-
- // connect the scene bus to the mix node
- addComponent(world, sceneEid, c.AudioBus, false);
- const newbus = new AudioBus(world, sceneEid);
- c.AudioBus[sceneEid] = newbus;
- newbus.connect(mixNode);
-
- let sampleIndex = 0;
- let audioSourcePromise: Promise<void> | null = null;
-
- const allAudioReceived = Promise.withResolvers<void>();
-
- sinkNode.port.onmessage = event => {
- if (output?.state === 'canceled') {
- return;
- }
-
- const planarBuffer = event.data as Float32Array;
-
- if (planarBuffer.length === 0) {
- allAudioReceived.resolve();
- return;
- }
-
- const sampleCount = planarBuffer.length / numberOfChannels;
-
- const audioSample = new AudioSample({
- data: planarBuffer,
- format: 'f32-planar',
- numberOfChannels,
- sampleRate,
- timestamp: sampleIndex / sampleRate,
- });
- audioSourcePromise = audioSource.add(audioSample).finally(() => {
- audioSample.close();
- });
-
- sampleIndex += sampleCount;
- };
-
- const emitProgress = createThrottledCallback(config.onProgress, 1000 / 3); // 3 times per second
-
- let canceled = false;
- const cancel = () => (canceled = true);
-
- const render = async (): Promise<ExportResult> => {
- try {
- await output.start();
- const start = performance.now();
- const startTime = start;
- const totalFrames = Math.floor(duration * frameRate);
-
- let audioRenderingDone = false;
- let audioRenderingCompleted: Promise<AudioBuffer> | null = null;
- if (audioEnabled) {
- audioRenderingCompleted = offlineAudioCtx.startRendering();
- audioRenderingCompleted.then(() => { audioRenderingDone = true; });
- }
-
- let lastAudioSampleCount = 0;
-
- for (let frame = 0; frame < totalFrames; frame++) {
- if (canceled) {
- await output.cancel();
- Atomics.store(sharedUint32Array, 0, 2 ** 31 - 1);
- return { type: 'canceled' };
- }
-
- const timestamp = frame * frameDuration;
- const playheadSeconds = playheadStartSeconds + timestamp;
-
- // advancePlayhead is a no-op in offline mode; the encoder owns the
- // playhead and writes both seconds + frames before each tick.
- c.Computed.localTimeInSeconds[sceneEid] = playheadSeconds;
- c.Computed.localTime[sceneEid] = Math.round(playheadSeconds * frameRate);
- world.timestamp.delta = frameDuration * 1000;
- world.timestamp.now += world.timestamp.delta;
-
- {
- playbackSystem(world);
- await resolverSystem(world);
- motionSystem(world);
- }
-
- if (videoEnabled) {
- // only visual systems
- transformSystem(world);
- renderSystem(world);
- }
-
- if (videoEnabled) {
- await videoSource.add(timestamp, frameDuration);
- }
-
- if (audioEnabled) {
- await audioSourcePromise;
-
- while (Atomics.load(sharedUint32Array, 0) > sampleRate && !audioRenderingDone) {
- await new Promise(resolve => setTimeout(resolve, 0));
- }
-
- const totalAudioSampleCount = Math.floor(timestamp * sampleRate);
- const diff = totalAudioSampleCount - lastAudioSampleCount;
- Atomics.add(sharedUint32Array, 0, diff);
- lastAudioSampleCount = totalAudioSampleCount;
- }
-
- emitProgress(createRenderEventDetail(frame, totalFrames, startTime));
- }
-
- console.info(
- `Encoded frames at ${((totalFrames * 1000) / (performance.now() - start)).toFixed(2)}FPS`
- );
-
- if (audioEnabled) {
- assert(audioRenderingCompleted !== null, 'Audio rendering was not started');
-
- Atomics.add(
- sharedUint32Array,
- 0,
- Math.ceil(duration * sampleRate) + sampleRate - lastAudioSampleCount,
- );
-
- await audioRenderingCompleted;
-
- sinkNode.port.postMessage(null);
- await allAudioReceived.promise;
- }
-
- console.info('Finalizing file');
-
- await output.finalize();
-
- console.info('Export complete');
-
- return {
- type: 'success',
- data: await buffer.close(containerFormat),
- };
- } catch (e) {
- return {
- type: 'error',
- error: e instanceof Error ? e : new Error('Unknown error'),
- };
- } finally {
- URL.revokeObjectURL(audioWorkletUrl);
- }
- };
-
- return {
- render,
- cancel,
- };
-}
-
-
-function audioWorkletCode() {
- // eslint-disable-next-line no-undef
- class SinkProcessor extends AudioWorkletProcessor {
- buffer: Uint32Array;
-
- constructor(options: {
- processorOptions: {
- buffer: Uint32Array;
- };
- }) {
- super();
-
- this.buffer = options.processorOptions.buffer;
-
- this.port.onmessage = () => {
- this.port.postMessage(new Float32Array(0));
- };
- }
-
- process(inputs: Float32Array[][], outputs: Float32Array[][]) {
- const input = inputs[0];
- const output = outputs[0];
- const renderQuantum = output[0].length;
-
- while (Atomics.load(this.buffer, 0) < renderQuantum);
-
- const planarBuffer = new Float32Array(output.length * output[0].length);
- for (let channel = 0; channel < Math.min(input.length, output.length); channel++) {
- planarBuffer.set(input[channel], channel * output[0].length);
- }
-
- this.port.postMessage(planarBuffer, [planarBuffer.buffer]);
-
- Atomics.sub(this.buffer, 0, renderQuantum);
-
- return true;
- }
- }
-
- // eslint-disable-next-line no-undef
- registerProcessor('sink', SinkProcessor);
-}
-
-function createAudioWorkletUrl(): string {
- const blob = new Blob([`(${audioWorkletCode.toString()})()`], { type: 'application/javascript' });
- return URL.createObjectURL(blob);
-}
-
-function createThrottledCallback<T>(callback: ((value: T) => void) | undefined, intervalMs: number): (value: T) => void {
- let lastCalledAt = -Infinity;
-
- return (value: T) => {
- const now = performance.now();
- if (now - lastCalledAt < intervalMs) {
- return;
- }
-
- lastCalledAt = now;
- callback?.(value);
- };
-}
-
-function rescaleFrameTimestamps(
- world: EngineWorld,
- sourceFrameRate: number,
- targetFrameRate: number,
-): void {
- if (sourceFrameRate === targetFrameRate) return;
-
- const c = world.components;
- const ratio = targetFrameRate / sourceFrameRate;
- const rescale = (v: number) => Math.round(v * ratio);
-
- for (const eid of query(world, [Or(c.Delay, c.Trim, c.Transition, c.Keyframe, c.Animation)])) {
- if (hasComponent(world, eid, c.Delay)) {
- c.Delay[eid] = rescale(c.Delay[eid] ?? 0);
- }
- if (hasComponent(world, eid, c.Trim)) {
- c.Trim.start[eid] = rescale(c.Trim.start[eid] ?? 0);
- c.Trim.end[eid] = rescale(c.Trim.end[eid] ?? 0);
- }
- if (hasComponent(world, eid, c.Transition)) {
- c.Transition.duration[eid] = rescale(c.Transition.duration[eid] ?? 0);
- }
- if (hasComponent(world, eid, c.Keyframe)) {
- c.Keyframe.time[eid] = rescale(c.Keyframe.time[eid] ?? 0);
- }
- if (hasComponent(world, eid, c.Animation)) {
- c.Animation.duration[eid] = rescale(c.Animation.duration[eid] ?? 0);
- c.Animation.delay[eid] = rescale(c.Animation.delay[eid] ?? 0);
- }
- }
-}
-
-function normalizeSceneTransform(world: EngineWorld, sceneEid: number): void {
- const c = world.components;
- c.Position.x[sceneEid] = 0;
- c.Position.y[sceneEid] = 0;
- c.Offset.x[sceneEid] = 0;
- c.Offset.y[sceneEid] = 0;
- c.Rotation[sceneEid] = 0;
- c.Scale.x[sceneEid] = 1;
- c.Scale.y[sceneEid] = 1;
- c.Skew.x[sceneEid] = 0;
- c.Skew.y[sceneEid] = 0;
- // computeLocalMatrix reads the Computed.* mirror, not the raw components.
- // The base components are populated via setComponent during the clone, which
- // fires the observer that copies the (canvas) position into Computed. Writing
- // the raw arrays above bypasses that observer, so the Computed mirror must be
- // reset here too — otherwise the scene renders at its canvas position instead
- // of centered in the export.
- c.Computed.positionX[sceneEid] = 0;
- c.Computed.positionY[sceneEid] = 0;
- c.Computed.offsetX[sceneEid] = 0;
- c.Computed.offsetY[sceneEid] = 0;
- c.Computed.rotation[sceneEid] = 0;
- c.Computed.scaleX[sceneEid] = 1;
- c.Computed.scaleY[sceneEid] = 1;
- c.Computed.skewX[sceneEid] = 0;
- c.Computed.skewY[sceneEid] = 0;
-}
-
-async function resolverSystem(world: EngineWorld) {
- if (world.promises?.length) {
- await Promise.all(world.promises.filter(promise => promise !== null));
- world.promises = [];
- }
-}
-
-function rescaleAllTimeRanges(world: EngineWorld): void {
- const c = world.components;
- // TODO: Find a better solution for this
- // Hierarchy(ChildOf) yields parents before children (top-down). A single pass
- // is wrong for group-like nodes: their bounds aggregate from children, but the
- // children haven't been rescaled yet when the parent is visited, so the group
- // would inherit stale (pre-rescale) child bounds. Do two passes:
- // 1. top-down — refresh accumulated delay + leaf/Trim bounds (parents first)
- // 2. bottom-up — groups now aggregate from already-rescaled children
- const entities = query(world, [Or(c.Geometry, c.Group), Hierarchy(ChildOf), Not(c.Deleted)]);
- for (let i = 0; i < entities.length; i++) {
- recomputeEntityTimeRange(world, entities[i]);
- }
- for (let i = entities.length - 1; i >= 0; i--) {
- recomputeEntityTimeRange(world, entities[i]);
- }
-}
diff --git a/apps/web/src/components/engine/encode/format.ts b/apps/web/src/components/engine/encode/format.ts
deleted file mode 100644
index 1b831363..00000000
--- a/apps/web/src/components/engine/encode/format.ts
+++ /dev/null
@@ -1,25 +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 type { TargetBuffer } from './buffer';
-import type { ContainerFormat } from './types';
-
-/**
- * Lazily imports the output format based on the format specified
- */
-export async function createOutputFormat(buffer: TargetBuffer, format?: ContainerFormat) {
- if (format == 'webm') {
- const { WebMOutputFormat } = await import('mediabunny');
- return new WebMOutputFormat();
- } else if (format == 'ogg') {
- const { OggOutputFormat } = await import('mediabunny');
- return new OggOutputFormat();
- } else if (format == 'mov') {
- const { MovOutputFormat } = await import('mediabunny');
- return new MovOutputFormat();
- } else {
- const { Mp4OutputFormat } = await import('mediabunny');
- return new Mp4OutputFormat({ fastStart: buffer.fastStart });
- }
-}
diff --git a/apps/web/src/components/engine/encode/image-encoder.ts b/apps/web/src/components/engine/encode/image-encoder.ts
deleted file mode 100644
index cb44003e..00000000
--- a/apps/web/src/components/engine/encode/image-encoder.ts
+++ /dev/null
@@ -1,262 +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 { hasComponent, Hierarchy, Not, Or, query } from 'bitecs';
-
-import { ChildOf } from '../components';
-import { createEngineWorld } from '../api';
-import { createEntity } from '../api/entities';
-import { appendChild } from '../api/hierarchy';
-import { recomputeEntityTimeRange } from '../api/utils';
-import { addComponent, removeComponent } from '../api/events';
-import { playbackSystem } from '../systems/playback';
-import { motionSystem } from '../systems/motion';
-import { transformSystem } from '../systems/transform';
-import { renderSystem } from '../systems/render';
-import { cloneFromRecords, serializeEntity } from '../api/serialize';
-import { getEntityTree } from '../api/query';
-import { framesToSeconds, formatTimecode } from '../utils';
-import { assert } from '@/utils';
-
-import type { EngineWorld } from '../api/world';
-import type { AABB } from '../math';
-
-type ImageEncoderConfig = {
- eid: number;
- /** Frames to capture, relative to the entity's first visible frame. */
- frames: number[];
- /** Target output height in px (default: the entity's native size). */
- resolution?: number;
-};
-
-/** One capture: the PNG plus the timecode of the frame rendered, e.g. `01s15f`. */
-export type CapturedImage = { base64: string; timecode: string };
-
-export type ImageExportResult =
- | { type: 'success'; data: CapturedImage[] }
- | { type: 'canceled' }
- | { type: 'error'; error: Error };
-
-/**
- * Renders single frames of one entity into standalone PNGs (base64, no
- * data-url prefix). The entity's subtree is cloned into a fresh offline
- * world and drawn at the top level: the canvas is sized to the union of the
- * entity's world-space AABBs across the requested frames and the camera
- * shifts that box onto the canvas, so the node is fully visible regardless
- * of where it sat in its source scene.
- */
-export async function createImageEncoder(sourceWorld: EngineWorld, config: ImageEncoderConfig) {
- assert(config.frames.length > 0, 'No frames requested');
-
- const subtree = getEntityTree(sourceWorld, config.eid);
- const records = subtree.map(eid => serializeEntity(sourceWorld, eid));
-
- // Re-root the node. Its parent isn't part of the clone, so a dangling
- // ChildOf would attach to an arbitrary entity in the new world; Delay and
- // Hidden only mean something on the timeline it was lifted out of.
- const rootRecord = records.find(record => record.eid === config.eid);
- assert(rootRecord !== undefined, `No such entity: ${config.eid}`);
- delete rootRecord.ChildOf;
- delete rootRecord.Delay;
- delete rootRecord.Hidden;
-
- const offscreenCanvas = new OffscreenCanvas(2, 2);
- const offscreenCtx = offscreenCanvas.getContext('2d')!;
-
- // Never started — image capture is video-only; the context just satisfies
- // world construction and lazy audio-bus wiring.
- const offlineAudioCtx = new OfflineAudioContext(2, 1, 48000);
-
- const world = createEngineWorld(sourceWorld.projectId, offlineAudioCtx);
- world.mode = 'offline-video';
- world.assets = sourceWorld.assets;
- world.fonts = [...sourceWorld.fonts];
- world.ctx = offscreenCtx;
- world.canvas = offscreenCanvas;
- world.resolution = 1;
- world.camera = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 };
- world.frameRate = sourceWorld.frameRate;
- world.promises = [];
-
- const eidMap = cloneFromRecords(world, records);
- const rootEid = eidMap.get(config.eid);
- assert(rootEid !== undefined, 'Failed to clone entity subtree');
-
- const clonedEids = [...eidMap.values()];
- const c = world.components;
-
- // Mute everything so the playback system never initializes audio decoders.
- for (const eid of clonedEids) {
- addComponent(world, eid, c.Muted, false);
- }
-
- // The motion system only samples parented entities, so the node's own
- // keyframes would be skipped as a top-level root. A synthetic group above
- // it owns the playhead instead and keeps the node an ordinary child.
- const stageEid = createEntity(world);
- addComponent(world, stageEid, c.Group, false);
- appendChild(world, rootEid, stageEid);
-
- recomputeAllTimeRanges(world);
-
- // Map "frame 0 = first visible frame": with Delay stripped the node's
- // computed start is 0 unless it carries a Trim, whose start offsets it.
- const startFrame = c.Computed.start[rootEid] ?? 0;
-
- // Entities in the clone that own a timeline clock
- const clockEids = [...query(world, [c.Playback, Not(c.Deleted)])];
-
- const seek = (frame: number) => {
- const stageFrame = startFrame + frame;
- const stageSeconds = framesToSeconds(stageFrame, world.frameRate);
- c.Computed.localTime[stageEid] = stageFrame;
- c.Computed.localTimeInSeconds[stageEid] = stageSeconds;
-
- for (const eid of clockEids) {
- c.Computed.localTime[eid] = stageFrame;
- c.Computed.localTimeInSeconds[eid] = stageSeconds;
- c.Playback.playing[eid] = 1;
- }
-
- world.timestamp.delta = 1000 / world.frameRate;
- world.timestamp.now += world.timestamp.delta;
-
- // Cull flags derived from a stale canvas/camera would make the motion
- // system skip entities and keep decoders idle — clear them every tick.
- for (const eid of clonedEids) {
- removeComponent(world, eid, c.Culled, false);
- }
-
- playbackSystem(world);
- };
-
- // Union an entity's world-space AABB into `bounds`, descending so children
- // that overflow a leaf's own rect still count — except where content is
- // clipped. Group bounds already aggregate their children.
- const measure = (eid: number, bounds: AABB): void => {
- if (hasComponent(world, eid, c.Hidden) || hasComponent(world, eid, c.IsMask)) return;
- if (c.Computed.visibility[eid] === 0) return;
-
- bounds.minX = Math.min(bounds.minX, c.WorldBounds.minX[eid]);
- bounds.minY = Math.min(bounds.minY, c.WorldBounds.minY[eid]);
- bounds.maxX = Math.max(bounds.maxX, c.WorldBounds.maxX[eid]);
- bounds.maxY = Math.max(bounds.maxY, c.WorldBounds.maxY[eid]);
-
- if (hasComponent(world, eid, c.ClipsContent)) return;
- for (const childEid of query(world, [Or(c.Geometry, c.Group), ChildOf(eid), Not(c.Deleted)])) {
- measure(childEid, bounds);
- }
- };
-
- // Warm-up tick: group bounds aggregate from their children's local
- // matrices, which don't exist until the first transform walk has visited
- // them — measuring on that walk would union stale boxes.
- seek(config.frames[0]);
- motionSystem(world);
- transformSystem(world);
-
- // Measure pass: run motion + transforms over every requested frame with an
- // identity camera and union the bounds, so an animated node stays fully
- // visible in a fixed-size output across all frames.
- const bounds: AABB = { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity };
- for (const frame of config.frames) {
- seek(frame);
- motionSystem(world);
- transformSystem(world);
- measure(rootEid, bounds);
- }
- assert(Number.isFinite(bounds.minX), 'Entity is not visible at any of the requested frames');
-
- const boundsWidth = Math.max(1, bounds.maxX - bounds.minX);
- const boundsHeight = Math.max(1, bounds.maxY - bounds.minY);
-
- /** Re-target the output height; callers that lay frames out do this once measured. */
- const resize = (height?: number) => {
- const scale = height ? height / boundsHeight : 1;
- offscreenCanvas.width = Math.max(1, Math.round(boundsWidth * scale));
- offscreenCanvas.height = Math.max(1, Math.round(boundsHeight * scale));
- world.resolution = scale;
- };
-
- resize(config.resolution);
- // Shift the measured box onto the canvas so the node is centered in view;
- // the camera translation is scaled by world.resolution at render time.
- world.camera.e = -bounds.minX;
- world.camera.f = -bounds.minY;
-
- let canceled = false;
- const cancel = () => (canceled = true);
-
- const render = async (): Promise<ImageExportResult> => {
- try {
- const images: CapturedImage[] = [];
-
- for (const frame of config.frames) {
- if (canceled) {
- return { type: 'canceled' };
- }
-
- seek(frame);
- await resolverSystem(world);
- motionSystem(world);
- transformSystem(world);
- renderSystem(world);
-
- const stampFrame = startFrame + frame;
- images.push({
- base64: await toBase64Png(offscreenCanvas),
- timecode: formatTimecode(framesToSeconds(stampFrame, world.frameRate), world.frameRate),
- });
- }
-
- return { type: 'success', data: images };
- } catch (e) {
- return {
- type: 'error',
- error: e instanceof Error ? e : new Error('Unknown error'),
- };
- }
- };
-
- return {
- render,
- cancel,
- resize,
- bounds: { width: boundsWidth, height: boundsHeight },
- };
-}
-
-async function toBase64Png(canvas: OffscreenCanvas): Promise<string> {
- const blob = await canvas.convertToBlob({ type: 'image/png' });
- const dataUrl = await new Promise<string>((resolve, reject) => {
- const reader = new FileReader();
- reader.onload = () => resolve(reader.result as string);
- reader.onerror = () => reject(reader.error);
- reader.readAsDataURL(blob);
- });
- return dataUrl.split(',')[1] ?? '';
-}
-
-async function resolverSystem(world: EngineWorld) {
- if (world.promises?.length) {
- await Promise.all(world.promises.filter(promise => promise !== null));
- world.promises = [];
- }
-}
-
-function recomputeAllTimeRanges(world: EngineWorld): void {
- const c = world.components;
- // Hierarchy(ChildOf) yields parents before children (top-down). A single pass
- // is wrong for group-like nodes: their bounds aggregate from children, but the
- // children haven't been recomputed yet when the parent is visited. Do two passes:
- // 1. top-down — refresh accumulated delay + leaf/Trim bounds (parents first)
- // 2. bottom-up — groups now aggregate from already-recomputed children
- const entities = query(world, [Or(c.Geometry, c.Group), Hierarchy(ChildOf), Not(c.Deleted)]);
- for (let i = 0; i < entities.length; i++) {
- recomputeEntityTimeRange(world, entities[i]);
- }
- for (let i = entities.length - 1; i >= 0; i--) {
- recomputeEntityTimeRange(world, entities[i]);
- }
-}
diff --git a/apps/web/src/components/engine/encode/interfaces.ts b/apps/web/src/components/engine/encode/interfaces.ts
deleted file mode 100644
index 0e8d7238..00000000
--- a/apps/web/src/components/engine/encode/interfaces.ts
+++ /dev/null
@@ -1,126 +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 type { VideoCodec, AudioCodec } from 'mediabunny';
-import type { ContainerFormat, EncoderProgress, WritableFileTarget } from './types';
-
-export interface AudioConfig {
- /**
- * Enable audio encoding
- * @default true
- */
- enabled?: boolean;
-
- /**
- * A floating point number indicating the audio context's sample rate, in samples per second.
- * @default 48000
- */
- sampleRate?: number;
-
- /**
- * Defines the number of channels of the composed audio
- * @default 2
- */
- numberOfChannels?: number;
-
- /**
- * Defines the bitrate at which the audio should be rendered at
- * @default 128e3
- */
- bitrate?: number;
-
- /**
- * Defines the codec to use for the audio
- * @default 'aac'
- */
- codec?: AudioCodec;
-}
-
-export interface VideoConfig {
- /**
- * Defines the codec to use for the video
- * @default 'avc'
- */
- codec?: VideoCodec;
-
- /**
- * The full codec string as specified in the WebCodecs API Codec Registry.
- */
- fullCodecString?: string;
-
- /**
- * Enable video encoding
- * @default true
- */
- enabled?: boolean;
-
- /**
- * Defines the bitrate at which the video should be rendered at
- * @default 10e6
- */
- bitrate?: number;
-
- /**
- * Defines the fps at which the composition will be rendered
- * @default 30
- */
- fps?: number;
-
- /**
- * Resolution of the composition
- * @example
- * 1080 for 1080p
- * 1440 for 1440p
- * 2160 for 4k
- * @default 1080
- */
- resolution?: number;
-}
-
-export interface EncoderConfig {
- /**
- * Video encoding configuration
- */
- video?: VideoConfig;
-
- /**
- * Audio encoding configuration
- */
- audio?: AudioConfig;
-
- /**
- * Defines the output format of the encoded file
- * @default 'mp4'
- */
- format?: ContainerFormat;
-
- /**
- * Defines the range of the composition to encode
- */
- trim?: {
- /**
- * Defines the time when the encoding should end
- * @default scene.duration
- */
- end?: number;
- };
-
- /**
- * Defines the target file to encode to.
- * When a FileSystemFileHandle (or any WritableFileTarget) is provided, the
- * output is streamed to the file. When omitted, the output is buffered in
- * memory and returned as a Blob in the ExportResult.
- */
- target?: FileSystemFileHandle | WritableFileTarget;
-
- /**
- * Defines the progress callback
- */
- onProgress?: (progress: EncoderProgress) => void;
-
- /**
- * Defines the source scene to encode
- */
- scene: number;
-}
diff --git a/apps/web/src/components/engine/encode/types.ts b/apps/web/src/components/engine/encode/types.ts
deleted file mode 100644
index 40408172..00000000
--- a/apps/web/src/components/engine/encode/types.ts
+++ /dev/null
@@ -1,31 +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 type { StreamTargetChunk } from 'mediabunny';
-
-export type ContainerFormat = 'mp4' | 'webm' | 'ogg' | 'mov';
-
-// Minimal FileSystemFileHandle-shaped target
-export interface WritableFileTarget {
- createWritable(): Promise<WritableStream<StreamTargetChunk>>;
-}
-
-export type EncoderProgress = {
- total: number;
- progress: number;
- remaining: Date;
-};
-
-export type ExportResult =
- | {
- type: 'success';
- data: Blob | undefined;
- }
- | {
- type: 'canceled';
- }
- | {
- type: 'error';
- error: Error;
- };
diff --git a/apps/web/src/components/engine/encode/utils.ts b/apps/web/src/components/engine/encode/utils.ts
deleted file mode 100644
index 99496a5a..00000000
--- a/apps/web/src/components/engine/encode/utils.ts
+++ /dev/null
@@ -1,22 +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/. */
-
-/**
- * Helper for creating the render event detail
- */
-export function createRenderEventDetail(progress: number, total: number, startTime: number) {
- const duration = performance.now() - startTime;
- const time = (duration / gte1(progress)) * (total - progress);
- const remaining = new Date(time);
-
- return { remaining, progress, total };
-}
-
-/**
- * Helper for making sure a number is greater than 1
- */
-function gte1(num: number): number {
- if (num < 1) return 1;
- return num;
-}
diff --git a/apps/web/src/components/engine/font/fixtures.ts b/apps/web/src/components/engine/font/fixtures.ts
deleted file mode 100644
index 62f7b079..00000000
--- a/apps/web/src/components/engine/font/fixtures.ts
+++ /dev/null
@@ -1,62 +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/. */
-
-export const WebFonts = {
- Inter: {
- weights: ['100', '200', '300', '400', '500', '600', '700', '800', '900'],
- url: 'https://fonts.gstatic.com/s/inter/v20/UcCo3FwrK3iLTcviYwYZ8UA3.woff2',
- },
- 'The Bold Font': {
- weights: ['500'],
- url: 'https://diffusion-studio-public.s3.eu-central-1.amazonaws.com/fonts/the-bold-font.ttf',
- },
- 'Komika Axis': {
- weights: ['400'],
- url: 'https://diffusion-studio-public.s3.eu-central-1.amazonaws.com/fonts/komika-axis.ttf',
- },
- Geologica: {
- weights: ['100', '200', '300', '400', '500', '600', '700', '800', '900'],
- url: 'https://fonts.gstatic.com/s/geologica/v1/oY1l8evIr7j9P3TN9YwNAdyjzUyDKkKdAGOJh1UlCDUIhAIdhCZOn1fLsig7jfvCCPHZckUWE1lELWNN-w.woff2',
- },
- Nunito: {
- weights: ['200', '300', '400', '500', '600', '700', '800', '900'],
- url: 'https://fonts.gstatic.com/s/nunito/v26/XRXV3I6Li01BKofINeaBTMnFcQ.woff2',
- },
- Figtree: {
- weights: ['300', '400', '500', '600', '700', '800', '900'],
- url: 'https://fonts.gstatic.com/s/figtree/v5/_Xms-HUzqDCFdgfMm4S9DaRvzig.woff2',
- },
- Urbanist: {
- weights: ['100', '200', '300', '400', '500', '600', '700', '800', '900'],
- url: 'https://fonts.gstatic.com/s/urbanist/v15/L0x-DF02iFML4hGCyMqlbS1miXK2.woff2',
- },
- Montserrat: {
- weights: ['100', '200', '300', '400', '500', '600', '700', '800', '900'],
- url: 'https://fonts.gstatic.com/s/montserrat/v26/JTUSjIg1_i6t8kCHKm459WlhyyTh89Y.woff2',
- },
- Bangers: {
- weights: ['400'],
- url: 'https://fonts.gstatic.com/s/bangers/v20/FeVQS0BTqb0h60ACH55Q2J5hm24.woff2',
- },
- Chewy: {
- weights: ['400'],
- url: 'https://fonts.gstatic.com/s/chewy/v18/uK_94ruUb-k-wn52KjI9OPec.woff2',
- },
- 'Source Code Pro': {
- weights: ['200', '300', '400', '500', '600', '700', '800', '900'],
- url: 'https://fonts.gstatic.com/s/sourcecodepro/v22/HI_SiYsKILxRpg3hIP6sJ7fM7PqlPevWnsUnxg.woff2',
- },
-} as const;
-
-export const FONT_WEIGHTS = {
- '100': 'Thin',
- '200': 'Extra Light',
- '300': 'Light',
- '400': 'Normal',
- '500': 'Medium',
- '600': 'Semi Bold',
- '700': 'Bold',
- '800': 'Extra Bold',
- '900': 'Black',
-} as const;
diff --git a/apps/web/src/components/engine/font/index.ts b/apps/web/src/components/engine/font/index.ts
deleted file mode 100644
index 1afa1c0f..00000000
--- a/apps/web/src/components/engine/font/index.ts
+++ /dev/null
@@ -1,7 +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/. */
-
-export * from './utils';
-export * from './types';
-export * from './fixtures';
diff --git a/apps/web/src/components/engine/font/types.ts b/apps/web/src/components/engine/font/types.ts
deleted file mode 100644
index 5723b586..00000000
--- a/apps/web/src/components/engine/font/types.ts
+++ /dev/null
@@ -1,44 +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 type { FontStyle } from '../components';
-
-
-
-/**
- * Defines the properties that are required
- * to load a new font
- */
-export type FontSource = {
- /**
- * Name of the Family
- * @example 'Arial'
- */
- family: string;
- /**
- * Source of the Variant
- * @example url(arial.ttf)
- */
- source: string;
- /**
- * Defines the font style
- * @example 'italic'
- */
- style?: FontStyle;
- /**
- * The weight of the font
- * @example '400'
- */
- weight?: string;
-};
-
-/**
- * Defines a single font that has one or
- * more variants
- */
-export type FontSources = {
- family: string;
- variants: FontSource[];
-};
diff --git a/apps/web/src/components/engine/font/utils.ts b/apps/web/src/components/engine/font/utils.ts
deleted file mode 100644
index 2be504fc..00000000
--- a/apps/web/src/components/engine/font/utils.ts
+++ /dev/null
@@ -1,179 +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 { WebFonts } from './fixtures';
-import { FontStyle } from '../components';
-
-import type * as types from './types';
-import type { EngineWorld } from '../api/world';
-
-const STYLE_MAP = {
- [FontStyle.NORMAL]: 'normal',
- [FontStyle.ITALIC]: 'italic',
- [FontStyle.OBLIQUE]: 'oblique',
-} as const;
-
-/**
- * Get all available local fonts, requires the
- * **Local Font Access API**
- */
-export async function getLocalFonts(): Promise<types.FontSources[]> {
- const localFonts: Record<string, any> = {};
-
- // group by font family
- for (const font of await window.queryLocalFonts()) {
- if (font.family in localFonts) {
- localFonts[font.family].push(font);
- continue;
- }
- localFonts[font.family] = [font];
- }
-
- return Object.keys(localFonts).map((family: any) => {
- return {
- family,
- variants: localFonts[family].map((font: any) => {
- return {
- family,
- source: `local('${font.fullName}'), local('${font.postscriptName}')`,
- weight: matchFontWeight(font.style),
- style: matchFontStyle(font.style),
- };
- }),
- };
- });
-}
-
-/**
- * Get common web fonts
- */
-export function getWebFonts(): types.FontSources[] {
- return Object.keys(WebFonts).map((family) => {
- return {
- family,
- variants: WebFonts[family as keyof typeof WebFonts].weights.map((weight) => {
- return {
- family,
- source: `url(${WebFonts[family as keyof typeof WebFonts].url})`,
- weight: weight,
- };
- }),
- };
- });
-}
-
-export async function loadWebFont(
- world: EngineWorld,
- family: keyof typeof WebFonts,
- style: FontStyle = FontStyle.NORMAL,
- weight?: string,
-) {
- const source = `url(${WebFonts[family].url})`;
- const font: types.FontSource = {
- source,
- family,
- weight,
- style,
- };
-
- // Already loaded
- if (world.fonts.some((f) => f.source === font.source && f.weight === font.weight)) {
- return font;
- }
-
- world.fonts.push(font);
-
- // Load all weights if no weight is specified
- if (!weight) {
- const config = WebFonts[family as keyof typeof WebFonts];
- const weights = config.weights;
- weight = weights.length > 1 ? `${weights[0]} ${weights[weights.length - 1]}` : weights[0]
- }
-
- const fontFace = new FontFace(family, source, { weight, style: STYLE_MAP[style] });
-
- await new Promise((resolve, reject) => {
- fontFace
- .load()
- .then((font) => {
- document.fonts.add(font);
- resolve(null);
- })
- .catch((error) => {
- world.fonts = world.fonts.filter((f) => f.source !== font.source && f.weight !== font.weight);
- reject(error);
- });
- });
-}
-
-/**
- * Re-loads previously used fonts. Placeholder: projects are held purely in
- * memory, so there is nothing to restore yet.
- */
-export async function restoreFonts(world: EngineWorld): Promise<void> {
- const fonts: types.FontSource[] = [];
-
- for (const font of fonts) {
- if (font.family in WebFonts) {
- await loadWebFont(
- world,
- font.family as keyof typeof WebFonts,
- font.style as FontStyle,
- font.weight,
- );
- }
- }
-}
-
-function matchFontStyle(style: any) {
- if (`${style}`.match(/oblique/i)) {
- return FontStyle.OBLIQUE;
- }
-
- if (`${style}`.match(/italic/i)) {
- return FontStyle.ITALIC;
- }
-
- return FontStyle.NORMAL;
-}
-
-function matchFontWeight(weight: any) {
- if (`${weight}`.match(/black|heavy/i)) {
- return '900';
- }
-
- if (`${weight}`.match(/extrabold|ultrabold/i)) {
- return '800';
- }
-
- if (`${weight}`.match(/semibold|demibold/i)) {
- return '600';
- }
-
- if (`${weight}`.match(/bold|strong/i)) {
- return '700';
- }
-
- if (`${weight}`.match(/medium/i)) {
- return '500';
- }
-
- if (`${weight}`.match(/normal|regular|book|plain/i)) {
- return '400';
- }
-
- if (`${weight}`.match(/extralight|ultralight/i)) {
- return '200';
- }
-
- if (`${weight}`.match(/thin|hairline/i)) {
- return '100';
- }
-
- if (`${weight}`.match(/light/i)) {
- return '300';
- }
-
- return '400';
-}
diff --git a/apps/web/src/components/engine/hooks/index.ts b/apps/web/src/components/engine/hooks/index.ts
deleted file mode 100644
index 951d0059..00000000
--- a/apps/web/src/components/engine/hooks/index.ts
+++ /dev/null
@@ -1,5 +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/. */
-
-export * from './use-ecs';
diff --git a/apps/web/src/components/engine/hooks/use-ecs.ts b/apps/web/src/components/engine/hooks/use-ecs.ts
deleted file mode 100644
index 5a912b24..00000000
--- a/apps/web/src/components/engine/hooks/use-ecs.ts
+++ /dev/null
@@ -1,253 +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 { createEffect, createMemo, createSignal, onCleanup, type Accessor } from 'solid-js';
-import { domSyncSubs, querySyncSubs, tagSyncSubs, worldStateSyncSubs } from '../systems/dom';
-
-import type { ValueSyncSub, TagSyncSub, QuerySyncSub, WorldStateSyncSub } from '../systems/dom';
-import type { EngineWorld } from '../api/world';
-import type { QueryTerm } from 'bitecs';
-
-type EidAccessor = number | null | undefined | Accessor<number | null | undefined>;
-
-
-/**
- * Subscribe to an ECS component field/scalar for a given entity.
- * Pass the SoA array directly (e.g. `world.components.Position.x`).
- * Returns a SolidJS accessor that stays in sync every frame.
- */
-export function useEntityState<T>(store: T[], eid: EidAccessor, initial: T): Accessor<T>;
-export function useEntityState<T>(store: T[], eid?: EidAccessor, initial?: undefined): Accessor<T | undefined>;
-export function useEntityState<T>(store: T[], eid?: EidAccessor, initial?: T): Accessor<T | undefined> {
- const [value, setValue] = createSignal(initial);
- let currentSub: ValueSyncSub | undefined;
-
- const resolvedEid = createMemo(() => {
- if (typeof eid === 'function') {
- return eid();
- }
- return eid;
- })
-
- createEffect(() => {
- const currentEid = resolvedEid();
-
- // Tear down previous subscription when eid changes
- if (currentSub) {
- domSyncSubs.delete(currentSub);
- currentSub = undefined;
- }
-
- if (typeof currentEid !== 'number') {
- setValue(() => initial);
- return;
- }
-
- // Prime synchronously from the store when (re)subscribing. Without this,
- // a value left over from a previously-tracked eid would persist whenever
- // the new eid's stored value equals `initial`, since dom-sync only fires
- // on a *change* and the fresh sub starts at `prev: initial`. (Same hazard
- // the plural `useEntityStates` guards against — reusing a row instance for
- // a different entity, e.g. on reorder, would otherwise show a stale value.)
- const stored = (store as unknown[])[currentEid] as T | undefined;
- const primed = stored !== undefined ? stored : initial;
- setValue(() => primed);
-
- const sub: ValueSyncSub = {
- store: store as unknown[],
- eid: currentEid,
- prev: primed,
- set: (v) => setValue(() => v as T),
- };
- domSyncSubs.add(sub);
- currentSub = sub;
- });
-
- onCleanup(() => {
- if (currentSub) {
- domSyncSubs.delete(currentSub);
- }
- });
-
- return value;
-}
-
-
-type EidAccessors = number[] | Accessor<number[]>;
-
-/**
- * Fan-out of `useEntityState` over a list of entities. The list itself may be
- * reactive (e.g. from `useQuery`). Returns one value per eid, in the same
- * order as the input. Subs are torn down and rebuilt when the eid list changes.
- */
-export function useEntityStates<T>(store: T[], eids: EidAccessors, initial: T): Accessor<T[]>;
-export function useEntityStates<T>(store: T[], eids: EidAccessors, initial?: undefined): Accessor<(T | undefined)[]>;
-export function useEntityStates<T>(store: T[], eids: EidAccessors, initial?: T): Accessor<(T | undefined)[]> {
- const resolvedEids = createMemo(() => {
- if (typeof eids === 'function') {
- return eids();
- }
- return eids;
- });
-
- const [values, setValues] = createSignal<(T | undefined)[]>([]);
- let currentSubs: ValueSyncSub[] = [];
-
- const unsubAll = () => {
- for (const sub of currentSubs) {
- domSyncSubs.delete(sub);
- }
- currentSubs = [];
- };
-
- createEffect(() => {
- unsubAll();
-
- const currentEids = resolvedEids();
-
- // Prime values synchronously from the store. Without this, consumers
- // would see `undefined` slots until the next dom-sync tick — and slots
- // whose stored value already equals `initial` would never trigger a
- // sync at all, since dom-sync only fires on a *change*.
- const primed: (T | undefined)[] = currentEids.map(eid => {
- const v = (store as unknown[])[eid] as T | undefined;
- return v !== undefined ? v : initial;
- });
- setValues(primed);
-
- currentSubs = currentEids.map((eid, i) => {
- const sub: ValueSyncSub = {
- store: store as unknown[],
- eid,
- prev: primed[i],
- set: (v) => setValues(prev => {
- const next = [...prev];
- next[i] = v as T;
- return next;
- }),
- };
- domSyncSubs.add(sub);
- return sub;
- });
- });
-
- onCleanup(unsubAll);
-
- return values;
-}
-
-/**
- * Subscribe to an ECS tag component for a given entity.
- * Returns a SolidJS accessor that stays in sync every frame.
- * To mutate, call `addComponentTracked` / `removeComponentTracked` directly.
- */
-export function useEntityTag(component: object, eid?: EidAccessor): Accessor<boolean> {
- const [value, setValue] = createSignal(false);
- let currentSub: TagSyncSub | undefined;
-
- const resolvedEid = createMemo(() => {
- if (typeof eid === 'function') {
- return eid();
- }
- return eid;
- })
-
-
-
- createEffect(() => {
- const currentEid = resolvedEid();
-
- if (currentSub) {
- tagSyncSubs.delete(currentSub);
- currentSub = undefined;
- }
-
- if (!currentEid) {
- setValue(false);
- return;
- }
-
- // Reset to the absent state whenever we (re)subscribe to a different eid.
- // Without this, a stale `true` left over from the previously-tracked eid
- // persists if the new eid lacks the tag: the fresh sub starts at
- // `prev: false` and dom-sync only fires `set` on a *change*, so it never
- // corrects `false`. This surfaces when an <Index> reuses a row instance
- // for a different entity (e.g. reordering layers) — two rows would then
- // appear Selected at once.
- setValue(false);
-
- const sub: TagSyncSub = {
- component,
- eid: currentEid,
- prev: false,
- set: (v) => setValue(v),
- };
- tagSyncSubs.add(sub);
- currentSub = sub;
- });
-
- onCleanup(() => {
- if (currentSub) {
- tagSyncSubs.delete(currentSub);
- }
- });
-
- return value;
-}
-
-export function useQuery(query: QueryTerm[]): Accessor<number[]> {
- const [value, setValue] = createSignal<number[]>([]);
- let currentSub: QuerySyncSub | undefined;
-
- createEffect(() => {
- const sub: QuerySyncSub = {
- query,
- prev: new Uint32Array(0),
- set: (result) => setValue(result),
- };
- querySyncSubs.add(sub);
- currentSub = sub;
- });
-
- onCleanup(() => {
- if (currentSub) {
- querySyncSubs.delete(currentSub);
- }
- });
-
- return value;
-}
-
-/**
- * Subscribe to a world-level state value via a getter. Examples:
- * useWorldState(w => w.selection.scene, null)
- * useWorldState(w => w.camera.a, 1)
- * useWorldState(w => w.background, 0x161616)
- */
-export function useWorldState<T>(get: (world: EngineWorld) => T, initial: T): Accessor<T> {
- const [value, setValue] = createSignal<T>(initial);
- let currentSub: WorldStateSyncSub | undefined;
-
- createEffect(() => {
- if (currentSub) {
- worldStateSyncSubs.delete(currentSub);
- }
-
- const sub: WorldStateSyncSub = {
- get: get as (world: EngineWorld) => unknown,
- prev: initial,
- set: (v) => setValue(() => v as T),
- };
- worldStateSyncSubs.add(sub);
- currentSub = sub;
- });
-
- onCleanup(() => {
- if (currentSub) {
- worldStateSyncSubs.delete(currentSub);
- }
- });
-
- return value;
-}
diff --git a/apps/web/src/components/engine/index.ts b/apps/web/src/components/engine/index.ts
deleted file mode 100644
index 965397ff..00000000
--- a/apps/web/src/components/engine/index.ts
+++ /dev/null
@@ -1,13 +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/. */
-
-export * from './types';
-export * from './math';
-export * from './components';
-export * from './utils';
-export * from './selection';
-export * from './systems';
-export * from './api';
-export * from './hooks';
-export * from './services';
diff --git a/apps/web/src/components/engine/math/aabb.ts b/apps/web/src/components/engine/math/aabb.ts
deleted file mode 100644
index e5856e5e..00000000
--- a/apps/web/src/components/engine/math/aabb.ts
+++ /dev/null
@@ -1,114 +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/. */
-
-/**
- * Axis-Aligned Bounding Box utilities.
- */
-
-import type { Mat2D } from './matrix2d';
-
-export type AABB = { minX: number; minY: number; maxX: number; maxY: number };
-
-/**
- * Compute the world-space AABB of a rectangle [0,0]→[w,h]
- * transformed by a 2D affine matrix.
- */
-export function aabbFromTransformedRect(mat: Mat2D, w: number, h: number): AABB {
- const { a, b, c, d, e, f } = mat;
-
- // Transform 4 corners: (0,0), (w,0), (w,h), (0,h)
- const x0 = e, y0 = f;
- const x1 = a * w + e, y1 = b * w + f;
- const x2 = a * w + c * h + e, y2 = b * w + d * h + f;
- const x3 = c * h + e, y3 = d * h + f;
-
- return {
- minX: Math.min(x0, x1, x2, x3),
- minY: Math.min(y0, y1, y2, y3),
- maxX: Math.max(x0, x1, x2, x3),
- maxY: Math.max(y0, y1, y2, y3),
- };
-}
-
-/** Test whether two AABBs overlap. */
-export function aabbsIntersect(a: AABB, b: AABB): boolean {
- return !(
- a.maxX < b.minX ||
- a.minX > b.maxX ||
- a.maxY < b.minY ||
- a.minY > b.maxY
- );
-}
-
-// ─── Polygon geometry ───────────────────────────────────────────
-
-export type Point = { x: number; y: number };
-
-/** Four corners of a transformed rect: [TL, TR, BR, BL]. */
-export type Quad = [Point, Point, Point, Point];
-
-/** Get the four world-space corners of a rect [0,0]→[w,h] through a Mat2D. */
-export function rectToQuad(mat: Mat2D, w: number, h: number): Quad {
- const { a, b, c, d, e, f } = mat;
- return [
- { x: e, y: f },
- { x: a * w + e, y: b * w + f },
- { x: a * w + c * h + e, y: b * w + d * h + f },
- { x: c * h + e, y: d * h + f },
- ];
-}
-
-/** Transform a single point through a Mat2D. */
-export function transformPoint(mat: Mat2D, px: number, py: number): Point {
- return {
- x: mat.a * px + mat.c * py + mat.e,
- y: mat.b * px + mat.d * py + mat.f,
- };
-}
-
-/** Invert a Mat2D. */
-export function invert2D(m: Mat2D): Mat2D {
- const { a, b, c, d, e, f } = m;
- const det = a * d - b * c;
-
- const inv = 1 / det;
- return {
- a: d * inv,
- b: -b * inv,
- c: -c * inv,
- d: a * inv,
- e: (c * f - d * e) * inv,
- f: (b * e - a * f) * inv,
- };
-}
-
-/** Point-in-polygon test (ray casting). */
-export function pointInQuad(px: number, py: number, q: Quad): boolean {
- let inside = false;
- for (let i = 0, j = 3; i < 4; j = i++) {
- const xi = q[i].x, yi = q[i].y;
- const xj = q[j].x, yj = q[j].y;
- if (((yi > py) !== (yj > py)) && (px < (xj - xi) * (py - yi) / (yj - yi) + xi)) {
- inside = !inside;
- }
- }
- return inside;
-}
-
-/** Test whether two quads intersect (vertex containment check). */
-export function quadsIntersect(a: Quad, b: Quad): boolean {
- for (let i = 0; i < 4; i++) {
- if (pointInQuad(a[i].x, a[i].y, b)) return true;
- if (pointInQuad(b[i].x, b[i].y, a)) return true;
- }
- return false;
-}
-
-/** Center point of a quad. */
-export function quadCenter(q: Quad): Point {
- return {
- x: (q[0].x + q[1].x + q[2].x + q[3].x) / 4,
- y: (q[0].y + q[1].y + q[2].y + q[3].y) / 4,
- };
-}
diff --git a/apps/web/src/components/engine/math/common.ts b/apps/web/src/components/engine/math/common.ts
deleted file mode 100644
index 3f13b64f..00000000
--- a/apps/web/src/components/engine/math/common.ts
+++ /dev/null
@@ -1,18 +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/. */
-
-/**
- * Shared math constants and helpers.
- */
-
-export const DEG_TO_RAD = Math.PI / 180;
-export const RAD_TO_DEG = 180 / Math.PI;
-
-export function degToRad(deg: number): number {
- return deg * DEG_TO_RAD;
-}
-
-export function radToDeg(rad: number): number {
- return rad * RAD_TO_DEG;
-}
diff --git a/apps/web/src/components/engine/math/index.ts b/apps/web/src/components/engine/math/index.ts
deleted file mode 100644
index 7366f8d0..00000000
--- a/apps/web/src/components/engine/math/index.ts
+++ /dev/null
@@ -1,7 +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/. */
-
-export * from './common';
-export * from './matrix2d';
-export * from './aabb';
diff --git a/apps/web/src/components/engine/math/matrix2d.ts b/apps/web/src/components/engine/math/matrix2d.ts
deleted file mode 100644
index 4ebbfcb0..00000000
--- a/apps/web/src/components/engine/math/matrix2d.ts
+++ /dev/null
@@ -1,226 +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/. */
-
-/**
- * Pure 2D affine matrix utilities.
- *
- * Mat2D uses named properties { a, b, c, d, e, f } matching
- * CanvasRenderingContext2D / DOMMatrix:
- *
- * | a c e |
- * | b d f |
- * | 0 0 1 |
- */
-
-import { degToRad } from './common';
-
-export type Mat2D = {
- a: number; b: number; c: number;
- d: number; e: number; f: number;
-};
-
-/** Return a fresh identity matrix. */
-export function identity2D(): Mat2D {
- return { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 };
-}
-
-/**
- * Compose a local 2D affine matrix from transform properties.
- *
- * Equivalent to: T(pos+pivot) · R(rot) · SkewX · SkewY · S(scale) · T(-pivot)
- *
- * @param x Position X
- * @param y Position Y
- * @param rotation Rotation in **degrees**
- * @param scaleX Scale X
- * @param scaleY Scale Y
- * @param pivotX Pivot X in pixels (anchorX × width)
- * @param pivotY Pivot Y in pixels (anchorY × height)
- * @param skewX Skew X in **degrees**
- * @param skewY Skew Y in **degrees**
- */
-export function compose2D(
- x: number,
- y: number,
- rotation: number,
- scaleX: number,
- scaleY: number,
- pivotX: number,
- pivotY: number,
- skewX: number,
- skewY: number,
-): Mat2D {
- const rot = degToRad(rotation);
- const skx = degToRad(skewX);
- const sky = degToRad(skewY);
-
- const cos = Math.cos(rot);
- const sin = Math.sin(rot);
- const tanSkewX = Math.tan(skx);
- const tanSkewY = Math.tan(sky);
-
- // SkewX · SkewY · Scale (pre-rotation matrix)
- const a1 = scaleX * (1 + tanSkewX * tanSkewY);
- const b1 = tanSkewY * scaleX;
- const c1 = tanSkewX * scaleY;
- const d1 = scaleY;
-
- // R(rot) · [a1 c1; b1 d1]
- const a = cos * a1 - sin * b1;
- const b = sin * a1 + cos * b1;
- const c = cos * c1 - sin * d1;
- const d = sin * c1 + cos * d1;
-
- // T(pos+pivot) · M · T(-pivot)
- const tx = x + pivotX;
- const ty = y + pivotY;
- const e = tx + a * (-pivotX) + c * (-pivotY);
- const f = ty + b * (-pivotX) + d * (-pivotY);
-
- return { a, b, c, d, e, f };
-}
-
-/**
- * Multiply two 2D affine matrices: result = p × q.
- */
-export function multiply2D(p: Mat2D, q: Mat2D): Mat2D {
- return {
- a: p.a * q.a + p.c * q.b,
- b: p.b * q.a + p.d * q.b,
- c: p.a * q.c + p.c * q.d,
- d: p.b * q.c + p.d * q.d,
- e: p.e + p.a * q.e + p.c * q.f,
- f: p.f + p.b * q.e + p.d * q.f,
- };
-}
-
-/** Return a translation matrix. */
-export function translate2D(tx: number, ty: number): Mat2D {
- return { a: 1, b: 0, c: 0, d: 1, e: tx, f: ty };
-}
-
-/** Return a rotation matrix. Angle is in **degrees**. */
-export function rotate2D(degrees: number): Mat2D {
- const r = degToRad(degrees);
- const cos = Math.cos(r);
- const sin = Math.sin(r);
- return { a: cos, b: sin, c: -sin, d: cos, e: 0, f: 0 };
-}
-
-/** Return a scale matrix. */
-export function scale2D(sx: number, sy: number): Mat2D {
- return { a: sx, b: 0, c: 0, d: sy, e: 0, f: 0 };
-}
-
-/** Return a skew matrix. Angles are in **degrees**. */
-export function skew2D(degreesX: number, degreesY: number): Mat2D {
- const tx = Math.tan(degToRad(degreesX));
- const ty = Math.tan(degToRad(degreesY));
- return { a: 1, b: ty, c: tx, d: 1, e: 0, f: 0 };
-}
-
-export type Decomposed2D = {
- x: number;
- y: number;
- rotation: number;
- scaleX: number;
- scaleY: number;
- skewX: number; // degrees
- skewY: number; // degrees
-};
-
-const EPS = 1e-12;
-
-/**
- * Decompose a 2D matrix into translate / rotate / scale / skew.
- *
- * Uses column-based QR decomposition. In our matrix layout
- * | a c e |
- * | b d f |
- * | 0 0 1 |
- * column 0 is (a, b) and column 1 is (c, d).
- */
-export function decompose2D(m: Mat2D): Decomposed2D {
- const { a, b, c, d, e, f } = m;
-
- // Translation is direct.
- const x = e;
- const y = f;
-
- // Compute scaleX as length of column 0 (a, b).
- let scaleX = Math.hypot(a, b);
-
- // If scaleX is ~0, matrix is degenerate.
- if (scaleX < EPS) {
- // Fall back: treat column 0 as zero, get scaleY from column 1.
- const scaleY = Math.hypot(c, d);
- return {
- x,
- y,
- rotation: 0,
- scaleX: 0,
- scaleY,
- skewX: 0,
- skewY: 0,
- };
- }
-
- // Normalize column 0 to get rotation.
- const r0x = a / scaleX;
- const r0y = b / scaleX;
-
- // "Shear" factor: dot of normalized column 0 with column 1.
- let shear = r0x * c + r0y * d;
-
- // Remove shear from column 1 to isolate scaleY.
- let c2 = c - r0x * shear;
- let d2 = d - r0y * shear;
-
- let scaleY = Math.hypot(c2, d2);
-
- // If scaleY is ~0, it's also degenerate.
- if (scaleY < EPS) {
- const rotationRad = Math.atan2(r0y, r0x);
- return {
- x,
- y,
- rotation: rotationRad * 180 / Math.PI,
- scaleX,
- scaleY: 0,
- skewX: 0,
- skewY: 0,
- };
- }
-
- // Normalize shear to get the actual shear value in "tan(angle)" space.
- shear /= scaleY;
-
- // Detect reflection (negative determinant) and fold it into scaleY by convention.
- const det = a * d - b * c;
- if (det < 0) {
- // Flip one axis. Common choice: flip scaleY.
- scaleY = -scaleY;
- shear = -shear;
- c2 = -c2;
- d2 = -d2;
- }
-
- // Rotation in radians from normalized column 0.
- const rotationRad = Math.atan2(r0y, r0x);
- const rotation = rotationRad * 180 / Math.PI;
-
- // Convert shear factor to skew angle (X-shear).
- const skewX = Math.atan(shear) * 180 / Math.PI;
- const skewY = 0;
-
- return {
- x,
- y,
- rotation,
- scaleX,
- scaleY,
- skewX,
- skewY,
- };
-}
diff --git a/apps/web/src/components/engine/selection.ts b/apps/web/src/components/engine/selection.ts
deleted file mode 100644
index 1383ab28..00000000
--- a/apps/web/src/components/engine/selection.ts
+++ /dev/null
@@ -1,76 +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 { hasComponent, Not, Or, query } from 'bitecs';
-import { ChildOf } from './components/relations';
-import { clearComponent, getParentEntity } from '.';
-import { addComponent } from './api/events';
-
-import type { EngineWorld } from './api/world';
-
-export function selectStageEntities(world: EngineWorld): void {
- const c = world.components;
- for (const eid of query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), Not(ChildOf('*')), Not(c.Sequential), Not(c.Deleted), Not(c.Hidden), Not(c.Culled)])) {
- addComponent(world, eid, c.Selected, false);
- }
-}
-
-/**
- * Replace the current selection with the parents of the selected nodes.
- * Top-level entities (no parent) are skipped; Sequential GROUP ancestors
- * are transparent — keep walking up until a selectable parent is found.
- * A no-op if no valid parent exists.
- */
-export function selectParentEntities(world: EngineWorld): void {
- const c = world.components;
- const selection = query(world, [c.Selected, Or(c.Geometry, c.Group, c.AdjustmentLayer)])
- if (selection.length === 0) return;
-
- const parents = new Set<number>();
- for (const eid of selection) {
- let parent = getParentEntity(world, eid);
- while (parent !== null && hasComponent(world, parent, c.Sequential)) {
- parent = getParentEntity(world, parent);
- }
- if (parent !== null) {
- parents.add(parent);
- }
- }
- if (parents.size === 0) return;
-
- clearComponent(world, c.Selected, false);
- parents.forEach(parent => addComponent(world, parent, c.Selected, false));
-}
-
-/**
- * Replace the current selection with the direct children of selected nodes.
- * Sequential GROUP children are transparent — descend into them so the
- * caller selects actual selectable nodes underneath. No-op when no selected
- * node has any selectable descendant.
- */
-export function selectChildEntities(world: EngineWorld): void {
- const c = world.components;
- const selection = query(world, [c.Selected, Or(c.Geometry, c.Group, c.AdjustmentLayer)])
- if (selection.length === 0) return;
-
- const children = new Set<number>();
-
- const collect = (parentEid: number) => {
- for (const child of query(world, [ChildOf(parentEid), Not(c.Deleted), Not(c.Hidden), Not(c.Culled)])) {
- if (hasComponent(world, child, c.Sequential)) {
- collect(child);
- continue;
- }
-
- children.add(child);
- }
- };
- for (const eid of selection) {
- collect(eid);
- }
- if (children.size === 0) return;
-
- clearComponent(world, c.Selected, false);
- children.forEach(child => addComponent(world, child, c.Selected, false));
-}
diff --git a/apps/web/src/components/engine/services/audio-bus.ts b/apps/web/src/components/engine/services/audio-bus.ts
deleted file mode 100644
index 4ce3a3a6..00000000
--- a/apps/web/src/components/engine/services/audio-bus.ts
+++ /dev/null
@@ -1,69 +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 { hasComponent } from 'bitecs';
-import { attempt } from '@/utils';
-
-import type { EngineWorld } from '../api/world';
-
-/**
- * Per-entity audio bus. Each clip and each scene gets its own gain node.
- * Child buses connect directly into the parent bus's input — there is no
- * per-track sub-mix anymore (each clip is its own layer).
- */
-export class AudioBus {
- public context: BaseAudioContext;
-
- private gain: GainNode;
- private eid: number;
- private world: EngineWorld;
- private _input: AudioNode;
-
- public constructor(world: EngineWorld, eid: number) {
- this.context = world.audio;
- this.gain = world.audio.createGain();
- this._input = this.gain;
- this.eid = eid;
- this.world = world;
- }
-
- public get input(): AudioNode {
- return this._input;
- }
-
- public getGain(): GainNode {
- return this.gain;
- }
-
- public sync(): void {
- this.gain.gain.value = this.getVolume();
- }
-
- public connect(node: AudioNode) {
- this.gain.connect(node);
- }
-
- public mute(): void {
- this.gain.gain.value = 0;
- }
-
- public disconnect() {
- attempt(() => this.gain.disconnect());
- this._input = this.gain;
- }
-
- private getVolume(): number {
- const c = this.world.components;
-
- const volumeDb = c.Computed.volume[this.eid] ?? 0;
- const muted = hasComponent(this.world, this.eid, c.Muted);
-
- /** Minimum dB value — treated as silence (maps to linear gain 0). */
- if (muted || volumeDb === -Infinity) {
- return 0;
- }
-
- return Math.pow(10, volumeDb / 20);
- }
-}
diff --git a/apps/web/src/components/engine/services/index.ts b/apps/web/src/components/engine/services/index.ts
deleted file mode 100644
index 61c00e1b..00000000
--- a/apps/web/src/components/engine/services/index.ts
+++ /dev/null
@@ -1,5 +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/. */
-
-export * from './audio-bus';
diff --git a/apps/web/src/components/engine/systems/camera-controller.ts b/apps/web/src/components/engine/systems/camera-controller.ts
deleted file mode 100644
index baee57a9..00000000
--- a/apps/web/src/components/engine/systems/camera-controller.ts
+++ /dev/null
@@ -1,276 +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 { persistWorldState } from '../api';
-import { isPanMode } from './input-system';
-import { ToolType } from '../components';
-
-import type { EngineWorld } from '../api/world';
-import { Or, query } from 'bitecs';
-
-export type Camera = ReturnType<typeof createCameraController>;
-
-export function createCameraController(world: EngineWorld) {
- let isPanning = false;
- let panStartX = 0;
- let panStartY = 0;
- let panStartCamera: [e: number, f: number];
-
- const c = world.components;
-
- let canvas: HTMLCanvasElement | null = null;
- const cam = world.camera;
-
-
- /** Pan the camera by a screen-space delta (CSS pixels). */
- const pan = (screenDx: number, screenDy: number): void => {
- const { a, b, c, d, e, f } = cam;
-
- // Invert the camera to convert screen delta to world delta
- const det = a * d - b * c;
- if (Math.abs(det) < 1e-12) return;
-
- const invDet = 1 / det;
- const worldDx = (d * screenDx - c * screenDy) * invDet;
- const worldDy = (-b * screenDx + a * screenDy) * invDet;
-
- // Translate: M' = M * T(-worldDelta)
- cam.e = e - (a * worldDx + c * worldDy);
- cam.f = f - (b * worldDx + d * worldDy);
- }
-
- /**
- * Zoom the camera towards a screen point (CSS pixels).
- * @param screenX Cursor X in CSS pixels relative to the canvas
- * @param screenY Cursor Y in CSS pixels relative to the canvas
- * @param factor Zoom multiplier (>1 = zoom in, <1 = zoom out)
- */
- const zoom = (screenX: number, screenY: number, factor: number): void => {
- const { a, b, c, d, e, f } = cam;
-
- // Convert screen point to world coordinates via camera inverse
- const det = a * d - b * c;
- if (Math.abs(det) < 1e-12) return;
- const invDet = 1 / det;
-
- const worldX = invDet * (d * (screenX - e) - c * (screenY - f));
- const worldY = invDet * (-b * (screenX - e) + a * (screenY - f));
-
- // Clamp resulting scale
- const currentScale = Math.hypot(a, b);
- const targetScale = currentScale * factor;
- const clampedScale = Math.max(0.01, Math.min(10, targetScale));
- const s = clampedScale / currentScale;
-
- // M' = T(world) * S(s) * T(-world) * M
- cam.a = a * s;
- cam.b = b * s;
- cam.c = c * s;
- cam.d = d * s;
- cam.e = screenX - (a * s * worldX + c * s * worldY);
- cam.f = screenY - (b * s * worldX + d * s * worldY);
- }
-
- /** Zoom at the canvas center by a factor (>1 zoom in, <1 zoom out). */
- const zoomBy = (factor: number): void => {
- if (!canvas) return;
- const rect = canvas.getBoundingClientRect();
- zoom(rect.width / 2, rect.height / 2, factor);
- persistWorldState(world);
- }
-
- /** Reset camera zoom to 100% at the canvas center. */
- const resetZoom = (): void => {
- const currentScale = Math.hypot(cam.a, cam.b);
- if (currentScale > 0) zoomBy(1 / currentScale);
- }
-
- /**
- * Center the camera on a world-space rectangle with optional padding.
- * @param bounds { x, y, width, height } in world coordinates
- * @param padding Padding in CSS pixels
- */
- const centerRect = (bounds: { x: number; y: number; width: number; height: number }, padding = 80): void => {
- const parent = canvas?.parentElement;
- if (!parent) return;
-
- const rect = parent.getBoundingClientRect();
- const screenWidth = rect.width;
- const screenHeight = rect.height;
- if (screenWidth <= 0 || screenHeight <= 0) return;
-
- const scaleX = (screenWidth - padding * 2) / bounds.width;
- const scaleY = (screenHeight - padding * 2) / bounds.height;
- const scale = Math.max(0.01, Math.min(scaleX, scaleY, 1));
-
- const centerX = bounds.x + bounds.width / 2;
- const centerY = bounds.y + bounds.height / 2;
- const tx = screenWidth / 2 - centerX * scale;
- const ty = screenHeight / 2 - centerY * scale;
-
- cam.a = scale;
- cam.b = 0;
- cam.c = 0;
- cam.d = scale;
- cam.e = tx;
- cam.f = ty;
-
- persistWorldState(world);
- }
-
- /**
- * Compute the union of WorldBounds for a set of entities in pre-camera
- * scene-space (the space centerRect expects). Returns null if no entity
- * has bounds. WorldBounds are stored post-camera (Camera × dpr), so we
- * invert that transform here.
- */
- const computeSceneSpaceBounds = (eids: number[]): { x: number; y: number; width: number; height: number } | null => {
- const sx = cam.a * world.resolution;
- const sy = cam.d * world.resolution;
- const tx = cam.e * world.resolution;
- const ty = cam.f * world.resolution;
- if (sx === 0 || sy === 0) return null;
-
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
- let found = false;
- for (const eid of eids) {
- if (c.WorldBounds.minX[eid] === undefined) continue;
- const x0 = ((c.WorldBounds.minX[eid] ?? 0) - tx) / sx;
- const x1 = ((c.WorldBounds.maxX[eid] ?? 0) - tx) / sx;
- const y0 = ((c.WorldBounds.minY[eid] ?? 0) - ty) / sy;
- const y1 = ((c.WorldBounds.maxY[eid] ?? 0) - ty) / sy;
- minX = Math.min(minX, x0, x1);
- maxX = Math.max(maxX, x0, x1);
- minY = Math.min(minY, y0, y1);
- maxY = Math.max(maxY, y0, y1);
- found = true;
- }
- if (!found) return null;
- const width = maxX - minX;
- const height = maxY - minY;
- if (width <= 0 || height <= 0) return null;
- return { x: minX, y: minY, width, height };
- }
-
- /** Fit the current selection into view. No-op if nothing is selected. */
- const fitToSelection = (): void => {
- const selection = [...query(world, [c.Selected, Or(c.Geometry, c.Group)])];
- const bounds = computeSceneSpaceBounds(selection);
- if (bounds) centerRect(bounds);
- }
-
- /** Fit the given entities into view. No-op if none have bounds yet. */
- const focusEntities = (eids: number[]): void => {
- if (eids.length === 0) return;
- const bounds = computeSceneSpaceBounds(eids);
- if (bounds) centerRect(bounds);
- }
-
- /** Fit the active scene into view. No-op if no active scene. */
- const fitToActiveScene = (): void => {
- const sceneEid = world.selection.scene;
- if (sceneEid === null) return;
- const Position = world.components.Position;
- const Computed = world.components.Computed;
- const x = Position.x[sceneEid] ?? 0;
- const y = Position.y[sceneEid] ?? 0;
- const width = Computed.width[sceneEid];
- const height = Computed.height[sceneEid];
- if (width <= 0 || height <= 0) return;
- centerRect({ x, y, width, height });
- }
-
- // ── Wheel: zoom + scroll-pan ──
-
- const handleWheel = (e: WheelEvent) => {
- if (!canvas) return;
-
- e.preventDefault();
- const delta = e.deltaMode === 0 ? 1 : (e.deltaMode === 1 ? 16 : 600);
-
- if (e.ctrlKey || e.metaKey) {
- const rect = canvas?.getBoundingClientRect();
- const screenX = e.clientX - rect.left;
- const screenY = e.clientY - rect.top;
- const dy = Math.max(-50, Math.min(50, e.deltaY * delta));
- zoom(screenX, screenY, Math.exp(-dy * 0.01));
- } else {
- pan(e.deltaX * delta, e.deltaY * delta);
- }
-
- persistWorldState(world);
- };
-
- // ── Space + drag: pan ──
- const handleKeyUp = (e: KeyboardEvent) => {
- // Releasing space mid-drag ends the pan even though the pointer is still down.
- if (e.code === 'Space') isPanning = false;
- if (world.selection.tool !== ToolType.HAND) {
- world.cursor.update('default');
- }
- };
-
- const handlePointerDown = (e: PointerEvent) => {
- if (!isPanMode(world) || !canvas) return;
-
- isPanning = true;
- canvas.setPointerCapture(e.pointerId);
-
- const rect = canvas.getBoundingClientRect();
- panStartX = e.clientX - rect.left;
- panStartY = e.clientY - rect.top;
- panStartCamera = [cam.e, cam.f];
- };
-
- const handlePointerMove = (e: PointerEvent) => {
- if (!isPanning || !canvas) return;
-
- const rect = canvas.getBoundingClientRect();
- const dx = (e.clientX - rect.left) - panStartX;
- const dy = (e.clientY - rect.top) - panStartY;
-
- cam.e = panStartCamera[0] + dx;
- cam.f = panStartCamera[1] + dy;
- };
-
- const handlePointerUp = () => {
- if (!isPanning || !canvas) return;
- isPanning = false;
- persistWorldState(world);
- };
-
- const init = (el: HTMLCanvasElement) => {
- if (canvas) return;
-
- canvas = el;
- canvas.addEventListener('wheel', handleWheel, { passive: false });
- canvas.addEventListener('pointerdown', handlePointerDown);
- canvas.addEventListener('pointermove', handlePointerMove);
- canvas.addEventListener('pointerup', handlePointerUp);
- canvas.addEventListener('pointercancel', handlePointerUp);
- window.addEventListener('keyup', handleKeyUp);
- };
-
- function dispose() {
- canvas?.removeEventListener('wheel', handleWheel);
- canvas?.removeEventListener('pointerdown', handlePointerDown);
- canvas?.removeEventListener('pointermove', handlePointerMove);
- canvas?.removeEventListener('pointerup', handlePointerUp);
- canvas?.removeEventListener('pointercancel', handlePointerUp);
- window.removeEventListener('keyup', handleKeyUp);
- }
-
- return {
- dispose,
- pan,
- zoom,
- zoomBy,
- resetZoom,
- centerRect,
- fitToSelection,
- fitToActiveScene,
- focusEntities,
- init,
- };
-}
diff --git a/apps/web/src/components/engine/systems/dom.ts b/apps/web/src/components/engine/systems/dom.ts
deleted file mode 100644
index 448853e2..00000000
--- a/apps/web/src/components/engine/systems/dom.ts
+++ /dev/null
@@ -1,88 +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 { query, asBuffer, hasComponent, type QueryTerm } from 'bitecs';
-
-import type { EngineWorld } from '../api/world';
-
-// Global set of active subscriptions.
-export type ValueSyncSub = {
- store: unknown[];
- eid: number;
- prev: unknown;
- set: (v: unknown) => void;
-};
-
-export const domSyncSubs = new Set<ValueSyncSub>();
-
-// Tag-based subscriptions (boolean presence via hasComponent).
-export type TagSyncSub = {
- component: object;
- eid: number;
- prev: boolean;
- set: (v: boolean) => void;
-};
-
-export const tagSyncSubs = new Set<TagSyncSub>();
-
-// Query-based subscriptions for tag components (e.g. Selected, Active).
-export type QuerySyncSub = {
- query: QueryTerm[];
- prev: Uint32Array;
- set: (result: number[]) => void;
-};
-
-export const querySyncSubs = new Set<QuerySyncSub>();
-
-// World-level state subscriptions. The getter reads a value out of the world
-// (e.g. `w => w.selection.scene`, `w => w.camera.a`, `w => w.background`).
-export type WorldStateSyncSub = {
- get: (world: EngineWorld) => unknown;
- prev: unknown;
- set: (v: unknown) => void;
-};
-
-export const worldStateSyncSubs = new Set<WorldStateSyncSub>();
-
-/**
- * Flush all subscriptions – called once per frame by the sync system.
- * Compares the current ECS value against the cached previous value and
- * updates the SolidJS signal only when they differ.
- */
-export function syncDomSystem(world: EngineWorld): void {
- for (const sub of domSyncSubs) {
- const next = sub.store[sub.eid];
- if (next !== undefined && next !== sub.prev) {
- sub.prev = next;
- sub.set(next);
- }
- }
-
- for (const sub of tagSyncSubs) {
- const next = hasComponent(world, sub.eid, sub.component);
- if (next !== sub.prev) {
- sub.prev = next;
- sub.set(next);
- }
- }
-
- for (const sub of querySyncSubs) {
- const result = query(world, sub.query, asBuffer) as Uint32Array;
-
- // We may need to find a better way to compare the arrays.
- // This is in a hot path, so we need to be careful.
- if (result.length !== sub.prev.length || result[0] !== sub.prev[0]) {
- sub.prev = new Uint32Array(result);
- sub.set([...Array.from(result)]);
- }
- }
-
- for (const sub of worldStateSyncSubs) {
- const next = sub.get(world);
- if (next !== sub.prev) {
- sub.prev = next;
- sub.set(next);
- }
- }
-}
diff --git a/apps/web/src/components/engine/systems/hud.ts b/apps/web/src/components/engine/systems/hud.ts
deleted file mode 100644
index cc03ee76..00000000
--- a/apps/web/src/components/engine/systems/hud.ts
+++ /dev/null
@@ -1,500 +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 { hasComponent, Not, Or, query } from "bitecs";
-import { invert2D, multiply2D, rectToQuad, rotate2D, scale2D, transformPoint, translate2D, type Mat2D } from "../math";
-import * as callbacks from "../api/interactions";
-import { entityQuad, entityWorldMat, getMarqueeQuad, getMountedNameInput, getSelectionMask } from "../utils/interaction";
-import { ChildOf } from "../components";
-
-import type { EngineWorld } from "../api/world";
-
-export function hudSystem(world: EngineWorld) {
- const c = world.components;
- const ctx = world.ctx!;
- const res = world.resolution;
-
- // Draw snap guides
- if (!world.keys.has('mod') && world.snapLines.length > 0) {
- const res = world.resolution;
- const crossSize = Math.round(3 * res);
-
- ctx.resetTransform();
- ctx.beginPath();
-
- for (const snapLine of world.snapLines) {
- ctx.moveTo(snapLine.from.x, snapLine.from.y);
- ctx.lineTo(snapLine.to.x, snapLine.to.y);
- }
-
- ctx.closePath();
-
- ctx.strokeStyle = '#F43535';
- ctx.lineWidth = Math.round(res);
- ctx.stroke();
-
- const drawCross = (x: number, y: number) => {
- ctx.moveTo(x - crossSize, y - crossSize);
- ctx.lineTo(x + crossSize, y + crossSize);
- ctx.moveTo(x - crossSize, y + crossSize);
- ctx.lineTo(x + crossSize, y - crossSize);
- };
-
- ctx.beginPath();
-
- for (const snapLine of world.snapLines) {
- drawCross(snapLine.from.x, snapLine.from.y);
- drawCross(snapLine.to.x, snapLine.to.y);
- }
-
- ctx.strokeStyle = '#F43535';
- ctx.lineWidth = Math.round(res);
- ctx.stroke();
- }
-
- // Draw hover indicators
- {
- for (const eid of query(world, [c.Hovering, Or(c.Geometry, c.Group), Not(c.Sequential), Not(c.Culled), Not(c.Deleted)])) {
- const quad = entityQuad(world, eid);
-
- ctx.resetTransform();
- ctx.beginPath();
- ctx.moveTo(quad[0].x, quad[0].y);
- ctx.lineTo(quad[1].x, quad[1].y);
- ctx.lineTo(quad[2].x, quad[2].y);
- ctx.lineTo(quad[3].x, quad[3].y);
- ctx.closePath();
-
- ctx.strokeStyle = '#008CFF';
- ctx.lineWidth = Math.round(2 * res);
- ctx.stroke();
- }
- }
-
- // Draw headers
- for (const eid of query(world, [Or(c.Name, c.Generating), Not(c.Deleted), Not(c.Culled), Not(c.Hidden), Not(ChildOf('*'))])) {
- const isGenerating = hasComponent(world, eid, c.Generating);
- if (!isGenerating && !hasComponent(world, eid, c.Name)) continue;
-
- const name = isGenerating ? 'Generating...' : c.Name[eid];
- if (!name) continue;
-
- const header = getHeaderLayout(world, eid);
- const hasTimelineComp = hasComponent(world, eid, c.Playback);
-
- ctx.setTransform(header.mat.a, header.mat.b, header.mat.c, header.mat.d, header.mat.e, header.mat.f);
-
- let labelStartX = 0;
-
- const isActive = world.selection.scene === eid;
- const isSelected = hasComponent(world, eid, c.Selected);
-
- // Play/pause button (only for entities with Timeline)
- if (hasTimelineComp) {
- ctx.save();
- ctx.fillStyle = isSelected ? '#cce8ff' : 'rgba(242, 242, 242, 0.64)';
- if (c.Playback.playing[eid] !== 1) {
- ctx.translate(2, 3);
- ctx.fill(PLAY_PATH);
- } else {
- ctx.translate(4, 3);
- ctx.fill(PAUSE_PATH);
- }
- ctx.restore();
- labelStartX = 18;
-
- world.hitRegions.push({
- target: { kind: 'hud', id: `play:${eid}`, quad: rectToQuad(header.mat, 16, 22) },
- callback: callbacks.handlePlayInteraction,
- });
- }
-
- // Label
- const labelMat = multiply2D(header.mat, translate2D(labelStartX, 3));
- ctx.setTransform(labelMat.a, labelMat.b, labelMat.c, labelMat.d, labelMat.e, labelMat.f);
- ctx.textAlign = 'left';
- ctx.textBaseline = 'top';
- ctx.font = '350 11px Inter, sans-serif';
-
- const activeIndicatorWidth = isActive ? 60 : 0;
- const fittedText = fitTextToWidth(ctx, name, header.width - labelStartX - activeIndicatorWidth);
-
- if (fittedText) {
- ctx.fillStyle = isSelected ? '#cce8ff' : 'rgba(242, 242, 242, 0.64)';
- ctx.fillText(fittedText.value, 0, 0);
-
- const labelHitMat = multiply2D(header.mat, translate2D(labelStartX, 0));
- world.hitRegions.push({
- target: { kind: 'hud', id: `label:${eid}`, quad: rectToQuad(labelHitMat, fittedText.width + 4, 22) },
- callback: callbacks.handleLabelInteraction,
- });
- }
-
- const { input, eid: inputEid } = getMountedNameInput();
-
- // Position the label input overlay if editing this entity
- if (input && inputEid === eid) {
- const inputMat = multiply2D(header.mat, translate2D(labelStartX - 3, 0));
- const res = world.resolution;
- input.style.left = `${inputMat.e / res}px`;
- input.style.top = `${inputMat.f / res}px`;
- input.style.transform = `rotate(${header.rotation}deg)`;
- input.style.width = `${(fittedText?.width ?? 40) + 4}px`;
- }
-
- // Active indicator
- if (header.width > activeIndicatorWidth && isActive) {
- ctx.setTransform(header.mat.a, header.mat.b, header.mat.c, header.mat.d, header.mat.e, header.mat.f);
- ctx.translate((fittedText?.width ?? 0) + labelStartX + 4, 0);
- ctx.beginPath();
- ctx.roundRect(0, 0, 40, 16, 2);
- ctx.closePath();
- ctx.fillStyle = isSelected ? '#013e70' : '#292929';
- ctx.fill();
-
- ctx.translate(4, 3);
- ctx.textAlign = 'left';
- ctx.textBaseline = 'top';
- ctx.font = '350 11px Inter, sans-serif';
- ctx.fillStyle = isSelected ? '#cce8ff' : 'rgba(242, 242, 242, 0.64)';
- ctx.fillText("Active", 0, 0);
- }
-
- // Duration (only for entities with Timeline)
- if (hasTimelineComp && ((fittedText?.width ?? 0) + activeIndicatorWidth + 60) < header.width) {
- ctx.setTransform(header.mat.a, header.mat.b, header.mat.c, header.mat.d, header.mat.e, header.mat.f);
- ctx.translate(header.width, 3);
-
- ctx.textAlign = 'right';
- ctx.textBaseline = 'top';
- ctx.font = '400 11px JetBrains Mono';
-
- const duration = c.Computed.duration[eid];
-
- const durationSeconds = duration / world.frameRate;
- const m = Math.floor(durationSeconds / 60).toString().padStart(2, '0');
- const s = Math.floor(durationSeconds % 60).toString().padStart(2, '0');
-
- if (isSelected) {
- ctx.globalAlpha = 0.7;
- ctx.fillStyle = '#cce8ff';
- } else {
- ctx.fillStyle = 'rgba(121, 121, 121, 1)';
- }
- ctx.fillText(`${m}:${s}`, 0, 0);
- ctx.globalAlpha = 1;
- }
- }
-
- const mask = getSelectionMask(world);
-
- // Draw selection mask
- if (mask && world.hudMode !== 'moving') {
- const { width, height, mat } = mask;
- const selection = query(world, [Or(c.Geometry, c.Group), c.Selected, Not(c.Sequential)]);
-
- ctx.resetTransform();
- ctx.beginPath();
-
- // Draw the quad of each node
- for (const eid of selection) {
- const quad = entityQuad(world, eid);
-
- ctx.moveTo(quad[0].x, quad[0].y);
- ctx.lineTo(quad[1].x, quad[1].y);
- ctx.lineTo(quad[2].x, quad[2].y);
- ctx.lineTo(quad[3].x, quad[3].y);
- ctx.lineTo(quad[0].x, quad[0].y);
- }
-
- // Draw the selection mask
- ctx.setTransform(mat.a, mat.b, mat.c, mat.d, mat.e, mat.f);
- ctx.rect(0, 0, width, height);
-
- ctx.closePath();
-
- ctx.strokeStyle = '#008CFF';
- ctx.lineWidth = Math.round(selection.length === 1 ? 2 : 1 * res);
- ctx.stroke();
-
- world.hitRegions.push({
- target: { kind: 'hud', id: 'selection', quad: rectToQuad(mat, width, height) },
- callback: callbacks.handleMaskInteraction,
- });
-
- // Set rotation regions
-
- const rr = Math.round(16 * res);
-
- const rotationRegions = {
- rtl: [-rr, -rr],
- rtr: [width, -rr],
- rbr: [width, height],
- rbl: [-rr, height],
- };
-
- for (const [id, [x, y]] of Object.entries(rotationRegions)) {
- const tr = multiply2D(mat, translate2D(x, y));
- world.hitRegions.push({
- target: { kind: 'hud', id, quad: rectToQuad(tr, rr, rr) },
- callback: callbacks.handleRotateInteraction,
- });
- }
-
- // Set scale regions
-
- const sr = Math.round(6 * res);
- const sr2 = Math.round(sr / 2);
-
- const scaleRegions = {
- l: [-sr2, 0, sr, height],
- t: [0, -sr2, width, sr],
- r: [width - sr2, 0, sr, height],
- b: [0, height - sr2, width, sr],
- };
-
- for (const [id, [x, y, width, height]] of Object.entries(scaleRegions)) {
- const tr = multiply2D(mat, translate2D(x, y));
- world.hitRegions.push({
- target: { kind: 'hud', id, quad: rectToQuad(tr, width, height) },
- callback: callbacks.handleResizeInteraction,
- });
- }
-
- // Draw the selection handles (handles move with the mask)
-
- ctx.beginPath();
-
- const hs = Math.round(8 * res);
- const hs2 = Math.round(hs / 2);
-
- const cornerHandles = {
- tl: [-hs2, -hs2],
- tr: [width - hs2, -hs2],
- br: [width - hs2, height - hs2],
- bl: [-hs2, height - hs2],
- };
-
- for (const [id, [x, y]] of Object.entries(cornerHandles)) {
- const tr = multiply2D(mat, translate2D(x, y));
- ctx.setTransform(tr.a, tr.b, tr.c, tr.d, tr.e, tr.f);
- ctx.rect(0, 0, hs, hs);
- world.hitRegions.push({
- target: { kind: 'hud', id, quad: rectToQuad(tr, hs, hs) },
- callback: callbacks.handleResizeInteraction,
- });
- }
-
- ctx.closePath();
-
- ctx.fillStyle = '#FFFFFF';
- ctx.fill();
- ctx.strokeStyle = '#008CFF';
- ctx.lineWidth = Math.round(res);
- ctx.stroke();
- }
-
- // Render the dimensions
- if (mask && world.hudMode !== 'moving') {
- const selection = query(world, [c.Selected, Or(c.Geometry, c.Group), Not(c.Sequential)]);
-
- let rotation = 0;
- let center = 0;
- let x = 0;
- let y = 0;
- let width = 0;
- let height = 0;
-
- if (selection.length === 1) {
- const eid = selection[0];
-
- // get the lowest 2 points sorted by x
- const lowestPoints = [...entityQuad(world, eid)]
- .sort((a, b) => b.y - a.y)
- .slice(0, 2)
- .sort((a, b) => a.x - b.x);
-
- // get rotation between points in degrees
- rotation = Math.atan2(
- lowestPoints[1].y - lowestPoints[0].y,
- lowestPoints[1].x - lowestPoints[0].x
- ) * 180 / Math.PI;
-
- const a = lowestPoints[0].x - lowestPoints[1].x;
- const b = lowestPoints[0].y - lowestPoints[1].y;
-
- center = Math.sqrt(a * a + b * b) / 2;
- width = c.Computed.width[eid] * Math.abs(c.Computed.scaleX[eid]);
- height = c.Computed.height[eid] * Math.abs(c.Computed.scaleY[eid]);
- x = lowestPoints[0].x;
- y = lowestPoints[0].y;
- } else {
- // Get inverse transform and zero out translation to transform as vector, not point
- const worldTransform = entityWorldMat(world, null);
- const inverseTransform = invert2D(worldTransform);
- inverseTransform.e = 0;
- inverseTransform.f = 0;
-
- const transformed = transformPoint(inverseTransform, mask.width, mask.height);
-
- width = transformed.x;
- height = transformed.y;
-
- x = mask.mat.e;
- y = mask.mat.f + mask.height;
- center = mask.width / 2;
- }
-
- ctx.save();
-
- let mat = translate2D(x, y);
- mat = multiply2D(mat, rotate2D(rotation));
- mat = multiply2D(mat, scale2D(res, res));
- mat = multiply2D(mat, translate2D(center / res, 16));
-
- ctx.setTransform(mat.a, mat.b, mat.c, mat.d, mat.e, mat.f);
-
- const roundedWidth = Math.round(width * 100) / 100;
- const roundedHeight = Math.round(height * 100) / 100;
- const text = `${roundedWidth}x${roundedHeight}`;
-
- ctx.textAlign = 'center';
- ctx.textBaseline = 'middle';
- ctx.font = '11px Inter, sans-serif';
-
- const tw = ctx.measureText(text).width + 8; // text width + padding
- const tw2 = tw / 2; // text width / 2
- const br = 3; // border radius
- const th = 16; // text height + padding
- const to = -(th + 2) / 2; // text offset
-
- ctx.beginPath();
- ctx.roundRect(-tw2, to, tw, th, br);
- ctx.closePath();
- ctx.fillStyle = '#008CFF';
- ctx.fill();
-
- ctx.fillStyle = '#FFFFFF';
- ctx.fillText(text, 0, 0);
-
- ctx.restore();
- }
-
-
- // Draw marquee selection rectangle
- const marqueeQuad = getMarqueeQuad(world);
- if (marqueeQuad) {
- const x = marqueeQuad[0].x;
- const y = marqueeQuad[0].y;
- const w = marqueeQuad[1].x - marqueeQuad[0].x;
- const h = marqueeQuad[3].y - marqueeQuad[0].y;
-
- ctx.resetTransform();
- ctx.beginPath();
- ctx.rect(x, y, w, h);
- ctx.closePath();
-
- ctx.fillStyle = '#008CFF';
- ctx.globalAlpha = 0.15;
- ctx.fill();
- ctx.globalAlpha = 1;
- ctx.strokeStyle = '#008CFF';
- ctx.lineWidth = Math.round(res);
- ctx.stroke();
- }
-}
-
-const PLAY_PATH = new Path2D("M0.24182 0.0683919C0.3919 -0.017789 0.5784 -0.022846 0.73338 0.0550586L9.7334 4.57886C9.8974 4.66129 10 4.82339 10 4.99999C10 5.17659 9.8974 5.3387 9.7334 5.42113L0.73338 9.94494C0.5784 10.0228 0.3919 10.0178 0.24182 9.93161C0.0917501 9.84541 0 9.69065 0 9.5238V0.476191C0 0.309296 0.0917501 0.154573 0.24182 0.0683919ZM1 1.26595V8.73399L8.4288 4.99999L1 1.26595Z");
-const PAUSE_PATH = new Path2D("M1.08197 0.518868C1.08197 0.232311 0.839803 0 0.540984 0C0.242203 0 0 0.232311 0 0.518868V9.48113C0 9.76764 0.242203 10 0.540984 10C0.839803 10 1.08197 9.76764 1.08197 9.48113V0.518868ZM6 0.518868C6 0.232311 5.75784 0 5.45902 0C5.1602 0 4.91803 0.232311 4.91803 0.518868V9.48113C4.91803 9.76764 5.1602 10 5.45902 10C5.75784 10 6 9.76764 6 9.48113V0.518868Z");
-
-function getHeaderLayout(world: EngineWorld, eid: number) {
- const quad = entityQuad(world, eid);
-
- // Find two highest points, sorted by x
- const highestPoints = [...quad]
- .sort((a, b) => a.y - b.y)
- .slice(0, 2)
- .sort((a, b) => a.x - b.x);
-
- const rotation = Math.atan2(
- highestPoints[1].y - highestPoints[0].y,
- highestPoints[1].x - highestPoints[0].x
- ) * 180 / Math.PI;
-
- const x = highestPoints[0].x;
- const y = highestPoints[0].y;
-
- const dx = highestPoints[0].x - highestPoints[1].x;
- const dy = highestPoints[0].y - highestPoints[1].y;
- const width = Math.sqrt(dx * dx + dy * dy) / world.resolution;
-
- const rad = rotation * Math.PI / 180;
- const cos = Math.cos(rad);
- const sin = Math.sin(rad);
-
- const res = world.resolution;
- const mat: Mat2D = {
- a: cos * res,
- b: sin * res,
- c: -sin * res,
- d: cos * res,
- e: x + (-22) * (-sin * res),
- f: y + (-22) * (cos * res),
- };
-
- return { x, y, rotation, width, mat };
-};
-
-const TEXT_WIDTH_CACHE: Map<string, number> = new Map();
-type Ctx2D = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
-
-function fitTextToWidth(ctx: Ctx2D, text: string, maxWidth: number) {
- const getTextWidth = (text: string) => {
- if (TEXT_WIDTH_CACHE.has(text)) {
- return TEXT_WIDTH_CACHE.get(text)!;
- }
- const width = ctx.measureText(text).width;
- TEXT_WIDTH_CACHE.set(text, width);
- return width;
- };
-
- const textWidth = getTextWidth(text);
-
- if (textWidth > maxWidth) {
- const availableWidth = maxWidth - getTextWidth('...');
-
- if (availableWidth > 0) {
- let start = 0;
- let end = text.length;
- let bestLength = 0;
-
- while (start <= end) {
- const mid = Math.floor((start + end) / 2);
- const testText = text.slice(0, mid);
- const testWidth = getTextWidth(testText);
-
- if (testWidth <= availableWidth) {
- bestLength = mid;
- start = mid + 1;
- } else {
- end = mid - 1;
- }
- }
-
- if (bestLength > 0) {
- const value = text.slice(0, bestLength) + '...';
- return {
- value,
- width: getTextWidth(value),
- };
- }
-
- return null;
- } else {
- return null;
- }
- }
-
- return { value: text, width: textWidth };
-}
diff --git a/apps/web/src/components/engine/systems/index.ts b/apps/web/src/components/engine/systems/index.ts
deleted file mode 100644
index 9ef32f1a..00000000
--- a/apps/web/src/components/engine/systems/index.ts
+++ /dev/null
@@ -1,10 +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/. */
-
-export * from './transform';
-export * from './render';
-export * from './camera-controller';
-export * from './playback';
-export * from './motion';
-export * from './keyboard';
diff --git a/apps/web/src/components/engine/systems/input-system.ts b/apps/web/src/components/engine/systems/input-system.ts
deleted file mode 100644
index 975bbc39..00000000
--- a/apps/web/src/components/engine/systems/input-system.ts
+++ /dev/null
@@ -1,195 +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 { assert, isInputTarget } from "@/utils";
-import { addComponent, removeComponent } from "bitecs";
-import { pointInQuad } from "../math";
-import { isPointerInEntity } from "../utils";
-import { ToolType } from "../components";
-import { clearComponent } from "../api/events";
-
-import type { EngineWorld } from "../api/world";
-import type { CustomPointerEvent, CustomPointerEventType, HitRegion } from "../types/input";
-
-export const isPanMode = (world: EngineWorld) =>
- world.selection.tool === ToolType.HAND || world.keys.has(' ');
-
-
-const events: CustomPointerEvent[] = []
-let initializedProject = null as string | null;
-
-function addEventListeners(world: EngineWorld) {
- const canvas = world.canvas;
- const projectId = world.projectId;
-
- if (initializedProject === projectId || !canvas) return;
- assert(canvas instanceof HTMLCanvasElement, 'Canvas must be an HTMLCanvasElement');
-
- initializedProject = projectId;
-
- const pushEvent = (type: CustomPointerEventType, event: PointerEvent | MouseEvent) => {
- const rect = canvas.getBoundingClientRect();
- const dpr = window.devicePixelRatio;
-
- events.push({
- type,
- clientX: (event.clientX - rect.left) * dpr,
- clientY: (event.clientY - rect.top) * dpr,
- button: event.button,
- });
- }
-
- // click/dblclick listeners
- canvas.addEventListener('click', (event) => pushEvent('click', event));
- canvas.addEventListener('dblclick', (event) => pushEvent('dblclick', event));
-
- // pointer down/up/cancel listeners
- canvas.addEventListener('pointerdown', (event) => pushEvent('pointerdown', event));
- canvas.addEventListener('pointermove', (event) => pushEvent('pointermove', event));
-
- // make globally
- window.addEventListener('pointerup', (event) => pushEvent('pointerup', event));
- window.addEventListener('pointercancel', (event) => pushEvent('pointerup', event));
-
- // keyboard listeners
- window.addEventListener('keydown', (event) => {
- if (isInputTarget(event)) return;
-
- world.keys.add(event.key.toLowerCase());
-
- if (event.key === 'Meta' || event.key === 'Control') {
- world.keys.add('mod');
- }
- });
- window.addEventListener('keyup', (event) => {
- world.keys.delete(event.key.toLowerCase());
-
- if (event.key === 'Meta' || event.key === 'Control') {
- world.keys.delete('mod');
- }
- });
-}
-
-
-let lastHoverRegion: HitRegion | null = null;
-let lastDragRegion: HitRegion | null = null;
-
-export function inputSystem(world: EngineWorld) {
- addEventListeners(world);
- const c = world.components;
- const panMode = isPanMode(world);
-
- for (const event of events) {
- world.pointer.clientX = event.clientX;
- world.pointer.clientY = event.clientY;
- world.pointer.button = event.button;
-
- if (event.type === 'pointerdown') {
- world.pointer.state = 'pressed';
- world.pointer.dragStartX = event.clientX;
- world.pointer.dragStartY = event.clientY;
- } else if (event.type === 'pointerup') {
- world.pointer.state = 'lifted';
- }
-
- if (panMode) continue;
-
- const region = hitTest(world, event);
-
- // handle pointer enter/leave callbacks
- if (event.type === 'pointermove') {
- const lastTarget = lastHoverRegion?.target.id ?? null;
- const currentTarget = region?.target.id ?? null;
-
- if (lastTarget !== currentTarget && lastTarget !== null) {
- if (lastHoverRegion?.target.kind === 'entity') {
- removeComponent(world, lastHoverRegion.target.id, c.Hovering);
- }
- lastHoverRegion?.callback(world, {
- ...event,
- target: lastHoverRegion.target,
- type: 'pointerleave' as const,
- })
- }
-
- if (currentTarget !== null && currentTarget !== lastTarget) {
- if (region?.target.kind === 'entity') {
- addComponent(world, region.target.id, c.Hovering);
- }
- region?.callback(world, {
- ...event,
- target: region.target,
- type: 'pointerenter' as const,
- })
- }
-
- lastHoverRegion = region;
- }
-
- // handle drag start/drag/end callbacks
- if (event.type === 'pointerdown') {
- if (region?.target.kind === 'entity') {
- addComponent(world, region.target.id, c.Dragging);
- }
- region?.callback(world, {
- ...event,
- target: region.target,
- type: 'dragstart' as const,
- });
- lastDragRegion = region;
- }
-
- if (event.type === 'pointermove') {
- lastDragRegion?.callback(world, {
- ...event,
- target: lastDragRegion.target,
- type: 'drag' as const,
- });
- }
-
- if (event.type === 'pointerup') {
- if (lastDragRegion?.target.kind === 'entity'){
- removeComponent(world, lastDragRegion.target.id, c.Dragging);
- }
- lastDragRegion?.callback(world, {
- ...event,
- target: lastDragRegion.target,
- type: 'dragend' as const,
- });
- lastDragRegion = null;
- }
-
- // all other callbacks
- region?.callback(world, { ...event, target: region.target });
- }
-
- if (panMode) {
- world.cursor.update(world.pointer.state === 'pressed' ? 'grabbing' : 'grab');
- clearComponent(world, c.Hovering, false);
- clearComponent(world, c.Dragging, false);
- }
-
- events.length = 0;
- world.hitRegions.length = 0;
-}
-
-function hitTest(world: EngineWorld, event: CustomPointerEvent): HitRegion | null {
- const x = event.clientX;
- const y = event.clientY;
-
- for (let i = world.hitRegions.length - 1; i >= 0; i--) {
- const hitRegion = world.hitRegions[i];
-
- if (hitRegion.target.kind === 'entity') {
- const eid = hitRegion.target.id;
- if (isPointerInEntity(world, eid, { x, y })) {
- return hitRegion;
- }
- } else if (pointInQuad(x, y, hitRegion.target.quad)) {
- return hitRegion;
- }
- }
-
- return null;
-}
diff --git a/apps/web/src/components/engine/systems/keyboard.ts b/apps/web/src/components/engine/systems/keyboard.ts
deleted file mode 100644
index 7ecf3688..00000000
--- a/apps/web/src/components/engine/systems/keyboard.ts
+++ /dev/null
@@ -1,425 +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 { ToolType, ChildOf } from '../components';
-import {
- copySelection,
- pasteClipboard,
- duplicateSelection,
- togglePlayback,
- shuttleForward,
- shuttleReverse,
- stopPlayback,
- splitAtPlayhead,
- groupSelection,
- ungroupSelection,
- frameSelection,
- isScene,
- isText,
- deleteEntity,
- switchActiveScene,
- reorderSelection,
- getParentEntity,
- isSequence,
-} from '../api';
-import {
- selectStageEntities,
- selectParentEntities,
- selectChildEntities,
-} from '../selection';
-import { SPACE_HOLD_DELAY_MS } from '../constants';
-import { isInputTarget } from '@/utils';
-import { query, None, hasComponent, Or, Not } from 'bitecs';
-import { addComponent, clearComponent, removeComponent, setComponent } from '../api/events';
-
-import type { EngineWorld } from '../api/world';
-import type { Camera } from './camera-controller';
-
-type KeyboardHandler = (e: KeyboardEvent) => void;
-
-export function createKeyboardSystem(world: EngineWorld, camera: Camera) {
- const c = world.components;
- const history = world.history;
-
- let spaceHandTimer: number | undefined;
- let spaceDownCameraHash: string | undefined;
- let timelineFocused = false;
-
- const onTimelineEnter = () => { timelineFocused = true; };
- const onTimelineLeave = () => { timelineFocused = false; };
-
- const cameraHash = () =>
- `${world.camera.a},${world.camera.d},${world.camera.e},${world.camera.f}`;
-
- const moveSelection = (amount: number, axis: 'x' | 'y') => {
- world.history.transaction('Nudge', () => {
- for (const eid of query(world, [c.Selected, Or(c.Geometry, c.Group, c.AdjustmentLayer)])) {
- setComponent(world, eid, c.Position, { [axis]: (c.Position[axis][eid] ?? 0) + amount });
- }
- });
- };
-
- const seek = (deltaFrames: number) => {
- const sid = world.selection.scene;
- const fps = world.frameRate;
- if (sid === null) return;
- const next = c.Computed.localTime[sid] + deltaFrames;
- c.Computed.localTimeInSeconds[sid] = next / fps;
- c.Computed.localTime[sid] = next;
- };
-
- const deleteSelection = () => {
- const selection = [...query(world, [world.components.Selected])];
- const activeScene = world.selection.scene;
-
- // Tracks owning any selected keyframe — a track whose every keyframe is
- // deleted is empty and should go too (mirrors toggleKeyframeTrack).
- const keyframeTracks = new Set<number>();
- // Parents of any selected node — candidates for empty-sequence cleanup.
- const containers = new Set<number>();
- for (const eid of selection) {
- if (hasComponent(world, eid, c.Keyframe)) {
- const tid = getParentEntity(world, eid);
- if (tid !== null) keyframeTracks.add(tid);
- } else {
- const pid = getParentEntity(world, eid);
- if (pid !== null) containers.add(pid);
- }
- }
-
- world.history.transaction('Delete', () => {
- selection.forEach(eid => deleteEntity(world, eid));
-
- for (const tid of keyframeTracks) {
- if (query(world, [c.Keyframe, ChildOf(tid), Not(c.Deleted)]).length === 0) {
- deleteEntity(world, tid);
- }
- }
-
- // A sequence emptied by the deletion is dropped too, cascading to any
- // parent sequence it in turn empties.
- for (const start of containers) {
- let pid: number | null = start;
- while (pid !== null && !hasComponent(world, pid, c.Deleted) && isSequence(world, pid)) {
- if (query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), ChildOf(pid), Not(c.Deleted)]).length > 0) break;
- const parent = getParentEntity(world, pid);
- deleteEntity(world, pid);
- pid = parent;
- }
- }
- });
- clearComponent(world, c.Selected, false);
-
- if (selection.find(eid => eid === activeScene)) {
- const children = query(world, [c.Geometry, None(ChildOf('*'), c.Deleted, c.Hidden)]);
- const nextScene = children.find((eid: number) => isScene(world, eid));
- switchActiveScene(world, nextScene ?? null);
- }
- };
-
- const handleSpaceDown = () => {
- if (!spaceDownCameraHash) {
- spaceDownCameraHash = cameraHash();
- if (timelineFocused) {
- togglePlayback(world);
- }
- }
- if (!spaceHandTimer) {
- spaceHandTimer = window.setTimeout(() => {
- world.selection.tool = ToolType.HAND;
- spaceHandTimer = undefined;
- }, SPACE_HOLD_DELAY_MS);
- }
- };
-
- const handleSpaceUp = () => {
- if (spaceHandTimer) {
- clearTimeout(spaceHandTimer);
- spaceHandTimer = undefined;
- }
- if (spaceDownCameraHash && !timelineFocused) {
- if (spaceDownCameraHash === cameraHash()) {
- togglePlayback(world);
- }
- }
- spaceDownCameraHash = undefined;
- world.selection.tool = ToolType.MOVE;
- };
-
- // ── Modifier key handlers ──
-
- const modUndo: KeyboardHandler = (e) => {
- e.preventDefault();
- if (e.shiftKey) {
- history.redo();
- } else {
- history.undo();
- }
- };
-
- const modCopy: KeyboardHandler = (e) => {
- if (!isInputTarget(e)) {
- e.preventDefault();
- copySelection(world);
- }
- };
-
- const modPaste: KeyboardHandler = (e) => {
- if (!isInputTarget(e)) {
- e.preventDefault();
- pasteClipboard(world);
- }
- };
-
- const modCut: KeyboardHandler = (e) => {
- if (!isInputTarget(e)) {
- e.preventDefault();
- copySelection(world);
- deleteSelection();
- }
- };
-
- const modDuplicate: KeyboardHandler = (e) => {
- if (!isInputTarget(e)) {
- e.preventDefault();
- duplicateSelection(world);
- }
- };
-
- const modSplit: KeyboardHandler = (e) => {
- if (!isInputTarget(e)) {
- e.preventDefault();
- splitAtPlayhead(world);
- }
- };
-
- const modBold: KeyboardHandler = (e) => {
- const eids = [...query(world, [c.Selected, c.Geometry])].filter(eid => isText(world, eid));
- if (eids.length === 0) {
- modSplit(e);
- return;
- }
- e.preventDefault();
- const allBold = eids.every((eid) => (c.TextStyle.fontWeight[eid] ?? '400') === '700');
- world.history.transaction('Toggle bold', () => {
- eids.forEach(eid => setComponent(world, eid, c.TextStyle, {
- fontWeight: allBold ? '400' : '700'
- }));
- });
- };
-
- const modHide: KeyboardHandler = (e) => {
- if (!e.shiftKey || isInputTarget(e)) return;
- e.preventDefault();
-
- const eids = query(world, [c.Selected, Or(c.Geometry, c.Group, c.AdjustmentLayer)]);
- const anyVisible = Array.from(eids).some((eid) => !hasComponent(world, eid, c.Hidden));
-
- world.history.transaction(anyVisible ? 'Hide' : 'Show', () => {
- if (anyVisible) {
- eids.forEach(eid => addComponent(world, eid, c.Hidden));
- } else {
- eids.forEach(eid => removeComponent(world, eid, c.Hidden));
- }
- });
- };
-
- const modGroup: KeyboardHandler = (e) => {
- if (isInputTarget(e)) return;
- e.preventDefault();
- if (e.altKey) {
- // Cmd/Ctrl+Alt+G — wrap the selection in a frame (scene) instead of a group.
- frameSelection(world);
- } else if (e.shiftKey) {
- ungroupSelection(world);
- } else {
- groupSelection(world);
- }
- };
-
- const modSequential: KeyboardHandler = (e) => {
- if (isInputTarget(e)) return;
- e.preventDefault();
- if (e.shiftKey) {
- ungroupSelection(world);
- } else {
- groupSelection(world, true);
- }
- };
-
- const modSelectAll: KeyboardHandler = (e) => {
- if (isInputTarget(e)) return;
- e.preventDefault();
- selectStageEntities(world);
- };
-
- const modZoomIn: KeyboardHandler = (e) => {
- e.preventDefault();
- camera.zoomBy(1.25);
- };
-
- const modZoomOut: KeyboardHandler = (e) => {
- e.preventDefault();
- camera.zoomBy(0.8);
- };
-
- const modZoomReset: KeyboardHandler = (e) => {
- e.preventDefault();
- camera.resetZoom();
- };
-
- const modZoomFit: KeyboardHandler = (e) => {
- if (isInputTarget(e)) return;
- e.preventDefault();
- camera.fitToActiveScene();
- };
-
- const modZoomSelection: KeyboardHandler = (e) => {
- if (isInputTarget(e)) return;
- e.preventDefault();
- camera.fitToSelection();
- };
-
- // ── Non-modifier key handlers ──
- const onSeekBackFrame: KeyboardHandler = () => seek(-1);
- const onSeekForwardFrame: KeyboardHandler = () => seek(1);
- const onSeekForwardSec: KeyboardHandler = () => seek(world.frameRate);
- const onSeekBackSec: KeyboardHandler = () => seek(-world.frameRate);
- const onMoveLeft: KeyboardHandler = (e) => moveSelection(e.shiftKey ? -10 : -1, 'x');
- const onMoveRight: KeyboardHandler = (e) => moveSelection(e.shiftKey ? 10 : 1, 'x');
- const onMoveUp: KeyboardHandler = (e) => moveSelection(e.shiftKey ? -10 : -1, 'y');
- const onMoveDown: KeyboardHandler = (e) => moveSelection(e.shiftKey ? 10 : 1, 'y');
- const onSpace: KeyboardHandler = (e) => {
- e.preventDefault();
- handleSpaceDown();
- };
- const onDelete: KeyboardHandler = (e) => {
- e.preventDefault();
- deleteSelection();
- };
- const onShuttleForward: KeyboardHandler = () => shuttleForward(world);
- const onShuttleReverse: KeyboardHandler = () => shuttleReverse(world);
- const onStopPlayback: KeyboardHandler = () => stopPlayback(world);
- const onToolMove: KeyboardHandler = () => (world.selection.tool = ToolType.MOVE);
- const onToolHand: KeyboardHandler = () => (world.selection.tool = ToolType.HAND);
- const onToolScene: KeyboardHandler = () => (world.selection.tool = ToolType.SCENE);
- const onToolText: KeyboardHandler = () => (world.selection.tool = ToolType.TEXT);
- const onToolRect: KeyboardHandler = () => (world.selection.tool = ToolType.RECT);
- const onSelectParent: KeyboardHandler = (e) => {
- e.preventDefault();
- selectParentEntities(world);
- };
- const onSelectChildren: KeyboardHandler = (e) => {
- e.preventDefault();
- selectChildEntities(world);
- };
- const onBringToFront: KeyboardHandler = (e) => {
- e.preventDefault();
- reorderSelection(world, 'front');
- };
- const onSendToBack: KeyboardHandler = (e) => {
- e.preventDefault();
- reorderSelection(world, 'back');
- };
- const onDeselect: KeyboardHandler = (e) => {
- e.preventDefault();
- clearComponent(world, c.Selected, false);
- const tool = world.selection.tool;
- if (tool === ToolType.MOVE || tool === ToolType.HAND) return;
- world.selection.tool = ToolType.MOVE;
- };
-
- // ── Keybinding tables ──
-
- const modBindings: Record<string, KeyboardHandler> = {
- 'z': modUndo,
- 'c': modCopy,
- 'v': modPaste,
- 'x': modCut,
- 'd': modDuplicate,
- 'b': modBold,
- 'h': modHide,
- 'g': modGroup,
- 's': modSequential,
- 'a': modSelectAll,
- '=': modZoomIn,
- '+': modZoomIn,
- '-': modZoomOut,
- '0': modZoomReset,
- '1': modZoomFit,
- '2': modZoomSelection,
- };
-
- const keyBindings: Record<string, KeyboardHandler> = {
- ' ': onSpace,
- 'arrowleft': onMoveLeft,
- 'arrowright': onMoveRight,
- 'arrowup': onMoveUp,
- 'arrowdown': onMoveDown,
- 'a': onSeekBackFrame,
- 'd': onSeekForwardFrame,
- 'w': onSeekForwardSec,
- 's': onSeekBackSec,
- 'backspace': onDelete,
- 'delete': onDelete,
- 'j': onShuttleReverse,
- 'k': onStopPlayback,
- 'l': onShuttleForward,
- 'v': onToolMove,
- 'h': onToolHand,
- 'f': onToolScene,
- 't': onToolText,
- 'r': onToolRect,
- '\\': onSelectParent,
- 'enter': onSelectChildren,
- 'escape': onDeselect,
- ']': onBringToFront,
- '[': onSendToBack,
- };
-
- const handleKeyDown = (e: KeyboardEvent) => {
- const isMod = e.metaKey || e.ctrlKey;
-
- if (isMod) {
- // On macOS, holding Option rewrites e.key for letter keys (Opt+G → "©"),
- // so fall back to the layout's physical code when Alt is held.
- const key = e.altKey && /^Key[A-Z]$/.test(e.code)
- ? e.code.slice(3).toLowerCase()
- : e.key.toLowerCase();
- modBindings[key]?.(e);
- return;
- }
-
- if (isInputTarget(e)) return;
- keyBindings[e.key.toLowerCase()]?.(e);
- };
-
- const handleKeyUp = (e: KeyboardEvent) => {
- if (e.key === ' ' && !isInputTarget(e)) {
- e.preventDefault();
- handleSpaceUp();
- }
- };
-
- function init() {
- window.addEventListener('keydown', handleKeyDown);
- window.addEventListener('keyup', handleKeyUp);
-
- const timelineCanvas = document.getElementById('timeline-canvas');
- timelineCanvas?.addEventListener('mouseenter', onTimelineEnter);
- timelineCanvas?.addEventListener('mouseleave', onTimelineLeave);
- }
-
- function dispose() {
- window.removeEventListener('keydown', handleKeyDown);
- window.removeEventListener('keyup', handleKeyUp);
- if (spaceHandTimer) clearTimeout(spaceHandTimer);
-
- const timelineCanvas = document.getElementById('timeline-canvas');
- timelineCanvas?.removeEventListener('mouseenter', onTimelineEnter);
- timelineCanvas?.removeEventListener('mouseleave', onTimelineLeave);
- }
-
- return { init, dispose };
-}
diff --git a/apps/web/src/components/engine/systems/motion.ts b/apps/web/src/components/engine/systems/motion.ts
deleted file mode 100644
index a65a29aa..00000000
--- a/apps/web/src/components/engine/systems/motion.ts
+++ /dev/null
@@ -1,450 +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 { query, Not, Or, Hierarchy, hasComponent } from 'bitecs';
-import { cubicBezier, steps, spring } from "animejs";
-import { AnimationType, ChildOf } from '../components';
-import type { EngineWorld } from '../api/world';
-import { revealChars, revealWords, scrambleChars } from '../utils/text-motion';
-import { resetAnimatedValues } from '../api/utils';
-
-
-/**
- * Apply an animation to a node.
- * @param world - The engine world.
- * @param eid - The entity id of the node to apply the animation to.
- * @param aid - The animation id.
- * @param progress - The progress of the animation.
- */
-function applyAnimation(world: EngineWorld, eid: number, aid: number, progress: number) {
- const c = world.components;
-
- switch (c.Animation.type[aid]) {
- case AnimationType.FADE: {
- const phase = c.Animation.phase[aid];
- const func = cubicBezier(0.1, 0.7, 0.5, 1);
- const eased = clamp01(func(progress));
- c.Computed.opacity[eid] = phase == 1 ? 1 - eased : eased;
- break;
- }
- case AnimationType.GAIN: {
- const phase = c.Animation.phase[aid];
- const func = cubicBezier(0.4, 0.095, 0.546, 0.875);
- const eased = clamp01(func(progress));
- c.Computed.volume[eid] = phase == 1 ? 1 - eased : eased;
- break;
- }
- case AnimationType.GROW: {
- const phase = c.Animation.phase[aid];
- const func = cubicBezier(0.1, 0.7, 0.5, 1);
- const eased = clamp01(func(progress));
- const t = phase == 1 ? eased : 1 - eased;
- const scale = 1 - 0.5 * t;
- c.Computed.scaleX[eid] = scale;
- c.Computed.scaleY[eid] = scale;
- break;
- }
- case AnimationType.SHRINK: {
- const phase = c.Animation.phase[aid];
- const func = cubicBezier(0.1, 0.7, 0.5, 1);
- const eased = clamp01(func(progress));
- const t = phase == 1 ? eased : 1 - eased;
- const scale = 1 + 0.5 * t;
- c.Computed.scaleX[eid] = scale;
- c.Computed.scaleY[eid] = scale;
- break;
- }
- case AnimationType.BLUR: {
- const phase = c.Animation.phase[aid];
- const func = phase == 1 ? cubicBezier(0.4, 0, 1, 1) : cubicBezier(0.33, 0, 0.2, 1);
- const eased = clamp01(func(progress));
- c.Computed.blur[eid] = phase == 1 ? lerp(0, 24, eased) : lerp(24, 0, eased);
- break;
- }
- case AnimationType.SLIDE_LEFT:
- case AnimationType.SLIDE_RIGHT:
- case AnimationType.SLIDE_UP:
- case AnimationType.SLIDE_DOWN: {
- const phase = c.Animation.phase[aid];
- const type = c.Animation.type[aid];
- const func = cubicBezier(0.1, 0.7, 0.5, 1);
- const eased = clamp01(func(progress));
- const t = phase == 1 ? eased : 1 - eased;
-
- const sign = phase == 1 ? -1 : 1;
- if (type === AnimationType.SLIDE_LEFT) {
- c.Computed.offsetX[eid] = sign * 100 * t;
- } else if (type === AnimationType.SLIDE_RIGHT) {
- c.Computed.offsetX[eid] = sign * -100 * t;
- } else if (type === AnimationType.SLIDE_UP) {
- c.Computed.offsetY[eid] = sign * 100 * t;
- } else if (type === AnimationType.SLIDE_DOWN) {
- c.Computed.offsetY[eid] = sign * -100 * t;
- }
- c.Computed.opacity[eid] = 1 - t;
- break;
- }
- case AnimationType.SPIN: {
- const phase = c.Animation.phase[aid];
- const func = cubicBezier(0.44, 0.02, 0.252, 0.992);
- const eased = clamp01(func(progress));
- const t = phase == 1 ? eased : 1 - eased;
- const scale = 1 - t;
- c.Computed.scaleX[eid] = scale;
- c.Computed.scaleY[eid] = scale;
- c.Computed.rotation[eid] = -45 * t;
- break;
- }
- case AnimationType.TWIST: {
- const phase = c.Animation.phase[aid];
- const func = cubicBezier(0.1, 0.7, 0.5, 1);
- const eased = clamp01(func(progress));
- const t = phase == 1 ? eased : 1 - eased;
- const scale = 1 + t;
- c.Computed.scaleX[eid] = scale;
- c.Computed.scaleY[eid] = scale;
- c.Computed.rotation[eid] = -10 * t;
- c.Computed.offsetX[eid] = -30 * t;
- c.Computed.offsetY[eid] = -30 * t;
- break;
- }
- case AnimationType.APPEAR_WORD: {
- const phase = c.Animation.phase[aid];
- const t = phase == 1 ? progress : 1 - progress;
- const chars = c.Chars[eid] ?? '';
- c.Computed.chars[eid] = revealWords(chars, 1 - t);
- break;
- }
- case AnimationType.APPEAR_CHAR: {
- const phase = c.Animation.phase[aid];
- const t = phase == 1 ? progress : 1 - progress;
- const chars = c.Chars[eid] ?? '';
- c.Computed.chars[eid] = revealChars(chars, 1 - t);
- break;
- }
- case AnimationType.SCRAMBLE: {
- const phase = c.Animation.phase[aid];
- const t = phase == 1 ? progress : 1 - progress;
- const chars = c.Chars[eid] ?? '';
- c.Computed.chars[eid] = scrambleChars(chars, 1 - t);
- break;
- }
- }
-}
-
-export function motionSystem(world: EngineWorld): void {
- const c = world.components;
- const worldProps = getPropertyPaths(world);
-
- for (const eid of query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), Hierarchy(ChildOf), ChildOf('*'), Not(c.Deleted), Not(c.Hidden), Not(c.Culled)])) {
-
- if (c.Computed.visibility[eid] === 0) continue;
-
- const hasAnimations = c.Cache.animations[eid].length > 0;
- const hasKeyframes = c.Cache.keyframeTracks[eid].length > 0;
- if (!hasAnimations && !hasKeyframes) continue;
-
- resetAnimatedValues(world, eid);
-
- let trimStart = 0
- let trimEnd = c.Computed.duration[eid];
-
- if (hasComponent(world, eid, c.Trim)) {
- trimStart = c.Trim.start[eid];
- trimEnd = c.Trim.end[eid] ?? trimEnd;
- }
-
- const localFrame = c.Computed.localTime[eid];
-
- // 1: Preset animations (FADE/GAIN/GROW/SHRINK/BLUR/SLIDE)
- for (const aid of c.Cache.animations[eid]) {
- const duration = c.Animation.duration[aid] ?? 0;
- if (duration <= 0) continue;
-
- const delay = c.Animation.delay[aid] ?? 0;
- const isOut = c.Animation.phase[aid] == 1;
- const windowStart = isOut
- ? trimEnd - duration - delay
- : trimStart + delay;
- const windowEnd = windowStart + duration;
- if (isOut ? localFrame < windowStart : localFrame >= windowEnd) continue;
-
- const progress = clamp01((localFrame - windowStart) / duration);
- applyAnimation(world, eid, aid, progress);
- }
-
- // 2: Keyframe tracks
- for (const tid of c.Cache.keyframeTracks[eid]) {
- const property = c.KeyframeTrack.property[tid] as PropertyPath;
- const target = c.KeyframeTrack.target[tid];
- const keyframes = c.Cache.keyframes[tid] ?? [];
- const result = sampleTrack(world, keyframes, localFrame, property);
- if (result === null) continue;
- worldProps[property].computed[target] = result;
- }
-
- // Uniform scale is authored/animated as scaleX only; mirror it onto scaleY.
- if (hasComponent(world, eid, c.UniformScale)) {
- c.Computed.scaleY[eid] = c.Computed.scaleX[eid];
- }
- }
-}
-
-/**
- * Read the current value of an ECS component property by dot-path string.
- */
-export function getPropertyPaths(world: EngineWorld) {
- const c = world.components;
- return {
- 'position.x': {
- computed: c.Computed.positionX,
- authored: c.Position.x,
- },
- 'position.y': {
- computed: c.Computed.positionY,
- authored: c.Position.y,
- },
- 'offset.x': {
- computed: c.Computed.offsetX,
- authored: c.Offset.x,
- },
- 'offset.y': {
- computed: c.Computed.offsetY,
- authored: c.Offset.y,
- },
- 'rotation': {
- computed: c.Computed.rotation,
- authored: c.Rotation,
- },
- 'scale.x': {
- computed: c.Computed.scaleX,
- authored: c.Scale.x,
- },
- 'scale.y': {
- computed: c.Computed.scaleY,
- authored: c.Scale.y,
- },
- 'scale': {
- computed: c.Computed.scaleX,
- authored: c.UniformScale,
- },
- 'skew.x': {
- computed: c.Computed.skewX,
- authored: c.Skew.x,
- },
- 'skew.y': {
- computed: c.Computed.skewY,
- authored: c.Skew.y,
- },
- 'width': {
- computed: c.Computed.width,
- authored: c.Size.width,
- },
- 'height': {
- computed: c.Computed.height,
- authored: c.Size.height,
- },
- 'opacity': {
- computed: c.Computed.opacity,
- authored: c.Appearance.opacity,
- },
- 'color': {
- computed: c.Computed.color,
- authored: c.Color,
- },
- 'blur': {
- computed: c.Computed.blur,
- authored: c.Blur,
- },
- 'volume': {
- computed: c.Computed.volume,
- authored: c.Volume,
- },
- 'effect.value': {
- computed: c.Computed.value,
- authored: c.Effect.value,
- },
- 'stroke.width': {
- computed: c.Computed.strokeWidth,
- authored: c.StrokeStyle.width,
- },
- 'vertexRadius': {
- computed: c.Computed.cornerRadius,
- authored: c.CornerRadius,
- },
- 'mixedVertexRadius.topLeft': {
- computed: c.Computed.cornerRadiusTopLeft,
- authored: c.MixedCornerRadius.topLeft,
- },
- 'mixedVertexRadius.topRight': {
- computed: c.Computed.cornerRadiusTopRight,
- authored: c.MixedCornerRadius.topRight,
- },
- 'mixedVertexRadius.bottomRight': {
- computed: c.Computed.cornerRadiusBottomRight,
- authored: c.MixedCornerRadius.bottomRight,
- },
- 'mixedVertexRadius.bottomLeft': {
- computed: c.Computed.cornerRadiusBottomLeft,
- authored: c.MixedCornerRadius.bottomLeft,
- },
- 'stop.offset': {
- computed: c.Computed.stopOffset,
- authored: c.ColorStop.offset,
- },
- 'stop.color': {
- computed: c.Computed.stopColor,
- authored: c.ColorStop.color,
- },
- 'stop.opacity': {
- computed: c.Computed.stopOpacity,
- authored: c.ColorStop.opacity,
- },
- 'chars': {
- computed: c.Computed.chars,
- authored: c.Chars,
- },
- };
-}
-
-export type PropertyPath = keyof ReturnType<typeof getPropertyPaths>;
-
-// ─── Easing ─────────────────────────────────────────────────
-
-type EasingFunction = (t: number) => number;
-
-const easingCache = new Map<string, EasingFunction>();
-
-/**
- * Parse an easing descriptor string and return an easing function.
- * Returns null for empty/undefined (= default linear interpolation).
- * Supported formats:
- * "steps(9)" or "steps(9,true)" → animejs steps(n, fromStart?)
- * "cubicBezier(0.25,0.1,0.25,1)" → animejs cubicBezier(x1,y1,x2,y2)
- * "spring(0.5,500)" → animejs spring({ bounce, duration })
- */
-function resolveEasing(descriptor: string | undefined): EasingFunction | null {
- if (!descriptor || descriptor === '') return null;
-
- const cached = easingCache.get(descriptor);
- if (cached) return cached;
-
- let fn: EasingFunction | null = null;
-
- const stepsMatch = descriptor.match(/^steps\((\d+)(?:,(true|false))?\)$/);
- if (stepsMatch) {
- const n = Number.parseInt(stepsMatch[1], 10);
- const fromStart = stepsMatch[2] === 'true';
- fn = steps(n, fromStart);
- }
-
- const bezierMatch = descriptor.match(/^cubicBezier\((-?[\d.]+),(-?[\d.]+),(-?[\d.]+),(-?[\d.]+)\)$/);
- if (bezierMatch) {
- const [, x1, y1, x2, y2] = bezierMatch;
- fn = cubicBezier(Number(x1), Number(y1), Number(x2), Number(y2));
- }
-
- const springMatch = descriptor.match(/^spring\(([\d.-]+),([\d.-]+)\)$/);
- if (springMatch) {
- const [, bounce, duration] = springMatch;
- const s = spring({
- bounce: Number(bounce),
- duration: Number(duration),
- });
- fn = (t: number) => s.ease(t);
- }
-
- if (fn) {
- easingCache.set(descriptor, fn);
- return fn;
- }
-
- return null;
-}
-
-/**
- * Sample a presorted aggregated keyframe track at a local frame.
- * Returns null only if the track has no keyframes.
- */
-function sampleTrack(
- world: EngineWorld,
- keyframes: number[],
- frame: number,
- property: PropertyPath,
-): number | null {
- const Keyframe = world.components.Keyframe;
- if (keyframes.length === 0) return null;
-
- if (keyframes.length === 1) {
- return Keyframe.value[keyframes[0]];
- }
-
- const firstValue = Keyframe.value[keyframes[0]];
- const lastValue = Keyframe.value[keyframes[keyframes.length - 1]];
-
- const firstFrame = Keyframe.time[keyframes[0]];
- const lastFrame = Keyframe.time[keyframes[keyframes.length - 1]];
-
- if (frame <= firstFrame) {
- return firstValue;
- }
-
- if (frame >= lastFrame) {
- return lastValue;
- }
-
- for (let i = 0; i < keyframes.length - 1; i++) {
- const start = Keyframe.time[keyframes[i]];
- const end = Keyframe.time[keyframes[i + 1]];
- if (frame < start || frame > end) continue;
-
- const span = end - start;
- if (span <= 0) continue;
-
- let progress = Math.max(0, Math.min(1, (frame - start) / span));
-
- const easingFn = resolveEasing(Keyframe.easing[keyframes[i]]);
- if (easingFn) {
- progress = easingFn(progress);
- }
-
- const startValue = Keyframe.value[keyframes[i]];
- const endValue = Keyframe.value[keyframes[i + 1]];
-
- if (property === 'color' || property === 'stop.color') {
- return lerpColor(startValue, endValue, progress);
- }
-
- // Linear interpolation
- return startValue + (endValue - startValue) * progress;
- }
-
- return lastValue;
-}
-
-/**
- * Channel-wise linear interpolation between two packed 0xRRGGBB colors.
- * Linear interpolation on the packed integer bleeds bits between channels
- * and produces wrong intermediate hues.
- */
-function lerpColor(from: number, to: number, progress: number): number {
- const r0 = (from >> 16) & 0xFF;
- const g0 = (from >> 8) & 0xFF;
- const b0 = from & 0xFF;
- const r1 = (to >> 16) & 0xFF;
- const g1 = (to >> 8) & 0xFF;
- const b1 = to & 0xFF;
- const r = Math.round(r0 + (r1 - r0) * progress);
- const g = Math.round(g0 + (g1 - g0) * progress);
- const b = Math.round(b0 + (b1 - b0) * progress);
- return (r << 16) | (g << 8) | b;
-}
-
-function clamp01(value: number): number {
- return Math.max(0, Math.min(1, value));
-}
-
-function lerp(from: number, to: number, progress: number): number {
- return from + (to - from) * progress;
-}
diff --git a/apps/web/src/components/engine/systems/playback.ts b/apps/web/src/components/engine/systems/playback.ts
deleted file mode 100644
index 87337d35..00000000
--- a/apps/web/src/components/engine/systems/playback.ts
+++ /dev/null
@@ -1,429 +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 { hasComponent, Not, Or, query } from 'bitecs';
-import { ChildOf, PaintType } from '../components';
-import { resolveAudioDecoder, resolveCaptionDecoder, resolveImageDecoder, resolveSequenceDecoder, resolveShaderHost, resolveVideoDecoder } from '../decoders';
-import { getParentEntity } from '../api';
-import { getTransitionWindow } from '../utils/transition';
-import { AudioBus } from '../services/audio-bus';
-import { clamp } from '@/utils';
-import { addComponent, removeComponent } from '../api/events';
-
-import type { EngineWorld } from '../api/world';
-
-const WARMUP_FRAMES = 15;
-
-function advancePlayhead(world: EngineWorld, eid: number): void {
- const c = world.components;
- // In offline mode the encoder sets the playhead explicitly per frame, so
- // skip the delta-based advance — running it would either double-step or
- // drift depending on how the caller set `world.timestamp.delta`.
- if (world.mode !== 'realtime') return;
-
- const fps = world.frameRate;
- let looped = false;
-
- // Only advance the playhead while playing. The audio play/stop bookkeeping
- // below must still run when stopped, otherwise a manual stop (playing
- // flipped to 0 externally) is never observed — `wasPlaying` stays stuck at
- // 1 and the next play isn't re-anchored, so scheduled buffers land in the
- // past and play silently.
- if (c.Playback.playing[eid] === 1) {
- const dt = world.timestamp.delta / 1000;
- const speed = c.Playback.speed[eid] || 1;
- const previousTime = c.Computed.localTimeInSeconds[eid];
- let time = previousTime + dt * speed;
-
- const hasWorkarea = hasComponent(world, eid, c.Workarea);
- const duration = c.Computed.duration[eid];
- const durationSeconds = duration / fps;
-
- const maxSeconds = hasWorkarea
- ? (c.Workarea.end[eid] ?? duration) / fps
- : durationSeconds;
-
- const minSeconds = hasWorkarea
- ? (c.Workarea.start[eid] ?? 0) / fps
- : 0;
-
- if (time >= maxSeconds) {
- if (c.Playback.loop[eid] === 1) {
- time = minSeconds;
- looped = true;
- } else {
- c.Playback.playing[eid] = 0;
- }
- } else if (time <= minSeconds) {
- if (c.Playback.loop[eid] === 1) {
- time = maxSeconds;
- looped = true;
- } else {
- c.Playback.playing[eid] = 0;
- time = minSeconds;
- }
- }
-
- c.Computed.localTimeInSeconds[eid] = clamp(time, minSeconds, maxSeconds);
- c.Computed.localTime[eid] = Math.round(c.Computed.localTimeInSeconds[eid] * fps);
- }
-
- // Handle audio play/stop transitions
- const ctx = world.audio;
- const playing = c.Playback.playing[eid] ?? 0;
- const wasPlaying = c.AudioPlayback.wasPlaying[eid] ?? 0;
-
- if ((playing && !wasPlaying) || looped) {
- c.AudioPlayback.contextOffsetInSeconds[eid] = ctx.currentTime;
- c.AudioPlayback.timelineOffsetInSeconds[eid] = c.Computed.localTimeInSeconds[eid];
- }
-
- if (!playing && wasPlaying) {
- resetDecoders(world, eid);
- }
-
- c.AudioPlayback.wasPlaying[eid] = playing;
-}
-
-function forwardVideoDecoder(world: EngineWorld, sid: number, eid: number, fid: number): void {
- const c = world.components;
- const fps = world.frameRate;
-
- const globalFrame = c.Computed.localTime[sid];
- const localFrame = c.Computed.localTime[eid];
- const start = c.Computed.start[eid];
- const end = c.Computed.end[eid];
- const hasCache = world.mode === 'realtime';
- const warmupDecoder = globalFrame >= start - WARMUP_FRAMES && globalFrame < end + WARMUP_FRAMES && hasCache;
-
- let trimStart = 0
- let trimEnd = c.Computed.duration[eid];
-
- if (hasComponent(world, eid, c.Trim)) {
- trimStart = c.Trim.start[eid];
- trimEnd = c.Trim.end[eid] ?? trimEnd;
- }
-
- const decoder = resolveVideoDecoder(world, fid);
- if (!decoder) return;
-
- if (c.Computed.visibility[eid] === 1 || warmupDecoder) {
- const seekFrame = clamp(localFrame, trimStart, trimEnd);
- const seekPromise = decoder.seekTo(seekFrame, fps);
- world.promises?.push(seekPromise ?? null);
- }
-}
-
-function forwardSequenceDecoder(world: EngineWorld, sid: number, eid: number, fid: number): void {
- const c = world.components;
- const fps = world.frameRate;
-
- const globalFrame = c.Computed.localTime[sid];
- const localFrame = c.Computed.localTime[eid];
- const start = c.Computed.start[eid];
- const end = c.Computed.end[eid];
- const hasCache = world.mode === 'realtime';
- const warmupDecoder = globalFrame >= start - WARMUP_FRAMES && globalFrame < end + WARMUP_FRAMES && hasCache;
-
- let trimStart = 0
- let trimEnd = c.Computed.duration[eid];
-
- if (hasComponent(world, eid, c.Trim)) {
- trimStart = c.Trim.start[eid];
- trimEnd = c.Trim.end[eid] ?? trimEnd;
- }
-
- const decoder = resolveSequenceDecoder(world, fid, hasCache);
- if (!decoder) return;
-
- if (c.Computed.visibility[eid] === 1 || warmupDecoder) {
- const seekFrame = clamp(localFrame, trimStart, trimEnd);
- const seekPromise = decoder.seekTo(seekFrame, fps);
- world.promises?.push(seekPromise);
- }
-}
-
-function forwardCaptionDecoder(world: EngineWorld, _sid: number, eid: number): void {
- const c = world.components;
-
- const localFrame = c.Computed.localTime[eid];
- const fps = world.frameRate;
-
- const decoder = resolveCaptionDecoder(world, eid);
- decoder?.seekTo(world, eid, localFrame / fps);
-}
-
-/**
- * Forward audio decoder for a child entity.
- * @param world Engine World
- * @param sid Scene Entity Id
- * @param eid Child Entity Id
- * @param aid Optional alterantive eid of the entity that carries the audio
- * @returns
- */
-function forwardAudioDecoder(world: EngineWorld, sid: number, eid: number, aid?: number): void {
- const c = world.components;
-
- if (hasComponent(world, eid, c.Muted)) return;
-
- const resolvedDecoder = resolveAudioDecoder(world, aid ?? eid);
- if (!resolvedDecoder) return;
-
- const { decoder, initPromise } = resolvedDecoder;
-
- const fps = world.frameRate;
- const currentTime = c.Computed.localTime[sid];
- const localFrame = c.Computed.localTime[eid];
- const trimStart = c.Trim.start[eid] ?? 0;
-
- const playbackRate = c.PlaybackRate[eid] ?? 1;
- const delay = c.Computed.delay[eid];
- const trimEnd = c.Trim.end[eid] ?? c.Computed.duration[eid] ?? 0;
-
- const audioOffset = c.AudioPlayback.contextOffsetInSeconds[sid] ?? 0;
- const playbackOffset = c.AudioPlayback.timelineOffsetInSeconds[sid] ?? 0;
- // In offline rendering the encoder pins contextOffset to 0 and playbackOffset
- // to the workarea start, so this term shifts scheduled audio back into the
- // encoded window (and resolves to 0 when there is no workarea).
- const audioDelay = audioOffset - playbackOffset
- const bus = resolveAudioBus(world, eid);
-
- if (!decoder.ready) {
- world.promises?.push(initPromise);
- } else if (c.Computed.visibility[eid] === 1 && c.Playback.playing[sid] === 1) {
- const playPromise = decoder.playTo(bus, {
- relativeFrom: localFrame / fps,
- relativeTo: (localFrame + 15) / fps,
- trimStart: trimStart / fps,
- trimEnd: trimEnd / fps,
- playbackRate,
- currentTime: currentTime / fps,
- relativeDelay: (delay / fps) + audioDelay,
- });
- world.promises?.push(playPromise);
- } else {
- decoder.reset();
- }
-}
-
-function forwardHtmlHost(world: EngineWorld, sid: number, eid: number, fid: number): void {
- const c = world.components;
- if (world.mode === 'realtime' || c.Computed.visibility[eid] !== 1 || !c.HtmlHost[fid]) return;
- world.promises?.push(c.HtmlHost[fid].whenReady(c.Computed.localTimeInSeconds[sid] ?? 0));
-}
-
-function forwardImageDecoder(world: EngineWorld, _sid: number, _eid: number, fid: number): void {
- const resolvedDecoder = resolveImageDecoder(world, fid);
- if (!resolvedDecoder) return;
-
- const { decoder, initPromise } = resolvedDecoder;
-
- if (!decoder.ready) {
- world.promises?.push(initPromise);
- }
-}
-
-/**
- * Forward decoders for a child entity.
- * @param world Engine World
- * @param sid Scene Entity Id
- * @param eid Child Entity Id
- */
-function forwardDecoders(world: EngineWorld, sid: number, eid: number): void {
- const c = world.components;
-
- const hidden =
- hasComponent(world, eid, c.Hidden)
- || hasComponent(world, eid, c.Culled);
- const visualsEnabled = !hidden && world.mode !== 'offline-audio';
-
- let paintAudioEid: number | undefined;
- if (!hidden) {
- for (const fid of query(world, [ChildOf(eid), c.Paint, Not(c.Deleted), Not(c.Hidden)])) {
- if (c.Paint[fid] === PaintType.VIDEO) {
- if (visualsEnabled) {
- forwardVideoDecoder(world, sid, eid, fid);
- }
- paintAudioEid = fid;
- }
-
- if (c.Paint[fid] === PaintType.SEQUENCE && visualsEnabled) {
- forwardSequenceDecoder(world, sid, eid, fid);
- }
-
- if (c.Paint[fid] === PaintType.IMAGE && visualsEnabled) {
- forwardImageDecoder(world, sid, eid, fid);
- }
-
- if (c.Paint[fid] === PaintType.HTML && visualsEnabled) {
- forwardHtmlHost(world, sid, eid, fid);
- }
-
- if (c.Paint[fid] === PaintType.SHADER && visualsEnabled) {
- const ready = resolveShaderHost(world, fid)?.whenReady();
- if (ready) world.promises?.push(ready);
- }
- }
- }
-
- if (hasComponent(world, eid, c.Caption) && visualsEnabled) {
- forwardCaptionDecoder(world, sid, eid);
- }
-
- if (hasComponent(world, eid, c.Audio) || paintAudioEid) {
- forwardAudioDecoder(world, sid, eid, paintAudioEid);
- }
-
- for (const cid of query(world, [ChildOf(eid), Or(c.Geometry, c.Group, c.AdjustmentLayer), Not(c.Deleted), Not(c.Hidden)])) {
- forwardDecoders(world, sid, cid);
- }
-}
-
-function updateVisibility(world: EngineWorld, sid: number, eid: number): void {
- const c = world.components;
-
- // Root or dragging entity is always visible
- if (eid === sid || hasComponent(world, eid, c.Dragging)) {
- c.Computed.visibility[eid] = 1;
- } else {
- const globalFrame = c.Computed.localTime[sid];
- const delay = c.Computed.delay[eid] ?? 0;
- const playbackRate = c.PlaybackRate[eid] ?? 1;
-
- const start = c.Computed.start[eid];
- const end = c.Computed.end[eid];
-
- c.Computed.localTime[eid] = Math.round((globalFrame - delay) * playbackRate);
- c.Computed.visibility[eid] = globalFrame >= start && globalFrame < end ? 1 : 0;
- }
-
- for (const cid of query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), ChildOf(eid), Not(c.Deleted)])) {
- updateVisibility(world, sid, cid);
- }
-}
-
-function resetDecoders(world: EngineWorld, eid: number): void {
- const c = world.components;
- c.AudioDecoder[eid]?.reset();
-
- for (const cid of query(world, [ChildOf(eid), c.AudioDecoder, Not(c.Deleted)])) {
- resetDecoders(world, cid);
- }
-}
-
-
-function collectAncestors(world: EngineWorld, eid: number): number[] {
- const path: number[] = [];
-
- let current: number | null = eid;
- while (current) {
- path.push(current);
- current = getParentEntity(world, current);
- }
-
- return path.toReversed();
-}
-
-function resolveAudioBus(world: EngineWorld, eid: number) {
- const c = world.components;
- let bus = c.AudioBus[eid];
- if (bus) return bus;
-
- const ancestors = collectAncestors(world, eid);
-
- // Walk root -> leaf, connecting each bus's gain into its parent's input
- // (root connects into the context destination). Reuse buses that already
- // exist; only newly created ones need wiring.
- let currentInput: AudioNode = world.audio.destination;
-
- for (const aeid of ancestors) {
- let abus = c.AudioBus[aeid];
-
- if (!abus) {
- abus = new AudioBus(world, aeid);
- addComponent(world, aeid, c.AudioBus, false);
- c.AudioBus[aeid] = abus;
- abus.connect(currentInput);
- }
-
- currentInput = abus.input;
- }
-
- return c.AudioBus[eid];
-}
-
-function getGlobalFrame(world: EngineWorld, eid: number): number {
- const c = world.components;
-
- let current: number | null = eid;
- while (current) {
- if (hasComponent(world, current, c.Playback)) {
- return c.Computed.localTime[current];
- }
- current = getParentEntity(world, current);
- }
-
- return 0;
-}
-
-export function playbackSystem(world: EngineWorld): void {
- const c = world.components;
-
- // handle root playback
- for (const eid of query(world, [c.Playback, Not(c.Deleted)])) {
- advancePlayhead(world, eid);
- }
-
- for (const eid of query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), Not(ChildOf('*')), Not(c.Deleted)])) {
- updateVisibility(world, eid, eid);
- forwardDecoders(world, eid, eid);
- }
-
- // handle transition visibility
- for (const cid of query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), c.Transition, Not(c.Deleted)])) {
- const parentEid = getParentEntity(world, cid);
-
- if (parentEid == null || !hasComponent(world, parentEid, c.Sequential)) {
- console.info('Removing transition due to non-sequential parent', cid);
- // System-driven cleanup of orphaned state — not a user edit.
- removeComponent(world, cid, c.Transition, false);
- continue;
- }
-
- const children = query(world, [ChildOf(parentEid), Or(c.Geometry, c.Group, c.AdjustmentLayer), Not(c.Deleted)]);
- const partner = children.find(rid => c.Computed.start[rid] === c.Computed.end[cid]);
- if (!partner) continue;
- const window = getTransitionWindow(world, cid, partner);
- const globalFrame = getGlobalFrame(world, cid);
- if (globalFrame >= window.start && globalFrame < window.end) {
- c.Computed.visibility[cid] = 1;
- c.Computed.visibility[partner] = 1;
- }
- }
-
- // Sync audio buses
- let soloedEntities: Set<number> | null = null;
- for (const eid of query(world, [c.AudioBus, Not(c.Deleted)])) {
- c.AudioBus[eid]?.sync();
- if (!hasComponent(world, eid, c.Soloed)) continue;
-
- if (soloedEntities === null) {
- soloedEntities = new Set();
- }
-
- for (const aid of collectAncestors(world, eid)) {
- soloedEntities.add(aid);
- }
- }
-
- if (soloedEntities) {
- for (const eid of query(world, [c.AudioBus, Not(c.Deleted)])) {
- if (!soloedEntities.has(eid)) {
- c.AudioBus[eid]?.mute();
- }
- }
- }
-
- world.liveMounts.advance();
-}
diff --git a/apps/web/src/components/engine/systems/render.ts b/apps/web/src/components/engine/systems/render.ts
deleted file mode 100644
index 83f6f92c..00000000
--- a/apps/web/src/components/engine/systems/render.ts
+++ /dev/null
@@ -1,945 +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 { hasComponent, query, Not, Or } from 'bitecs';
-import { ChildOf, COMPOSITE_OPERATIONS, EffectType, GeometryType, PaintType, ScaleMode, StrokeCap, StrokeJoin, TransitionType } from '../components';
-import { getParentEntity } from '../api/base';
-import { cubicBezier } from 'animejs';
-import { resolveImageDecoder, resolveVideoDecoder, resolveSequenceDecoder, resolveCaptionDecoder, resolveShaderHost } from '../decoders';
-import { getAudioPeaks } from '../decoders/audio-peaks';
-import { renderText } from '../utils/text';
-import { getTransitionWindow } from '../utils/transition';
-import { colorToHex } from '../../../utils/color';
-import { handleCanvasInteraction, handleGeometryInteraction } from '../api/interactions';
-
-import type { EngineWorld } from '../api/world';
-import type { Quad } from '..';
-
-type Ctx2D = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
-
-const MISSING_ASSET_COLOR = '#5C2828';
-
-export function drawRectPath(world: EngineWorld, eid: number): void {
- const ctx = world.ctx!;
- const c = world.components;
- const w = c.Computed.width[eid];
- const h = c.Computed.height[eid];
-
- const computed = c.Computed;
- const hasMixed = hasComponent(world, eid, c.MixedCornerRadius);
- let tl = hasMixed ? computed.cornerRadiusTopLeft[eid] : computed.cornerRadius[eid];
- let tr = hasMixed ? computed.cornerRadiusTopRight[eid] : tl;
- let br = hasMixed ? computed.cornerRadiusBottomRight[eid] : tl;
- let bl = hasMixed ? computed.cornerRadiusBottomLeft[eid] : tl;
-
- ctx.beginPath();
-
- if (tl === 0 && tr === 0 && br === 0 && bl === 0) {
- ctx.rect(0, 0, w, h);
- } else if (tl === tr && tr === br && br === bl) {
- ctx.roundRect(0, 0, w, h, tl);
- } else {
- // Clamp radii so adjacent corners don't exceed the edge length (CSS spec algorithm)
- const scale = Math.min(
- w / (tl + tr || 1),
- h / (tr + br || 1),
- w / (br + bl || 1),
- h / (bl + tl || 1),
- 1,
- );
- if (scale < 1) {
- tl *= scale;
- tr *= scale;
- br *= scale;
- bl *= scale;
- }
-
- ctx.moveTo(tl, 0);
- ctx.lineTo(w - tr, 0);
- if (tr > 0) ctx.arcTo(w, 0, w, tr, tr);
- else ctx.lineTo(w, 0);
- ctx.lineTo(w, h - br);
- if (br > 0) ctx.arcTo(w, h, w - br, h, br);
- else ctx.lineTo(w, h);
- ctx.lineTo(bl, h);
- if (bl > 0) ctx.arcTo(0, h, 0, h - bl, bl);
- else ctx.lineTo(0, h);
- ctx.lineTo(0, tl);
- if (tl > 0) ctx.arcTo(0, 0, tl, 0, tl);
- else ctx.lineTo(0, 0);
- }
-
- ctx.closePath();
-}
-
-function getScaledImageProps(
- mode: number,
- imgW: number,
- imgH: number,
- targetW: number,
- targetH: number,
-): [dx: number, dy: number, sw: number, sh: number] {
- if (mode === ScaleMode.FILL) {
- return [0, 0, targetW, targetH];
- }
- if (mode === ScaleMode.FIT) {
- const scale = Math.min(targetW / imgW, targetH / imgH);
- const sw = imgW * scale;
- const sh = imgH * scale;
- return [(targetW - sw) / 2, (targetH - sh) / 2, sw, sh];
- }
- if (mode === ScaleMode.COVER) {
- const scale = Math.max(targetW / imgW, targetH / imgH);
- const sw = imgW * scale;
- const sh = imgH * scale;
- return [(targetW - sw) / 2, (targetH - sh) / 2, sw, sh];
- }
- // ScaleMode.NONE — original size
- return [0, 0, imgW, imgH];
-}
-
-/** Create a canvas linear gradient from ECS components. */
-export function createLinearGradient(
- world: EngineWorld,
- fid: number,
- ctx: Ctx2D,
- w: number,
- h: number,
-): CanvasGradient {
- const Computed = world.components.Computed;
-
- // (px, py) is the gradient center in normalized shape-local space. Fills
- // without a Position component (the default for stock gradient fills) get
- // (0.5, 0.5) so the gradient spans the shape. With Position present, the
- // motion-system-sampled Computed values are used (keyframeable).
- const hasPos = hasComponent(world, fid, world.components.Position);
- const px = hasPos ? Computed.positionX[fid] : 0.5;
- const py = hasPos ? Computed.positionY[fid] : 0.5;
- const sx = Computed.scaleX[fid];
- const sy = Computed.scaleY[fid];
- const angle = (Computed.rotation[fid] * Math.PI) / 180;
- const cos = Math.cos(angle);
- const sin = Math.sin(angle);
-
- // Transform normalized start (0, 0.5) and end (1, 0.5) points
- const startLocalX = (0 - 0.5) * sx;
- const startLocalY = (0.5 - 0.5) * sy;
- const x0 = (px + startLocalX * cos - startLocalY * sin) * w;
- const y0 = (py + startLocalX * sin + startLocalY * cos) * h;
-
- const endLocalX = (1 - 0.5) * sx;
- const endLocalY = (0.5 - 0.5) * sy;
- const x1 = (px + endLocalX * cos - endLocalY * sin) * w;
- const y1 = (py + endLocalX * sin + endLocalY * cos) * h;
-
- const gradient = ctx.createLinearGradient(x0, y0, x1, y1);
-
- addStopsTo(world, fid, gradient);
- return gradient;
-}
-
-/** Create a canvas radial gradient from ECS components. */
-export function createRadialGradient(
- world: EngineWorld,
- fid: number,
- ctx: Ctx2D,
- w: number,
- h: number,
-): CanvasGradient {
- const Computed = world.components.Computed;
-
- const hasPos = hasComponent(world, fid, world.components.Position);
- const px = hasPos ? Computed.positionX[fid] : 0.5;
- const py = hasPos ? Computed.positionY[fid] : 0.5;
- const sx = Computed.scaleX[fid];
- const sy = Computed.scaleY[fid];
- const angle = (Computed.rotation[fid] * Math.PI) / 180;
- const cos = Math.cos(angle);
- const sin = Math.sin(angle);
-
- // Center point (0.5, 0.5) in normalized space
- const cx = px * w;
- const cy = py * h;
-
- // Edge points to determine radius
- const edgeXLocalX = (1 - 0.5) * sx;
- const edgeXLocalY = (0.5 - 0.5) * sy;
- const edgeXAbsX = (px + edgeXLocalX * cos - edgeXLocalY * sin) * w;
- const edgeXAbsY = (py + edgeXLocalX * sin + edgeXLocalY * cos) * h;
-
- const edgeYLocalX = (0.5 - 0.5) * sx;
- const edgeYLocalY = (1 - 0.5) * sy;
- const edgeYAbsX = (px + edgeYLocalX * cos - edgeYLocalY * sin) * w;
- const edgeYAbsY = (py + edgeYLocalX * sin + edgeYLocalY * cos) * h;
-
- const distX = Math.hypot(edgeXAbsX - cx, edgeXAbsY - cy);
- const distY = Math.hypot(edgeYAbsX - cx, edgeYAbsY - cy);
- const outerRadius = Math.max(0.0001, distX, distY);
-
- const gradient = ctx.createRadialGradient(cx, cy, 0, cx, cy, outerRadius);
-
- addStopsTo(world, fid, gradient);
- return gradient;
-}
-
-/** Push a fill's ColorStop children onto a CanvasGradient. Reads Computed
- * values so the latest keyframe-sampled state is what gets painted. Offsets
- * beyond 1 wrap via modulo (1.5 → 0.5) so animated stops can cycle; offset
- * exactly 1 is preserved as the gradient end. */
-function addStopsTo(world: EngineWorld, fid: number, gradient: CanvasGradient): void {
- const c = world.components;
- const Computed = c.Computed;
- const stops = [...query(world, [c.ColorStop, ChildOf(fid), Not(c.Deleted)])]
- .map(sid => {
- const raw = Computed.stopOffset[sid];
- return {
- offset: raw <= 1 ? Math.max(0, raw) : raw % 1,
- color: Computed.stopColor[sid],
- opacity: Computed.stopOpacity[sid],
- };
- })
- .sort((a, b) => a.offset - b.offset);
-
- for (const { offset, color, opacity } of stops) {
- gradient.addColorStop(offset, toGradientColor(color, opacity));
- }
-}
-
-function toGradientColor(color: number, opacity: number): string {
- if (opacity >= 1) return colorToHex(color);
-
- const r = (color >> 16) & 0xFF;
- const g = (color >> 8) & 0xFF;
- const b = color & 0xFF;
- return `rgba(${r},${g},${b},${Math.max(0, Math.min(1, opacity))})`;
-}
-
-const EPSILON = 1e-4;
-
-/** Build a single CSS filter fragment from an effect sub-entity. Returns null if hidden or no-op. */
-function effectFilter(world: EngineWorld, subEid: number): string | null {
- const c = world.components;
-
- if (hasComponent(world, subEid, c.Hidden)) return null;
-
- const value = c.Computed.value[subEid];
- const type = c.Effect.type[subEid] ?? 0;
-
- if (type === EffectType.LAYER_BLUR) {
- const clamped = Math.max(0, value);
- return clamped > EPSILON ? `blur(${clamped}px)` : null;
- }
- if (type === EffectType.BRIGHTNESS) {
- const clamped = Math.min(1, Math.max(0, value));
- return Math.abs(clamped - 1) > EPSILON ? `brightness(${clamped})` : null;
- }
- if (type === EffectType.CONTRAST) {
- const clamped = Math.min(1, Math.max(0, value));
- return Math.abs(clamped - 1) > EPSILON ? `contrast(${clamped})` : null;
- }
- if (type === EffectType.GRAYSCALE) {
- const clamped = Math.min(1, Math.max(0, value));
- return clamped > EPSILON ? `grayscale(${clamped})` : null;
- }
- if (type === EffectType.HUE_ROTATION) {
- return Math.abs(value) > EPSILON ? `hue-rotate(${value}deg)` : null;
- }
- if (type === EffectType.INVERT) {
- const clamped = Math.min(1, Math.max(0, value));
- return clamped > EPSILON ? `invert(${clamped})` : null;
- }
- if (type === EffectType.SATURATE) {
- const clamped = Math.min(1, Math.max(0, value));
- return Math.abs(clamped - 1) > EPSILON ? `saturate(${clamped})` : null;
- }
- if (type === EffectType.SEPIA) {
- const clamped = Math.min(1, Math.max(0, value));
- return clamped > EPSILON ? `sepia(${clamped})` : null;
- }
- return null;
-}
-
-/** Apply CSS filter from effect sub-entities. Returns true if a filter was applied. */
-function buildEffects(world: EngineWorld, eid: number): string | null {
- const c = world.components;
-
- const parts: string[] = [];
-
- const blurVal = c.Computed.blur[eid];
- if (blurVal > EPSILON) {
- parts.push(`blur(${blurVal}px)`);
- }
-
- const effects = c.Cache.effects[eid];
- for (const effect of effects) {
- const f = effectFilter(world, effect);
- if (f) parts.push(f);
- }
-
- if (parts.length === 0) return null;
-
- return parts.join(' ');
-}
-
-export function renderFills(world: EngineWorld, eid: number): void {
- const c = world.components;
- const ctx = world.ctx!;
- const fills = c.Cache.fills[eid];
-
- for (let index = 0; index < fills.length; index++) {
- const fid = fills[index];
- if (hasComponent(world, fid, c.Hidden) || shaderConsumesFill(world, eid, fills, index)) continue;
- const savedAlpha = ctx.globalAlpha;
- const savedCO = ctx.globalCompositeOperation;
- const bi = c.Appearance.blendMode[fid] ?? 0;
-
- if (bi !== 0) {
- ctx.globalCompositeOperation = COMPOSITE_OPERATIONS[bi];
- }
-
- ctx.globalAlpha = savedAlpha * c.Computed.opacity[fid];
-
- if (c.Paint[fid] === PaintType.IMAGE) {
- const decoder = resolveImageDecoder(world, fid)?.decoder;
- const w = c.Computed.width[eid];
- const h = c.Computed.height[eid];
- const bitmap = decoder?.getBitmap(w, h);
-
- if (bitmap) {
- ctx.save();
- ctx.clip();
-
- const mode = c.ScaleMode[fid] ?? 0;
- const [dx, dy, sw, sh] = getScaledImageProps(mode, bitmap.width, bitmap.height, w, h);
- ctx.drawImage(bitmap, dx, dy, sw, sh);
-
- ctx.restore();
- } else if (decoder?.failed) {
- ctx.fillStyle = MISSING_ASSET_COLOR;
- ctx.fill();
- }
- } else if (c.Paint[fid] === PaintType.VIDEO) {
- const decoder = resolveVideoDecoder(world, fid);
- const frame = decoder?.toBitmap();
-
- if (frame) {
- const w = c.Computed.width[eid];
- const h = c.Computed.height[eid];
-
- ctx.save();
- ctx.clip();
-
- const mode = c.ScaleMode[fid] ?? 0;
- const [dx, dy, sw, sh] = getScaledImageProps(mode, frame.width, frame.height, w, h);
- ctx.drawImage(frame, dx, dy, sw, sh);
-
- ctx.restore();
- } else if (decoder?.errored) {
- ctx.fillStyle = MISSING_ASSET_COLOR;
- ctx.fill();
- }
- } else if (c.Paint[fid] === PaintType.SEQUENCE) {
- const decoder = resolveSequenceDecoder(world, fid);
- const frame = decoder?.toBitmap();
-
- if (frame) {
- const w = c.Computed.width[eid];
- const h = c.Computed.height[eid];
-
- ctx.save();
- ctx.clip();
-
- const mode = c.ScaleMode[fid] ?? 0;
- const [dx, dy, sw, sh] = getScaledImageProps(mode, frame.width, frame.height, w, h);
- ctx.drawImage(frame, dx, dy, sw, sh);
-
- ctx.restore();
- } else if (decoder?.errored) {
- ctx.fillStyle = MISSING_ASSET_COLOR;
- ctx.fill();
- }
- } else if (c.Paint[fid] === PaintType.HTML) {
- const host = c.HtmlHost[fid];
-
- if (host) {
- ctx.save();
- ctx.clip();
- host.draw(ctx, c.Computed.width[eid], c.Computed.height[eid]);
- ctx.restore();
- }
- } else if (c.Paint[fid] === PaintType.SURFACE) {
- const host = c.SurfaceHost[fid];
-
- if (host) {
- ctx.save();
- ctx.clip();
- host.draw(ctx, c.Computed.width[eid], c.Computed.height[eid]);
- ctx.restore();
- }
- } else if (c.Paint[fid] === PaintType.SOLID) {
- ctx.fillStyle = colorToHex(c.Computed.color[fid]);
- ctx.fill();
- } else if (c.Paint[fid] === PaintType.LINEAR_GRADIENT) {
- const w = c.Computed.width[eid];
- const h = c.Computed.height[eid];
- ctx.fillStyle = createLinearGradient(world, fid, ctx, w, h);
- ctx.fill();
- } else if (c.Paint[fid] === PaintType.RADIAL_GRADIENT) {
- const w = c.Computed.width[eid];
- const h = c.Computed.height[eid];
- ctx.fillStyle = createRadialGradient(world, fid, ctx, w, h);
- ctx.fill();
- } else if (c.Paint[fid] === PaintType.WAVEFORM) {
- renderWaveform(world, eid, fid);
- } else if (c.Paint[fid] === PaintType.SHADER) {
- renderShaderFill(world, eid, fills, index);
- }
-
- ctx.globalCompositeOperation = savedCO;
- ctx.globalAlpha = savedAlpha;
- }
-}
-
-/** The current frame of a video/image paint, as a GPU-uploadable source. */
-function shaderSourceBitmap(
- world: EngineWorld,
- fid: number,
- w: number,
- h: number,
-): { source: GPUCopyExternalImageSource; width: number; height: number } | null {
- const c = world.components;
-
- if (c.Paint[fid] === PaintType.IMAGE) {
- const bitmap = resolveImageDecoder(world, fid)?.decoder?.getBitmap(w, h);
- return bitmap ? { source: bitmap, width: bitmap.width, height: bitmap.height } : null;
- }
- if (c.Paint[fid] === PaintType.VIDEO) {
- const frame = resolveVideoDecoder(world, fid)?.toBitmap();
- return frame ? { source: frame, width: frame.width, height: frame.height } : null;
- }
- return null;
-}
-
-/**
- * Whether the fill at `index` is the input of a ready shader paint directly
- * above it. Only the immediate neighbor counts (a hidden paint in between
- * decouples the pair). Must mirror `renderShaderFill`'s input selection
- * exactly — a consumed media paint that the shader then fails to draw would
- * blank the element, so both sides check pipeline readiness and frame
- * availability.
- */
-function shaderConsumesFill(world: EngineWorld, eid: number, fills: number[], index: number): boolean {
- const c = world.components;
- const fid = fills[index];
- if (c.Paint[fid] !== PaintType.IMAGE && c.Paint[fid] !== PaintType.VIDEO) return false;
-
- const next = fills[index + 1];
- if (next === undefined || c.Paint[next] !== PaintType.SHADER) return false;
- if (hasComponent(world, next, c.Hidden)) return false;
- if (!resolveShaderHost(world, next)?.ready) return false;
-
- const w = c.Computed.width[eid];
- const h = c.Computed.height[eid];
- return shaderSourceBitmap(world, fid, w, h) !== null;
-}
-
-/**
- * Draws a shader paint: the media paint directly below it in the fill stack
- * is sampled as the shader's `source` texture and its output lands in the
- * parent's box in the media paint's place. Without a media paint below the
- * shader runs procedurally over a transparent source; before the pipeline is
- * ready it draws nothing and the media, if any, draws normally.
- */
-function renderShaderFill(world: EngineWorld, eid: number, fills: number[], index: number): void {
- const c = world.components;
- const ctx = world.ctx!;
-
- const host = resolveShaderHost(world, fills[index]);
- if (!host?.ready) return;
-
- const w = c.Computed.width[eid];
- const h = c.Computed.height[eid];
-
- // A visible media paint directly below is the shader's input. Anything
- // else (no fill below, a hidden one, a solid/gradient) runs the shader
- // procedurally over a transparent source, stacking like a normal paint.
- const mid = fills[index - 1];
- const isMedia = mid !== undefined
- && !hasComponent(world, mid, c.Hidden)
- && (c.Paint[mid] === PaintType.IMAGE || c.Paint[mid] === PaintType.VIDEO);
- let input: ReturnType<typeof shaderSourceBitmap> = null;
- if (isMedia) {
- input = shaderSourceBitmap(world, mid, w, h);
- if (!input) return;
- }
-
- const fit = input
- ? getScaledImageProps(c.ScaleMode[mid] ?? 0, input.width, input.height, w, h)
- : [0, 0, w, h] as [number, number, number, number];
- const time = (c.Computed.localTime[eid] ?? 0) / world.frameRate;
-
- ctx.save();
- ctx.clip();
- host.draw(ctx, w, h, input?.source ?? null, input?.width ?? 1, input?.height ?? 1, fit, time, c.Shader.uniforms[fills[index]] ?? null);
- ctx.restore();
-}
-
-function renderShadows(world: EngineWorld, eid: number): void {
- const ctx = world.ctx!;
- const c = world.components;
-
- const shadows = c.Cache.shadows[eid];
- if (!shadows) return;
-
- ctx.save();
- const savedAlpha = ctx.globalAlpha;
-
- // ctx.shadowBlur/OffsetX/OffsetY are in device-pixel space and are not
- // affected by the current transform, so scale them up to match the
- // content transform (camera * resolution).
- const shadowScale = world.camera.a * world.resolution;
-
- for (const sid of shadows) {
- if (hasComponent(world, sid, c.Hidden)) continue;
- const color = colorToHex(c.Computed.color[sid]);
- ctx.shadowColor = color;
- ctx.fillStyle = color;
- ctx.globalAlpha = savedAlpha * c.Computed.opacity[sid];
- ctx.shadowBlur = c.Computed.blur[sid] * shadowScale;
- ctx.shadowOffsetX = c.Computed.offsetX[sid] * shadowScale;
- ctx.shadowOffsetY = c.Computed.offsetY[sid] * shadowScale;
- ctx.fill();
- }
-
- ctx.restore();
-}
-
-function renderStrokes(world: EngineWorld, eid: number): void {
- const ctx = world.ctx!;
- const strokes = world.components.Cache.strokes[eid];
- if (!strokes) return;
-
- const Stroke = world.components.StrokeStyle;
- const Computed = world.components.Computed;
-
- ctx.lineWidth = Computed.strokeWidth[eid];
- ctx.lineJoin = StrokeJoin[Stroke.join[eid] ?? 0].toLocaleLowerCase() as CanvasLineJoin;
- ctx.lineCap = StrokeCap[Stroke.cap[eid] ?? 0].toLocaleLowerCase() as CanvasLineCap;
- ctx.miterLimit = Stroke.miterLimit[eid] ?? 3;
-
- const Hidden = world.components.Hidden;
- const Appearance = world.components.Appearance;
- const Paint = world.components.Paint;
-
- for (const sid of strokes) {
- if (hasComponent(world, sid, Hidden)) continue;
- const savedAlpha = ctx.globalAlpha;
- const savedCO = ctx.globalCompositeOperation;
- const bi = Appearance.blendMode[sid] ?? 0;
-
- if (bi !== 0) {
- ctx.globalCompositeOperation = COMPOSITE_OPERATIONS[bi];
- }
-
- ctx.globalAlpha = savedAlpha * Computed.opacity[sid];
-
- const paintType = Paint[sid];
- if (paintType === PaintType.LINEAR_GRADIENT) {
- const w = Computed.width[eid];
- const h = Computed.height[eid];
- ctx.strokeStyle = createLinearGradient(world, sid, ctx, w, h);
- } else if (paintType === PaintType.RADIAL_GRADIENT) {
- const w = Computed.width[eid];
- const h = Computed.height[eid];
- ctx.strokeStyle = createRadialGradient(world, sid, ctx, w, h);
- } else {
- ctx.strokeStyle = colorToHex(Computed.color[sid]);
- }
- ctx.stroke();
-
- ctx.globalCompositeOperation = savedCO;
- ctx.globalAlpha = savedAlpha;
- }
-}
-
-/**
- * Pulse animation for entities with the Generating tag.
- *
- * Easing: cubic-bezier(0.52, 0.18, 0.56, 0.88)
- * Duration: 0.6s per half-cycle (alternating)
- * Delay: 0.2s before the first transition
- * Full cycle: 0.2 delay + 0.6 forward + 0.6 reverse = 1.4s
- * repeats ∞ for the entire gen phase (~4.5s)
- *
- * Pulses between --background (#1c1c1c) and --secondary (#292929).
- */
-const generatingEase = cubicBezier(0.52, 0.18, 0.56, 0.88);
-
-const GEN_DELAY = 200; // ms
-const GEN_DURATION = 600; // ms per half-cycle
-const GEN_CYCLE = GEN_DURATION * 2; // full forward+reverse cycle
-
-// Colors: #1c1c1c → rgb(28,28,28) and #292929 → rgb(41,41,41)
-const GEN_FROM_R = 28, GEN_FROM_G = 28, GEN_FROM_B = 28;
-const GEN_TO_R = 41, GEN_TO_G = 41, GEN_TO_B = 41;
-
-function renderGenerating(world: EngineWorld, eid: number): void {
- const c = world.components;
- const ctx = world.ctx!;
- if (!hasComponent(world, eid, c.Generating)) return;
-
- const t = world.timestamp.now;
-
- // Before initial delay → show base color
- const elapsed = t % (GEN_DELAY + GEN_CYCLE);
- let factor = 0;
-
- if (elapsed >= GEN_DELAY) {
- const cycleT = elapsed - GEN_DELAY;
- const half = cycleT % GEN_DURATION;
- const progress = half / GEN_DURATION;
- const eased = generatingEase(progress);
-
- // Alternate direction: first half forward, second half reverse
- const inReverse = cycleT >= GEN_DURATION;
- factor = inReverse ? 1 - eased : eased;
- }
-
- const r = Math.round(GEN_FROM_R + (GEN_TO_R - GEN_FROM_R) * factor);
- const g = Math.round(GEN_FROM_G + (GEN_TO_G - GEN_FROM_G) * factor);
- const b = Math.round(GEN_FROM_B + (GEN_TO_B - GEN_FROM_B) * factor);
-
- ctx.fillStyle = `rgb(${r},${g},${b})`;
- ctx.fill();
-}
-
-// ── WAVEFORM paint ─────────────────────────────────────
-//
-// Renders an audio asset's pre-computed peaks as a bar chart inside the
-// parent geometry's bounds. Sourced from `c.AssetId[fid]` — the paint
-// carries its own asset reference, exactly like IMAGE/VIDEO/SEQUENCE paints.
-
-const WAVEFORM_BAR_WIDTH = 6;
-const WAVEFORM_BAR_GAP = 6;
-const WAVEFORM_BAR_RADIUS = WAVEFORM_BAR_WIDTH / 2;
-const WAVEFORM_MIN_BAR_HEIGHT = 4;
-const WAVEFORM_PADDING = 12;
-const WAVEFORM_BG_COLOR = '#202020';
-const WAVEFORM_BG_RADIUS = 12;
-
-function renderWaveform(world: EngineWorld, eid: number, fid: number): void {
- const c = world.components;
- const ctx = world.ctx!;
-
- const assetId = c.AssetId[fid];
- if (!assetId) return;
-
- const asset = world.assets.get(assetId);
- // Both AUDIO and VIDEO assets carry pre-computed peaks (video has audio).
- if (asset?.type !== 'AUDIO' && asset?.type !== 'VIDEO') return;
-
- const peaks = getAudioPeaks(asset);
- if (!peaks || peaks.length === 0) return;
-
- const w = c.Computed.width[eid];
- const h = c.Computed.height[eid];
-
- ctx.save();
- ctx.clip();
-
- // Background
- ctx.fillStyle = WAVEFORM_BG_COLOR;
- ctx.beginPath();
- ctx.roundRect(0, 0, w, h, WAVEFORM_BG_RADIUS);
- ctx.fill();
-
- // Bars
- const step = WAVEFORM_BAR_WIDTH + WAVEFORM_BAR_GAP;
- const availableWidth = w - WAVEFORM_PADDING * 2;
- const barCount = Math.floor(availableWidth / step);
- const maxBarHeight = h - WAVEFORM_PADDING * 2;
- if (barCount <= 0 || maxBarHeight <= 0) {
- ctx.restore();
- return;
- }
-
- const startX = WAVEFORM_PADDING + (availableWidth - barCount * step + WAVEFORM_BAR_GAP) / 2;
-
- ctx.fillStyle = '#ffffff';
-
- for (let i = 0; i < barCount; i++) {
- const peakIndex = Math.floor((i / barCount) * peaks.length);
- const value = (peaks[peakIndex] ?? 0) / 255;
- const barHeight = Math.max(value * maxBarHeight, WAVEFORM_MIN_BAR_HEIGHT);
- const x = startX + i * step;
- const y = (h - barHeight) / 2;
-
- ctx.beginPath();
- ctx.roundRect(x, y, WAVEFORM_BAR_WIDTH, barHeight, WAVEFORM_BAR_RADIUS);
- ctx.fill();
- }
-
- ctx.restore();
-}
-
-function renderShapeNode(world: EngineWorld, eid: number): void {
- drawRectPath(world, eid);
- renderShadows(world, eid);
- renderFills(world, eid);
- renderGenerating(world, eid);
- renderStrokes(world, eid);
-}
-
-function renderTextNode(world: EngineWorld, eid: number): void {
- renderText(world, eid);
-}
-
-function renderCaptionNode(world: EngineWorld, eid: number): void {
- resolveCaptionDecoder(world, eid)?.draw(world, eid);
-}
-
-// ── Transition rendering ─────────────────────────────────────
-
-function renderTransition(world: EngineWorld, sceneEid: number, leftEid: number): void {
- const ctx = world.ctx!;
-
- const c = world.components;
-
- const currentTime = c.Computed.localTime[sceneEid];
-
- const children = world.components.Cache.children[sceneEid];
- const rightEid = children.find(rid => c.Computed.start[rid] === c.Computed.end[leftEid]);
- if (!rightEid) return;
-
- const win = getTransitionWindow(world, leftEid, rightEid);
-
- if (currentTime < win.start || currentTime >= win.end) return;
-
- // we are transitioning
- const duration = win.end - win.start;
- const completion = (currentTime - win.start) / duration;
-
- const Transition = world.components.Transition;
- const type = Transition.type[leftEid] ?? TransitionType.DISSOLVE;
-
- const Computed = world.components.Computed;
- const parentEid = getParentEntity(world, leftEid);
- if (parentEid === null) return;
- const width = Computed.width[parentEid];
- const height = Computed.height[parentEid];
-
- switch (type) {
- case TransitionType.SLIDE_FROM_RIGHT: {
- renderNode(world, leftEid);
- ctx.save();
- ctx.translate(((1 - completion) ** 2 * width) | 0, 0);
- renderNode(world, rightEid);
- ctx.restore();
- break;
- }
- case TransitionType.SLIDE_FROM_LEFT: {
- renderNode(world, leftEid);
- ctx.save();
- ctx.translate(((1 - completion) ** 2 * width * -1) | 0, 0);
- renderNode(world, rightEid);
- ctx.restore();
- break;
- }
- case TransitionType.FADE_TO_BLACK: {
- if (completion < 0.5) {
- renderNode(world, leftEid);
- } else {
- renderNode(world, rightEid);
- }
- ctx.save();
- ctx.beginPath();
- ctx.rect(0, 0, width, height);
- ctx.closePath();
- ctx.fillStyle = '#000000';
- ctx.globalAlpha = completion < 0.5 ? 2 * completion : 2 * (1 - completion);
- ctx.fill();
- ctx.restore();
- break;
- }
- case TransitionType.FADE_TO_WHITE: {
- if (completion < 0.5) {
- renderNode(world, leftEid);
- } else {
- renderNode(world, rightEid);
- }
- ctx.save();
- ctx.beginPath();
- ctx.rect(0, 0, width, height);
- ctx.closePath();
- ctx.fillStyle = '#FFFFFF';
- ctx.globalAlpha = completion < 0.5 ? 2 * completion : 2 * (1 - completion);
- ctx.fill();
- ctx.restore();
- break;
- }
- default: {
- // Dissolve (default)
- renderNode(world, leftEid);
- ctx.save();
- ctx.globalAlpha = completion;
- renderNode(world, rightEid);
- ctx.restore();
- break;
- }
- }
-
- // Mark both partners as already drawn this frame so the parent's
- // children loop skips its plain renderNode pass for them.
- c.Computed.visibility[leftEid] = 0;
- c.Computed.visibility[rightEid] = 0;
-}
-
-export function renderNode(world: EngineWorld, eid: number): void {
- const c = world.components;
- const ctx = world.ctx!;
-
- if (c.Computed.visibility[eid] === 0 || hasComponent(world, eid, c.Culled)) return;
-
- if (hasComponent(world, eid, c.Interactive)) {
- world.hitRegions.push({
- callback: handleGeometryInteraction,
- target: { kind: 'entity', id: eid },
- });
- }
-
- if (hasComponent(world, eid, c.IsMask) || hasComponent(world, eid, c.Hidden)) return;
-
- ctx.save();
-
- ctx.transform(
- c.LocalTransform.a[eid],
- c.LocalTransform.b[eid],
- c.LocalTransform.c[eid],
- c.LocalTransform.d[eid],
- c.LocalTransform.e[eid],
- c.LocalTransform.f[eid],
- );
-
- for (const maskEid of world.components.Cache.masks[eid]) {
- if (c.Computed.visibility[maskEid] === 0) continue;
- ctx.save();
- ctx.setTransform(
- c.WorldTransform.a[maskEid],
- c.WorldTransform.b[maskEid],
- c.WorldTransform.c[maskEid],
- c.WorldTransform.d[maskEid],
- c.WorldTransform.e[maskEid],
- c.WorldTransform.f[maskEid],
- );
- drawRectPath(world, maskEid);
- ctx.restore();
- ctx.clip();
- }
-
- // Appearance
- ctx.globalAlpha *= c.Computed.opacity[eid];
- const bi = c.Appearance.blendMode[eid] ?? 0;
- if (bi !== 0) ctx.globalCompositeOperation = COMPOSITE_OPERATIONS[bi];
-
- const effects = buildEffects(world, eid);
- let initialFilter = 'none';
-
- if (effects !== null) {
- initialFilter = ctx.filter;
- ctx.filter = effects;
- }
-
-
- if (hasComponent(world, eid, c.Caption)) {
- renderCaptionNode(world, eid);
- } else if (c.Geometry[eid] === GeometryType.TEXT) {
- renderTextNode(world, eid);
- } else if (c.Geometry[eid] === GeometryType.RECT) {
- renderShapeNode(world, eid);
- }
-
- // Clip and render children
- const children = c.Cache.children[eid];
- if (children.length) {
- if (hasComponent(world, eid, c.ClipsContent)) {
- ctx.save();
- ctx.clip();
- }
-
- for (const child of children) {
- // Edge case: Child with transition
- if (hasComponent(world, child, c.Transition)) {
- renderTransition(world, eid, child);
- // Note: we are not breaking here since the transition handler will hide/unhide the children
- }
-
- renderNode(world, child);
- }
-
- if (hasComponent(world, eid, c.ClipsContent)) {
- ctx.restore();
- }
- }
-
- // Reset filter after drawing
- if (initialFilter !== 'none') {
- ctx.filter = initialFilter;
- }
-
- ctx.restore();
-}
-
-/**
- * Render system entry point. Call after transformSystem.
- *
- * Reads camera, background, and canvas size from world state and applies
- * DPR * Camera as the base canvas transform before drawing top-level nodes.
- */
-export function renderSystem(world: EngineWorld): void {
- const ctx = world.ctx!;
-
- const cw = world.canvas!.width;
- const ch = world.canvas!.height;
- const c = world.components;
-
- // Clear + background (identity transform for full-canvas clear)
- ctx.setTransform(1, 0, 0, 1, 0, 0);
- ctx.clearRect(0, 0, cw, ch);
-
- // The stage background is a preview-only affordance; offline encoding
- // renders just the scene onto a transparent canvas (the scene paints its
- // own fill if it has one).
- if (world.mode === 'realtime') {
- ctx.fillStyle = colorToHex(world.background);
- ctx.fillRect(0, 0, cw, ch);
- world.hitRegions.push({
- target: { kind: 'hud', id: 'canvas', quad: getCanvasQuad(world) },
- callback: handleCanvasInteraction,
- });
- }
-
- // Apply camera transform: DPR * Camera
- ctx.setTransform(
- world.camera.a * world.resolution,
- world.camera.b * world.resolution,
- world.camera.c * world.resolution,
- world.camera.d * world.resolution,
- world.camera.e * world.resolution,
- world.camera.f * world.resolution,
- );
-
- // Render top-level nodes.
- for (const eid of query(world, [Or(c.Geometry, c.Group), Not(ChildOf('*')), Not(c.Deleted), Not(c.Culled)])) {
- renderNode(world, eid);
- }
-}
-
-function getCanvasQuad(world: EngineWorld): Quad {
- return [
- { x: 0, y: 0 },
- { x: world.canvas!.width, y: 0 },
- { x: world.canvas!.width, y: world.canvas!.height },
- { x: 0, y: world.canvas!.height },
- ];
-}
diff --git a/apps/web/src/components/engine/systems/transform.ts b/apps/web/src/components/engine/systems/transform.ts
deleted file mode 100644
index 79c1144d..00000000
--- a/apps/web/src/components/engine/systems/transform.ts
+++ /dev/null
@@ -1,308 +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 { hasComponent, query, Not, Or } from 'bitecs';
-
-import { ChildOf } from '../components/relations';
-import { addComponent, removeComponent } from '../api/events';
-import { getParentEntity } from '../api/base';
-
-import type { EngineWorld } from '../api/world';
-
-import {
- multiply2D,
- aabbFromTransformedRect,
- aabbsIntersect,
- translate2D,
- rotate2D,
- skew2D,
- scale2D,
- type Mat2D,
-} from '../math';
-
-/**
- * Compute the local 2D affine matrix from Offset, Rotation, Scale,
- * Anchor, Skew and Size. Stores result in LocalMatrix[eid].
- */
-export function computeLocalMatrix(world: EngineWorld, eid: number): void {
- const Flip = world.components.Flip;
- const Anchor = world.components.Anchor;
- const Computed = world.components.Computed;
- const LocalTransform = world.components.LocalTransform;
-
- const positionX = Computed.positionX[eid] + Computed.offsetX[eid];
- const positionY = Computed.positionY[eid] + Computed.offsetY[eid];
- const pivotX = (Anchor.x[eid] ?? 0.5) * Computed.width[eid];
- const pivotY = (Anchor.y[eid] ?? 0.5) * Computed.height[eid];
- const scaleX = (Flip.x[eid] ?? 1) * Computed.scaleX[eid];
- const scaleY = (Flip.y[eid] ?? 1) * Computed.scaleY[eid];
- const rotation = Computed.rotation[eid];
- const skewX = Computed.skewX[eid];
- const skewY = Computed.skewY[eid];
-
- let mat = translate2D(positionX, positionY);
- mat = multiply2D(mat, translate2D(pivotX, pivotY));
- mat = multiply2D(mat, rotate2D(rotation));
- mat = multiply2D(mat, skew2D(skewX, skewY));
- mat = multiply2D(mat, scale2D(scaleX, scaleY));
- mat = multiply2D(mat, translate2D(-pivotX, -pivotY));
-
- LocalTransform.a[eid] = mat.a;
- LocalTransform.b[eid] = mat.b;
- LocalTransform.c[eid] = mat.c;
- LocalTransform.d[eid] = mat.d;
- LocalTransform.e[eid] = mat.e;
- LocalTransform.f[eid] = mat.f;
-}
-
-/**
- * WorldTransform = Parent.WorldTransform * LocalMatrix
- * For top-level entities (no entity parent), pass the camera × DPR matrix.
- */
-export function computeWorldTransform(world: EngineWorld, eid: number, parentEid: number | null): void {
- const WorldTransform = world.components.WorldTransform;
- const LocalTransform = world.components.LocalTransform;
-
- if (LocalTransform.a[eid] === undefined) {
- LocalTransform.a[eid] = 1;
- LocalTransform.b[eid] = 0;
- LocalTransform.c[eid] = 0;
- LocalTransform.d[eid] = 1;
- LocalTransform.e[eid] = 0;
- LocalTransform.f[eid] = 0;
- }
-
- let parent: Mat2D;
- if (parentEid !== null) {
- parent = {
- a: WorldTransform.a[parentEid], b: WorldTransform.b[parentEid],
- c: WorldTransform.c[parentEid], d: WorldTransform.d[parentEid],
- e: WorldTransform.e[parentEid], f: WorldTransform.f[parentEid],
- }
- } else {
- const res = world.resolution;
- parent = {
- a: world.camera.a * res, b: world.camera.b * res,
- c: world.camera.c * res, d: world.camera.d * res,
- e: world.camera.e * res, f: world.camera.f * res,
- };
- }
-
- const local: Mat2D = {
- a: LocalTransform.a[eid], b: LocalTransform.b[eid],
- c: LocalTransform.c[eid], d: LocalTransform.d[eid],
- e: LocalTransform.e[eid], f: LocalTransform.f[eid],
- };
-
- const m = multiply2D(parent, local);
-
- WorldTransform.a[eid] = m.a;
- WorldTransform.b[eid] = m.b;
- WorldTransform.c[eid] = m.c;
- WorldTransform.d[eid] = m.d;
- WorldTransform.e[eid] = m.e;
- WorldTransform.f[eid] = m.f;
-}
-
-/**
- * Compute world-space AABB from world transform and size.
- */
-export function computeWorldBounds(world: EngineWorld, eid: number): void {
- const WorldBounds = world.components.WorldBounds;
- const WorldTransform = world.components.WorldTransform;
- const Computed = world.components.Computed;
-
- const worldMat: Mat2D = {
- a: WorldTransform.a[eid], b: WorldTransform.b[eid],
- c: WorldTransform.c[eid], d: WorldTransform.d[eid],
- e: WorldTransform.e[eid], f: WorldTransform.f[eid],
- };
- const mat = multiply2D(worldMat, translate2D(Computed.originX[eid], Computed.originY[eid]));
- const bounds = aabbFromTransformedRect(mat, Computed.width[eid], Computed.height[eid]);
-
- WorldBounds.minX[eid] = bounds.minX;
- WorldBounds.minY[eid] = bounds.minY;
- WorldBounds.maxX[eid] = bounds.maxX;
- WorldBounds.maxY[eid] = bounds.maxY;
-}
-
-function cullEntity(world: EngineWorld, eid: number, parentEid: number | null): void {
- const WorldBounds = world.components.WorldBounds;
- const Culled = world.components.Culled;
-
- // Inherit cull from the parent: the renderer already skips entire
- // subtrees of culled parents, so descendants are effectively invisible
- // even when their own AABB intersects the parent's. Propagating the tag
- // down lets us free their decoders too. Pre-order DFS guarantees the
- // parent's tag is already settled by the time we reach the child.
-
-
- let intersect = true;
-
- if (parentEid === null) {
- intersect = aabbsIntersect({
- minX: WorldBounds.minX[eid],
- minY: WorldBounds.minY[eid],
- maxX: WorldBounds.maxX[eid],
- maxY: WorldBounds.maxY[eid],
- }, {
- minX: 0,
- minY: 0,
- maxX: world.canvas!.width,
- maxY: world.canvas!.height,
- });
- } else {
- intersect = !hasComponent(world, parentEid, Culled);
- }
-
- const wasCulled = hasComponent(world, eid, Culled);
-
- if (!intersect) {
- if (!wasCulled) {
- addComponent(world, eid, Culled, false);
- }
- } else if (wasCulled) {
- removeComponent(world, eid, Culled, false);
- }
-}
-
-/**
- * Recompute a Group entity's Computed bounds from the union of its children's
- * transformed boxes. Sequences aren't spatial constructs — they mirror their
- * parent's frame instead (the walk is parent-first, so the parent's Computed
- * values are already settled). Leaf entities own an absolute Size and need no
- * resolution here.
- */
-export function computeGroupBounds(world: EngineWorld, eid: number): void {
- const c = world.components;
-
- if (hasComponent(world, eid, c.Sequential)) {
- const parentEid = getParentEntity(world, eid);
- if (parentEid !== null) {
- c.Computed.width[eid] = c.Computed.width[parentEid];
- c.Computed.height[eid] = c.Computed.height[parentEid];
- c.Computed.originX[eid] = c.Computed.originX[parentEid] ?? 0;
- c.Computed.originY[eid] = c.Computed.originY[parentEid] ?? 0;
- return;
- }
- }
-
- if (hasComponent(world, eid, c.Group)) {
- let minX = Infinity;
- let minY = Infinity;
- let maxX = -Infinity;
- let maxY = -Infinity;
- let any = false;
-
- for (const childEid of query(world, [Or(c.Geometry, c.Group), ChildOf(eid), Not(c.Deleted), Not(c.Hidden)])) {
- const mat: Mat2D = {
- a: c.LocalTransform.a[childEid] ?? 1, b: c.LocalTransform.b[childEid] ?? 0,
- c: c.LocalTransform.c[childEid] ?? 0, d: c.LocalTransform.d[childEid] ?? 1,
- e: c.LocalTransform.e[childEid] ?? 0, f: c.LocalTransform.f[childEid] ?? 0,
- };
- const bounds = aabbFromTransformedRect(mat, c.Computed.width[childEid], c.Computed.height[childEid]);
- if (bounds.minX < minX) minX = bounds.minX;
- if (bounds.minY < minY) minY = bounds.minY;
- if (bounds.maxX > maxX) maxX = bounds.maxX;
- if (bounds.maxY > maxY) maxY = bounds.maxY;
- any = true;
- }
-
- if (!any) {
- c.Computed.width[eid] = 0;
- c.Computed.height[eid] = 0;
- return;
- }
-
- c.Computed.width[eid] = Math.max(0, maxX - minX);
- c.Computed.height[eid] = Math.max(0, maxY - minY);
- c.Computed.originX[eid] = minX;
- c.Computed.originY[eid] = minY;
- }
-}
-
-function adjustLayers(world: EngineWorld, adjust: number) {
- const c = world.components;
-
- if (c.Computed.visibility[adjust] === 0 || hasComponent(world, adjust, c.Hidden)) return;
-
-
- // get next non sequential parent
- let slot = adjust;
- let parent = getParentEntity(world, slot);
- while (parent !== null && hasComponent(world, parent, c.Sequential)) {
- slot = parent;
- parent = getParentEntity(world, slot);
- }
- if (parent === null) return;
-
- const siblings = c.Cache.children[parent];
- if (!siblings) return;
- const idx = siblings.indexOf(slot);
- if (idx <= 0) return;
-
- const target = siblings[idx - 1];
- if (target === undefined) return;
-
- // adjust's own local matrix must be current; it is not visited by the walk
- // (adjustment layers are neither Geometry nor Group) and reads only
- // motion-resolved Computed values.
- computeLocalMatrix(world, adjust);
-
- const adjustLocal: Mat2D = {
- a: c.LocalTransform.a[adjust], b: c.LocalTransform.b[adjust], c: c.LocalTransform.c[adjust],
- d: c.LocalTransform.d[adjust], e: c.LocalTransform.e[adjust], f: c.LocalTransform.f[adjust],
- };
- const clipLocal: Mat2D = {
- a: c.LocalTransform.a[target], b: c.LocalTransform.b[target], c: c.LocalTransform.c[target],
- d: c.LocalTransform.d[target], e: c.LocalTransform.e[target], f: c.LocalTransform.f[target],
- };
-
- const composed = multiply2D(adjustLocal, clipLocal);
- c.LocalTransform.a[target] = composed.a;
- c.LocalTransform.b[target] = composed.b;
- c.LocalTransform.c[target] = composed.c;
- c.LocalTransform.d[target] = composed.d;
- c.LocalTransform.e[target] = composed.e;
- c.LocalTransform.f[target] = composed.f;
-
- const reworld = (eid: number, parentEid: number | null) => {
- computeWorldTransform(world, eid, parentEid);
- computeWorldBounds(world, eid);
- cullEntity(world, eid, parentEid);
-
- for (const childEid of query(world, [Or(c.Geometry, c.Group), ChildOf(eid), Not(c.Deleted)])) {
- reworld(childEid, eid);
- }
- }
-
- reworld(target, parent);
-}
-
-/**
- * Transform system entry point. Call once per frame before renderSystem.
- */
-export function transformSystem(world: EngineWorld): void {
- const c = world.components;
-
- const walk = (eid: number, parentEid: number | null) => {
- computeGroupBounds(world, eid);
- computeLocalMatrix(world, eid);
- computeWorldTransform(world, eid, parentEid);
- computeWorldBounds(world, eid);
- cullEntity(world, eid, parentEid);
-
- for (const childEid of query(world, [Or(c.Geometry, c.Group), ChildOf(eid), Not(c.Deleted)])) {
- walk(childEid, eid);
- }
- }
-
- for (const eid of query(world, [Not(ChildOf('*')), Or(c.Geometry, c.Group), Not(c.Deleted)])) {
- walk(eid, null);
- }
-
- for (const adjust of query(world, [c.AdjustmentLayer, Not(c.Deleted)])) {
- adjustLayers(world, adjust);
- }
-}
diff --git a/apps/web/src/components/engine/thumbnail.ts b/apps/web/src/components/engine/thumbnail.ts
deleted file mode 100644
index 7701febd..00000000
--- a/apps/web/src/components/engine/thumbnail.ts
+++ /dev/null
@@ -1,45 +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/. */
-
-const THUMBNAIL_WIDTH = 464;
-const THUMBNAIL_HEIGHT = 260;
-const JPEG_QUALITY = 0.8;
-
-export async function captureCanvasThumbnail(
- canvas?: HTMLCanvasElement | OffscreenCanvas | null,
-): Promise<Blob | null> {
- if (!(canvas instanceof HTMLCanvasElement)) {
- return null;
- }
-
- const offscreen = document.createElement('canvas');
- offscreen.width = THUMBNAIL_WIDTH;
- offscreen.height = THUMBNAIL_HEIGHT;
-
- const ctx = offscreen.getContext('2d');
- if (!ctx) return null;
-
- const targetAspect = THUMBNAIL_WIDTH / THUMBNAIL_HEIGHT;
- let sw = canvas.width;
- let sh = canvas.height;
- let sx = 0;
- let sy = 0;
- if (sw / sh > targetAspect) {
- sw = sh * targetAspect;
- sx = (canvas.width - sw) / 2;
- } else {
- sh = sw / targetAspect;
- sy = (canvas.height - sh) / 2;
- }
-
- ctx.drawImage(canvas, sx, sy, sw, sh, 0, 0, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
-
- return new Promise((resolve) => {
- offscreen.toBlob(
- resolve,
- 'image/jpeg',
- JPEG_QUALITY,
- );
- });
-}
diff --git a/apps/web/src/components/engine/timeline/config.ts b/apps/web/src/components/engine/timeline/config.ts
deleted file mode 100644
index ab955775..00000000
--- a/apps/web/src/components/engine/timeline/config.ts
+++ /dev/null
@@ -1,41 +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/. */
-
-export const MIN_CLIP_HEIGHT = 28;
-export const MAX_CLIP_HEIGHT = 116;
-export const KEYFRAME_TRACK_HEIGHT = 32;
-export const TARGET_MAJOR_TICK_DISTANCE = 160;
-
-export const TIMELINE_RESOLUTION_RANGE = [0.03, 120] as const;
-export const DEFAULT_TIMELINE_RESOLUTION = 1 / 0.7;
-export const DEFAULT_CLIP_HEIGHT = 40;
-
-export const TIMELINE_PADDING_LEFT = 8;
-
-export const WHEEL_LINE_HEIGHT = 16;
-export const WHEEL_PAGE_HEIGHT = 600;
-
-export const ZOOM_DELTA_CLAMP = 50;
-export const ZOOM_SENSITIVITY = 0.01;
-
-export const SCROLL_X_SENSITIVITY = 2;
-
-export const CLICK_DISTANCE_THRESHOLD = 5;
-export const DOUBLE_CLICK_THRESHOLD = 500;
-
-export const RULER_HEIGHT = 36;
-export const RULER_TICK_HEIGHT_MAJOR = 9;
-export const RULER_TICK_HEIGHT_MINOR = 3;
-export const RULER_LABEL_Y = 18;
-
-export const CLIP_CORNER_RADIUS = 4;
-export const CLIP_FONT = '11px Inter';
-export const CLIP_LABEL_X = 6;
-export const CLIP_LABEL_Y = 10;
-export const CLIP_LABEL_HEIGHT = 20;
-
-export const CLIP_BREAKPOINTS = {
- xs: 32,
- sm: 40,
-}
diff --git a/apps/web/src/components/engine/timeline/constants.ts b/apps/web/src/components/engine/timeline/constants.ts
deleted file mode 100644
index fce7d481..00000000
--- a/apps/web/src/components/engine/timeline/constants.ts
+++ /dev/null
@@ -1,131 +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 { secondsToFrames } from "../utils";
-
-export const RULER_INTERVALS = [
- {
- numerator: secondsToFrames(600),
- denominator: 10,
- },
- {
- numerator: secondsToFrames(300),
- denominator: 5,
- },
- {
- numerator: secondsToFrames(120),
- denominator: 4,
- },
- {
- numerator: secondsToFrames(60),
- denominator: 6,
- },
- {
- numerator: secondsToFrames(30),
- denominator: 10,
- },
- {
- numerator: secondsToFrames(10),
- denominator: 10,
- },
- {
- numerator: secondsToFrames(5),
- denominator: 10,
- },
- {
- numerator: secondsToFrames(3),
- denominator: 10,
- },
- {
- numerator: secondsToFrames(2),
- denominator: 10,
- },
- {
- numerator: secondsToFrames(1),
- denominator: 10,
- },
- {
- numerator: 15,
- denominator: 5,
- },
- {
- numerator: 10,
- denominator: 10,
- },
- {
- numerator: 5,
- denominator: 5,
- },
-];
-
-// window.getComputedStyle(document.body).getPropertyValue('--background')
-
-export const COLORS = {
- background: {
- default: '#1C1C1C',
- muted: '#292929',
- accent: '#292929',
- },
- border: {
- darker: '#1C1C1C',
- input: '#3E3F41',
- ring: '#008CFF',
- scrubber: '#F43535', // ambient red
- },
- ruler: {
- tick: 'rgba(53, 53, 53, 1)',
- text: 'rgba(75, 75, 75, 1)',
- },
- clip: {
- group: {
- background: '#40464F',
- primary: '#545D67',
- foreground: '#EDF0F3',
- },
- video: {
- background: '#0F3C8A',
- primary: '#70A7FF', // Waveform
- foreground: '#CCE0FF', // Label
- },
- audio: {
- background: '#004732',
- primary: '#0DBF8A',
- foreground: '#CBFAED',
- },
- caption: {
- background: '#7B1E5A',
- primary: '#AA317B', // Word background
- foreground: '#F9DCED',
- },
- image: {
- background: '#A55912',
- foreground: '#F9F2EC',
- },
- text: {
- background: '#303E4F',
- foreground: '#E7EFF9',
- },
- shape: {
- background: '#933325',
- foreground: '#FAEDEB',
- },
- scene: {
- background: '#066284',
- primary: '#5AB9DD',
- foreground: '#DBEAF0',
- },
- mask: {
- background: '#5455DE',
- foreground: '#EAE8FC',
- },
- adjustment: {
- background: '#4827B0',
- foreground: '#E7DFFB',
- },
- html: {
- background: '#2B525F',
- foreground: '#E7F4F9',
- },
- },
-} as const;
diff --git a/apps/web/src/components/engine/timeline/index.ts b/apps/web/src/components/engine/timeline/index.ts
deleted file mode 100644
index 18b2b868..00000000
--- a/apps/web/src/components/engine/timeline/index.ts
+++ /dev/null
@@ -1,7 +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/. */
-
-export * from './timeline';
-export * from './render';
-export * from './utils';
diff --git a/apps/web/src/components/engine/timeline/paths.ts b/apps/web/src/components/engine/timeline/paths.ts
deleted file mode 100644
index b2e56573..00000000
--- a/apps/web/src/components/engine/timeline/paths.ts
+++ /dev/null
@@ -1,7 +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/. */
-
-export const KNOB_PATH = new Path2D("M6.47987 12.372C5.68637 13.2449 4.31359 13.2449 3.52004 12.372L0.520121 9.07213C0.185444 8.70399 4.73995e-07 8.22432 4.61669e-07 7.72679L3.19793e-07 2C2.92428e-07 0.89543 0.895431 1.57728e-07 2 2.54292e-07L7.99987 7.78817e-07C9.10449 8.75386e-07 9.99994 0.895511 9.99987 2.00013L9.99949 7.72692C9.99946 8.22437 9.81405 8.70396 9.47945 9.07206L6.47987 12.372Z");
-export const IN_POINT_PATH = new Path2D("M1.95052 0H4.61719V10H1.95052V8.5V6.25L0.771284 5.97362C-0.257794 5.73243 -0.257794 4.26757 0.771284 4.02638L1.95052 3.75V1.5V0Z");
-export const OUT_POINT_PATH = new Path2D("M2.66667 0H0V10H2.66667V8.5V6.25L3.8459 5.97362C4.87498 5.73243 4.87498 4.26757 3.8459 4.02638L2.66667 3.75V1.5V0Z");
diff --git a/apps/web/src/components/engine/timeline/pointer.ts b/apps/web/src/components/engine/timeline/pointer.ts
deleted file mode 100644
index 1e7caab9..00000000
--- a/apps/web/src/components/engine/timeline/pointer.ts
+++ /dev/null
@@ -1,302 +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 { assert } from "@/utils";
-import * as cfg from "./config";
-
-type MousePosition = {
- state: 'idle';
- currentX: number;
- currentY: number;
-} | {
- state: 'pressed' | 'pressing' | 'lifted';
- initialX: number;
- initialY: number;
- timestamp: number;
- currentX: number;
- currentY: number;
- deltaX: number;
- deltaY: number;
-}
-
-type TransformedRegion = {
- minX: number;
- minY: number;
- maxX: number;
- maxY: number;
- id: string;
- passthrough: boolean;
-}
-
-type HitRegions = {
- prev: Array<TransformedRegion>;
- next: Array<TransformedRegion>;
- id: string;
- index: number;
-};
-
-export type Pointer = ReturnType<typeof createPointer>;
-
-type PointerOptions = {
- marquee?: {
- x: number;
- y: number;
- width: number;
- height: number;
- };
- canvas?: HTMLCanvasElement | null;
- ctx?: CanvasRenderingContext2D | null;
-};
-
-export function createPointer(options: PointerOptions) {
- let position: MousePosition | null = null;
- let pressRegionID: string | null = null;
- let hitRegions: HitRegions = {
- prev: [],
- next: [],
- id: '',
- index: 0,
- };
- let lastClickPosition: {
- x: number;
- y: number;
- timestamp: number;
- } | null = null;
-
- let shiftPressed = false;
-
- function down(event: PointerEvent) {
- const rect = options.canvas?.getBoundingClientRect();
- if (!rect || event.button !== 0) return;
-
- const currentX = event.clientX - rect.left;
- const currentY = event.clientY - rect.top;
-
- position = {
- state: 'pressed',
- timestamp: performance.now(),
- initialX: currentX,
- initialY: currentY,
- currentX,
- currentY,
- deltaX: 0,
- deltaY: 0,
- };
-
- shiftPressed = event.shiftKey;
- }
-
- function move(event: PointerEvent) {
- const rect = options.canvas?.getBoundingClientRect();
- if (!rect) return;
-
- shiftPressed = event.shiftKey;
-
- const currentX = event.clientX - rect.left;
- const currentY = event.clientY - rect.top;
-
- if (!position) {
- position = {
- state: 'idle',
- currentX,
- currentY,
- };
- return;
- }
-
- if (position.state != 'idle') {
- position.deltaX = currentX - position.initialX;
- position.deltaY = currentY - position.initialY;
- }
-
- position.currentX = currentX;
- position.currentY = currentY;
- }
-
- function up(event: PointerEvent) {
- const rect = options.canvas?.getBoundingClientRect();
- if (!rect) return;
- if (!position || position.state === 'idle') return;
-
- const currentX = event.clientX - rect.left;
- const currentY = event.clientY - rect.top;
-
- position = {
- ...position,
- state: 'lifted',
- currentX,
- currentY,
- deltaX: currentX - position.initialX,
- deltaY: currentY - position.initialY,
- };
- pressRegionID = null;
- shiftPressed = event.shiftKey;
- }
-
-
- function scope(id: string) {
- hitRegions.id = id;
- hitRegions.index = 0;
-
- return pointer;
- }
-
- function region(x = 0, y = 0, w: number, h: number, id?: string, passthrough = false) {
- assert(options.ctx, 'Canvas context must be set');
-
- if (!position) return {
- hovering: false,
- clicked: false,
- pressing: false,
- pressed: false,
- dragging: false,
- doubleClicked: false,
- intersectsMarquee: false,
- };
-
- const regionData = {
- id: `${hitRegions.id}/${id ?? hitRegions.index}`,
- ...transformRegionToCanvas(options.ctx, x, y, w, h),
- passthrough,
- };
-
- hitRegions.index++;
- hitRegions.next.push(regionData);
-
- // Mouse position is in CSS pixels, scale to device pixels to match canvas coordinates
- const mouseX = position.currentX * window.devicePixelRatio;
- const mouseY = position.currentY * window.devicePixelRatio;
-
- const matches = hitRegions.prev.filter(({ minX, minY, maxX, maxY }) => (
- mouseX >= minX &&
- mouseX < maxX &&
- mouseY >= minY &&
- mouseY < maxY
- ));
-
- const lastMatch = matches.findLast(({ passthrough }) => !passthrough);
-
- let isPrimary = lastMatch?.id === regionData.id;
- let isPassive = matches.some(({ passthrough, id }) => passthrough && id == regionData.id);
- let hovering = isPrimary || isPassive;
- let pressing = hovering && position?.state == 'pressing';
- let pressed = hovering && position?.state == 'pressed';
-
- // Track the region that was pressed down on
- if (pressed) {
- pressRegionID = lastMatch?.id ?? null;
- }
-
- // Assign dragging state after a distance threshold has been passed
- let dragging = false;
- if (position?.state == 'pressing' && pressRegionID == regionData.id) {
- const distance = Math.sqrt(position.deltaX ** 2 + position.deltaY ** 2);
- dragging = distance >= cfg.CLICK_DISTANCE_THRESHOLD;
- }
-
- // Only consider click if pointer hasn't moved beyond the distance threshold
- let clicked = false;
- if (position?.state == 'lifted') {
- const distance = Math.sqrt(position.deltaX ** 2 + position.deltaY ** 2);
- clicked = distance < cfg.CLICK_DISTANCE_THRESHOLD && hovering;
- }
-
- let doubleClicked = false;
- if (clicked && lastClickPosition) {
- const delta = performance.now() - lastClickPosition.timestamp;
- const distance = Math.sqrt(
- Math.pow(position.currentX - lastClickPosition.x, 2) +
- Math.pow(position.currentY - lastClickPosition.y, 2)
- );
- doubleClicked = delta < cfg.DOUBLE_CLICK_THRESHOLD && hovering && distance < 10;
- }
-
- let intersectsMarquee = false;
- if (options.marquee) {
- // Convert marquee selection from CSS pixels to canvas coordinates
- const marqueeMinX = options.marquee.x * window.devicePixelRatio;
- const marqueeMaxX = (options.marquee.x + options.marquee.width) * window.devicePixelRatio;
- const marqueeMinY = options.marquee.y * window.devicePixelRatio;
- const marqueeMaxY = (options.marquee.y + options.marquee.height) * window.devicePixelRatio;
-
- // Check intersection: regions intersect if they overlap
- intersectsMarquee = !(
- regionData.maxX < marqueeMinX ||
- regionData.minX > marqueeMaxX ||
- regionData.maxY < marqueeMinY ||
- regionData.minY > marqueeMaxY
- );
- }
-
- return {
- hovering, // If the pointer is over the rect
- clicked, // If the rect was clicked this frame
- pressing, // If the pointer is pressing down on the rect this frame
- pressed, // If the just started pressing down on the rect this frame
- dragging, // If the pointer is dragging the rect this frame
- doubleClicked, // If the rect was double clicked this frame
- intersectsMarquee, // If the region intersects the marquee selection
- };
- }
-
- function reset() {
- hitRegions.prev = [...hitRegions.next];
- hitRegions.next.length = 0;
-
- if (position?.state == 'lifted') {
- position = {
- ...position,
- state: 'idle',
- };
- lastClickPosition = {
- x: position.currentX,
- y: position.currentY,
- timestamp: performance.now(),
- };
- }
-
- if (position?.state == 'pressed') {
- position = {
- ...position,
- state: 'pressing',
- };
- }
- }
-
- const pointer = {
- get position() {
- return position;
- },
- get shiftPressed() {
- return shiftPressed;
- },
- down,
- move,
- up,
- scope,
- region,
- reset,
- };
-
- return pointer;
-}
-
-function transformRegionToCanvas(ctx: CanvasRenderingContext2D, x: number, y: number, w: number, h: number) {
- // Transform the region to canvas coordinates when storing it
- const transform = ctx.getTransform();
-
- // Transform all four corners of the region to canvas coordinates
- const topLeft = transform.transformPoint(new DOMPoint(x, y));
- const topRight = transform.transformPoint(new DOMPoint(x + w, y));
- const bottomLeft = transform.transformPoint(new DOMPoint(x, y + h));
- const bottomRight = transform.transformPoint(new DOMPoint(x + w, y + h));
-
- // Compute bounding box in canvas coordinates
- return {
- minX: Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
- maxX: Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
- minY: Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y),
- maxY: Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y),
- };
-}
diff --git a/apps/web/src/components/engine/timeline/render/audio.ts b/apps/web/src/components/engine/timeline/render/audio.ts
deleted file mode 100644
index 330425a0..00000000
--- a/apps/web/src/components/engine/timeline/render/audio.ts
+++ /dev/null
@@ -1,516 +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 { assert } from '@/utils';
-import { AudioSample, AudioSampleSink } from 'mediabunny';
-import { framesToPixels, getRelativeViewport, getCurrentResolution } from '../utils';
-import { CLIP_BREAKPOINTS, CLIP_CORNER_RADIUS } from '../config';
-import { getAudioTrack } from '../../decoders/audio';
-
-import type { EngineWorld } from '../../api/world';
-import type { TimelineContext } from '../world';
-import type { AudioAsset, VideoAsset } from '../../db';
-
-interface StoredPeaks {
- start: number;
- end: number;
- id: string;
-}
-
-interface SampledPeaks extends StoredPeaks {
- data: Uint8ClampedArray;
- peaksPerSecond: number;
-};
-
-type SampleOptions = {
- clipEid: number;
- asset: VideoAsset | AudioAsset;
- peaksPerSecond: number;
- start: number;
- end: number;
-}
-
-type PeakData = {
- start: number;
- end: number;
- data: Uint8ClampedArray;
-}
-
-type WaveformDrawOptions = {
- asset: VideoAsset | AudioAsset;
- offsetY: number;
- padding: number;
- color: string;
-}
-
-type AudioCache = {
- samples: SampledPeaks[];
- start: number;
- end: number;
- hash: string;
- updating: boolean;
- pendingUpdate: SampleOptions | null;
-}
-
-type AudioStore = {
- initializing: boolean;
- data: StoredPeaks[];
-}
-
-const HIGH_RES_PEAKS_PER_SECOND = 800;
-const MAX_CHUNK_DURATION = 2;
-const SAMPLE_WIDTH = 1;
-
-const IDB_NAME = "peak-cache-db";
-const IDB_VERSION = 1;
-const IDB_STORE_NAME = "data";
-
-const audioCache = new Map<number, AudioCache>(); // By clip eid
-const audioStore = new Map<string, AudioStore>(); // By asset id
-
-export function pixelsToSeconds(world: EngineWorld, pixels: number) {
- const resolution = getCurrentResolution(world);
- const fps = world.frameRate;
-
- return pixels / (fps * resolution);
-}
-
-export function cloneAudioCacheForSplit(clipEid: number, splitClipEid: number) {
- const sourceCache = audioCache.get(clipEid);
- if (!sourceCache) return;
-
- audioCache.set(splitClipEid, {
- ...sourceCache,
- // Keep a separate cache object/array but reuse underlying peak buffers.
- samples: sourceCache.samples.map(sample => ({ ...sample })),
- updating: false,
- pendingUpdate: null,
- });
-}
-
-export function clearAudioCache(eid?: number) {
- if (eid != undefined) {
- audioCache.delete(eid);
- return;
- }
-
- audioCache.clear();
-}
-
-export function clearAudioBuffers() {
- audioCache.clear();
- audioStore.clear();
-}
-
-export function renderAudioWaveform(world: EngineWorld, timeline: TimelineContext, eid: number, options: WaveformDrawOptions) {
- const ctx = timeline.ctx!;
-
- if (!options.asset.sampleRate) return;
-
- const resolution = getCurrentResolution(world);
-
- const c = world.components;
- const fps = world.frameRate;
-
- const layerHeight = timeline.currentLayerHeight;
- const clipLeft = framesToPixels(world, c.Computed.start[eid]);
- const clipRight = framesToPixels(world, c.Computed.end[eid]);
- const clipWidth = clipRight - clipLeft;
- const maskHeight = layerHeight - options.offsetY;
-
- const [vpl, vpr] = getRelativeViewport(world, timeline);
-
- ctx.save();
-
- // Clip the waveform to the background
- {
- ctx.beginPath();
- ctx.roundRect(clipLeft, options.offsetY, clipWidth, maskHeight, CLIP_CORNER_RADIUS);
- ctx.clip();
- }
-
- // Make the samples a bit smaller to avoid clipping
- const waveformHeight = maskHeight - options.padding;
-
- const adjustedViewportLeft = vpl - 5;
- const adjustedViewportRight = vpr + 5;
-
- const playbackRate = c.PlaybackRate[eid] ?? 1;
-
- // Each pixel covers playbackRate× more source audio when sped up, so we
- // need proportionally fewer peaks per source second to keep one peak/pixel.
- const pixelsPerSecond = Math.floor(fps * resolution);
- const peaksPerSecond = pixelsPerSecond / SAMPLE_WIDTH / playbackRate;
-
- // Trim is in source frames; the clip occupies trim/playbackRate timeline frames.
- const inPx = framesToPixels(world, (c.Trim.start[eid] ?? 0) / playbackRate);
- const outPx = framesToPixels(world, (c.Trim.end[eid] ?? c.Computed.duration[eid]) / playbackRate);
- const delayPx = framesToPixels(world, c.Computed.delay[eid]);
-
- const firstSample = Math.max(inPx, adjustedViewportLeft - delayPx);
- const lastSample = Math.min(outPx, adjustedViewportRight - delayPx);
-
- updateSamples({
- clipEid: eid,
- asset: options.asset,
- start: pixelsToSeconds(world, firstSample) * playbackRate,
- end: pixelsToSeconds(world, lastSample) * playbackRate,
- peaksPerSecond,
- });
-
- ctx.fillStyle = options.color;
-
- const positionMultiplier = maskHeight > CLIP_BREAKPOINTS.sm ? 0.5 : 1;
- const minSampleHeight = maskHeight > CLIP_BREAKPOINTS.sm ? 1 : 0;
-
- // Tone the waveform down on short clip rows so the label stays readable.
- if (timeline.currentLayerHeight <= CLIP_BREAKPOINTS.sm) {
- ctx.globalAlpha *= 0.6;
- }
-
- // Draw samples
- ctx.translate(delayPx, options.offsetY + options.padding * positionMultiplier);
-
- // Enables search at O(1) time complexity when peaks are sorted by start time ascending
- let lastPeakIndex = 0;
-
- for (let sampleX = firstSample; sampleX < lastSample; sampleX += SAMPLE_WIDTH) {
- const samples = audioCache.get(eid)?.samples;
- const time = pixelsToSeconds(world, sampleX) * playbackRate;
- const index = samples?.findIndex(p => time >= p.start && time <= p.end, lastPeakIndex) ?? -1;
- const sampleData = samples?.[index];
- lastPeakIndex = index;
-
- if (!sampleData) continue;
-
- const sampleIndex = Math.floor((time - sampleData.start) * sampleData.peaksPerSecond);
- const sample: number | undefined = sampleData.data[sampleIndex];
-
- if (sample === undefined) continue;
-
- const value = sample / 255;
- const sampleHeight = Math.max(value * waveformHeight, minSampleHeight);
- const sampleTop = (waveformHeight - sampleHeight);
- const sampleY = sampleTop * positionMultiplier;
-
- ctx.fillRect(sampleX, sampleY, SAMPLE_WIDTH, sampleHeight);
- }
-
- // close clip path
- ctx.closePath();
- ctx.restore();
-}
-
-function updateSamples(options: SampleOptions) {
- const stored = audioStore.get(options.asset.id);
- if (!stored || (!stored.initializing && stored.data.length === 0)) {
- initializePeakCache(options);
- }
-
- if (stored && stored.data.length > 0) {
- updateSampleCache(options, stored);
- }
-}
-
-
-function findBestPeakRange(options: SampleOptions) {
- const stored = audioStore.get(options.asset.id);
-
- assert(stored, 'Stored must be set');
-
- const minValue = Math.max(0, options.start);
- const maxValue = Math.min(options.asset.duration, options.end);
-
- const fistMatch = stored.data.find(p => inRange(minValue, [p.start, p.end]));
- const lastMatch = stored.data.find(p => inRange(maxValue, [p.start, p.end]));
-
- const start = fistMatch?.start ?? stored.data.at(0)?.start ?? 0;
- const end = lastMatch?.end ?? stored.data.at(-1)?.end ?? 0;
-
- const hash = createRangeHash({ ...options, start, end });
-
- return { start, end, hash };
-}
-
-async function updateSampleCache(options: SampleOptions, stored: AudioStore) {
- let cache = audioCache.get(options.clipEid);
- if (!cache) {
- cache = {
- samples: [],
- start: 0,
- end: 0,
- hash: '',
- updating: false,
- pendingUpdate: null,
- }
- audioCache.set(options.clipEid, cache);
- }
-
- if (cache.updating) {
- cache.pendingUpdate = options;
- return;
- }
-
- try {
- const { peaksPerSecond } = options;
- const bestRange = findBestPeakRange(options);
-
- if (bestRange.hash === cache.hash) {
- return;
- }
-
- cache.updating = true;
- cache.hash = bestRange.hash;
-
- for (const peak of stored.data) {
- if (!inRange(peak.start, [bestRange.start, bestRange.end])) {
- continue;
- }
-
- // Downsample to desired peaks per second
- const duration = peak.end - peak.start;
- const targetPeaks = Math.ceil(duration * peaksPerSecond);
- const downsampledBytes = new Uint8ClampedArray(targetPeaks);
- const data = await loadPeaks(peak.id);
-
- if (!data) continue;
- const numFrames = data.length;
-
- // Downsample by calculating peaks directly for target positions
- const ratio = numFrames / targetPeaks;
- for (let i = 0; i < targetPeaks; i++) {
- const start = Math.floor(i * ratio);
- const end = Math.floor((i + 1) * ratio);
- let max = 0;
-
- // For each frame in the range, find max across all channels
- for (let frameIdx = start; frameIdx < end && frameIdx < numFrames; frameIdx++) {
- max = Math.max(max, data[frameIdx]);
- }
-
- downsampledBytes[i] = max;
- }
-
- cache.samples = cache.samples.filter(p => p.id !== peak.id);
- cache.samples.push({
- ...peak,
- data: downsampledBytes,
- peaksPerSecond,
- });
- // order by start time ascending
- cache.samples.sort((a, b) => a.start - b.start);
- }
-
- cache.samples = cache.samples.filter(p => inRange(p.start, [
- bestRange.start,
- bestRange.end,
- ]));
- } catch (error) {
- console.error('Error updating peak cache', error);
- } finally {
- cache.updating = false;
- }
-
- if (cache.pendingUpdate) {
- const pending = cache.pendingUpdate;
- if (!pending) return;
- cache.pendingUpdate = null;
- void updateSampleCache(pending, stored);
- }
-}
-
-async function initializePeakCache(options: SampleOptions) {
- let stored = audioStore.get(options.asset.id);
- if (!stored) {
- stored = { initializing: false, data: [] };
- audioStore.set(options.asset.id, stored);
- }
-
- try {
- stored.initializing = true;
- const track = await getAudioTrack(options.asset);
- if (!track) return;
- const sink = new AudioSampleSink(track);
- // const iterator = sink.samples(...clip.range);
- const iterator = sink.samples();
-
- const peaks: PeakData[] = [];
- let duration = 0;
-
- for await (const sample of iterator) {
- duration += sample.duration;
- try {
- peaks.push(getPeaksFromAudioSample(sample))
- } finally {
- sample.close();
- }
-
- if (duration > MAX_CHUNK_DURATION) {
- const mergedPeaks = mergePeaks(peaks);
- const id = `${options.asset.id}-${mergedPeaks.start}`;
-
- stored.data.push({
- start: mergedPeaks.start,
- end: mergedPeaks.end,
- id,
- });
- await storePeaks(id, mergedPeaks.data);
- peaks.length = 0;
- duration = 0;
- }
- }
-
- if (peaks.length > 0) {
- const mergedPeaks = mergePeaks(peaks);
- const id = `${options.asset.id}-${mergedPeaks.start}`;
-
- stored.data.push({
- start: mergedPeaks.start,
- end: mergedPeaks.end,
- id,
- });
-
- await storePeaks(id, mergedPeaks.data);
- }
-
- stored.data.sort((a, b) => a.start - b.start);
- } catch (error) {
- console.error('Error initializing peak cache', error);
- } finally {
- stored.initializing = false;
- }
-}
-
-function getPeaksFromAudioSample(sample: AudioSample): PeakData {
- const size = sample.allocationSize({ format: 'f32', planeIndex: 0 });
- const floats = new Float32Array(size / Float32Array.BYTES_PER_ELEMENT);
- sample.copyTo(floats, { format: 'f32', planeIndex: 0 });
-
- const numChannels = sample.numberOfChannels;
- const numFrames = floats.length / numChannels;
- const timestamp = sample.timestamp;
- const duration = sample.duration;
-
- // Downsample to 500 peaks per second
- const targetPeaks = Math.ceil(duration * HIGH_RES_PEAKS_PER_SECOND);
- const downsampledBytes = new Uint8ClampedArray(targetPeaks);
-
- // Downsample by calculating peaks directly for target positions
- const ratio = numFrames / targetPeaks;
-
- for (let i = 0; i < targetPeaks; i++) {
- const start = Math.floor(i * ratio);
- const end = Math.floor((i + 1) * ratio);
- let max = 0;
-
- // For each frame in the range, find max across all channels
- for (let frameIdx = start; frameIdx < end && frameIdx < numFrames; frameIdx++) {
- // Find the max across all channels for this frame
- for (let ch = 0; ch < numChannels; ch++) {
- const floatIdx = frameIdx * numChannels + ch;
- // make a more visually pleasing waveform using a power function
- max = Math.max(max, Math.pow(Math.abs(floats[floatIdx]), 0.8));
- }
- }
-
- downsampledBytes[i] = Math.floor(255 * max);
- }
-
- return {
- start: timestamp,
- end: timestamp + duration,
- data: downsampledBytes,
- }
-}
-
-function mergePeaks(peaks: PeakData[]): PeakData {
- const length = peaks.reduce((acc, peak) => acc + peak.data.length, 0);
- const mergedPeaks = new Uint8ClampedArray(length);
-
- let offset = 0;
- for (const peak of peaks) {
- mergedPeaks.set(peak.data, offset);
- offset += peak.data.length;
- }
-
- return {
- start: peaks[0].start,
- end: peaks[peaks.length - 1].end,
- data: mergedPeaks,
- };
-}
-
-type RangeHashOptions = {
- peaksPerSecond: number;
- start?: number;
- end?: number;
-}
-
-function createRangeHash(options: RangeHashOptions) {
- return `${options.peaksPerSecond}.${options.start}.${options.end}`;
-}
-
-let dbPromise: Promise<IDBDatabase> | null = null;
-
-function getDB(): Promise<IDBDatabase> {
- if (!dbPromise) {
- dbPromise = new Promise((resolve, reject) => {
- const req = indexedDB.open(IDB_NAME, IDB_VERSION);
-
- req.onupgradeneeded = () => {
- req.result.createObjectStore(IDB_STORE_NAME);
- };
-
- req.onsuccess = () => {
- const db = req.result;
-
- const tx = db.transaction(IDB_STORE_NAME, "readwrite");
- tx.objectStore(IDB_STORE_NAME).clear();
- tx.oncomplete = () => resolve(db);
- tx.onerror = () => reject(tx.error);
- };
-
- req.onerror = () => reject(req.error);
- });
- }
- return dbPromise;
-}
-
-async function storePeaks(key: string, peaks: Uint8ClampedArray) {
- const db = await getDB();
-
- const tx = db.transaction(IDB_STORE_NAME, "readwrite");
- const store = tx.objectStore(IDB_STORE_NAME);
-
- const blob = new Blob([new Uint8Array(peaks)]);
- store.put(blob, key);
-
-
- return new Promise((resolve, reject) => {
- tx.oncomplete = () => resolve(true);
- tx.onerror = () => reject(tx.error);
- });
-}
-
-async function loadPeaks(key: string): Promise<Uint8ClampedArray | null> {
- const db = await getDB();
-
- const tx = db.transaction(IDB_STORE_NAME, "readonly");
- const store = tx.objectStore(IDB_STORE_NAME);
-
- const blob: Blob | null = await new Promise((resolve, reject) => {
- const req = store.get(key);
- req.onsuccess = () => resolve(req.result);
- req.onerror = () => reject(req.error);
- });
-
- if (!blob) return null;
-
- const buffer = await blob.arrayBuffer();
- return new Uint8ClampedArray(buffer);
-}
-
-const inRange = (value: number, range: [number, number]) => {
- return value >= range[0] && value <= range[1];
-}
diff --git a/apps/web/src/components/engine/timeline/render/background.ts b/apps/web/src/components/engine/timeline/render/background.ts
deleted file mode 100644
index b9950a55..00000000
--- a/apps/web/src/components/engine/timeline/render/background.ts
+++ /dev/null
@@ -1,45 +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 { assert } from "@/utils";
-
-import type { EngineWorld } from "../../api/world";
-import type { TimelineContext } from "../world";
-import { clearComponent } from "../../api/events";
-
-export function renderBackground(world: EngineWorld, timeline: TimelineContext) {
- const canvas = timeline.canvas!;
- const pointer = timeline.pointer!;
-
- const c = world.components;
-
- // Handle marquee selection
- {
- const { clicked, dragging } = pointer
- .scope('canvas')
- .region(0, 0, canvas.width, canvas.height);
-
- if (clicked) {
- clearComponent(world, c.Selected, false);
- }
-
- if (dragging) {
- assert(pointer.position, 'Mouse position must be set');
- assert(pointer.position?.state != 'idle', 'Mouse position must be pressing');
-
- const x = Math.min(pointer.position.currentX, pointer.position.initialX);
- const y = Math.min(pointer.position.currentY, pointer.position.initialY);
- const width = Math.abs(pointer.position.currentX - pointer.position.initialX);
- const height = Math.abs(pointer.position.currentY - pointer.position.initialY);
-
- if (!timeline.marquee) {
- clearComponent(world, c.Selected, false);
- }
-
- timeline.marquee = { x, y, width, height };
- } else if (timeline.marquee) {
- timeline.marquee = undefined;
- }
- }
-}
diff --git a/apps/web/src/components/engine/timeline/render/caption.ts b/apps/web/src/components/engine/timeline/render/caption.ts
deleted file mode 100644
index 06d4252f..00000000
--- a/apps/web/src/components/engine/timeline/render/caption.ts
+++ /dev/null
@@ -1,103 +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 { binarySearchTextLength, framesToPixels } from "../utils";
-import { CaptionType } from "../../components";
-import { isCaption } from "../../api";
-import { CLIP_CORNER_RADIUS, CLIP_FONT, CLIP_BREAKPOINTS, CLIP_LABEL_HEIGHT } from "../config";
-import { getClipStyle } from "./clip";
-import { secondsToFrames } from "../../utils";
-
-import type { EngineWorld } from "../../api/world";
-import type { TimelineContext } from "../world";
-
-const NAME_MAP: Record<CaptionType, string> = {
- [CaptionType.CLASSIC]: 'Classic',
- [CaptionType.CASCADE]: 'Cascade',
- [CaptionType.SPOTLIGHT]: 'Spotlight',
- [CaptionType.WHISPER]: 'Whisper',
- [CaptionType.PAPER]: 'Paper',
- [CaptionType.GUINEA]: 'Guinea',
- [CaptionType.STARK]: 'Stark',
-} as const;
-
-const GROUP_GAP = 2;
-const GROUP_PADDING = 8;
-const GROUP_PADDING_TOP = 12;
-
-export function renderCaption(world: EngineWorld, timeline: TimelineContext, eid: number) {
- const ctx = timeline.ctx!;
- const c = world.components;
-
- if (!isCaption(world, eid)) return;
-
- const decoder = c.CaptionDecoder[eid];
- const groups = decoder?.groups;
- if (!groups?.length) return;
-
- const start = c.Computed.start[eid];
- const end = c.Computed.end[eid];
- const layerHeight = timeline.currentLayerHeight;
-
- const clipLeft = framesToPixels(world, start);
- const clipRight = framesToPixels(world, end);
- const delay = c.Computed.delay[eid];
-
- const style = getClipStyle(world, timeline, eid, null);
-
- ctx.save();
-
- // Clip to the caption clip bounds
- ctx.beginPath();
- ctx.roundRect(clipLeft, 0, clipRight - clipLeft, layerHeight, CLIP_CORNER_RADIUS);
- ctx.clip();
-
- for (const group of groups) {
- if (!group.length) continue;
-
- const groupStart = secondsToFrames(group[0]?.start) + delay;
- const groupEnd = secondsToFrames(group[group.length - 1]?.end) + delay;
-
- // Skip groups outside the clip range
- if (groupEnd < start || groupStart >= end) continue;
-
- const clampedStart = Math.max(groupStart, start);
- const clampedEnd = Math.min(groupEnd, end);
- const groupX = framesToPixels(world, clampedStart);
- const groupWidth = framesToPixels(world, clampedEnd) - groupX;
- const groupY = layerHeight < CLIP_BREAKPOINTS.sm ? GROUP_PADDING_TOP : CLIP_LABEL_HEIGHT;
- const groupHeight = layerHeight - groupY - 1;
-
- if (groupWidth < 2 || groupHeight < 4) continue;
-
- // Word group background
- ctx.beginPath();
- ctx.roundRect(groupX, groupY, groupWidth - GROUP_GAP, groupHeight, CLIP_CORNER_RADIUS);
- ctx.closePath();
-
- ctx.fillStyle = style.primary!;
- ctx.fill();
-
- // Word group text
- const groupText = group.map(w => w.text).join(' ');
- ctx.fontStretch = 'ultra-condensed';
- ctx.font = CLIP_FONT;
- ctx.fillStyle = style.foreground;
- ctx.textAlign = 'left';
- ctx.textBaseline = 'middle';
-
- const text = binarySearchTextLength(ctx, groupText, groupWidth - GROUP_PADDING);
- if (text) {
- ctx.fillText(text, groupX + 4, groupY + Math.round(groupHeight / 2));
- }
- }
-
- // Let's assing the name of the clip here to avoid another branch in the main clip function
- {
- const type = c.Caption.type[eid] ?? CaptionType.CLASSIC;
- c.Name[eid] = NAME_MAP[type as CaptionType] + ' Captions';
- }
-
- ctx.restore();
-}
diff --git a/apps/web/src/components/engine/timeline/render/clip.ts b/apps/web/src/components/engine/timeline/render/clip.ts
deleted file mode 100644
index 6f626c85..00000000
--- a/apps/web/src/components/engine/timeline/render/clip.ts
+++ /dev/null
@@ -1,473 +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 { assert, clamp } from "@/utils";
-import { binarySearchTextLength, getCurrentFrame, getRelativeViewport, pixelsToFrames, framesToPixels } from "../utils";
-import { secondsToFrames, computeTrimEnd, computeTrimStart } from "../../utils";
-import { CLIP_BREAKPOINTS, CLIP_CORNER_RADIUS, CLIP_FONT, CLIP_LABEL_HEIGHT, CLIP_LABEL_X, CLIP_LABEL_Y } from "../config";
-import { renderImageThumbnails } from "./image";
-import { renderAudioWaveform } from "./audio";
-import { renderVideoThumbnails } from "./video";
-import { getParentEntity, hasSurfacePaint, hasHtmlPaint, isAdjustmentLayer, isCaption, isGroup, isMask, isScene, isText } from "../../api";
-import { renderCaption } from "./caption";
-import { hasComponent, Not, Or, query } from "bitecs";
-import { addComponent, clearComponent, removeComponent, setComponent } from "../../api/events";
-import { resolveSequentialOverlaps } from "../../api/overlap";
-import { renderGroup } from "./group";
-
-import type { EngineWorld } from "../../api/world";
-import type { TimelineContext } from "../world";
-import type { Asset } from "../../db";
-import { ChildOf } from "../../components";
-
-const TRIM_AREA_WIDTH = 10;
-
-export function renderClip(world: EngineWorld, timeline: TimelineContext, eid: number) {
- const ctx = timeline.ctx!;
- const pointer = timeline.pointer!;
-
- const c = world.components;
- const scene = world.selection.scene;
-
- if (scene === null) return;
-
- const start = c.Computed.start[eid];
- const end = c.Computed.end[eid];
-
- let clipLeft = framesToPixels(world, start);
- const clipRight = framesToPixels(world, end);
- const clipWidth = clipRight - clipLeft;
- // Plain text nodes label with their content; captions label with their name
- // (set by the caption renderer to "{preset} Captions").
- const label = isText(world, eid) && !isCaption(world, eid)
- ? (c.Chars[eid] ?? c.Name[eid] ?? '')
- : (c.Name[eid] ?? '');
-
- pointer.scope(String(eid));
-
- const assetId = findAssetId(world, eid);
- const asset = world.assets.get(assetId ?? '') ?? null;
- const style = getClipStyle(world, timeline, eid, asset);
-
- // Handle background interactions
- {
- const { clicked, dragging, intersectsMarquee } = pointer.region(clipLeft, 0, clipWidth, timeline.currentLayerHeight);
- const clipIsSelected = hasComponent(world, eid, c.Selected);
-
- if (intersectsMarquee && !clipIsSelected) {
- addComponent(world, eid, c.Selected, false);
- }
-
- if (timeline.marquee && !intersectsMarquee && clipIsSelected) {
- removeComponent(world, eid, c.Selected, false);
- }
-
- if (clicked && pointer.shiftPressed) {
- addComponent(world, eid, c.Selected, false);
- }
-
- if (clicked && !pointer.shiftPressed) {
- clearComponent(world, c.Selected, false);
- addComponent(world, eid, c.Selected, false);
- }
-
- // Starts the drag interaction. Snapshot the original local delay and
- // start/end frames once so subsequent frames apply target =
- // originDelay + offsetFrames absolutely. Start/end snapshots
- // are needed because groups' visual edges aren't derivable from delay.
- if (dragging && !hasComponent(world, eid, c.ClipDragOrigin)) {
- // Open the gesture transaction once, when the grabbed clip starts
- // dragging. Every per-frame Delay write accumulates into it;
- // it commits in renderLayers when the drag ends.
- world.history.startTransaction('Move clip');
- setComponent(world, eid, c.ClipDragOrigin, {
- delay: c.Delay[eid] ?? 0,
- start: c.Computed.start[eid] ?? 0,
- end: c.Computed.end[eid] ?? 0,
- }, false);
- }
- }
-
- // Apply the drag live: write the snapped delay every frame
- if (hasComponent(world, eid, c.ClipDragOrigin) && pointer.position?.state !== 'idle') {
- const dragX = pointer.position?.deltaX ?? 0;
- const originDelay = c.ClipDragOrigin.delay[eid] ?? 0;
- const offsetFrames = pixelsToFrames(world, dragX);
- const snapFrameDelta = findSnapFrameDelta(world, timeline, offsetFrames);
- const newDelay = originDelay + offsetFrames - snapFrameDelta;
-
- // this should recompute the start frame
- setComponent(world, eid, c.Delay, newDelay);
-
- clipLeft = framesToPixels(world, c.Computed.start[eid]);
- }
-
- // Draw the background of the clip
- {
- ctx.save();
-
- ctx.beginPath();
- ctx.roundRect(clipLeft, 0, clipWidth, timeline.currentLayerHeight, CLIP_CORNER_RADIUS);
- ctx.closePath();
-
- ctx.fillStyle = style.background;
- ctx.fill();
-
- ctx.restore();
- }
-
- if (isGroup(world, eid)) {
- renderGroup(world, timeline, eid);
- } else if (asset?.type === 'IMAGE' || asset?.type === 'SEQUENCE') {
- renderImageThumbnails(world, timeline, eid, { asset });
- } else if (asset?.type === 'AUDIO') {
- renderAudioWaveform(world, timeline, eid, {
- asset,
- color: timeline.colors.clip.audio.primary,
- offsetY: timeline.currentLayerHeight > CLIP_BREAKPOINTS.sm ? CLIP_LABEL_HEIGHT : 2,
- padding: 4,
- });
- } else if (asset?.type === 'VIDEO') {
- renderVideoThumbnails(world, timeline, eid, { asset });
- }
-
- if (isCaption(world, eid)) {
- renderCaption(world, timeline, eid);
- }
-
- // Render the label
- if (!(isCaption(world, eid) && timeline.currentLayerHeight < CLIP_BREAKPOINTS.sm)) {
- ctx.save();
-
- const [vpl] = getRelativeViewport(world, timeline);
- const labelX = Math.max(clipLeft, vpl);
- const labelMaxWidth = clipLeft + clipWidth - labelX - 2 * CLIP_LABEL_X;
-
- ctx.translate(labelX, 0);
-
- ctx.fillStyle = style.foreground;
- ctx.fontStretch = 'ultra-condensed';
- ctx.font = CLIP_FONT;
- ctx.textAlign = 'left';
- ctx.textBaseline = 'middle';
-
- const transformedLabel = binarySearchTextLength(ctx, label, labelMaxWidth);
-
- if (transformedLabel) {
- const h = timeline.currentLayerHeight;
- const labelY = h >= CLIP_BREAKPOINTS.xs ? CLIP_LABEL_Y : h / 2;
- ctx.fillText(transformedLabel, CLIP_LABEL_X, labelY);
- }
-
- ctx.restore();
- }
-
- // Draw the border last so it sits above thumbnails, waveforms, and labels
- {
- ctx.save();
-
- ctx.beginPath();
- ctx.roundRect(clipLeft, 0, clipWidth, timeline.currentLayerHeight, CLIP_CORNER_RADIUS);
- ctx.closePath();
-
- ctx.clip();
- ctx.strokeStyle = hasComponent(world, eid, c.Selected)
- ? timeline.colors.border.ring
- : timeline.colors.border.darker;
- ctx.lineWidth = hasComponent(world, eid, c.Selected) ? 2 : 1;
-
- ctx.stroke();
-
- ctx.restore();
- }
-
- // Handle trim — groups derive their range from children and aren't directly trimmable.
- {
- const trimAreaWidth = Math.min(Math.max(clipWidth / 3, 3), clipWidth / 2, TRIM_AREA_WIDTH);
- const isTrimming = hasComponent(world, eid, c.TrimDragOrigin);
-
- const { hovering: hoveringLeft, dragging: draggingLeft } = pointer.region(
- clipLeft,
- 0,
- trimAreaWidth,
- timeline.currentLayerHeight,
- );
-
- const { hovering: hoveringRight, dragging: draggingRight } = pointer.region(
- clipLeft + clipWidth - trimAreaWidth,
- 0,
- trimAreaWidth,
- timeline.currentLayerHeight,
- );
-
- if (hoveringLeft || draggingLeft) {
- timeline.cursor = 'trim-right';
- } else if (hoveringRight || draggingRight) {
- timeline.cursor = 'trim-left';
- }
-
- const dragging = draggingLeft || draggingRight;
-
- if (dragging && !isTrimming) {
- world.history.startTransaction('Trim');
- setComponent(world, eid, c.TrimDragOrigin, {
- start: start,
- end: end,
- }, false);
- }
-
- if (dragging && hasComponent(world, eid, c.TrimDragOrigin)) {
- assert(pointer.position, 'Mouse position must be set');
- assert(pointer.position.state != 'idle', 'Mouse position must be pressing');
-
- const deltaFrames = pixelsToFrames(world, pointer.position.deltaX);
- const initialStart = c.TrimDragOrigin.start[eid];
- const initialEnd = c.TrimDragOrigin.end[eid];
-
- let minFrame: number;
- let maxFrame: number;
-
- // In the motion-graphics model each clip is its own layer, so trim
- // bounds are no longer constrained by neighboring clips — only by the
- // clip's own range and (below) the underlying asset duration.
- if (draggingLeft) {
- minFrame = 0;
- maxFrame = initialEnd - 1;
- } else {
- minFrame = initialStart + 1;
- maxFrame = Number.POSITIVE_INFINITY;
- }
-
- if (asset && (asset.type === 'AUDIO' || asset.type === 'VIDEO' || asset.type === 'SEQUENCE')) {
- const delay = c.Computed.delay[eid];
- const playbackRate = c.PlaybackRate[eid] ?? 1;
- const assetDuration = secondsToFrames(asset.duration);
- const assetEnd = delay + assetDuration / playbackRate;
-
- if (draggingLeft) {
- minFrame = Math.max(minFrame, delay);
- } else {
- maxFrame = Math.min(maxFrame, assetEnd);
- }
- }
-
- let newFrame: number;
- if (draggingLeft) {
- newFrame = initialStart + deltaFrames;
- } else {
- newFrame = initialEnd + deltaFrames;
- }
-
- const snapPoints = getSnapFrames(world, timeline);
- const matchedPoint = snapPoints.find(({ frame }) => {
- const distance = framesToPixels(world, Math.abs(frame - newFrame))
- const snapStart = distance < timeline.snapDistance;
- const inRange = frame > minFrame && frame < maxFrame;
-
- return inRange && snapStart;
- });
-
- if (matchedPoint) {
- newFrame = matchedPoint.frame;
- const pointX = framesToPixels(world, newFrame);
- timeline.snapX = pointX;
- }
-
- const clampedValue = clamp(newFrame, minFrame, maxFrame);
- const clampedTrimStart = computeTrimStart(world, eid, clampedValue);
- const clampedTrimEnd = computeTrimEnd(world, eid, clampedValue);
-
- if (draggingLeft) {
- setComponent(world, eid, c.Trim, { start: clampedTrimStart });
- } else {
- setComponent(world, eid, c.Trim, { end: clampedTrimEnd });
- }
- }
-
- if (!dragging && isTrimming) {
- resolveSequentialOverlaps(world, [eid]);
- removeComponent(world, eid, c.TrimDragOrigin, false);
- world.history.commitTransaction();
- }
- }
-}
-
-type ClipStyle = {
- background: string;
- foreground: string;
- primary?: string;
-}
-
-export function getClipStyle(
- world: EngineWorld,
- timeline: TimelineContext,
- eid: number,
- asset: Asset | null
-): ClipStyle {
- // Captions are text-geometry too, so check the more-specific tag first.
- if (isCaption(world, eid)) {
- return timeline.colors.clip.caption;
- }
-
- if (isText(world, eid)) {
- return timeline.colors.clip.text;
- }
-
- if (isScene(world, eid)) {
- return timeline.colors.clip.scene;
- }
-
- if (isGroup(world, eid)) {
- return timeline.colors.clip.group;
- }
-
- if (isMask(world, eid)) {
- return timeline.colors.clip.mask;
- }
-
- if (isAdjustmentLayer(world, eid)) {
- return timeline.colors.clip.adjustment;
- }
-
- if (hasHtmlPaint(world, eid) || hasSurfacePaint(world, eid)) {
- return timeline.colors.clip.html;
- }
-
- if (asset?.type == 'VIDEO') {
- return timeline.colors.clip.video;
- }
- if (asset?.type == 'IMAGE') {
- return timeline.colors.clip.image;
- }
- if (asset?.type == 'AUDIO') {
- return timeline.colors.clip.audio;
- }
-
- return timeline.colors.clip.shape;
-}
-
-function getSnapFrames(world: EngineWorld, timeline: TimelineContext) {
- const c = world.components;
- const scene = world.selection.scene;
-
- if (!timeline.snapping || scene === null) return [];
-
- // Any entity whose time range moves with — or is derived from — an active
- // drag/trim is a useless snap target. Walk ancestors (groups whose extents
- // come from their children) and descendants (children that shift along
- // with their dragged parent group) of every active item.
- const activeItems = [
- ...query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), c.ClipDragOrigin]),
- ...query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), c.TrimDragOrigin]),
- ];
- const excluded = new Set<number>();
- for (const item of activeItems) {
- let parent = getParentEntity(world, item);
- while (parent !== null && parent !== scene) {
- excluded.add(parent);
- parent = getParentEntity(world, parent);
- }
- for (const descendant of getSceneChildren(world, item)) {
- excluded.add(descendant);
- }
- }
-
- const availableClips = getSceneChildren(world, scene)
- .filter(cid => {
- if (hasComponent(world, cid, c.ClipDragOrigin)) return false;
- if (hasComponent(world, cid, c.TrimDragOrigin)) return false;
- if (hasComponent(world, cid, c.Selected)) return false;
- if (excluded.has(cid)) return false;
- return true;
- });
-
- const snapPoints = new Map<number, number | null>();
- snapPoints.set(0, null);
- snapPoints.set(getCurrentFrame(world), null);
-
- for (const cid of availableClips) {
- snapPoints.set(c.Computed.start[cid], cid);
- snapPoints.set(c.Computed.end[cid], cid);
- }
-
- return Array.from(snapPoints.entries()).map(([frame, clipEid]) => ({ frame, clipEid }));
-}
-
-/**
- * Find the frame-space adjustment needed to snap the active drag to a
- * nearby reference point. Returns the signed frame delta (candidate edge -
- * snap point) the caller must subtract from its offsetFrames to land on the
- * snap target, or 0 if nothing is in range.
- *
- * Doing this in frame space (not pixel space) avoids a floor/round
- * round-trip that otherwise jitters the clip by ±1 frame near the snap
- * point at fractional resolutions.
- */
-function findSnapFrameDelta(world: EngineWorld, timeline: TimelineContext, offsetFrames: number) {
- const c = world.components;
-
- // Candidates: each dragging item contributes its snapshotted start and end
- // frame shifted by the current drag offset. Using snapshots (rather than
- // reconstructing from delay + trim) keeps groups correct — their visual
- // edges come from children, not their own delay.
- const dragItems = query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), c.ClipDragOrigin]);
- const snapCandidates = new Set<number>();
- for (const itemEid of dragItems) {
- const originStart = c.ClipDragOrigin.start[itemEid] ?? 0;
- const originEnd = c.ClipDragOrigin.end[itemEid] ?? 0;
- snapCandidates.add(originStart + offsetFrames);
- snapCandidates.add(originEnd + offsetFrames);
- }
-
- const snapPoints = getSnapFrames(world, timeline);
-
- let bestMatch: { snapFrame: number; frameDelta: number; pxDelta: number } | null = null;
- for (const point of snapPoints) {
- for (const candidate of snapCandidates) {
- const frameDelta = candidate - point.frame;
- const pxDelta = framesToPixels(world, frameDelta);
- if (Math.abs(pxDelta) >= timeline.snapDistance) continue;
- if (bestMatch && Math.abs(pxDelta) >= Math.abs(bestMatch.pxDelta)) continue;
- bestMatch = { snapFrame: point.frame, frameDelta, pxDelta };
- }
- }
-
- if (bestMatch) {
- timeline.snapX = framesToPixels(world, bestMatch.snapFrame);
- return bestMatch.frameDelta;
- }
-
- return 0;
-}
-
-function findAssetId(world: EngineWorld, entityEid: number) {
- const AssetId = world.components.AssetId;
-
- if (hasComponent(world, entityEid, AssetId) && AssetId[entityEid]) {
- return AssetId[entityEid];
- }
-
- for (const fill of world.components.Cache.fills[entityEid]) {
- if (hasComponent(world, fill, AssetId) && AssetId[fill]) {
- return AssetId[fill];
- };
- }
-
- return undefined;
-}
-
-function getSceneChildren(world: EngineWorld, eid: number): number[] {
- const tree: number[] = [];
- const c = world.components;
-
- const walk = (eid: number) => {
- for (const cid of query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), ChildOf(eid), Not(c.Deleted)])) {
- tree.push(cid);
- walk(cid);
- }
- }
- walk(eid);
-
- return tree;
-}
diff --git a/apps/web/src/components/engine/timeline/render/group.ts b/apps/web/src/components/engine/timeline/render/group.ts
deleted file mode 100644
index 70a317ac..00000000
--- a/apps/web/src/components/engine/timeline/render/group.ts
+++ /dev/null
@@ -1,49 +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 { framesToPixels } from "../utils";
-import { CLIP_CORNER_RADIUS } from "../config";
-
-import type { EngineWorld } from "../../api/world";
-import type { TimelineContext } from "../world";
-
-export function renderGroup(world: EngineWorld, timeline: TimelineContext, eid: number) {
- const ctx = timeline.ctx!;
- const c = world.components;
-
- const children = c.Cache.children[eid];
- if (children.length === 0) return;
-
- const layerHeight = timeline.currentLayerHeight;
- const start = c.Computed.start[eid];
- const end = c.Computed.end[eid];
- const clipLeft = framesToPixels(world, start);
- const clipRight = framesToPixels(world, end);
-
- const childY = 20;
- const childHeight = layerHeight - childY;
- if (childHeight < 12) return;
-
- ctx.save();
-
- // Clip to the group clip bounds
- ctx.beginPath();
- ctx.roundRect(clipLeft, 0, clipRight - clipLeft, layerHeight, CLIP_CORNER_RADIUS);
- ctx.clip();
-
- for (const childEid of children) {
- const childStart = Math.max(start, c.Computed.start[childEid]);
- const childEnd = Math.min(end, c.Computed.end[childEid]);
- const childLeft = framesToPixels(world, childStart);
- const childWidth = framesToPixels(world, childEnd) - childLeft;
- if (childWidth < 2) continue;
-
- ctx.beginPath();
- ctx.roundRect(childLeft, childY, childWidth, childHeight, CLIP_CORNER_RADIUS);
- ctx.fillStyle = timeline.colors.clip.group.primary;
- ctx.fill();
- }
-
- ctx.restore();
-}
diff --git a/apps/web/src/components/engine/timeline/render/image.ts b/apps/web/src/components/engine/timeline/render/image.ts
deleted file mode 100644
index 29866a68..00000000
--- a/apps/web/src/components/engine/timeline/render/image.ts
+++ /dev/null
@@ -1,167 +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 { framesToPixels, getRelativeViewport } from "../utils";
-import { CLIP_BREAKPOINTS, CLIP_LABEL_HEIGHT, MAX_CLIP_HEIGHT } from '../config';
-import { getAssetFile } from '../../api/assets';
-
-import type { EngineWorld } from "../../api/world";
-import type { TimelineContext } from "../world";
-import type { ImageAsset, SequenceAsset } from '../../db';
-
-type ThumbnailAsset = ImageAsset | SequenceAsset;
-
-type ThumbnailCache = {
- canvas: OffscreenCanvas;
- hash: string;
- width: number;
- height: number;
-}
-
-type ThumbnailOptions = {
- width: number;
- height: number;
-}
-
-let thumbnailCache = new WeakMap<ThumbnailAsset, ThumbnailCache>();
-
-// 3 discrete thumbnail widths to prevent constant repositioning during resize
-const THUMBNAIL_WIDTHS = [80, 100, 120] as const;
-
-function getSnappedThumbnailWidth(clipHeight: number): number {
- if (clipHeight >= 100) return THUMBNAIL_WIDTHS[2];
- if (clipHeight >= 70) return THUMBNAIL_WIDTHS[1];
- return THUMBNAIL_WIDTHS[0];
-}
-
-type ImageThumbnailOptions = {
- asset: ThumbnailAsset;
-}
-
-export function clearImageThumbnailCache() {
- thumbnailCache = new WeakMap<ThumbnailAsset, ThumbnailCache>();
-}
-
-export function renderImageThumbnails(world: EngineWorld, timeline: TimelineContext, eid: number, { asset }: ImageThumbnailOptions) {
- const ctx = timeline.ctx!;
-
- // SVGs aren't supported by createImageBitmap on every browser, so skip
- // the timeline thumbnail strip for them.
- if (asset.mimeType === 'image/svg+xml') return;
-
- const c = world.components;
-
- // Don't render thumbnails if clip is too small
- const layerHeight = timeline.currentLayerHeight;
- if (layerHeight <= CLIP_BREAKPOINTS.sm) return;
-
- const start = c.Computed.start[eid];
- const end = c.Computed.end[eid];
-
- const clipDelayPx = framesToPixels(world, start);
- const clipWidth = framesToPixels(world, end) - clipDelayPx;
-
- const [firstX, lastX] = getRelativeViewport(world, timeline);
-
- // Thumbnail fills full height, but width snaps to 3 discrete values for stability
- const thumbnailWidth = getSnappedThumbnailWidth(layerHeight);
- const thumbnailHeight = Math.round(thumbnailWidth * (asset.height / asset.width));
-
- const tileCount = Math.ceil(clipWidth / thumbnailWidth);
- const firstIndex = Math.max(0, Math.floor((firstX - clipDelayPx) / thumbnailWidth));
- const lastIndex = Math.min(tileCount - 1, Math.floor((lastX - clipDelayPx) / thumbnailWidth));
-
- // Update the thumbnail cache
- updateThumbnailCache(asset, {
- width: thumbnailWidth,
- height: thumbnailHeight,
- });
-
- const cached = thumbnailCache.get(asset);
-
- if (!cached) return;
-
- const thumbnailY = 18;
- const maskHeight = layerHeight - CLIP_LABEL_HEIGHT;
-
- // Calculate image crop to match object-fit: cover behavior
- const srcAspect = cached.width / cached.height;
- const destAspect = thumbnailWidth / maskHeight;
-
- let cropX = 0;
- let cropY = 0;
- let cropWidth = cached.width;
- let cropHeight = cached.height;
-
- if (srcAspect > destAspect) {
- // Source is wider - crop horizontally
- cropWidth = cached.height * destAspect;
- cropX = (cached.width - cropWidth) / 2;
- } else {
- // Source is taller - crop vertically
- cropHeight = cached.width / destAspect;
- cropY = (cached.height - cropHeight) / 2;
- }
-
- // Draw thumbnails
- {
- ctx.save();
-
- // translate to the delay of the clip
- ctx.translate(clipDelayPx, 0);
-
- // create a drawing area for the thumbnails
- ctx.beginPath();
- ctx.roundRect(2, 20, clipWidth - 4, maskHeight - 2, 2);
- ctx.clip();
-
- ctx.translate(-clipDelayPx, 0);
-
- for (let i = firstIndex; i <= lastIndex; i++) {
- const thumbnailX = clipDelayPx + i * thumbnailWidth;
-
- ctx.drawImage(
- cached.canvas,
- cropX, cropY, cropWidth, cropHeight,
- thumbnailX, thumbnailY, thumbnailWidth, maskHeight,
- );
- }
-
- ctx.closePath();
-
- ctx.restore();
- }
-}
-
-let decoding = false;
-async function updateThumbnailCache(asset: ThumbnailAsset, options: ThumbnailOptions) {
- const hash = `$${options.width}x${options.height}`;
-
- try {
- if (hash != thumbnailCache.get(asset)?.hash && !decoding) {
- decoding = true;
- const blob = await getAssetFile(asset);
- const bitmap = await createImageBitmap(blob);
-
- const dpr = window.devicePixelRatio;
- const scale = Math.min(1, Math.max(
- (options.width * dpr) / bitmap.width,
- (MAX_CLIP_HEIGHT * dpr) / bitmap.height,
- ));
- const canvasWidth = Math.max(1, Math.round(bitmap.width * scale));
- const canvasHeight = Math.max(1, Math.round(bitmap.height * scale));
- const canvas = new OffscreenCanvas(canvasWidth, canvasHeight);
- const ctx = canvas.getContext('2d')!;
-
- ctx.drawImage(bitmap, 0, 0, canvasWidth, canvasHeight);
- bitmap.close();
-
- thumbnailCache.set(asset, { canvas, hash, width: canvasWidth, height: canvasHeight });
- }
- } catch (e) {
- console.error('Error updating thumbnail cache', e);
- } finally {
- decoding = false;
- }
-}
diff --git a/apps/web/src/components/engine/timeline/render/index.ts b/apps/web/src/components/engine/timeline/render/index.ts
deleted file mode 100644
index 6a11dc5a..00000000
--- a/apps/web/src/components/engine/timeline/render/index.ts
+++ /dev/null
@@ -1,15 +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/. */
-
-export * from './background';
-export * from './layers';
-export * from './marquee';
-export * from './playhead';
-export * from './ruler';
-export * from './workarea';
-export * from './snap';
-export * from './clip';
-export * from './audio';
-export * from './video';
-export * from './image';
diff --git a/apps/web/src/components/engine/timeline/render/keyframe.ts b/apps/web/src/components/engine/timeline/render/keyframe.ts
deleted file mode 100644
index 0cfac6e0..00000000
--- a/apps/web/src/components/engine/timeline/render/keyframe.ts
+++ /dev/null
@@ -1,136 +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 { KEYFRAME_TRACK_HEIGHT } from "../config";
-import { getRelativeViewport, pixelsToFrames, framesToPixels, getClipTransform } from "../utils";
-import { hasComponent } from "bitecs";
-import { addComponent, clearComponent, removeComponent, setComponent } from "../../api/events";
-
-import type { EngineWorld } from "../../api/world";
-import type { TimelineContext } from "../world";
-
-const KEYFRAME_COLOR = 'rgba(242, 242, 242, 0.64)';
-const KEYFRAME_SIZE = 8;
-const KEYFRAME_HALF_SIZE = KEYFRAME_SIZE / 2;
-const KEYFRAME_HITBOX_PADDING = 2;
-const KEYFRAME_HITBOX_SIZE = KEYFRAME_SIZE + KEYFRAME_HITBOX_PADDING * 2;
-const KEYFRAME_HITBOX_HALF_SIZE = KEYFRAME_HITBOX_SIZE / 2;
-
-export function renderKeyframeTrack(
- world: EngineWorld,
- timeline: TimelineContext,
- trackEid: number,
- clipEid: number | null,
-) {
- if (!clipEid) return;
-
- const ctx = timeline.ctx!;
- const pointer = timeline.pointer!;
-
- const c = world.components;
- const keyframeEids = c.Cache.keyframes[trackEid];
- if (!keyframeEids?.length) return;
-
- const transform = getClipTransform(world, timeline);
- if (!transform) return;
-
- const [vpl, vpr] = getRelativeViewport(world, timeline);
- const delay = c.Computed.delay[clipEid] ?? 0;
- const playbackRate = c.PlaybackRate[clipEid] ?? 1;
- const rowCenterY = KEYFRAME_TRACK_HEIGHT / 2;
- const firstFrame = c.Keyframe.time[keyframeEids[0]];
- const lastFrame = c.Keyframe.time[keyframeEids[keyframeEids.length - 1]];
-
- ctx.save();
- ctx.setTransform(transform);
- ctx.lineWidth = 1;
-
- if (hasComponent(world, trackEid, c.Hovering)) {
- ctx.fillStyle = timeline.colors.background.muted;
- ctx.globalAlpha = 0.6;
- ctx.fillRect(vpl, 0, vpr - vpl, KEYFRAME_TRACK_HEIGHT);
- ctx.globalAlpha = 1;
- }
-
- pointer.scope(String(trackEid));
-
- for (const kid of keyframeEids) {
- const kfTime = c.Keyframe.time[kid] ?? 0;
- const absoluteFrame = delay + kfTime / playbackRate;
- const x = framesToPixels(world, absoluteFrame);
-
- if (x + KEYFRAME_HITBOX_HALF_SIZE < vpl || x - KEYFRAME_HITBOX_HALF_SIZE > vpr) continue;
-
- const { intersectsMarquee, clicked, dragging } = pointer.region(
- x - KEYFRAME_HITBOX_HALF_SIZE,
- rowCenterY - KEYFRAME_HITBOX_HALF_SIZE,
- KEYFRAME_HITBOX_SIZE,
- KEYFRAME_HITBOX_SIZE,
- String(kid),
- );
-
- const isKeyframeSelected = hasComponent(world, kid, c.Selected);
-
- if (intersectsMarquee && !isKeyframeSelected) {
- addComponent(world, kid, c.Selected, false);
- }
- if (timeline.marquee && !intersectsMarquee && isKeyframeSelected) {
- removeComponent(world, kid, c.Selected, false);
- }
-
- if (clicked && pointer.shiftPressed) {
- addComponent(world, kid, c.Selected, false);
- }
- if (clicked && !pointer.shiftPressed) {
- clearComponent(world, c.Selected, false);
- addComponent(world, kid, c.Selected, false);
- }
-
- if (dragging && !hasComponent(world, kid, c.KeyframeDragOrigin)) {
- world.history.startTransaction('Move keyframes');
- setComponent(world, kid, c.KeyframeDragOrigin, { time: kfTime }, false);
- }
-
- if (hasComponent(world, kid, c.KeyframeDragOrigin) && pointer.position && pointer.position.state !== 'idle') {
- const target = c.KeyframeDragOrigin.time[kid] + pixelsToFrames(world, pointer.position.deltaX) * playbackRate;
- setComponent(world, kid, c.Keyframe, { time: target });
- }
-
- const keyframeColor = isKeyframeSelected ? timeline.colors.border.ring : KEYFRAME_COLOR;
- ctx.strokeStyle = keyframeColor;
- ctx.fillStyle = keyframeColor;
- ctx.beginPath();
- ctx.moveTo(x, rowCenterY - KEYFRAME_HALF_SIZE);
- ctx.lineTo(x + KEYFRAME_HALF_SIZE, rowCenterY);
- ctx.lineTo(x, rowCenterY + KEYFRAME_HALF_SIZE);
- ctx.lineTo(x - KEYFRAME_HALF_SIZE, rowCenterY);
- ctx.closePath();
- ctx.stroke();
-
- const isFirst = kfTime === firstFrame;
- const isLast = kfTime === lastFrame;
-
- if (!isFirst && !isLast) {
- ctx.fill();
- } else if (isFirst && isLast) {
- ctx.fill();
- } else if (isFirst) {
- ctx.beginPath();
- ctx.moveTo(x, rowCenterY - KEYFRAME_HALF_SIZE);
- ctx.lineTo(x + KEYFRAME_HALF_SIZE, rowCenterY);
- ctx.lineTo(x, rowCenterY + KEYFRAME_HALF_SIZE);
- ctx.closePath();
- ctx.fill();
- } else if (isLast) {
- ctx.beginPath();
- ctx.moveTo(x, rowCenterY - KEYFRAME_HALF_SIZE);
- ctx.lineTo(x - KEYFRAME_HALF_SIZE, rowCenterY);
- ctx.lineTo(x, rowCenterY + KEYFRAME_HALF_SIZE);
- ctx.closePath();
- ctx.fill();
- }
- }
-
- ctx.restore();
-}
diff --git a/apps/web/src/components/engine/timeline/render/layers.ts b/apps/web/src/components/engine/timeline/render/layers.ts
deleted file mode 100644
index ff5251bb..00000000
--- a/apps/web/src/components/engine/timeline/render/layers.ts
+++ /dev/null
@@ -1,179 +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 { DEFAULT_CLIP_HEIGHT } from "../config";
-import { getRelativeViewport, pixelsToFrames, getClipTransform, getNodeHeight, getSubtreeHeight } from "../utils";
-import { clearAudioCache } from "./audio";
-import { clearThumbnailCache } from "./video";
-import { renderClip } from "./clip";
-import { hasComponent, Not, Or, query } from "bitecs";
-import { removeComponent, setComponent } from "../../api/events";
-import { resolveSequentialOverlaps } from "../../api/overlap";
-import { ChildOf } from "../../components";
-import { renderKeyframeTrack } from "./keyframe";
-
-import type { EngineWorld } from "../../api/world";
-import type { TimelineNode } from "../../api/timeline-index";
-import type { TimelineContext } from "../world";
-
-const VIEWPORT_PADDING = 10; // 10 pixels
-
-export function renderLayers(world: EngineWorld, timeline: TimelineContext) {
- const ctx = timeline.ctx!;
-
- const c = world.components;
- const scene = world.selection.scene;
-
- if (scene === null) return;
-
- ctx.save();
-
- const [left, right] = getRelativeViewport(world, timeline);
- const minFrame = pixelsToFrames(world, left - VIEWPORT_PADDING);
- const maxFrame = pixelsToFrames(world, right + VIEWPORT_PADDING);
-
- const dragging = isPointerDragging(timeline);
-
- const clipDragItems = [...query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), c.ClipDragOrigin])];
- if (!dragging) {
- if (clipDragItems.length > 0) {
- for (const cid of clipDragItems) {
- removeComponent(world, cid, c.ClipDragOrigin, false);
- }
- resolveSequentialOverlaps(world, clipDragItems);
- world.history.commitTransaction();
- }
- } else {
- for (const cid of clipDragItems) {
- if (hasComponent(world, cid, c.Selected)) {
- // case multidrag — snapshot origin once per newly-tagged sibling so
- // subsequent frames keep applying target = origin + delta absolutely.
- for (const sid of query(world, [c.Selected, Or(c.Geometry, c.Group, c.AdjustmentLayer), Not(c.ClipDragOrigin)])) {
- setComponent(world, sid, c.ClipDragOrigin, {
- delay: c.Delay[sid] ?? 0,
- start: c.Computed.start[sid] ?? 0,
- end: c.Computed.end[sid] ?? 0,
- }, false);
- }
- }
- }
- }
-
- // Keyframe drag management
- const keyframeDragItems = [...query(world, [c.Keyframe, c.KeyframeDragOrigin])];
- if (!dragging) {
- if (keyframeDragItems.length > 0) {
- // Drag has ended — close the 'Move keyframes' transaction opened in
- // renderKeyframeTrack.
- for (const kid of keyframeDragItems) {
- removeComponent(world, kid, c.KeyframeDragOrigin, false);
- }
- world.history.commitTransaction();
- }
- } else {
- for (const kid of keyframeDragItems) {
- if (hasComponent(world, kid, c.Selected)) {
- // case multidrag — snapshot originFrame once per newly-tagged keyframe
- // so subsequent frames keep applying target = origin + delta absolutely.
- for (const skid of query(world, [c.Selected, c.Keyframe, Not(c.KeyframeDragOrigin)])) {
- setComponent(world, skid, c.KeyframeDragOrigin, { time: c.Keyframe.time[skid] ?? 0 }, false);
- }
- }
- }
- }
-
- timeline.currentLayerTop = 0;
-
- const renderNodes = (nodes: TimelineNode[], clipEid: number | null) => {
- for (const node of nodes) {
- if (node.kind === 'keyframe-track') {
- renderKeyframeTrack(world, timeline, node.eid, clipEid);
- timeline.currentLayerTop += getNodeHeight(world, node);
- continue;
- }
-
- if (node.kind === 'sub-item') {
- timeline.currentLayerTop += getNodeHeight(world, node);
- renderNodes(node.children, clipEid);
- continue;
- }
-
- const start = c.Computed.start[node.eid];
- const end = c.Computed.end[node.eid];
-
- if (end < minFrame || start > maxFrame) {
- clearThumbnailCache(node.eid);
- clearAudioCache(node.eid);
- timeline.currentLayerTop += getSubtreeHeight(world, node);
- continue;
- }
-
- // Don't render expanded children of a sequential container.
- if (clipEid === null || !hasComponent(world, clipEid, c.Sequential)) {
- renderLayer(world, timeline, node.eid);
- }
-
- timeline.currentLayerTop += getNodeHeight(world, node);
- renderNodes(node.children, node.eid);
- }
- }
-
- renderNodes(world.timelineIndex().layers, null);
-
- ctx.restore();
-}
-
-/**
- * Render one timeline row. For a regular clip, this is just renderClip with
- * the row's transform applied. For a GROUP, the row becomes a horizontal
- * track: the group itself isn't drawn, and each of its direct children is
- * rendered as a clip inside the group's row (sharing its transform and
- * row height).
- */
-function renderLayer(world: EngineWorld, timeline: TimelineContext, layerEid: number) {
- const c = world.components;
- const ctx = timeline.ctx!;
-
- const transform = getClipTransform(world, timeline);
- if (!transform) return;
- ctx.setTransform(transform);
-
- timeline.currentLayerHeight = c.ClipHeight[layerEid] ?? DEFAULT_CLIP_HEIGHT;
- ctx.globalAlpha = hasComponent(world, layerEid, c.Hidden) ? 0.5 : 1;
-
- if (hasComponent(world, layerEid, c.Hovering)) {
- const [vpl, vpr] = getRelativeViewport(world, timeline);
- ctx.globalAlpha = 0.5;
- ctx.fillStyle = timeline.colors.background.accent;
- ctx.fillRect(vpl, 0, vpr - vpl, timeline.currentLayerHeight);
- ctx.globalAlpha = 1;
- }
-
- if (!hasComponent(world, layerEid, c.Sequential)) {
- timeline.currentClip = layerEid;
- renderClip(world, timeline, layerEid);
- return;
- }
-
- const deferred: number[] = [];
- for (const cid of query(world, [Or(c.Geometry, c.Group, c.AdjustmentLayer), ChildOf(layerEid), Not(c.Deleted)])) {
- if (hasComponent(world, cid, c.ClipDragOrigin) || hasComponent(world, cid, c.TrimDragOrigin)) {
- deferred.push(cid);
- continue;
- }
- timeline.currentClip = cid;
- renderClip(world, timeline, cid);
- }
-
- for (const cid of deferred) {
- timeline.currentClip = cid;
- renderClip(world, timeline, cid);
- }
-}
-
-
-function isPointerDragging(timeline: TimelineContext) {
- const pointer = timeline.pointer!;
- return !!pointer.position && pointer.position.state !== 'idle';
-}
diff --git a/apps/web/src/components/engine/timeline/render/marquee.ts b/apps/web/src/components/engine/timeline/render/marquee.ts
deleted file mode 100644
index 0d7a14e9..00000000
--- a/apps/web/src/components/engine/timeline/render/marquee.ts
+++ /dev/null
@@ -1,28 +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 type { EngineWorld } from "../../api/world";
-import type { TimelineContext } from "../world";
-
-export function renderMarquee(_world: EngineWorld, timeline: TimelineContext) {
- const ctx = timeline.ctx!;
-
- if (timeline.marquee) {
- const { x, y, width, height } = timeline.marquee;
-
- ctx.save();
-
- ctx.fillStyle = timeline.colors.border.ring;
- ctx.strokeStyle = timeline.colors.border.ring;
- ctx.lineWidth = 1;
-
- ctx.globalAlpha = 0.2;
- ctx.fillRect(x, y, width, height);
-
- ctx.globalAlpha = 1;
- ctx.strokeRect(x, y, width, height);
-
- ctx.restore();
- }
-}
diff --git a/apps/web/src/components/engine/timeline/render/playhead.ts b/apps/web/src/components/engine/timeline/render/playhead.ts
deleted file mode 100644
index 1bc6bfb2..00000000
--- a/apps/web/src/components/engine/timeline/render/playhead.ts
+++ /dev/null
@@ -1,163 +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 { KNOB_PATH } from "../paths";
-import { getCurrentScrollX, getCurrentResolution, getCurrentFrame, framesToPixels } from "../utils";
-import { RULER_HEIGHT } from "../config";
-
-import type { EngineWorld } from "../../api/world";
-import type { TimelineContext } from "../world";
-
-// Rounded-rect knob shown while the timeline is minimized
-const PILL_TOP = 8;
-const PILL_HEIGHT = 19;
-const PILL_PADDING_X = 4;
-const PILL_RADIUS = 9;
-const PILL_CHAR_WIDTH = 6;
-
-// Critically-damped spring state for gradient width
-const GRADIENT_PX_PER_VELOCITY = 25;
-const GRADIENT_RESPONSE_TIME = 0.15;
-const GRADIENT_MAX_WIDTH = 150;
-let gradientWidthValue = 0;
-let gradientWidthVelocity = 0;
-let lastSampledFrame = 0;
-let lastFrameTimestamp = 0;
-
-export function renderPlayhead(world: EngineWorld, timeline: TimelineContext) {
- const ctx = timeline.ctx!;
- const canvas = timeline.canvas!;
-
- const currentTime = getCurrentFrame(world);
- const scrollX = getCurrentScrollX(world);
- const resolution = getCurrentResolution(world);
-
- ctx.save();
-
- const x = framesToPixels(world, currentTime);
-
- ctx.translate(-(scrollX * resolution), 0);
- ctx.translate(x, 0);
-
- if (timeline.minimized) {
- renderMinimizedPlayhead(timeline, currentTime);
- ctx.restore();
- return;
- }
-
- // Draw knob
- {
- ctx.save();
-
- ctx.translate(-5, 2);
-
- ctx.fillStyle = timeline.colors.border.ring;
- ctx.strokeStyle = timeline.colors.border.darker;
- ctx.lineWidth = 2;
- ctx.stroke(KNOB_PATH);
- ctx.fill(KNOB_PATH);
-
- ctx.restore();
- }
-
- // Draw playhead line
- {
- ctx.translate(0, RULER_HEIGHT);
-
- // Critically-damped spring on signed gradient width, driven by measured playhead velocity.
- // Sign carries direction; magnitude reflects actual motion (handles play/pause, scrub, speed change).
- const now = performance.now();
- const realDt = lastFrameTimestamp ? (now - lastFrameTimestamp) / 1000 : 0;
- lastFrameTimestamp = now;
-
- const fps = world.frameRate;
- const deltaSeconds = (currentTime - lastSampledFrame) / fps;
- const measuredVelocity = realDt > 0 ? deltaSeconds / realDt : 0;
- lastSampledFrame = currentTime;
-
- let target = GRADIENT_PX_PER_VELOCITY * measuredVelocity;
- if (target > GRADIENT_MAX_WIDTH) {
- target = GRADIENT_MAX_WIDTH;
- } else if (target < -GRADIENT_MAX_WIDTH) {
- target = -GRADIENT_MAX_WIDTH;
- }
-
- // Closed-form critically-damped spring step — unconditionally stable for any dt.
- if (realDt > 0) {
- const omega = 2 / GRADIENT_RESPONSE_TIME;
- const dt = Math.min(realDt, 0.25);
- const decay = Math.exp(-omega * dt);
- const error = gradientWidthValue - target;
- const b = gradientWidthVelocity + omega * error;
- gradientWidthValue = target + (error + b * dt) * decay;
- gradientWidthVelocity = (gradientWidthVelocity - omega * b * dt) * decay;
- }
-
- const absWidth = Math.abs(gradientWidthValue);
- if (absWidth > 0.5) {
- const reverse = gradientWidthValue < 0;
- const startX = reverse ? absWidth : -absWidth;
-
- const gradient = ctx.createLinearGradient(startX, 0, 0, 0);
- gradient.addColorStop(0, timeline.colors.border.ring + '00');
- gradient.addColorStop(1, timeline.colors.border.ring);
-
- ctx.fillStyle = gradient;
- ctx.globalAlpha = 0.16;
- ctx.fillRect(reverse ? 0 : -absWidth, 0, absWidth, canvas.height);
- ctx.globalAlpha = 1;
- }
-
- ctx.beginPath();
- ctx.moveTo(0, 0);
- ctx.lineTo(0, canvas.height);
- ctx.closePath();
-
- ctx.strokeStyle = timeline.colors.border.darker;
- ctx.lineWidth = 3;
- ctx.stroke();
-
- ctx.strokeStyle = timeline.colors.border.ring;
- ctx.lineWidth = 1;
- ctx.stroke();
- }
-
- ctx.restore();
-}
-
-function renderMinimizedPlayhead(timeline: TimelineContext, currentTime: number) {
- const ctx = timeline.ctx!;
-
- const label = `${Math.round(currentTime)}`;
-
- ctx.font = '400 10px JetBrains Mono';
- ctx.textAlign = 'center';
- ctx.textBaseline = 'middle';
-
- const width = label.length * PILL_CHAR_WIDTH + PILL_PADDING_X * 2;
-
- ctx.beginPath();
- ctx.moveTo(0, PILL_TOP + PILL_HEIGHT);
- ctx.lineTo(0, RULER_HEIGHT);
-
- ctx.strokeStyle = timeline.colors.border.input;
- ctx.lineWidth = 4;
- ctx.stroke();
-
- ctx.strokeStyle = timeline.colors.border.ring;
- ctx.lineWidth = 2;
- ctx.stroke();
-
- ctx.beginPath();
- ctx.roundRect(-width / 2, PILL_TOP, width, PILL_HEIGHT, PILL_RADIUS);
-
- ctx.fillStyle = timeline.colors.border.ring;
- ctx.strokeStyle = timeline.colors.border.darker;
- ctx.lineWidth = 1;
- ctx.fill();
- ctx.stroke();
-
- ctx.fillStyle = 'white';
- ctx.fillText(label, 0, PILL_TOP + PILL_HEIGHT / 2 + 0.5);
-}
diff --git a/apps/web/src/components/engine/timeline/render/ruler.ts b/apps/web/src/components/engine/timeline/render/ruler.ts
deleted file mode 100644
index 58134554..00000000
--- a/apps/web/src/components/engine/timeline/render/ruler.ts
+++ /dev/null
@@ -1,154 +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 { assert } from "@/utils";
-import { RULER_INTERVALS } from "../constants";
-import { RULER_HEIGHT, RULER_LABEL_Y, RULER_TICK_HEIGHT_MAJOR, RULER_TICK_HEIGHT_MINOR, TARGET_MAJOR_TICK_DISTANCE } from "../config";
-import { pixelsToFrames, framesToPixels, getRelativeViewport, getCurrentScrollX, getCurrentResolution, setCurrentFrame } from "../utils";
-import { setComponent } from "../../api/events";
-
-import type { EngineWorld } from "../../api/world";
-import type { TimelineContext } from "../world";
-
-// Tracks the open 'Set work area' gesture transaction across frames so the
-// whole shift-drag collapses into a single undo step.
-let workareaTxOpen = false;
-
-export function renderRuler(world: EngineWorld, timeline: TimelineContext) {
- const ctx = timeline.ctx!;
- const pointer = timeline.pointer!;
-
- const scrollX = getCurrentScrollX(world);
- const resolution = getCurrentResolution(world);
-
- const c = world.components;
-
- const fps = world.frameRate;
- const scene = world.selection.scene;
-
- ctx.save();
-
- ctx.translate(-(scrollX * resolution), 0);
-
- const layout = getRulerLayout(world);
- const [minX, maxX] = getRelativeViewport(world, timeline);
-
- const { dragging, clicked } = pointer
- .scope('ruler')
- .region(minX, 0, maxX - minX, RULER_HEIGHT);
-
- if (clicked || (dragging && !pointer.shiftPressed)) {
- assert(pointer.position, 'Mouse position must be set');
-
- const frame = pixelsToFrames(
- world,
- pointer.position.currentX + scrollX * resolution
- );
-
- if (scene !== null) {
- c.Playback.playing[scene] = 0;
- }
- setCurrentFrame(world, frame);
- }
-
- if (scene !== null && dragging && pointer.shiftPressed) {
- assert(pointer.position, 'Mouse position must be set');
- assert(pointer.position?.state != 'idle', 'Mouse position must be pressing');
-
- if (!workareaTxOpen) {
- world.history.startTransaction('Set work area');
- workareaTxOpen = true;
- }
-
- const newStart = pixelsToFrames(world, pointer.position.initialX + scrollX * resolution);
- const newStop = pixelsToFrames(world, pointer.position.currentX + scrollX * resolution);
- setComponent(world, scene, c.Workarea, { start: newStart, end: newStop });
-
- if (pointer.position.currentX > pointer.position.initialX) {
- setCurrentFrame(world, newStop);
- } else {
- setCurrentFrame(world, newStart);
- }
- } else if (workareaTxOpen) {
- // Shift-drag ended — close the gesture transaction.
- world.history.commitTransaction();
- workareaTxOpen = false;
- }
-
- const firstFrame = pixelsToFrames(world, minX);
- const lastFrame = pixelsToFrames(world, maxX);
- const frameInterval = Math.max(1, Math.round(layout.numerator / layout.denominator));
-
- const majorOffset = RULER_HEIGHT - RULER_TICK_HEIGHT_MAJOR;
- const minorOffset = RULER_HEIGHT - RULER_TICK_HEIGHT_MINOR;
-
- ctx.font = '300 10px JetBrains Mono';
- ctx.fillStyle = timeline.colors.ruler.text;
- ctx.textBaseline = 'middle';
- ctx.textAlign = 'center';
-
- const start = Math.floor(firstFrame / frameInterval) * frameInterval;
- for (let frame = start; frame <= lastFrame; frame += frameInterval) {
- const x = framesToPixels(world, frame);
- let y = minorOffset;
-
- if (frame % layout.numerator === 0) {
- y = majorOffset;
- const label = formatTickLabel(frame, fps);
- ctx.fillText(label, x, RULER_LABEL_Y);
- }
-
- ctx.lineWidth = 1;
- ctx.strokeStyle = timeline.colors.ruler.tick;
- ctx.beginPath();
- ctx.moveTo(x, y);
- ctx.lineTo(x, RULER_HEIGHT);
- ctx.stroke();
- }
-
- ctx.restore();
-}
-
-
-function formatTickLabel(frame: number, fps: number) {
- if (frame === 0) {
- return '0';
- }
-
- if (frame % fps != 0) {
- // this means its in frames
- return `${frame}f`;
- }
-
- const minutes = Math.floor(Math.round(frame / fps) / 60).toString().padStart(2, '0');
- const seconds = Math.floor(Math.round(frame / fps) % 60).toString().padStart(2, '0');
-
- return `${minutes}:${seconds}`;
-}
-
-function getRulerLayout(world: EngineWorld) {
- let interval: typeof RULER_INTERVALS[number] | null = null;
-
- for (const value of RULER_INTERVALS) {
- if (!interval) {
- interval = value;
- continue;
- }
-
- const currentDistance = framesToPixels(world, interval.numerator);
- const currentDelta = Math.abs(currentDistance - TARGET_MAJOR_TICK_DISTANCE);
-
- const distance = framesToPixels(world, value.numerator);
- const delta = Math.abs(distance - TARGET_MAJOR_TICK_DISTANCE);
-
-
- if (delta < currentDelta) {
- interval = value;
- }
- }
-
- assert(interval, 'No ruler interval found');
-
- return interval;
-}
diff --git a/apps/web/src/components/engine/timeline/render/snap.ts b/apps/web/src/components/engine/timeline/render/snap.ts
deleted file mode 100644
index 23e883d2..00000000
--- a/apps/web/src/components/engine/timeline/render/snap.ts
+++ /dev/null
@@ -1,38 +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 { RULER_HEIGHT } from "../config";
-import { getCurrentResolution, getCurrentScrollX } from "../utils";
-
-import type { EngineWorld } from "../../api/world";
-import type { TimelineContext } from "../world";
-
-export function renderSnapPoints(world: EngineWorld, timeline: TimelineContext) {
- if (timeline.snapX == null) return;
-
- const ctx = timeline.ctx!;
- const canvas = timeline.canvas!;
-
- const scrollX = getCurrentScrollX(world);
- const resolution = getCurrentResolution(world);
-
- ctx.save();
-
- ctx.translate(-(scrollX * resolution), 0);
-
- const pointX = timeline.snapX;
- const height = canvas.height / window.devicePixelRatio;
-
- ctx.beginPath();
- ctx.moveTo(pointX, RULER_HEIGHT);
- ctx.lineTo(pointX, height);
-
- ctx.strokeStyle = timeline.colors.border.scrubber;
- ctx.lineWidth = 1;
- ctx.stroke();
-
- ctx.restore();
-
- timeline.snapX = null;
-}
diff --git a/apps/web/src/components/engine/timeline/render/video.ts b/apps/web/src/components/engine/timeline/render/video.ts
deleted file mode 100644
index 39f4d26b..00000000
--- a/apps/web/src/components/engine/timeline/render/video.ts
+++ /dev/null
@@ -1,515 +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 { assert } from '@/utils';
-import { CanvasSink } from 'mediabunny';
-import { framesToPixels, getRelativeViewport, pixelsToFrames, getCurrentResolution } from '../utils';
-import { getVideoTrack } from '../../decoders/video';
-import { renderAudioWaveform } from './audio';
-import { CLIP_BREAKPOINTS, CLIP_LABEL_HEIGHT } from '../config';
-import { secondsToFrames } from '../../utils/time';
-
-import type { TimelineContext } from '../world';
-import type { VideoAsset } from '../../db';
-import type { EngineWorld } from '../../api/world';
-
-type ThumbnailOptions = {
- clipId: number;
- asset: VideoAsset;
- interval: number;
- width: number;
- height: number;
- firstFrame: number;
- lastFrame: number;
- fps: number;
-};
-
-type Thumbnail = {
- timestamp: number;
- canvas: OffscreenCanvas | HTMLCanvasElement;
- tombstoned?: boolean;
-}
-
-type ThumbnailCache = {
- thumbnails: Thumbnail[];
- options?: ThumbnailOptions;
- start: number;
- end: number;
- pendingUpdate: ThumbnailOptions | null;
- decoding: boolean;
- fps: number;
-};
-
-type ThumbnailStore = {
- thumbnails: Thumbnail[];
- decoding: boolean;
-};
-
-const thumbnailCache = new Map<number, ThumbnailCache>(); // By clip eid
-const thumbnailStore = new Map<string, ThumbnailStore>(); // By asset id
-
-export function cloneThumbnailCacheForSplit(sourceClipId: number, splitClipId: number) {
- const sourceCache = thumbnailCache.get(sourceClipId);
- if (!sourceCache) return;
-
- thumbnailCache.set(splitClipId, {
- ...sourceCache,
- // Keep per-clip mutable metadata separate; reuse canvas buffers by reference.
- thumbnails: sourceCache.thumbnails.map(thumbnail => ({ ...thumbnail })),
- pendingUpdate: null,
- decoding: false,
- });
-}
-
-export function clearThumbnailCache(id?: number) {
- if (id) {
- thumbnailCache.delete(id);
- return;
- }
-
- thumbnailCache.clear();
-}
-
-export function clearVideoBuffers() {
- thumbnailCache.clear();
- thumbnailStore.clear();
-}
-
-type VideoThumbnailOptions = {
- asset: VideoAsset;
-}
-
-const MIN_MASK_HEIGHT = 42;
-
-function getClipLayout(timeline: TimelineContext) {
- let maskHeight = 0;
-
- const clipHeight = timeline.currentLayerHeight;
-
- if ((clipHeight - CLIP_LABEL_HEIGHT) > MIN_MASK_HEIGHT) {
- maskHeight = (clipHeight - CLIP_LABEL_HEIGHT) * 0.5;
- }
-
- let waveformOffsetY = clipHeight > CLIP_BREAKPOINTS.sm ? maskHeight + CLIP_LABEL_HEIGHT : 2;
-
- let thumbnailHeight = 46;
- let thumbnailWidth = 56;
-
- if (maskHeight > 44) {
- thumbnailHeight = 52;
- thumbnailWidth = 84;
- }
-
- let thumbnailOffsetY = CLIP_LABEL_HEIGHT + (maskHeight - thumbnailHeight);
-
- return {
- maskHeight,
- waveformOffsetY,
- thumbnailOffsetY,
- thumbnailHeight,
- thumbnailWidth,
- }
-}
-
-export function renderVideoThumbnails(world: EngineWorld, timeline: TimelineContext, eid: number, { asset }: VideoThumbnailOptions) {
- const ctx = timeline.ctx!;
-
- const c = world.components;
- const fps = world.frameRate;
-
- const layout = getClipLayout(timeline);
-
- const start = c.Computed.start[eid];
- const end = c.Computed.end[eid];
-
- const delay = c.Computed.delay[eid];
- const trimStart = c.Trim.start[eid] ?? 0;
- const trimEnd = c.Trim.end[eid] ?? c.Computed.duration[eid] ?? 0;
- const playbackRate = c.PlaybackRate[eid] ?? 1;
-
- renderAudioWaveform(world, timeline, eid, {
- color: timeline.colors.clip.video.primary,
- asset,
- padding: 0,
- offsetY: layout.waveformOffsetY,
- });
-
- // Don't render thumbnails if clip is too small
- if (layout.maskHeight == 0) {
- return;
- }
-
- const clipLeft = framesToPixels(world, start);
- const clipRight = framesToPixels(world, end);
- const clipWidth = clipRight - clipLeft;
-
- const [vpl, vpr] = getRelativeViewport(world, timeline);
-
- ctx.save();
-
- {
- // create a drawing area for the thumbnails
- ctx.beginPath();
- ctx.roundRect(clipLeft + 2, CLIP_LABEL_HEIGHT, clipWidth - 4, layout.maskHeight, 2);
- ctx.clip();
- }
-
- // Thumbnail interval in asset (source) frames, scaled by playbackRate so a
- // thumbnailWidth of timeline pixels maps to the correct chunk of source.
- // Kept as a float — at high zoom this can be sub-frame (e.g. 0.28); rounding
- // it to 0 with `pixelsToFrames` makes the decode range collapse to NaN and
- // every thumbnail goes missing.
- const interval = (layout.thumbnailWidth / getCurrentResolution(world)) * playbackRate;
-
- const adjustedVpl = vpl;
- const adjustedVpr = vpr;
-
- const delayPx = framesToPixels(world, delay);
-
- // Intersect the trim range with the viewport directly in frames — going
- // through pixels first lets `framesToPixels` floor and `pixelsToFrames`
- // round, which can shave a frame off `lastFrame` when `trimEnd`
- // sits at a thumbnail boundary, leaving the last thumbnail un-decoded.
- // Multiply by playbackRate to match the trim frames' source time base.
- const vplFrames = (pixelsToFrames(world, adjustedVpl) - delay) * playbackRate;
- const vprFrames = (pixelsToFrames(world, adjustedVpr) - delay) * playbackRate;
-
- const firstFrame = Math.max(trimStart, vplFrames);
- const lastFrame = Math.min(trimEnd, vprFrames);
-
- updateThumbnails({
- clipId: eid,
- asset,
- fps,
- firstFrame,
- lastFrame,
- width: layout.thumbnailWidth,
- height: layout.thumbnailHeight,
- interval,
- });
-
- ctx.translate(delayPx, 0);
-
- // draw thumnail when available — indices are in asset time so they
- // line up with the asset-time thumbnails after the delayPx translate.
- const firstIndex = Math.floor(trimStart / interval);
- const lastIndex = Math.floor(trimEnd / interval);
-
- const cachedStart = thumbnailCache.get(eid)?.start;
- const cachedEnd = thumbnailCache.get(eid)?.end;
-
- for (let i = firstIndex; i <= lastIndex; i++) {
- const timestamp = i * interval;
-
- const hasCached = cachedStart != undefined && cachedEnd != undefined;
- if (!hasCached || (timestamp < cachedStart - interval || timestamp > cachedEnd + interval)) {
- var thumbnails = thumbnailStore.get(asset.id)?.thumbnails ?? [];
- } else {
- var thumbnails = thumbnailCache.get(eid)?.thumbnails ?? [];
- }
-
- const thumbnailIndex = binarySearchClosest(thumbnails, timestamp, x => x.timestamp);
- const thumbnail = thumbnails[thumbnailIndex];
-
- if (!thumbnail) continue;
-
- const thumbnailX = i * layout.thumbnailWidth;
- const thumbnailY = layout.thumbnailOffsetY;
-
- ctx.drawImage(
- thumbnail.canvas,
- thumbnailX,
- thumbnailY,
- layout.thumbnailWidth,
- layout.thumbnailHeight,
- );
- }
-
- ctx.closePath();
-
- ctx.restore();
-}
-
-
-function updateThumbnails(options: ThumbnailOptions) {
- const stored = thumbnailStore.get(options.asset.id);
- if (!stored || (!stored.decoding && stored.thumbnails.length === 0)) {
- initializeThumbnailCache(options);
- }
-
- if (stored && stored.thumbnails.length > 0) {
- updateThumbnailCache(options);
- }
-}
-
-function createBestThumbnailRange(options: ThumbnailOptions) {
- const { firstFrame, lastFrame, interval } = options;
- const { asset } = options;
-
- // decode one more thumbnail than the visible range
- const margin = 1;
-
- const minIndex = 0;
- const maxIndex = Math.floor(secondsToFrames(asset.duration) / interval);
-
- const startIndex = Math.max(Math.floor(firstFrame / interval) - margin, minIndex);
- const endIndex = Math.min(Math.floor(lastFrame / interval) + margin, maxIndex);
-
- return {
- start: startIndex * interval,
- end: endIndex * interval,
- startIndex,
- endIndex,
- }
-}
-
-async function updateThumbnailCache(options: ThumbnailOptions) {
- let cached = thumbnailCache.get(options.clipId);
-
- if (cached?.decoding == true) {
- cached.pendingUpdate = options;
- return;
- }
-
- if (cached == undefined) {
- cached = {
- thumbnails: [],
- start: -1,
- end: -1,
- pendingUpdate: null,
- decoding: true,
- fps: options.fps,
- }
- thumbnailCache.set(options.clipId, cached);
- }
-
- try {
- cached.decoding = true;
-
- const currentHash = createRangeHash(cached.options);
- const newHash = createRangeHash(options);
-
- if (currentHash == newHash) {
- return;
- }
-
- const bestRange = createBestThumbnailRange(options);
- const intersects = intersect(
- [bestRange.start, bestRange.end],
- [cached.start, cached.end],
- );
- const resized = createSizeHash(options) != createSizeHash(cached.options);
- const intervalChanged = options.interval != cached.options?.interval;
-
- // indices of the thumbnails to decode
- let decodeStartIndex: number = 0;
- let decodeEndIndex: number = 0;
-
- // if the interval has changed, we need to re-initialize the cache
- // or there is no intersection between the new range and the cache
- if (intervalChanged || !intersects || resized) {
- decodeStartIndex = bestRange.startIndex;
- decodeEndIndex = bestRange.endIndex;
-
- // Tombstone all the canvases in the cache
- cached.thumbnails.forEach(c => c.tombstoned = true);
- } else if (bestRange.start < cached.start) {
- decodeStartIndex = bestRange.startIndex;
- decodeEndIndex = Math.floor(cached.start / options.interval);
- } else if (bestRange.end > cached.end) {
- decodeStartIndex = Math.floor(cached.end / options.interval);
- decodeEndIndex = bestRange.endIndex;
- }
-
- // Tombstone the canvases that are outside the new range
- cached.thumbnails.forEach(c => {
- if (c.timestamp > bestRange.end || c.timestamp < bestRange.start) {
- c.tombstoned = true;
- }
- });
-
- const decodeCount = decodeEndIndex - decodeStartIndex;
-
- // This case handles when no new thumbnails need to be decoded because the
- // existing cache already covers the visible range.
- if (decodeCount <= 0) {
- cached.thumbnails = cached.thumbnails
- .filter(c => !c.tombstoned)
- .sort((a, b) => a.timestamp - b.timestamp);
- cached.start = bestRange.start;
- cached.end = bestRange.end;
- cached.options = options;
- return;
- }
-
- const track = await getVideoTrack(options.asset);
- assert(track, 'Track must be set');
-
- const sink = new CanvasSink(track, {
- width: options.width * window.devicePixelRatio,
- height: options.height * window.devicePixelRatio,
- fit: 'cover',
- });
-
- for (let i = 0; i < decodeCount; i++) {
- try {
- const frameTimestamp = (decodeStartIndex + i) * options.interval;
- const canvas = await sink.getCanvas(frameTimestamp / options.fps);
- if (!canvas) continue;
-
- // Decoder may snap to the nearest keyframe — store the actual returned
- // timestamp so the binary search at draw time finds the right thumbnail.
- const decodedFrame = secondsToFrames(canvas.timestamp);
-
- cached.thumbnails = cached.thumbnails
- .filter(c => !c.tombstoned || c.timestamp > decodedFrame)
- .sort((a, b) => a.timestamp - b.timestamp);
-
- cached.thumbnails.push({
- timestamp: decodedFrame,
- canvas: canvas.canvas,
- tombstoned: false,
- });
- } catch {
- // Must not be handled
- }
- }
-
- cached.thumbnails = cached.thumbnails
- .filter(c => !c.tombstoned)
- .sort((a, b) => a.timestamp - b.timestamp);
- cached.start = bestRange.start;
- cached.end = bestRange.end;
- cached.options = options;
- } catch (e) {
- console.error('Error updating thumbnail cache', e);
- } finally {
- cached.decoding = false;
- }
-
- if (cached.pendingUpdate) {
- const options = cached.pendingUpdate;
- if (!options) return;
- cached.pendingUpdate = null;
- void updateThumbnailCache(options);
- }
-}
-
-async function initializeThumbnailCache(options: ThumbnailOptions) {
- const { asset } = options;
-
- let stored = thumbnailStore.get(options.asset.id);
-
- if (stored?.decoding) {
- return;
- }
-
- if (!stored) {
- stored = { thumbnails: [], decoding: true };
- thumbnailStore.set(options.asset.id, stored);
- }
-
- try {
- stored.decoding = true;
-
- if (asset.duration < 60 * 10) {
- var count = 7
- } else if (asset.duration < 20) {
- var count = 5
- } else {
- var count = 10;
- }
-
- const track = await getVideoTrack(asset);
- assert(track, 'Track must be set');
- const firstTimestamp = await track.getFirstTimestamp();
- const lastTimestamp = await track.computeDuration();
-
- const dpr = window.devicePixelRatio;
- const sink = new CanvasSink(track, {
- width: options.width * dpr,
- height: options.height * dpr,
- fit: 'cover',
- });
-
- for (let i = 0; i < count; i++) {
- try {
- const timestamp = firstTimestamp + i * (lastTimestamp - firstTimestamp) / count;
- const canvas = await sink.getCanvas(timestamp);
- if (!canvas) continue;
-
- stored.thumbnails.push({
- timestamp: secondsToFrames(canvas.timestamp),
- canvas: canvas.canvas,
- });
- } catch {
- // Must not be handled
- }
- }
- } catch (e) {
- console.error('Error initializing thumbnail cache', e);
- } finally {
- stored.decoding = false;
- }
-}
-
-function binarySearchClosest<T>(
- arr: T[],
- key: number,
- valueGetter: (x: T) => number
-) {
- if (arr.length === 0) return -1;
-
- let low = 0;
- let high = arr.length - 1;
- let closestIndex = -1;
- let closestDistance = Infinity;
-
- while (low <= high) {
- const mid = (low + (high - low + 1) / 2) | 0;
- const midVal = valueGetter(arr[mid]!);
- const distance = Math.abs(midVal - key);
-
- if (distance < closestDistance) {
- closestDistance = distance;
- closestIndex = mid;
- }
-
- if (midVal < key) {
- low = mid + 1;
- } else if (midVal > key) {
- high = mid - 1;
- } else {
- // Exact match
- return mid;
- }
- }
-
- return closestIndex;
-}
-
-function createRangeHash(options?: ThumbnailOptions) {
- if (!options) return '';
-
- return `$${options.width}x${options.height}@${window.devicePixelRatio}-${options.interval}-${options.firstFrame}-${options.lastFrame}`;
-}
-
-function createSizeHash(options?: ThumbnailOptions) {
- if (!options) return '';
-
- return `$${options.width}x${options.height}@${window.devicePixelRatio}`;
-}
-
-/**
- * Check if two ranges intersect (non-inclusive)
- * @param range1 [start, end]
- * @param range2 [start, end]
- * @returns true if the ranges intersect, false otherwise
- */
-export function intersect(range1: [number, number], range2: [number, number]): boolean {
- const [x1, x2] = range1;
- const [y1, y2] = range2;
-
- return x1 < y2 && y1 < x2;
-}
diff --git a/apps/web/src/components/engine/timeline/render/workarea.ts b/apps/web/src/components/engine/timeline/render/workarea.ts
deleted file mode 100644
index 5bcc16d0..00000000
--- a/apps/web/src/components/engine/timeline/render/workarea.ts
+++ /dev/null
@@ -1,147 +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 { hasComponent } from "bitecs";
-import { assert } from "@/utils";
-import { IN_POINT_PATH, OUT_POINT_PATH } from "../paths";
-import { framesToPixels, pixelsToFrames, getCurrentScrollX, getCurrentResolution } from "../utils";
-import { RULER_HEIGHT } from "../config";
-import { removeComponent, setComponent } from "../../api/events";
-
-import type { EngineWorld } from "../../api/world";
-import type { TimelineContext } from "../world";
-
-
-let workareaDragStart: [number, number] = [0, 0];
-// Tracks the open work-area drag transaction across frames so the whole drag
-// (move / in-handle / out-handle) collapses into a single undo step.
-let workareaTxOpen = false;
-
-export function renderWorkarea(world: EngineWorld, timeline: TimelineContext) {
- const ctx = timeline.ctx!;
- const pointer = timeline.pointer!;
-
- // Set true by any of the three handle regions while it is being dragged.
- let anyDragging = false;
- const beginGesture = () => {
- anyDragging = true;
- if (!workareaTxOpen) {
- world.history.startTransaction('Adjust work area');
- workareaTxOpen = true;
- }
- };
-
- const scrollX = getCurrentScrollX(world);
- const resolution = getCurrentResolution(world);
- const sid = world.selection.scene;
- if (sid === null) return;
- const c = world.components;
- if (!hasComponent(world, sid, c.Workarea)) return;
-
- const start = c.Workarea.start[sid] ?? 0;
- const stopFrame = c.Workarea.end[sid] ?? 0;
-
- ctx.save();
-
- ctx.translate(-(scrollX * resolution), 0);
-
- const inPx = framesToPixels(world, start);
- const outPx = framesToPixels(world, stopFrame);
-
- const height = 10;
- const y = RULER_HEIGHT - height;
-
- {
- const { dragging, pressed, doubleClicked } = pointer
- .scope('workarea')
- .region(inPx, y, outPx - inPx, height);
-
- if (dragging) {
- assert(pointer.position, 'Mouse position must be set');
- assert(pointer.position?.state != 'idle', 'Mouse position must be pressing');
-
- beginGesture();
-
- const deltaFrames = pixelsToFrames(world, pointer.position.deltaX);
-
- setComponent(world, sid, c.Workarea, {
- start: workareaDragStart[0] + deltaFrames,
- end: workareaDragStart[1] + deltaFrames,
- });
- }
-
- if (pressed) {
- workareaDragStart = [start, stopFrame];
- }
-
- if (doubleClicked) {
- removeComponent(world, sid, c.Workarea);
- }
-
- ctx.globalCompositeOperation = 'difference';
- ctx.fillStyle = 'rgba(100, 100, 100, 1)';
- ctx.fillRect(inPx, y, outPx - inPx, height);
- ctx.globalCompositeOperation = 'source-over';
- }
- {
- ctx.lineWidth = 1;
- ctx.fillStyle = timeline.colors.border.ring;
- ctx.strokeStyle = timeline.colors.border.darker;
-
- ctx.translate(inPx - 5, y);
- ctx.stroke(IN_POINT_PATH);
- ctx.fill(IN_POINT_PATH);
-
- const { dragging, pressed } = pointer.region(0, 0, 5, height);
-
- if (dragging) {
- assert(pointer.position, 'Mouse position must be set');
- assert(pointer.position?.state != 'idle', 'Mouse position must be pressing');
-
- beginGesture();
-
- const deltaFrames = pixelsToFrames(world, pointer.position.deltaX);
-
- setComponent(world, sid, c.Workarea, {
- start: workareaDragStart[0] + deltaFrames,
- });
- }
-
- if (pressed) {
- workareaDragStart = [start, stopFrame];
- }
- }
- {
- ctx.translate(outPx - inPx + 5, 0);
- ctx.stroke(OUT_POINT_PATH);
- ctx.fill(OUT_POINT_PATH);
-
- const { dragging, pressed } = pointer.region(0, 0, 5, height);
-
- if (dragging) {
- assert(pointer.position, 'Mouse position must be set');
- assert(pointer.position?.state != 'idle', 'Mouse position must be pressing');
-
- beginGesture();
-
- const deltaFrames = pixelsToFrames(world, pointer.position.deltaX);
-
- setComponent(world, sid, c.Workarea, {
- end: workareaDragStart[1] + deltaFrames,
- });
- }
-
- if (pressed) {
- workareaDragStart = [start, stopFrame];
- }
- }
-
- // No handle dragged this frame — close any open work-area transaction.
- if (workareaTxOpen && !anyDragging) {
- world.history.commitTransaction();
- workareaTxOpen = false;
- }
-
- ctx.restore();
-}
diff --git a/apps/web/src/components/engine/timeline/timeline.ts b/apps/web/src/components/engine/timeline/timeline.ts
deleted file mode 100644
index 8988f9e4..00000000
--- a/apps/web/src/components/engine/timeline/timeline.ts
+++ /dev/null
@@ -1,325 +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 { createPointer } from "./pointer";
-import { assert, clamp } from "@/utils";
-import {
- renderBackground,
- renderMarquee,
- renderPlayhead,
- renderRuler,
- renderWorkarea,
- renderSnapPoints,
- renderLayers,
-} from "./render";
-import { pixelsToFrames, updateTimelineTransform } from "./utils";
-import {
- DEFAULT_TIMELINE_RESOLUTION,
- TIMELINE_RESOLUTION_RANGE,
- TIMELINE_PADDING_LEFT,
- RULER_HEIGHT,
- WHEEL_LINE_HEIGHT,
- WHEEL_PAGE_HEIGHT,
- ZOOM_DELTA_CLAMP,
- ZOOM_SENSITIVITY,
- SCROLL_X_SENSITIVITY,
-} from "./config";
-import { useEngine } from "@/context/engine";
-import { useCursor } from "@/hooks/use-cursor";
-import { createTimelineContext } from "./world";
-
-export type { Marquee, TimelineContext } from "./world";
-
-export function createTimeline() {
- const engine = useEngine();
- const world = engine.world;
- const c = world.components;
-
- const timeline = createTimelineContext();
-
- const pointer = createPointer({
- get marquee() { return timeline.marquee },
- get canvas() { return timeline.canvas },
- get ctx() { return timeline.ctx },
- });
-
- const cursor = useCursor();
-
- let layersEl: HTMLElement | null = null;
- let layersContainerEl: HTMLElement | null = null;
- let animationId: number | null = null;
- let layerScrollX = 0;
-
- const applyResize = () => {
- const canvas = timeline.canvas;
- const parent = canvas?.parentElement;
- if (!parent || !canvas) return;
-
- timeline.layout = parent.getBoundingClientRect();
- applyScroll();
-
- const dpr = window.devicePixelRatio;
- const nextWidth = Math.floor(timeline.layout.width * dpr);
- const nextHeight = Math.floor(timeline.layout.height * dpr);
-
- canvas.style.width = `${timeline.layout.width}px`;
- canvas.style.height = `${timeline.layout.height}px`;
-
- if (canvas.width === nextWidth && canvas.height === nextHeight) return;
-
- canvas.width = nextWidth;
- canvas.height = nextHeight;
- }
-
- const resize = () => {
- applyResize();
-
- updateTimelineTransform(world);
-
- if (engine.running && timeline.canvas && timeline.ctx) {
- drawFrame();
- }
- }
-
- const observer = new ResizeObserver(resize);
-
- const layerObserver = new ResizeObserver(() => {
- applyScroll();
- updateTimelineTransform(world);
- });
-
- const drawFrame = () => {
- const canvas = timeline.canvas;
- const ctx = timeline.ctx;
- if (!canvas || !ctx || !timeline.pointer) return;
-
- try {
- ctx.setTransform(1, 0, 0, 1, 0, 0);
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
-
- timeline.cursor = 'default';
-
- if (!timeline.minimized) {
- renderBackground(world, timeline);
-
- ctx.save();
- ctx.beginPath();
- ctx.rect(0, RULER_HEIGHT, canvas.width, canvas.height);
- ctx.clip();
- renderLayers(world, timeline);
- renderMarquee(world, timeline);
- ctx.restore();
- }
-
- renderRuler(world, timeline);
- renderWorkarea(world, timeline);
- renderPlayhead(world, timeline);
-
- if (!timeline.minimized) {
- renderSnapPoints(world, timeline);
- }
-
- cursor.set(canvas, timeline.cursor);
- } catch (e) {
- console.error(e);
- } finally {
- timeline.pointer.reset();
- }
- }
-
- const render = () => {
- if (engine.running) {
- drawFrame();
- }
-
- animationId = requestAnimationFrame(render);
- }
-
- const applyScroll = () => {
- if (!layersEl || !layersContainerEl) return;
-
- const sid = world.selection.scene;
- if (sid === null) return;
-
- const container = layersContainerEl;
- const layers = layersEl;
-
- const scrollY = c.Timeline.scrollY[sid] ?? 0;
- const newScrollY = Math.max(0, Math.min(scrollY, layers.scrollHeight - container.clientHeight));
-
- c.Timeline.scrollY[sid] = newScrollY;
- layers.style.transform = `translateY(${-newScrollY}px)`;
-
- const windows = layers.querySelectorAll<HTMLElement>('[data-layer-label]');
- let maxScrollX = 0;
- windows.forEach((el) => { maxScrollX = Math.max(maxScrollX, el.scrollWidth - el.clientWidth); });
- layerScrollX = Math.max(0, Math.min(layerScrollX, maxScrollX));
- layers.style.setProperty('--layer-x', `${layerScrollX}px`);
- }
-
- const handleWheelEvent = (event: WheelEvent) => {
- event.preventDefault();
-
- const sid = world.selection.scene;
- if (sid === null) return;
- const scrollX = c.Timeline.scrollX[sid] ?? 0;
- const resolution = c.Timeline.resolution[sid] || DEFAULT_TIMELINE_RESOLUTION;
- const { deltaX, deltaY } = normalizeWheel(event);
-
- // Prioritize: Z (zoom) > X (horizontal) > Y (vertical)
- // Only one axis can be scrolled at a time
- if (event.ctrlKey || event.metaKey) {
- // Zoom (Z axis)
- const rect = timeline.canvas?.getBoundingClientRect();
- const timelineX = event.clientX - (rect?.left ?? 0);
-
- const dy = clamp(deltaY, -ZOOM_DELTA_CLAMP, ZOOM_DELTA_CLAMP);
- let newResolution = resolution * Math.exp(-dy * ZOOM_SENSITIVITY);
- newResolution = clamp(newResolution, 1 / TIMELINE_RESOLUTION_RANGE[1], 1 / TIMELINE_RESOLUTION_RANGE[0]);
-
- let newScrollX = scrollX + timelineX / resolution - timelineX / newResolution;
- newScrollX = Math.max(-TIMELINE_PADDING_LEFT / newResolution, newScrollX);
- c.Timeline.scrollX[sid] = newScrollX;
- c.Timeline.resolution[sid] = newResolution;
- } else if (Math.abs(deltaX) > Math.abs(deltaY)) {
- // Horizontal scroll (X axis) - when horizontal movement is dominant
- const frameDelta = (deltaX * SCROLL_X_SENSITIVITY) / resolution;
-
- let newScrollX = scrollX + frameDelta;
- newScrollX = Math.max(-TIMELINE_PADDING_LEFT / resolution, newScrollX);
- c.Timeline.scrollX[sid] = newScrollX;
- } else {
- // Vertical scroll (Y axis) - default when no other conditions match
- c.Timeline.scrollY[sid] = (c.Timeline.scrollY[sid] ?? 0) + deltaY;
-
- applyScroll();
- }
-
- updateTimelineTransform(world);
- }
-
-
- const unmount = () => {
- layerObserver.disconnect();
-
- if (animationId) {
- cancelAnimationFrame(animationId);
- animationId = null;
- }
-
- document.body.removeEventListener('pointermove', pointer.move);
- document.body.removeEventListener('pointerup', pointer.up);
- }
-
- const scroll = (event: WheelEvent) => {
- event.preventDefault();
-
- const sid = world.selection.scene;
- if (sid === null) return;
-
- const { deltaX, deltaY } = normalizeWheel(event);
-
- if (Math.abs(deltaX) > Math.abs(deltaY)) {
- layerScrollX += deltaX;
- } else {
- c.Timeline.scrollY[sid] = (c.Timeline.scrollY[sid] ?? 0) + deltaY;
- }
-
- applyScroll();
- updateTimelineTransform(world);
- }
-
- const clientToFrame = (clientX: number) => {
- const sid = world.selection.scene;
- if (sid === null) return 0;
- const scrollX = c.Timeline.scrollX[sid] ?? 0;
- const resolution = c.Timeline.resolution[sid] || DEFAULT_TIMELINE_RESOLUTION;
- const rect = timeline.canvas?.getBoundingClientRect();
- const worldX = clientX - (rect?.left ?? 0) + scrollX * resolution;
- return pixelsToFrames(world, worldX);
- }
-
- const clientToTime = (clientX: number) => (clientToFrame(clientX) / world.frameRate);
-
- const setMinimized = (minimized: boolean) => {
- timeline.minimized = minimized;
- }
-
- const attachCanvas = () => {
- const canvas = document.getElementById('timeline-canvas') as HTMLCanvasElement;
- assert(canvas, 'Timeline canvas must be defined');
-
- const parent = canvas.parentElement;
- assert(parent, 'Timeline canvas must have a parent element');
-
- timeline.canvas = canvas;
- timeline.layout = parent.getBoundingClientRect();
- timeline.ctx = canvas.getContext('2d')!;
-
- observer.observe(parent);
-
- canvas.addEventListener('wheel', handleWheelEvent);
- canvas.addEventListener('pointerdown', pointer.down, { passive: true });
-
- applyResize();
- }
-
- const detachCanvas = () => {
- observer.disconnect();
-
- const canvas = timeline.canvas;
- canvas?.removeEventListener('wheel', handleWheelEvent);
- canvas?.removeEventListener('pointerdown', pointer.down);
-
- // Drop the stale references so the render loop no-ops until the next
- // attachCanvas() rebinds a live element.
- timeline.canvas = null;
- timeline.ctx = null;
- }
-
- // The layer refs, pointer and render loop live as long as <Layers>, which
- // stays mounted across timeline visibility toggles.
- const mount = () => {
- const layers = document.querySelector<HTMLElement>('[data-timeline-layers]');
- assert(layers, 'Timeline layers element must be defined');
-
- const layersContainer = document.querySelector<HTMLElement>('[data-timeline-layers-viewport]');
- assert(layersContainer, 'Timeline layers viewport element must be defined');
-
- timeline.pointer = pointer;
- layersEl = layers;
- layersContainerEl = layersContainer;
-
- layerObserver.observe(layers);
-
- document.body.addEventListener('pointermove', pointer.move, { passive: true });
- document.body.addEventListener('pointerup', pointer.up, { passive: true });
-
- if (!animationId) {
- animationId = requestAnimationFrame(render);
- }
- }
-
- return {
- mount,
- unmount,
- attachCanvas,
- detachCanvas,
- scroll,
- clientToTime,
- clientToFrame,
- setMinimized,
- };
-}
-
-function normalizeWheel(event: WheelEvent) {
- const scale = event.deltaMode === WheelEvent.DOM_DELTA_LINE
- ? WHEEL_LINE_HEIGHT
- : event.deltaMode === WheelEvent.DOM_DELTA_PAGE
- ? WHEEL_PAGE_HEIGHT
- : 1;
-
- return { deltaX: event.deltaX * scale, deltaY: event.deltaY * scale };
-}
diff --git a/apps/web/src/components/engine/timeline/utils.ts b/apps/web/src/components/engine/timeline/utils.ts
deleted file mode 100644
index 3bd10bc1..00000000
--- a/apps/web/src/components/engine/timeline/utils.ts
+++ /dev/null
@@ -1,182 +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 { hasComponent } from "bitecs";
-import { DEFAULT_CLIP_HEIGHT, DEFAULT_TIMELINE_RESOLUTION, KEYFRAME_TRACK_HEIGHT, RULER_HEIGHT, TIMELINE_PADDING_LEFT } from "./config";
-import { addComponent } from "../api/events";
-
-import type { TimelineContext } from "./world";
-import type { EngineWorld } from "../api/world";
-import type { TimelineNode } from "../api/timeline-index";
-
-export function getCurrentFrame(world: EngineWorld) {
- const sid = world.selection.scene;
- if (sid === null) return 0;
- return world.components.Computed.localTime[sid];
-}
-
-export function setCurrentFrame(world: EngineWorld, frame: number) {
- const sid = world.selection.scene;
- const fps = world.frameRate;
- if (sid === null) return;
- const clamped = Math.max(0, frame);
- world.components.Computed.localTime[sid] = clamped;
- world.components.Computed.localTimeInSeconds[sid] = clamped / fps;
-}
-
-export function getCurrentResolution(world: EngineWorld) {
- const sid = world.selection.scene;
- if (sid === null) return DEFAULT_TIMELINE_RESOLUTION;
- return world.components.Timeline.resolution[sid] ?? DEFAULT_TIMELINE_RESOLUTION;
-}
-
-
-export function getCurrentScrollX(world: EngineWorld) {
- const sid = world.selection.scene;
- if (sid === null) return -TIMELINE_PADDING_LEFT / getCurrentResolution(world);
- return world.components.Timeline.scrollX[sid] ?? 0;
-}
-
-export function getCurrentScrollY(world: EngineWorld) {
- const sid = world.selection.scene;
- if (sid === null) return 0;
- return world.components.Timeline.scrollY[sid] ?? 0;
-}
-
-export function getRelativeViewport(world: EngineWorld, timeline: TimelineContext): [number, number] {
- const scrollX = getCurrentScrollX(world);
- const resolution = getCurrentResolution(world);
- const left = scrollX * resolution;
-
- const right = left + timeline.layout.width;
- return [left, right];
-}
-
-
-export function framesToPixels(world: EngineWorld, frames: number = 0) {
- const resolution = getCurrentResolution(world);
- return Math.floor(frames * resolution);
-}
-
-export function pixelsToFrames(world: EngineWorld, pixels: number) {
- const resolution = getCurrentResolution(world);
- return Math.round(pixels / resolution);
-}
-
-export function clientToWorldX(world: EngineWorld, timeline: TimelineContext, clientX: number) {
- const scrollX = getCurrentScrollX(world);
- const resolution = getCurrentResolution(world);
- return clientX - (timeline.layout?.left ?? 0) + scrollX * resolution;
-}
-
-export function clientToWorldY(world: EngineWorld, timeline: TimelineContext, clientY: number) {
- const scrollY = getCurrentScrollY(world);
- return clientY - (timeline.layout?.top ?? 0) + scrollY;
-}
-
-export function getClipTransform(world: EngineWorld, timeline: TimelineContext): DOMMatrix | undefined {
- const c = world.components;
- const sid = world.selection.scene ?? -1;
- const transform = c.Timeline.transform[sid];
- return transform?.translate(0, timeline.currentLayerTop);
-}
-
-export function getNodeHeight(world: EngineWorld, node: TimelineNode) {
- if (node.kind === 'geometry') {
- return world.components.ClipHeight[node.eid] ?? DEFAULT_CLIP_HEIGHT;
- }
- return KEYFRAME_TRACK_HEIGHT;
-}
-
-/**
- * Total height of a row plus all of its (expanded) descendant rows.
- */
-export function getSubtreeHeight(world: EngineWorld, node: TimelineNode): number {
- let height = getNodeHeight(world, node);
- for (const child of node.children) {
- height += getSubtreeHeight(world, child);
- }
- return height;
-}
-
-export function updateTimelineTransform(world: EngineWorld) {
- const sid = world.selection.scene;
- if (sid === null) return;
-
- const c = world.components;
-
- if (!hasComponent(world, sid, c.Timeline)) {
- addComponent(world, sid, c.Timeline, false);
- c.Timeline.resolution[sid] = DEFAULT_TIMELINE_RESOLUTION;
- c.Timeline.scrollX[sid] = -TIMELINE_PADDING_LEFT / DEFAULT_TIMELINE_RESOLUTION;
- c.Timeline.scrollY[sid] = 0;
- c.Timeline.transform[sid] = new DOMMatrix();
- }
-
- const resolution = c.Timeline.resolution[sid];
- const scrollX = c.Timeline.scrollX[sid];
- const scrollY = c.Timeline.scrollY[sid];
- const dpr = window.devicePixelRatio;
-
- const transform = new DOMMatrix();
- transform.scaleSelf(dpr, dpr);
- transform.translateSelf(-(scrollX * resolution), -scrollY);
- transform.translateSelf(0, RULER_HEIGHT);
- c.Timeline.transform[sid] = transform;
-}
-
-const TEXT_WIDTH_CACHE: Map<string, number> = new Map();
-
-// Widths measured before web fonts finish loading reflect the fallback font and
-// will under-report once the real font swaps in, causing labels to overflow.
-document?.fonts.addEventListener('loadingdone', () => TEXT_WIDTH_CACHE.clear());
-
-export function binarySearchTextLength(
- ctx: CanvasRenderingContext2D,
- text: string,
- maxWidth: number,
- cacheId: string = 'default'
-): string | null {
- const getTextWidth = (text: string) => {
- const cacheKey = `${cacheId}-${text}`;
-
- if (TEXT_WIDTH_CACHE.has(cacheKey)) {
- return TEXT_WIDTH_CACHE.get(cacheKey)!;
- }
-
- const width = ctx.measureText(text).width;
- TEXT_WIDTH_CACHE.set(cacheKey, width);
- return width;
- }
-
- if (getTextWidth(text) > maxWidth) {
- const availableWidth = maxWidth - getTextWidth('...');
-
- if (availableWidth > 0) {
- // Binary search for optimal text length
- let start = 0;
- let end = text.length;
- let bestLength = 0;
-
- while (start <= end) {
- const mid = Math.floor((start + end) / 2);
- const testText = text.slice(0, mid);
- const testWidth = getTextWidth(testText);
-
- if (testWidth <= availableWidth) {
- bestLength = mid;
- start = mid + 1;
- } else {
- end = mid - 1;
- }
- }
-
- return bestLength > 0 ? text.slice(0, bestLength) + '...' : null;
- } else {
- return null;
- }
- }
-
- return text;
-}
diff --git a/apps/web/src/components/engine/timeline/world.ts b/apps/web/src/components/engine/timeline/world.ts
deleted file mode 100644
index 594805dc..00000000
--- a/apps/web/src/components/engine/timeline/world.ts
+++ /dev/null
@@ -1,37 +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 { COLORS } from "./constants";
-import { DEFAULT_CLIP_HEIGHT } from "./config";
-
-import type { CursorType } from "@/hooks/use-cursor";
-import type { Pointer } from "./pointer";
-
-export type Marquee = {
- x: number;
- y: number;
- width: number;
- height: number;
-}
-
-export type TimelineContext = ReturnType<typeof createTimelineContext>;
-
-export function createTimelineContext() {
- return {
- colors: COLORS,
- snapping: true,
- snapDistance: 10,
- snapX: null as number | null,
- currentClip: null as number | null,
- currentLayerTop: 0,
- currentLayerHeight: DEFAULT_CLIP_HEIGHT,
- cursor: 'default' as CursorType,
- minimized: false,
- canvas: null as HTMLCanvasElement | null,
- ctx: null as CanvasRenderingContext2D | null,
- layout: new DOMRect(),
- pointer: null as Pointer | null,
- marquee: undefined as Marquee | undefined,
- };
-}
diff --git a/apps/web/src/components/engine/types/common.ts b/apps/web/src/components/engine/types/common.ts
deleted file mode 100644
index dc7c290d..00000000
--- a/apps/web/src/components/engine/types/common.ts
+++ /dev/null
@@ -1,21 +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/. */
-
-export type RuntimeMode = 'realtime' | 'offline-video' | 'offline-audio';
-
-export type HudMode = 'idle' | 'moving' | 'marquee';
-
-/**
- * 2D affine matrix representing the camera transform in CSS pixel space
- * (before DPR scaling). The render system multiplies this by devicePixelRatio
- * to derive the canvas transform.
- */
-export type Camera2D = {
- a: number;
- b: number;
- c: number;
- d: number;
- e: number;
- f: number;
-};
diff --git a/apps/web/src/components/engine/types/db.ts b/apps/web/src/components/engine/types/db.ts
deleted file mode 100644
index 20681265..00000000
--- a/apps/web/src/components/engine/types/db.ts
+++ /dev/null
@@ -1,152 +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/. */
-
-export interface DBEntity {
- id: string;
- eid: number;
- Geometry?: number;
- Paint?: number;
- Group?: {};
- Scene?: {};
- Audio?: {};
- AdjustmentLayer?: {};
- Effect?: {
- type?: number;
- value?: number;
- };
- Caption?: {
- type?: number;
- colors?: number[];
- verticalAlign?: number;
- };
- IsMask?: {};
- Shadow?: {};
- Stroke?: {};
- Name?: string;
- Key?: string;
- MountScript?: {
- mountId: string;
- scriptAssetId: string;
- };
- MountPath?: {
- mountId: string;
- path: string;
- };
- ChildOf?: number;
- ItemIndex?: number;
- ClipHeight?: number;
- Expanded?: {};
- Position?: {
- x?: number;
- y?: number;
- };
- Offset?: {
- x?: number;
- y?: number;
- };
- Rotation?: number;
- Scale?: {
- x?: number;
- y?: number;
- };
- UniformScale?: number;
- Anchor?: {
- x?: number;
- y?: number;
- };
- Skew?: {
- x?: number;
- y?: number;
- };
- Size?: {
- width?: number;
- height?: number;
- };
- Flip?: {
- x?: -1 | 1;
- y?: -1 | 1;
- };
- Appearance?: {
- opacity?: number;
- blendMode?: number;
- };
- CornerRadius?: number;
- MixedCornerRadius?: {
- topLeft: number;
- topRight: number;
- bottomRight: number;
- bottomLeft: number;
- };
- Color?: number;
- Blur?: number;
- AssetId?: string;
- ScaleMode?: number;
- Shader?: {
- code?: string;
- uniforms?: Record<string, number | number[] | string>;
- };
- Volume?: number;
- Muted?: {};
- ColorStop?: {
- offset?: number;
- color?: number;
- opacity?: number;
- };
- StrokeStyle?: {
- width?: number;
- join?: number;
- cap?: number;
- miterLimit?: number;
- };
- Hidden?: boolean;
- ClipsContent?: boolean;
- Sequential?: {};
- Playback?: {
- loop?: number;
- };
- Delay?: number;
- PlaybackRate?: number;
- Trim?: {
- start?: number;
- end?: number;
- };
- Constraint?: {
- horizontal?: number;
- vertical?: number;
- };
- KeepAspectRatio?: {
- width?: number;
- height?: number;
- };
- Transition?: {
- type?: number;
- duration?: number;
- };
- KeyframeTrack?: {
- property?: string;
- };
- Keyframe?: {
- time?: number;
- value?: number;
- easing?: string;
- };
- Animation?: {
- duration?: number;
- delay?: number;
- type?: number;
- phase?: number;
- };
- Chars?: string;
- TextStyle?: {
- leading?: number;
- fontSize?: number;
- fontFamily?: string;
- fontWeight?: string;
- fontStyle?: number;
- textAlign?: number;
- textBaseline?: number;
- textCase?: number;
- letterSpacing?: number;
- };
-};
diff --git a/apps/web/src/components/engine/types/index.ts b/apps/web/src/components/engine/types/index.ts
deleted file mode 100644
index 19fe103b..00000000
--- a/apps/web/src/components/engine/types/index.ts
+++ /dev/null
@@ -1,7 +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/. */
-
-export * from './input';
-export * from './db';
-export * from './common';
diff --git a/apps/web/src/components/engine/types/input.ts b/apps/web/src/components/engine/types/input.ts
deleted file mode 100644
index 018a6046..00000000
--- a/apps/web/src/components/engine/types/input.ts
+++ /dev/null
@@ -1,57 +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 type { Point, Quad } from "../math";
-import type { EngineWorld } from "../api/world";
-
-export type CustomPointerEventType =
- | "pointermove"
- | "pointerdown"
- | "pointerup"
- | "pointerenter"
- | "pointerleave"
- | "dragstart"
- | "drag"
- | "dragend"
- | "click"
- | "dblclick";
-
-
-export type CustomPointerEvent = {
- type: CustomPointerEventType;
- clientX: number;
- clientY: number;
- button: number; // 0: left, 1: middle, 2: right
-}
-
-export type PointerState = {
- state: 'pressed' | 'lifted';
- button: number;
- clientX: number;
- clientY: number;
- dragStartX: number;
- dragStartY: number;
-}
-
-export type SnapLine = { from: Point; to: Point; }
-
-export type DispatchedPointerEvent = CustomPointerEvent & {
- target: HudTarget | EntityTarget;
-}
-
-export type HudTarget = {
- kind: 'hud';
- id: string;
- quad: Quad;
-}
-
-export type EntityTarget = {
- kind: 'entity';
- id: number;
-};
-
-export type HitRegion = {
- target: HudTarget | EntityTarget;
- callback: (world: EngineWorld, event: DispatchedPointerEvent) => void;
-}
diff --git a/apps/web/src/components/engine/utils/audio-sync.ts b/apps/web/src/components/engine/utils/audio-sync.ts
deleted file mode 100644
index 85c65648..00000000
--- a/apps/web/src/components/engine/utils/audio-sync.ts
+++ /dev/null
@@ -1,232 +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 { ALL_FORMATS, AudioSampleSink, BlobSource, Input } from 'mediabunny';
-import { assert } from '@/utils';
-import { getAssetFile } from '../api/assets';
-
-import type { Asset } from '../db';
-
-// Envelope sample rate. 500 Hz → 2 ms resolution (well under one video frame),
-// and keeps the FFT small enough for multi-minute clips. Parabolic
-// interpolation around the peak refines below one bin.
-const SYNC_ENVELOPE_RATE_HZ = 500;
-// Require at least this much overlapping audio for a lag to be considered.
-const SYNC_MIN_OVERLAP_SEC = 1;
-// Below this correlation the match is treated as no match, and we fail loudly
-// rather than return a confident-looking but meaningless offset.
-const SYNC_NO_MATCH_FLOOR = 0.1;
-
-export type AudioSyncResult = {
- // Place the audio at: audioStart = videoStart + offsetSeconds.
- // Positive = the recording started after the camera rolled.
- offsetSeconds: number;
- confidence: number; // 0..1 (clamped Pearson correlation at the best lag)
-};
-
-// RMS energy envelope of an asset's primary audio track, at `rate` bins/sec.
-async function extractAudioEnvelope(asset: Asset, rate: number): Promise<Float32Array> {
- const blob = await getAssetFile(asset);
- const input = new Input({ formats: ALL_FORMATS, source: new BlobSource(blob) });
- try {
- const track = await input.getPrimaryAudioTrack();
- assert(track, `Asset ${asset.id} has no audio track to sync.`);
- const sink = new AudioSampleSink(track);
- const bins: number[] = [];
- for await (const sample of sink.samples()) {
- try {
- const size = sample.allocationSize({ format: 'f32', planeIndex: 0 });
- const floats = new Float32Array(size / Float32Array.BYTES_PER_ELEMENT);
- sample.copyTo(floats, { format: 'f32', planeIndex: 0 });
-
- const numChannels = sample.numberOfChannels;
- const numFrames = floats.length / numChannels;
- const targetBins = Math.max(1, Math.round(sample.duration * rate));
- const ratio = numFrames / targetBins;
-
- for (let i = 0; i < targetBins; i++) {
- const start = Math.floor(i * ratio);
- const end = Math.max(start + 1, Math.floor((i + 1) * ratio));
- let sumSq = 0;
- let count = 0;
- for (let f = start; f < end && f < numFrames; f++) {
- for (let ch = 0; ch < numChannels; ch++) {
- const v = floats[f * numChannels + ch];
- sumSq += v * v;
- count++;
- }
- }
- bins.push(count ? Math.sqrt(sumSq / count) : 0);
- }
- } finally {
- sample.close();
- }
- }
- return Float32Array.from(bins);
- } finally {
- input.dispose();
- }
-}
-
-function nextPow2(n: number): number {
- let p = 1;
- while (p < n) p <<= 1;
- return p;
-}
-
-// In-place iterative radix-2 Cooley-Tukey FFT (inverse when `invert`).
-function fft(re: Float64Array, im: Float64Array, invert: boolean): void {
- const n = re.length;
- for (let i = 1, j = 0; i < n; i++) {
- let bit = n >> 1;
- for (; j & bit; bit >>= 1) j ^= bit;
- j ^= bit;
- if (i < j) {
- const tr = re[i]; re[i] = re[j]; re[j] = tr;
- const ti = im[i]; im[i] = im[j]; im[j] = ti;
- }
- }
- for (let len = 2; len <= n; len <<= 1) {
- const ang = ((invert ? 2 : -2) * Math.PI) / len;
- const wr = Math.cos(ang);
- const wi = Math.sin(ang);
- for (let i = 0; i < n; i += len) {
- let cwr = 1;
- let cwi = 0;
- for (let k = 0; k < len >> 1; k++) {
- const a = i + k;
- const b = a + (len >> 1);
- const vr = re[b] * cwr - im[b] * cwi;
- const vi = re[b] * cwi + im[b] * cwr;
- re[b] = re[a] - vr; im[b] = im[a] - vi;
- re[a] += vr; im[a] += vi;
- const ncwr = cwr * wr - cwi * wi;
- cwi = cwr * wi + cwi * wr;
- cwr = ncwr;
- }
- }
- }
- if (invert) {
- for (let i = 0; i < n; i++) { re[i] /= n; im[i] /= n; }
- }
-}
-
-// Linear cross-correlation via FFT. Returns cc where cc[m] = Σ a[i]·b[i+m]
-// (negative lags wrap to the high indices; safe because of zero padding).
-function crossCorrelate(a: Float32Array, b: Float32Array): Float64Array {
- const n = nextPow2(a.length + b.length);
- const ar = new Float64Array(n);
- const ai = new Float64Array(n);
- const br = new Float64Array(n);
- const bi = new Float64Array(n);
- ar.set(a);
- br.set(b);
- fft(ar, ai, false);
- fft(br, bi, false);
- // conj(FA) .* FB
- for (let k = 0; k < n; k++) {
- const cr = ar[k] * br[k] + ai[k] * bi[k];
- const ci = ar[k] * bi[k] - ai[k] * br[k];
- ar[k] = cr;
- ai[k] = ci;
- }
- fft(ar, ai, true);
- return ar; // real part holds the correlation
-}
-
-function prefixSums(x: Float32Array): { sum: Float64Array; sumSq: Float64Array } {
- const sum = new Float64Array(x.length + 1);
- const sumSq = new Float64Array(x.length + 1);
- for (let i = 0; i < x.length; i++) {
- sum[i + 1] = sum[i] + x[i];
- sumSq[i + 1] = sumSq[i] + x[i] * x[i];
- }
- return { sum, sumSq };
-}
-
-const offsetCache = new Map<string, Promise<AudioSyncResult>>();
-
-export function computeAudioSyncOffsetCached(a: Asset, b: Asset): Promise<AudioSyncResult> {
- const key = `${a.hash}:${b.hash}`;
- const cached = offsetCache.get(key);
- if (cached) return cached;
- const promise = computeAudioSyncOffset(a, b);
- offsetCache.set(key, promise);
- promise.catch(() => offsetCache.delete(key));
- return promise;
-}
-
-export async function computeAudioSyncOffset(
- audioAsset: Asset,
- videoAsset: Asset,
-): Promise<AudioSyncResult> {
- const rate = SYNC_ENVELOPE_RATE_HZ;
- const A = await extractAudioEnvelope(audioAsset, rate);
- const B = await extractAudioEnvelope(videoAsset, rate);
- const La = A.length;
- const Lb = B.length;
- assert(La > 0 && Lb > 0, "No reliable alignment found: an asset has no decodable audio.");
-
- const cc = crossCorrelate(A, B);
- const n = cc.length;
- const pa = prefixSums(A);
- const pb = prefixSums(B);
- const minOverlap = Math.max(1, Math.floor(rate * SYNC_MIN_OVERLAP_SEC));
-
- // Pearson correlation of the overlapping region at lag L (audio index i ↔
- // video index i+L), computed in O(1) from prefix sums + the FFT dot product.
- const nccAt = (L: number): number => {
- const iLo = Math.max(0, -L);
- const iHi = Math.min(La, Lb - L);
- const cnt = iHi - iLo;
- if (cnt < minOverlap) return -Infinity;
- const jLo = iLo + L;
- const jHi = iHi + L;
- const sumA = pa.sum[iHi] - pa.sum[iLo];
- const sumSqA = pa.sumSq[iHi] - pa.sumSq[iLo];
- const sumB = pb.sum[jHi] - pb.sum[jLo];
- const sumSqB = pb.sumSq[jHi] - pb.sumSq[jLo];
- const meanA = sumA / cnt;
- const meanB = sumB / cnt;
- const varA = sumSqA - cnt * meanA * meanA;
- const varB = sumSqB - cnt * meanB * meanB;
- if (varA <= 1e-9 || varB <= 1e-9) return -Infinity;
- const dot = cc[((L % n) + n) % n];
- const cov = dot - cnt * meanA * meanB;
- return cov / Math.sqrt(varA * varB);
- };
-
- let bestL = 0;
- let bestNcc = -Infinity;
- for (let L = -(La - 1); L <= Lb - 1; L++) {
- const v = nccAt(L);
- if (v > bestNcc) {
- bestNcc = v;
- bestL = L;
- }
- }
- assert(
- bestNcc > -Infinity,
- "No reliable alignment found: the clips don't overlap enough to align.",
- );
- assert(
- bestNcc >= SYNC_NO_MATCH_FLOOR,
- "No reliable alignment found: the audio tracks share too little common sound.",
- );
-
- // Parabolic interpolation around the peak for sub-bin precision.
- let delta = 0;
- const ym = nccAt(bestL - 1);
- const yp = nccAt(bestL + 1);
- if (Number.isFinite(ym) && Number.isFinite(yp)) {
- const denom = ym - 2 * bestNcc + yp;
- if (denom !== 0) delta = (0.5 * (ym - yp)) / denom;
- if (!Number.isFinite(delta) || Math.abs(delta) > 1) delta = 0;
- }
-
- return {
- offsetSeconds: (bestL + delta) / rate,
- confidence: Math.max(0, Math.min(1, bestNcc)),
- };
-}
diff --git a/apps/web/src/components/engine/utils/captions.ts b/apps/web/src/components/engine/utils/captions.ts
deleted file mode 100644
index 5b29e5b3..00000000
--- a/apps/web/src/components/engine/utils/captions.ts
+++ /dev/null
@@ -1,168 +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 { hasComponent, entityExists } from "bitecs";
-import { trpc } from "@/lib/trpc";
-import { PaintType, getEntityTree, getParentEntity, secondsToFrames, uploadBlob, loadAsset } from "@/components/engine";
-import { createEncoder } from "@/components/engine/encode/encoder";
-import { resolveTranscript } from "@/components/engine/decoders/caption/utils";
-import { assert } from "@/utils";
-
-import type { Transcript } from "@diffusionstudio/api-contract";
-import type { Engine, EngineWorld } from "@/components/engine";
-import type { Asset } from "@/components/engine/db";
-
-const CAPTIONS_NAME_REGEX = /^Captions (\d+)$/;
-
-function nextCaptionsName(all: ReadonlyArray<Asset>): string {
- let max = 0;
- for (const asset of all) {
- const match = asset.name.match(CAPTIONS_NAME_REGEX);
- if (match) max = Math.max(max, Number(match[1]));
- }
- return `Captions ${max + 1}`;
-}
-
-export function hasAudioSources(world: EngineWorld, sceneEid: number): boolean {
- const c = world.components;
- for (const eid of getEntityTree(world, sceneEid)) {
- if (hasComponent(world, eid, c.Muted)) continue;
- if (hasComponent(world, eid, c.Audio) && c.AssetId[eid]) return true;
- if (hasComponent(world, eid, c.Paint) && c.Paint[eid] === PaintType.VIDEO && c.AssetId[eid]) {
- return true;
- }
- }
- return false;
-}
-
-/**
- * Fingerprint of everything that shapes a scene's audible mix — source
- * content, placement, source offset, rate, and gain. Volume fades and audio
- * transitions are not captured; they don't change the spoken words.
- */
-export function sceneAudioFingerprint(world: EngineWorld, sceneEid: number, seed?: number): string {
- const c = world.components;
- const entries: string[] = [];
-
- for (const eid of getEntityTree(world, sceneEid)) {
- if (hasComponent(world, eid, c.Muted)) continue;
-
- let audioEid: number | null = null;
-
- if (c.Paint[eid] === PaintType.VIDEO) {
- audioEid = getParentEntity(world, eid) ?? null;
- }
-
- if (!hasComponent(world, eid, c.Audio)) {
- audioEid = eid;
- }
-
-
- const asset = world.assets.get(c.AssetId[eid] ?? "");
- if (audioEid === null || !asset) continue;
-
- entries.push([
- asset.hash,
- c.Computed.start[audioEid] ?? 0,
- c.Computed.end[audioEid] ?? 0,
- c.Computed.delay[audioEid] ?? 0,
- c.PlaybackRate[audioEid] ?? 1,
- c.Volume[audioEid] ?? 0,
- ].join(":"));
- }
-
- const mix = entries.sort().join("|");
- return `v1:${fnv1a(seed === undefined ? mix : `${mix}#seed=${seed}`)}`;
-}
-
-function fnv1a(input: string): string {
- let hash = 0x811c9dc5;
- for (let i = 0; i < input.length; i++) {
- hash ^= input.charCodeAt(i);
- hash = Math.imul(hash, 0x01000193);
- }
- return (hash >>> 0).toString(16);
-}
-
-export function transcriptTrim(transcript: Transcript): { start: number; end: number } {
- const firstWord = transcript.at(0)?.words.at(0);
- const lastWord = transcript.at(-1)?.words.at(-1);
- return { start: secondsToFrames(firstWord?.start), end: secondsToFrames(lastWord?.end) };
-}
-
-export type TranscribeStatus = "encoding" | "uploading" | "transcribing";
-
-/**
- * Encodes a scene's audio, transcribes it, and stores the transcript as an asset.
- */
-export async function transcribeScene(
- engine: Engine,
- sceneEid: number,
- onStatus?: (status: TranscribeStatus) => void,
- seed?: number,
-) {
- const world = engine.world;
- const c = world.components;
-
- if (!entityExists(world, sceneEid) || !hasComponent(world, sceneEid, c.Scene)) {
- throw new Error(`Node ${sceneEid} is not a scene.`);
- }
- if (!hasAudioSources(world, sceneEid)) {
- throw new Error("No audio found. Add an audio or video clip to the scene to generate captions.");
- }
-
- const generationKey = sceneAudioFingerprint(world, sceneEid, seed);
- const cached = Array.from(world.assets.values())
- .find(a => a.type === "TRANSCRIPT" && a.generationKey === generationKey);
-
- if (cached) {
- const transcript = await resolveTranscript(cached);
- if (transcript.length) {
- return {
- asset: { id: cached.id, name: cached.name, type: cached.type },
- trim: transcriptTrim(transcript),
- };
- }
- }
-
- onStatus?.("encoding");
- engine.stop();
- let result;
- try {
- const encoder = await createEncoder(world, {
- scene: sceneEid,
- video: { enabled: false },
- audio: { enabled: true, codec: "opus", sampleRate: 24000 },
- format: "ogg",
- });
- result = await encoder.render();
- } finally {
- engine.start();
- }
-
- if (result.type !== "success" || !result.data) {
- throw new Error("Failed to encode audio");
- }
-
- onStatus?.("uploading");
- const assetid = crypto.randomUUID();
- const audioFile = new File([result.data], `${assetid}.ogg`, { type: "audio/ogg" });
- const fileRef = await uploadBlob(audioFile, assetid);
- assert(fileRef, "Failed to upload audio");
-
- onStatus?.("transcribing");
- const { results: transcript } = await trpc.transcribe.mutate({ audio: fileRef });
-
- assert(transcript.length, "No speech detected. The audio does not appear to contain recognizable speech.");
- assert(transcript.every((s) => s.words.length > 0), "No speech detected. The audio does not appear to contain recognizable speech.");
-
- const name = nextCaptionsName(Array.from(world.assets.values()));
- const blob = new Blob([JSON.stringify(transcript)], { type: "application/json" });
- const file = new File([blob], `${name}.json`, { type: "application/json" });
- const asset = await loadAsset(world, file, { name, generationKey });
-
- assert(asset.type === "TRANSCRIPT", "Expected a transcript asset");
-
- return { asset: { id: asset.id, name: asset.name, type: asset.type }, trim: transcriptTrim(transcript) };
-}
diff --git a/apps/web/src/components/engine/utils/contact-sheet.ts b/apps/web/src/components/engine/utils/contact-sheet.ts
deleted file mode 100644
index 483cc504..00000000
--- a/apps/web/src/components/engine/utils/contact-sheet.ts
+++ /dev/null
@@ -1,142 +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 { MAX_FRAMES_PER_SHEET } from '@diffusionstudio/cli/channels';
-import { stampTimestampLabel } from './transcode';
-
-/**
- * Contact sheets: a handful of frames merged into one labelled image, so a
- * grab or a capture reads as a single picture instead of a directory of PNGs.
- * The sheet never exceeds the largest image a vision model reads at full
- * detail (92x52 patches of 28px), and frames are never upscaled past their
- * source resolution.
- */
-export const SHEET_MAX_WIDTH = 2576;
-export const SHEET_MAX_HEIGHT = 1456;
-
-const MARGIN = 4;
-const GUTTER = 8;
-const BACKGROUND = '#444';
-
-export type SheetPlan = {
- columns: number;
- rows: number;
- /** Size each frame is drawn at, in px. */
- cellWidth: number;
- cellHeight: number;
- width: number;
- height: number;
-};
-
-/**
- * Pick the grid for `count` frames of the given source size: the layout that
- * draws each frame as large as possible within the sheet budget, preferring
- * fewer blank cells and then wider rows when the sizes tie. The last row may be
- * partially filled, and frames are never enlarged past `source`.
- */
-export function planSheet(count: number, source: { width: number; height: number }): SheetPlan {
- const cells = Math.max(1, Math.min(count, MAX_FRAMES_PER_SHEET));
- let best: SheetPlan | undefined;
-
- for (let columns = 1; columns <= cells; columns++) {
- const rows = Math.ceil(cells / columns);
- const availableWidth = SHEET_MAX_WIDTH - 2 * MARGIN - GUTTER * (columns - 1);
- const availableHeight = SHEET_MAX_HEIGHT - 2 * MARGIN - GUTTER * (rows - 1);
- if (availableWidth < columns || availableHeight < rows) continue;
-
- // Fit the frame into its cell, never enlarging it past its own pixels.
- const scale = Math.min(availableWidth / columns / source.width, availableHeight / rows / source.height, 1);
- const cellWidth = Math.max(1, Math.floor(source.width * scale));
- const cellHeight = Math.max(1, Math.floor(source.height * scale));
-
- const candidate: SheetPlan = {
- columns,
- rows,
- cellWidth,
- cellHeight,
- width: 2 * MARGIN + columns * cellWidth + GUTTER * (columns - 1),
- height: 2 * MARGIN + rows * cellHeight + GUTTER * (rows - 1),
- };
- if (best === undefined || isBetterPlan(candidate, best, cells)) best = candidate;
- }
-
- // Every source fits at 1x1, so the loop always yields a plan.
- return best!;
-}
-
-function isBetterPlan(candidate: SheetPlan, best: SheetPlan, cells: number): boolean {
- const candidateArea = candidate.cellWidth * candidate.cellHeight;
- const bestArea = best.cellWidth * best.cellHeight;
- if (candidateArea !== bestArea) return candidateArea > bestArea;
-
- const candidateBlanks = candidate.columns * candidate.rows - cells;
- const bestBlanks = best.columns * best.rows - cells;
- if (candidateBlanks !== bestBlanks) return candidateBlanks < bestBlanks;
-
- // Same cells either way: fill rows before starting new ones.
- return candidate.columns > best.columns;
-}
-
-/**
- * A sheet is stamped with the span it covers, earliest to latest: `0f-08s10f`,
- * or just the one timecode when every cell lands on the same frame. The cells
- * themselves stay in the order they were requested in, hence `at`.
- */
-export function sheetTimecode(cells: Array<{ at: number; timecode: string }>): string {
- const sorted = [...cells].sort((a, b) => a.at - b.at);
- const first = sorted[0].timecode;
- const last = sorted[sorted.length - 1].timecode;
- return first === last ? first : `${first}-${last}`;
-}
-
-/**
- * Split `total` frames into balanced sheets of at most `perSheet`, so a run of
- * 13 becomes 7 + 6 rather than 12 + 1. Returns the size of each sheet.
- */
-export function planSheetSizes(total: number, perSheet?: number): number[] {
- const max = Math.max(1, Math.min(Math.round(perSheet ?? MAX_FRAMES_PER_SHEET), MAX_FRAMES_PER_SHEET));
- const sheets = Math.max(1, Math.ceil(total / max));
- const base = Math.floor(total / sheets);
- const extra = total % sheets;
- return Array.from({ length: sheets }, (_, i) => base + (i < extra ? 1 : 0));
-}
-
-/**
- * Draw frames into a sheet in reading order, each stamped with its timecode.
- * The gutters are opaque, so transparent captures composite onto flat grey.
- */
-export function composeSheet(
- frames: Array<{ image: CanvasImageSource; label: string }>,
- plan: SheetPlan,
-): string {
- const canvas = document.createElement('canvas');
- canvas.width = plan.width;
- canvas.height = plan.height;
- const ctx = canvas.getContext('2d')!;
-
- ctx.fillStyle = BACKGROUND;
- ctx.fillRect(0, 0, canvas.width, canvas.height);
-
- for (const [i, frame] of frames.entries()) {
- const column = i % plan.columns;
- const row = Math.floor(i / plan.columns);
- const x = MARGIN + column * (plan.cellWidth + GUTTER);
- const y = MARGIN + row * (plan.cellHeight + GUTTER);
-
- ctx.drawImage(frame.image, x, y, plan.cellWidth, plan.cellHeight);
-
- ctx.save();
- ctx.translate(x, y);
- stampTimestampLabel(ctx, plan.cellHeight, frame.label, { minBandHeight: 30 });
- ctx.restore();
- }
-
- const dataUrl = canvas.toDataURL('image/png');
- return dataUrl.slice(dataUrl.indexOf(',') + 1);
-}
-
-export async function decodePngBase64(base64: string): Promise<ImageBitmap> {
- const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
- return createImageBitmap(new Blob([bytes], { type: 'image/png' }));
-}
diff --git a/apps/web/src/components/engine/utils/coordinates.ts b/apps/web/src/components/engine/utils/coordinates.ts
deleted file mode 100644
index 8e031c81..00000000
--- a/apps/web/src/components/engine/utils/coordinates.ts
+++ /dev/null
@@ -1,107 +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 { Not, query } from 'bitecs';
-import { ChildOf } from '../components';
-import { isScene } from '../api/query';
-import type { EngineWorld } from '../api/world';
-import { aabbFromTransformedRect, invert2D, transformPoint, type Mat2D, type Point } from '../math';
-
-/** Convert a screen-space point (CSS pixels) to world coordinates via the camera matrix. */
-export function screenToWorld(world: EngineWorld, sx: number, sy: number): Point {
- const { a, b, c, d, e, f } = world.camera;
-
- const det = a * d - b * c;
- const inv = 1 / det;
-
- return {
- x: inv * (d * (sx - e) - c * (sy - f)),
- y: inv * (-b * (sx - e) + a * (sy - f)),
- };
-}
-
-/** Convert a world-space point (as produced by `screenToWorld`) to an entity's
- * local coordinates via its LocalTransform. Assumes the entity is top-level
- * (parent-less) so its LocalTransform is in world space. */
-export function worldToLocal(world: EngineWorld, eid: number, wx: number, wy: number): Point {
- const LocalTransform = world.components.LocalTransform;
-
- const a = LocalTransform.a[eid] ?? 1;
- const b = LocalTransform.b[eid] ?? 0;
- const c = LocalTransform.c[eid] ?? 0;
- const d = LocalTransform.d[eid] ?? 1;
- const e = LocalTransform.e[eid] ?? 0;
- const f = LocalTransform.f[eid] ?? 0;
-
- const det = a * d - b * c;
- const inv = 1 / det;
-
- return {
- x: inv * (d * (wx - e) - c * (wy - f)),
- y: inv * (-b * (wx - e) + a * (wy - f)),
- };
-}
-
-/** Find the first scene entity whose world-space bounds contain the given
- * world-space point (as produced by `screenToWorld`). */
-export function findSceneAt(world: EngineWorld, wx: number, wy: number): number | null {
- const c = world.components;
-
- for (const cid of query(world, [Not(ChildOf('*')), c.Geometry, Not(c.Deleted), Not(c.Culled), Not(c.Hidden)])) {
- if (!isScene(world, cid)) continue;
-
- const mat: Mat2D = {
- a: c.LocalTransform.a[cid] ?? 1,
- b: c.LocalTransform.b[cid] ?? 0,
- c: c.LocalTransform.c[cid] ?? 0,
- d: c.LocalTransform.d[cid] ?? 1,
- e: c.LocalTransform.e[cid] ?? 0,
- f: c.LocalTransform.f[cid] ?? 0,
- };
- const bounds = aabbFromTransformedRect(
- mat,
- c.Computed.width[cid],
- c.Computed.height[cid],
- );
-
- if (wx >= bounds.minX && wx <= bounds.maxX && wy >= bounds.minY && wy <= bounds.maxY) {
- return cid;
- }
- }
-
- return null;
-}
-
-export function isPointerInEntity(world: EngineWorld, eid: number, point: Point) {
- const c = world.components;
- const minX = c.WorldBounds.minX[eid];
- const minY = c.WorldBounds.minY[eid];
- const maxX = c.WorldBounds.maxX[eid];
- const maxY = c.WorldBounds.maxY[eid];
-
- const x = point.x;
- const y = point.y;
-
- if (x < minX || x > maxX || y < minY || y > maxY) return false;
-
- // Precise test: map pointer into entity-local space so rotation/skew
- // don't produce false positives along the AABB's outer slack.
- const world2D: Mat2D = {
- a: c.WorldTransform.a[eid], b: c.WorldTransform.b[eid],
- c: c.WorldTransform.c[eid], d: c.WorldTransform.d[eid],
- e: c.WorldTransform.e[eid], f: c.WorldTransform.f[eid],
- };
- const inv = invert2D(world2D);
- const local = transformPoint(inv, x, y);
- const w = c.Computed.width[eid];
- const h = c.Computed.height[eid];
- const ox = c.Computed.originX[eid];
- const oy = c.Computed.originY[eid];
-
- if (local.x >= ox && local.x <= ox + w && local.y >= oy && local.y <= oy + h) {
- return true;
- }
-
- return false;
-}
diff --git a/apps/web/src/components/engine/utils/cursor.ts b/apps/web/src/components/engine/utils/cursor.ts
deleted file mode 100644
index 08d2ded3..00000000
--- a/apps/web/src/components/engine/utils/cursor.ts
+++ /dev/null
@@ -1,70 +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 { MAIN_CANVAS_ID } from "@/components/canvas"
-import { assert } from "@/utils"
-
-const CORNER_SVG = `<path d='m19.7432 17.0869-4.072 4.068 2.829 2.828-8.473-.013-.013-8.47 2.841 2.842 4.075-4.068 1.414-1.415-2.844-2.842h8.486v8.484l-2.83-2.827z' fill='%23fff'/><path d='m18.6826 16.7334-4.427 4.424 1.828 1.828-5.056-.016-.014-5.054 1.842 1.841 4.428-4.422 2.474-2.475-1.844-1.843h5.073v5.071l-1.83-1.828z' fill='%23000'/>`
-const EDGE_SVG = `<path d='m9 17.9907v.005l5.997 5.996.001-3.999h1.999 2.02v4l5.98-6.001-5.98-5.999.001 4.019-2.021.002h-2l.001-4.022zm1.411.003 3.587-3.588-.001 2.587h3.5 2.521v-2.585l3.565 3.586-3.564 3.585-.001-2.585h-2.521l-3.499-.001-.001 2.586z' fill='%23fff'/><path d='m17.4971 18.9932h2.521v2.586l3.565-3.586-3.565-3.585v2.605h-2.521-3.5v-2.607l-3.586 3.587 3.586 3.586v-2.587z' fill='%23000'/>`
-const ROTATE_SVG = `<path d="M22.4789 9.45728L25.9935 12.9942L22.4789 16.5283V14.1032C18.126 14.1502 14.6071 17.6737 14.5675 22.0283H17.05L13.513 25.543L9.97889 22.0283H12.5674C12.6071 16.5691 17.0214 12.1503 22.4789 12.1031L22.4789 9.45728Z" fill="black"/><path fill-rule="evenodd" clip-rule="evenodd" d="M21.4789 7.03223L27.4035 12.9945L21.4789 18.9521V15.1868C18.4798 15.6549 16.1113 18.0273 15.649 21.0284H19.475L13.5128 26.953L7.55519 21.0284H11.6189C12.1243 15.8155 16.2679 11.6677 21.4789 11.1559L21.4789 7.03223ZM22.4789 12.1031C17.0214 12.1503 12.6071 16.5691 12.5674 22.0284H9.97889L13.513 25.543L17.05 22.0284H14.5675C14.5705 21.6896 14.5947 21.3558 14.6386 21.0284C15.1157 17.4741 17.9266 14.6592 21.4789 14.1761C21.8063 14.1316 22.1401 14.1069 22.4789 14.1032V16.5284L25.9935 12.9942L22.4789 9.45729L22.4789 12.1031Z" fill="white"/>`
-
-const TRIM_SINGLE_PATH_W = `M11.8787 2C12.7072 2 13.3787 2.67157 13.3787 3.5V18.5C13.3787 19.3284 12.7072 20 11.8787 20C11.0504 19.9999 10.3787 19.3284 10.3787 18.5V12.1572H8.12093L9.09554 13.1318C9.68134 13.7176 9.68128 14.6672 9.09554 15.2529V15.2539C8.51037 15.838 7.56062 15.8391 6.97445 15.2529L3.60921 11.8877C3.58251 11.8609 3.56103 11.8314 3.5389 11.8057C3.50719 11.7796 3.47273 11.7512 3.43929 11.7178C2.85306 11.1315 2.8544 10.1817 3.43832 9.59668L6.97445 6.06055C7.56013 5.47494 8.50978 5.47528 9.09554 6.06055L9.19906 6.1748C9.67956 6.76396 9.64471 7.63345 9.09554 8.18262L8.12093 9.15723H10.3787V3.5C10.3787 2.67161 11.0504 2.00006 11.8787 2ZM11.8787 3C11.6027 3.00006 11.3787 3.22389 11.3787 3.5V9.54004C11.6495 9.78246 11.8319 10.1212 11.8709 10.5029L11.8787 10.6572C11.8785 11.1008 11.685 11.4987 11.3787 11.7734V18.5L11.3885 18.6006C11.435 18.8285 11.6371 19 11.8787 19C12.1549 19 12.3787 18.7761 12.3787 18.5V3.5C12.3787 3.22386 12.1549 3 11.8787 3ZM8.38851 6.76758C8.19324 6.57276 7.87661 6.57252 7.68148 6.76758L4.14632 10.3037C3.95145 10.499 3.9512 10.8156 4.14632 11.0107C4.17397 11.0384 4.20533 11.0603 4.23714 11.0801C4.25992 11.1149 4.28563 11.15 4.31722 11.1816L7.68148 14.5459C7.87664 14.7411 8.19322 14.7408 8.38851 14.5459C8.58376 14.3506 8.58377 14.0341 8.38851 13.8389L5.70687 11.1572H10.3787V10.1572H5.70687L8.38851 7.47559C8.58377 7.28032 8.58377 6.96284 8.38851 6.76758Z`
-const TRIM_SINGLE_PATH_B = `M11.8785 3C12.1546 3 12.3785 3.22386 12.3785 3.5V18.5C12.3785 18.7761 12.1546 19 11.8785 19C11.6025 18.9998 11.3785 18.776 11.3785 18.5V11.1562H5.70757L8.38921 13.8389C8.5843 14.0341 8.58423 14.3507 8.38921 14.5459C8.19396 14.7409 7.87737 14.741 7.68217 14.5459L4.31694 11.1807C4.28531 11.149 4.25867 11.115 4.23589 11.0801C4.20439 11.0604 4.17441 11.0372 4.14702 11.0098C3.95186 10.8145 3.95179 10.498 4.14702 10.3027L7.68217 6.76758C7.87742 6.57256 8.194 6.57249 8.38921 6.76758C8.5844 6.96277 8.58426 7.27933 8.38921 7.47461L5.70659 10.1572H11.3785V3.5C11.3785 3.22399 11.6025 3.00021 11.8785 3Z`
-const TRIM_DOUBLE_PATH_W = `M14.8787 2C15.7072 2 16.3787 2.67157 16.3787 3.5V18.5C16.3787 19.3284 15.7072 20 14.8787 20C14.0504 19.9999 13.3787 19.3284 13.3787 18.5C13.3787 19.3284 12.7072 20 11.8787 20C11.0504 19.9999 10.3787 19.3284 10.3787 18.5V12.1572H8.12093L9.09554 13.1318C9.68134 13.7176 9.68128 14.6672 9.09554 15.2529V15.2539C8.51037 15.838 7.56062 15.8391 6.97445 15.2529L3.60921 11.8877C3.58251 11.8609 3.56103 11.8314 3.5389 11.8057C3.50719 11.7796 3.47273 11.7512 3.43929 11.7178C2.85306 11.1315 2.8544 10.1817 3.43832 9.59668L6.97445 6.06055C7.56013 5.47494 8.50978 5.47528 9.09554 6.06055L9.19906 6.1748C9.67956 6.76396 9.64471 7.63345 9.09554 8.18262L8.12093 9.15723H10.3787V3.5C10.3787 2.67161 11.0504 2.00006 11.8787 2C12.7072 2 13.3787 2.67157 13.3787 3.5C13.3787 2.67161 14.0504 2.00007 14.8787 2ZM11.8787 3C11.6027 3.00006 11.3787 3.22389 11.3787 3.5V9.54004C11.6495 9.78246 11.8319 10.1212 11.8709 10.5029L11.8787 10.6572C11.8785 11.1008 11.685 11.4987 11.3787 11.7734V18.5L11.3885 18.6006C11.435 18.8285 11.6371 19 11.8787 19C12.1549 19 12.3787 18.7761 12.3787 18.5V3.5C12.3787 3.22386 12.1549 3 11.8787 3ZM14.8787 3C14.6027 3.00007 14.3787 3.2239 14.3787 3.5V18.5L14.3885 18.6006C14.435 18.8285 14.6371 18.9999 14.8787 19C15.1549 19 15.3787 18.7761 15.3787 18.5V3.5C15.3787 3.22386 15.1549 3 14.8787 3ZM8.38851 6.76758C8.19324 6.57276 7.87661 6.57252 7.68148 6.76758L4.14632 10.3037C3.95145 10.499 3.9512 10.8156 4.14632 11.0107C4.17397 11.0384 4.20533 11.0603 4.23714 11.0801C4.25992 11.1149 4.28563 11.15 4.31722 11.1816L7.68148 14.5459C7.87664 14.7411 8.19322 14.7408 8.38851 14.5459C8.58376 14.3506 8.58377 14.0341 8.38851 13.8389L5.70687 11.1572H10.3787V10.1572H5.70687L8.38851 7.47559C8.58377 7.28032 8.58377 6.96284 8.38851 6.76758Z`
-const TRIM_DOUBLE_PATH_B = `M11.8785 3C12.1546 3 12.3785 3.22386 12.3785 3.5V18.5C12.3785 18.7761 12.1546 19 11.8785 19C11.6025 18.9998 11.3785 18.776 11.3785 18.5V11.1562H5.70757L8.38921 13.8389C8.5843 14.0341 8.58423 14.3507 8.38921 14.5459C8.19396 14.7409 7.87737 14.741 7.68217 14.5459L4.31694 11.1807C4.28531 11.149 4.25867 11.115 4.23589 11.0801C4.20439 11.0604 4.17441 11.0372 4.14702 11.0098C3.95186 10.8145 3.95179 10.498 4.14702 10.3027L7.68217 6.76758C7.87742 6.57256 8.194 6.57249 8.38921 6.76758C8.5844 6.96277 8.58426 7.27933 8.38921 7.47461L5.70659 10.1572H11.3785V3.5C11.3785 3.22398 11.6025 3.0002 11.8785 3ZM14.8785 3C15.1546 3 15.3785 3.22386 15.3785 3.5V18.5C15.3785 18.7761 15.1546 19 14.8785 19C14.6025 18.9998 14.3785 18.776 14.3785 18.5V3.5C14.3785 3.22398 14.6025 3.0002 14.8785 3Z`
-
-// Position the bar (clip edge marker) at the cursor hotspot (16, 16). Bar in the
-// source SVG is centered at x≈11.85 — translate by 4.15 in X to align it with the
-// hotspot. RIGHT variant scales -1 horizontally and translates so the mirrored bar
-// also lands on the hotspot. DOUBLE places the midpoint between the two bars on it.
-const TRIM_LEFT_SVG = `<g transform='translate(4.15 4)'><path d='${TRIM_SINGLE_PATH_W}' fill='%23fff'/><path d='${TRIM_SINGLE_PATH_B}' fill='%23000'/></g>`
-const TRIM_RIGHT_SVG = `<g transform='translate(27.85 4) scale(-1 1)'><path d='${TRIM_SINGLE_PATH_W}' fill='%23fff'/><path d='${TRIM_SINGLE_PATH_B}' fill='%23000'/></g>`
-const TRIM_DOUBLE_SVG = `<g transform='translate(2.65 4)'><path d='${TRIM_DOUBLE_PATH_W}' fill='%23fff'/><path d='${TRIM_DOUBLE_PATH_B}' fill='%23000'/></g>`
-
-const STATIC_CURSORS = new Set(['default', 'pointer', 'cross', 'move', 'grab', 'grabbing', 'text', 'zoom-in', 'zoom-out'])
-
-function getCursorCss(svg: string, r: number, tr: number, color: string, hx = 16, hy = 16) {
- const a = (-tr - r) * (Math.PI / 180)
- const dx = Math.cos(a) - Math.sin(a)
- const dy = Math.sin(a) + Math.cos(a)
- return `url("data:image/svg+xml,<svg height='32' width='32' viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg' style='color: ${color};'><defs><filter id='shadow' y='-40%' x='-40%' width='180px' height='180%' color-interpolation-filters='sRGB'><feDropShadow dx='${dx}' dy='${dy}' stdDeviation='1.2' flood-opacity='.5'/></filter></defs><g fill='none' transform='rotate(${r + tr} 16 16)' filter='url(%23shadow)'>${svg.replaceAll(`"`, `'`)}</g></svg>") ${hx} ${hy}, pointer`
-}
-
-const CURSORS: Record<string, (r: number, c: string) => string> = {
- 'ew-resize': (r, c) => getCursorCss(EDGE_SVG, r, 0, c),
- 'ns-resize': (r, c) => getCursorCss(EDGE_SVG, r, 90, c),
- 'nesw-resize': (r, c) => getCursorCss(CORNER_SVG, r, 0, c),
- 'nwse-resize': (r, c) => getCursorCss(CORNER_SVG, r, 90, c),
- 'nwse-rotate': (r, c) => getCursorCss(ROTATE_SVG, r, 0, c),
- 'nesw-rotate': (r, c) => getCursorCss(ROTATE_SVG, r, 90, c),
- 'senw-rotate': (r, c) => getCursorCss(ROTATE_SVG, r, 180, c),
- 'swne-rotate': (r, c) => getCursorCss(ROTATE_SVG, r, 270, c),
- 'trim-left': (_r, c) => getCursorCss(TRIM_LEFT_SVG, 0, 0, c),
- 'trim-right': (_r, c) => getCursorCss(TRIM_RIGHT_SVG, 0, 0, c),
- 'trim-both': (_r, c) => getCursorCss(TRIM_DOUBLE_SVG, 0, 0, c),
-}
-
-export type CursorType = 'none' | 'default' | 'pointer' | 'cross' | 'grab' | 'rotate' | 'grabbing' | 'resize-edge' | 'resize-corner' | 'text' | 'move' | 'ew-resize' | 'ns-resize' | 'nesw-resize' | 'nwse-resize' | 'nesw-rotate' | 'nwse-rotate' | 'swne-rotate' | 'senw-rotate' | 'zoom-in' | 'zoom-out' | 'trim-left' | 'trim-right' | 'trim-both';
-
-let currentCursor: string | null = null;
-
-/**
- * Sets the cursor on the main canvas element.
- */
-export function updateCursor(type: CursorType, rotation = 0, color = 'black') {
- const hash = `${type}-${rotation}-${color}`
- if (currentCursor === hash) return;
- currentCursor = hash;
-
- const el = document.getElementById(MAIN_CANVAS_ID);
- assert(el, 'Main canvas not found');
-
- el.style.cursor = 'var(--tl-cursor)'
-
- const cursor = STATIC_CURSORS.has(type)
- ? type
- : CURSORS[type]?.(rotation * 180 / Math.PI, color) ?? 'default'
-
- el.style.setProperty('--tl-cursor', cursor)
-}
diff --git a/apps/web/src/components/engine/utils/history.ts b/apps/web/src/components/engine/utils/history.ts
deleted file mode 100644
index d120abe2..00000000
--- a/apps/web/src/components/engine/utils/history.ts
+++ /dev/null
@@ -1,256 +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 { createSignal } from "solid-js";
-
-export type HistoryEntry = {
- label: string;
- execute: () => void;
- reverse: () => void;
- /** Called after execute and reverse to enqueue persistence. */
- finalize?: () => void;
- /** Called when the entry is permanently evicted from history and can never be undone/redone. */
- dispose?: () => void;
-};
-
-export type HistoryTransaction = {
- label: string;
- entries: HistoryEntry[];
-};
-
-export type History = ReturnType<typeof createHistory>;
-
-function disposeTransaction(tx: HistoryTransaction): void {
- for (const entry of tx.entries) {
- entry.dispose?.();
- }
-}
-
-export function createHistory(maxSize = 100) {
- const undoStack: HistoryTransaction[] = [];
- const redoStack: HistoryTransaction[] = [];
- const [dirty, setDirty] = createSignal(0);
-
- let activeTransaction: HistoryTransaction | null = null;
- // Nesting depth: an inner start/commit pair appends to the outermost
- // transaction and only the outermost commit flushes it to the stack.
- let txDepth = 0;
- let untracked = false;
-
- /**
- * Push and immediately execute a history entry.
- * If a transaction is active the entry is appended to it;
- * otherwise it becomes its own single-entry transaction.
- */
- function push(entry: HistoryEntry): void {
- entry.execute();
- entry.finalize?.();
-
- if (untracked) return;
-
- if (activeTransaction) {
- activeTransaction.entries.push(entry);
- return;
- }
-
- undoStack.push({ label: entry.label, entries: [entry] });
- if (undoStack.length > maxSize) {
- const evicted = undoStack.shift();
- if (evicted) {
- disposeTransaction(evicted);
- }
- }
-
- // Any new action kills the redo branch
- for (const tx of redoStack) {
- disposeTransaction(tx);
- }
- redoStack.length = 0;
- setDirty(d => d + 1);
- }
-
- /**
- * Begin a transaction. All entries pushed until `commit()` are
- * grouped as one undoable step.
- */
- function startTransaction(label: string): void {
- if (activeTransaction) {
- // Nested: keep appending to the outer one; track depth so the
- // matching inner commit doesn't flush the outer transaction early.
- txDepth++;
- return;
- }
- activeTransaction = { label, entries: [] };
- txDepth = 1;
- }
-
- /** Commit the active transaction onto the undo stack. */
- function commitTransaction(): void {
- if (!activeTransaction) return;
-
- // Unwind one nesting level; only the outermost commit flushes.
- if (txDepth > 1) {
- txDepth--;
- return;
- }
-
- if (activeTransaction.entries.length > 0) {
- undoStack.push(activeTransaction);
- if (undoStack.length > maxSize) {
- const evicted = undoStack.shift();
- if (evicted) {
- disposeTransaction(evicted);
- }
- }
- for (const tx of redoStack) {
- disposeTransaction(tx);
- }
- redoStack.length = 0;
- }
-
- activeTransaction = null;
- txDepth = 0;
- setDirty(d => d + 1);
- }
-
- /** Discard the active transaction and reverse all its entries. */
- function rollbackTransaction(): void {
- if (!activeTransaction) return;
-
- const tx = activeTransaction;
- // Reverse in LIFO order. Suppress recording so observer reactions fired
- // inside reverse() don't push fresh entries.
- withUntracked(() => {
- for (let i = tx.entries.length - 1; i >= 0; i--) {
- tx.entries[i]?.reverse();
- tx.entries[i]?.finalize?.();
- }
- });
-
- activeTransaction = null;
- txDepth = 0;
- setDirty(d => d + 1);
- }
-
- /**
- * Callback-style transaction: wraps `fn` in start/commit.
- * Rolls back automatically if `fn` throws.
- */
- function transaction<T>(label: string, fn: () => T): T {
- startTransaction(label);
- try {
- const result = fn();
- commitTransaction();
- return result;
- } catch (e) {
- rollbackTransaction();
- throw e;
- }
- }
-
- /** Undo the most recent action or transaction. */
- function undo(): void {
- const tx = undoStack.pop();
- if (!tx) return;
-
- // The recorded entries already replay the change. Observers fired inside
- // reverse() may call the primitives again — suppress recording so they
- // don't push new entries onto the stacks mid-undo.
- withUntracked(() => {
- for (let i = tx.entries.length - 1; i >= 0; i--) {
- tx.entries[i]?.reverse();
- tx.entries[i]?.finalize?.();
- }
- });
-
- redoStack.push(tx);
- setDirty(d => d + 1);
- }
-
- /** Redo the most recently undone action or transaction. */
- function redo(): void {
- const tx = redoStack.pop();
- if (!tx) return;
-
- withUntracked(() => {
- for (const entry of tx.entries) {
- entry.execute();
- entry.finalize?.();
- }
- });
-
- undoStack.push(tx);
- setDirty(d => d + 1);
- }
-
- /** Clear all history. */
- function clear(): void {
- for (const tx of undoStack) {
- disposeTransaction(tx);
- }
- for (const tx of redoStack) {
- disposeTransaction(tx);
- }
- undoStack.length = 0;
- redoStack.length = 0;
- activeTransaction = null;
- txDepth = 0;
- setDirty(d => d + 1);
- }
-
- /**
- * Run `fn` with recording suppressed, restoring the previous flag after.
- * Save/restore (rather than force-off) so nested calls — e.g. an ephemeral
- * push inside an undo replay — don't re-enable recording mid-operation.
- */
- function withUntracked<T>(fn: () => T): T {
- const prev = untracked;
- untracked = true;
- try {
- return fn();
- } finally {
- untracked = prev;
- }
- }
-
- /**
- * Execute `fn` without recording any history entries.
- * Calls inside still execute and finalize, but are not undoable.
- */
- function untrack(fn: () => void): void {
- withUntracked(fn);
- }
-
- return {
- push,
- untrack,
- startTransaction,
- commitTransaction,
- rollbackTransaction,
- transaction,
- undo,
- redo,
- clear,
- get canUndo() {
- dirty();
- return undoStack.length > 0;
- },
- get canRedo() {
- dirty();
- return redoStack.length > 0;
- },
- get undoCount() {
- dirty();
- return undoStack.length;
- },
- get redoCount() {
- dirty();
- return redoStack.length;
- },
- get isTransacting() {
- dirty();
- return activeTransaction !== null;
- },
- };
-}
diff --git a/apps/web/src/components/engine/utils/index.ts b/apps/web/src/components/engine/utils/index.ts
deleted file mode 100644
index a3124219..00000000
--- a/apps/web/src/components/engine/utils/index.ts
+++ /dev/null
@@ -1,14 +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/. */
-
-export * from './coordinates';
-export * from './history';
-export * from './live-mounts';
-export * from './time';
-export * from './sort';
-export * from './cursor';
-export * from './transcode';
-export * from './contact-sheet';
-export * from './audio-sync';
-export * from './captions';
diff --git a/apps/web/src/components/engine/utils/interaction.ts b/apps/web/src/components/engine/utils/interaction.ts
deleted file mode 100644
index 7a40dafd..00000000
--- a/apps/web/src/components/engine/utils/interaction.ts
+++ /dev/null
@@ -1,431 +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 { hasComponent, Not, query, Or } from "bitecs";
-import { getParentEntity, setComponent, type EngineWorld } from "../api";
-import { decompose2D, identity2D, multiply2D, quadCenter, rotate2D, scale2D, skew2D, translate2D, type Mat2D } from "../math";
-import { rectToQuad, type Point, type Quad } from "../math";
-import { ChildOf } from "../components";
-import { assert } from "@/utils";
-
-import type { CursorType } from "./cursor";
-
-type NodeState = {
- localTransform: Mat2D;
- parentTransform: Mat2D;
- height: number;
- width: number;
- scaleX: number;
- scaleY: number;
-}
-
-const selectionTransformSnapshot: Map<number, NodeState> = new Map();
-export function snapshotSelectionTransforms(world: EngineWorld) {
- const c = world.components;
- selectionTransformSnapshot.clear();
-
- const snapshot = (eid: number) => {
- if (selectionTransformSnapshot.has(eid)) return;
- const parentEid = getParentEntity(world, eid);
- selectionTransformSnapshot.set(eid, {
- localTransform: entityLocalMat(world, eid),
- parentTransform: entityWorldMat(world, parentEid),
- width: c.Computed.width[eid],
- height: c.Computed.height[eid],
- scaleX: c.Scale.x[eid] ?? 1,
- scaleY: c.Scale.y[eid] ?? 1,
- });
-
- if (hasComponent(world, eid, c.Group)) {
- for (const child of query(world, [ChildOf(eid), Or(c.Geometry, c.Group), Not(c.Deleted)])) {
- snapshot(child);
- }
- }
- };
-
- // Store initial node states (local transforms and parent transforms).
- // Groups have no Size of their own, so we also snapshot their direct
- // children — those are the entities a group-resize actually transforms.
- for (const eid of query(world, [c.Selected, Or(c.Geometry, c.Group)])) {
- snapshot(eid);
- }
-}
-
-export function getSelectionTransfromSnapshot(id: number) {
- return selectionTransformSnapshot.get(id);
-}
-
-type TransformMaskState = {
- width: number;
- height: number;
- mat: Mat2D;
-}
-
-export function getSelectionMask(world: EngineWorld): TransformMaskState | null {
- const c = world.components;
- const selection = query(world, [c.Selected, Or(c.Geometry, c.Group), Not(c.Sequential)]);
- if (selection.length === 0) return null;
-
- let width: number = 0;
- let height: number = 0;
- let mat = identity2D();
-
- if (selection.length === 1) {
- const eid = selection[0];
- const worldMat = multiply2D(entityWorldMat(world, eid), translate2D(
- c.Computed.originX[eid],
- c.Computed.originY[eid],
- ));
- const d = decompose2D(worldMat);
-
- width = c.Computed.width[eid] * Math.abs(d.scaleX);
- height = c.Computed.height[eid] * Math.abs(d.scaleY);
-
- const signX = Math.sign(d.scaleX) || 1;
- const signY = Math.sign(d.scaleY) || 1;
-
- mat = multiply2D(mat, translate2D(d.x, d.y));
- mat = multiply2D(mat, rotate2D(d.rotation));
- mat = multiply2D(mat, scale2D(signX, signY));
- mat = multiply2D(mat, skew2D(d.skewX, d.skewY));
- } else {
- let minX = Infinity, minY = Infinity;
- let maxX = -Infinity, maxY = -Infinity;
-
- for (const eid of selection) {
- const q = entityQuad(world, eid);
- for (const p of q) {
- if (p.x < minX) minX = p.x;
- if (p.y < minY) minY = p.y;
- if (p.x > maxX) maxX = p.x;
- if (p.y > maxY) maxY = p.y;
- }
- }
-
- width = maxX - minX;
- height = maxY - minY;
- mat = translate2D(minX, minY);
- }
-
- return { width, height, mat };
-}
-
-let transformStartMaskState: TransformMaskState | null = null;
-export function snapshotSelectionMask(world: EngineWorld) {
- transformStartMaskState = getSelectionMask(world);
-}
-
-export function getSelectionMaskSnapshot() {
- return transformStartMaskState;
-}
-
-export function updateRotationCursor(world: EngineWorld) {
- const state = getSelectionMask(world);
- if (state === null) return;
- const { mat, width, height } = state;
-
- // Mouse position in world space
- const mouseX = world.pointer.clientX;
- const mouseY = world.pointer.clientY;
-
- // Convert mask to quad and get center
- const quad = rectToQuad(mat, width, height);
- const centerX = (quad[0].x + quad[1].x + quad[2].x + quad[3].x) / 4;
- const centerY = (quad[0].y + quad[1].y + quad[2].y + quad[3].y) / 4;
-
- // Calculate angle from mouse to center
- const angle = Math.atan2(centerY - mouseY, centerX - mouseX);
-
- world.cursor.update('nwse-rotate', angle - Math.PI / 4);
-}
-
-const HANDLE_RESIZE_CURSOR: Record<string, CursorType> = {
- tr: 'nesw-resize', br: 'nwse-resize',
- bl: 'nesw-resize', tl: 'nwse-resize',
- t: 'ns-resize', r: 'ew-resize',
- b: 'ns-resize', l: 'ew-resize',
-};
-
-export function updateResizeCursor(world: EngineWorld, orientation: string) {
- const state = getSelectionMask(world);
- if (state === null) return;
- const { a, b } = state.mat;
- const rotation = Math.atan2(b, a);
-
- world.cursor.update(HANDLE_RESIZE_CURSOR[orientation], rotation);
-}
-
-type SnapAxisCandidate = { value: number; point: Point };
-type SnapCandidates = { x: SnapAxisCandidate[]; y: SnapAxisCandidate[] };
-
-const snapCandidatesSnapshot: SnapCandidates = { x: [], y: [] };
-export function snapshotSnapCandidates(world: EngineWorld) {
- snapCandidatesSnapshot.x.length = 0;
- snapCandidatesSnapshot.y.length = 0;
-
- const seen = new Set<string>();
- const c = world.components;
- const selection = new Set<number>(query(world, [c.Selected, Or(c.Geometry, c.Group)]));
-
- const addNodeCandidates = (eid: number) => {
- if (c.Computed.visibility[eid] === 0) return;
-
- const quad = entityQuad(world, eid);
- const nodeCandidates = buildSnapCandidatesFromQuad(quad);
- const axisCandidates = [
- ...nodeCandidates.x.map(candidate => ({ axis: 'x' as const, candidate })),
- ...nodeCandidates.y.map(candidate => ({ axis: 'y' as const, candidate })),
- ];
-
- for (const { axis, candidate } of axisCandidates) {
- const key = axis === 'x'
- ? `${axis}:${Math.round(candidate.value * 100) / 100}:${Math.round(candidate.point.y * 100) / 100}`
- : `${axis}:${Math.round(candidate.value * 100) / 100}:${Math.round(candidate.point.x * 100) / 100}`;
- if (seen.has(key)) continue;
- seen.add(key);
-
- if (axis === 'x') {
- snapCandidatesSnapshot.x.push(candidate);
- } else {
- snapCandidatesSnapshot.y.push(candidate);
- }
- }
- };
-
- let parentEid: number | null = null;
- if (selection.size === 1) {
- parentEid = getParentEntity(world, selection.values().next().value);
- }
-
- let parentComponent = Not(ChildOf('*'));
- if (parentEid) {
- parentComponent = ChildOf(parentEid);
- }
-
- const siblings = [
- ...query(world, [
- parentComponent,
- Or(c.Geometry, c.Group),
- Not(c.Deleted),
- Not(c.Culled),
- Not(c.Hidden),
- ])
- ];
-
- for (const childEid of siblings) {
- if (selection.has(childEid)) continue;
- addNodeCandidates(childEid);
- }
-
- if (parentEid !== null && !selection.has(parentEid)) {
- addNodeCandidates(parentEid);
- }
-}
-
-export function getSnapCandidatesSnapshot(): SnapCandidates {
- return snapCandidatesSnapshot;
-}
-
-const LABEL_INPUT_STYLE = {
- position: 'absolute',
- transformOrigin: 'left top',
- font: '350 11px Inter, sans-serif',
- color: 'var(--foreground)',
- background: 'var(--input)',
- border: '1px solid var(--primary)',
- borderRadius: '2px',
- outline: 'none',
- padding: '0 0 0 2px',
- margin: '0',
- boxSizing: 'content-box',
- zIndex: '1000',
-}
-
-let mountedNameInput: HTMLInputElement | null = null;
-let mountedNameInputEid: number | null = null;
-
-export function mountNameInput(world: EngineWorld, eid: number) {
- const canvas = world.canvas;
- assert(canvas instanceof HTMLCanvasElement, 'Canvas must be an HTMLCanvasElement');
- const container = canvas.parentElement;
- const c = world.components;
- if (!container) return;
-
- const originalName = c.Name[eid] ?? '';
- world.history.startTransaction('Edit label');
-
- const labelInput = document.createElement('input');
- labelInput.type = 'text';
- labelInput.value = originalName;
-
- Object.assign(labelInput.style, LABEL_INPUT_STYLE);
-
- labelInput.addEventListener('input', (e) => {
- const input = e.currentTarget as HTMLInputElement;
- setComponent(world, eid, c.Name, input.value ?? '');
- });
-
- labelInput.addEventListener('blur', (e) => {
- const input = e.currentTarget as HTMLInputElement;
- input.remove();
-
- if (!input.value.trim()) {
- setComponent(world, eid, c.Name, originalName);
- }
-
- world.history.commitTransaction();
- mountedNameInput = null;
- mountedNameInputEid = null;
- });
-
- labelInput.addEventListener('keydown', (e) => {
- e.stopPropagation();
- const input = e.currentTarget as HTMLInputElement;
-
- if (e.key === 'Escape') {
- setComponent(world, eid, c.Name, originalName);
- input.blur();
- }
-
- if (e.key === 'Enter') {
- input.blur();
- }
- });
-
- container.appendChild(labelInput);
- labelInput.focus();
- labelInput.select();
-
- mountedNameInput = labelInput;
- mountedNameInputEid = eid;
-};
-
-export function getMountedNameInput() {
- return { input: mountedNameInput, eid: mountedNameInputEid };
-}
-
-export function findSnapTarget(source: SnapAxisCandidate[], targets: SnapAxisCandidate[], threshold: number, axis: 'x' | 'y') {
- let best: {
- offset: number;
- distance: number;
- sourcePoint: Point;
- targetPoint: Point;
- } | null = null;
-
- for (const sourceCandidate of source) {
- for (const target of targets) {
- const offset = target.value - sourceCandidate.value;
- const distance = Math.abs(offset);
- const crossDistance = axis === 'x'
- ? Math.abs(target.point.y - sourceCandidate.point.y)
- : Math.abs(target.point.x - sourceCandidate.point.x);
-
- if (distance > threshold) continue;
- if (!best || distance < best.distance || (distance === best.distance && crossDistance < (axis === 'x'
- ? Math.abs(best.targetPoint.y - best.sourcePoint.y)
- : Math.abs(best.targetPoint.x - best.sourcePoint.x)))) {
- best = {
- offset,
- distance,
- sourcePoint: sourceCandidate.point,
- targetPoint: target.point,
- };
- }
- }
- }
-
- return best;
-};
-
-export function getMarqueeQuad(world: EngineWorld): Quad | null {
- if (world.hudMode !== 'marquee') return null;
-
- const x0 = world.pointer.dragStartX;
- const y0 = world.pointer.dragStartY;
- const x1 = world.pointer.clientX;
- const y1 = world.pointer.clientY;
-
- const minX = Math.min(x0, x1);
- const minY = Math.min(y0, y1);
- const maxX = Math.max(x0, x1);
- const maxY = Math.max(y0, y1);
-
- return [
- { x: minX, y: minY },
- { x: maxX, y: minY },
- { x: maxX, y: maxY },
- { x: minX, y: maxY },
- ];
-}
-
-export function buildSnapCandidatesFromQuad(quad: Quad): { x: SnapAxisCandidate[], y: SnapAxisCandidate[] } {
- const center = quadCenter(quad);
- const corners = quad;
- const points: Point[] = [...corners, center];
-
- return {
- x: points.map(point => ({ value: point.x, point })),
- y: points.map(point => ({ value: point.y, point })),
- };
-}
-
-export function buildSnapCandidatesFromCorners(points: Point[]): { x: SnapAxisCandidate[], y: SnapAxisCandidate[] } {
- return {
- x: points.map(point => ({ value: point.x, point })),
- y: points.map(point => ({ value: point.y, point })),
- };
-}
-
-export function entityWorldMat(world: EngineWorld, eid: number | null): Mat2D {
- if (eid === null) {
- const res = world.resolution;
- return {
- a: world.camera.a * res, b: world.camera.b * res,
- c: world.camera.c * res, d: world.camera.d * res,
- e: world.camera.e * res, f: world.camera.f * res,
- };
- }
-
- const WorldTransform = world.components.WorldTransform;
- return {
- a: WorldTransform.a[eid] ?? 1, b: WorldTransform.b[eid] ?? 0,
- c: WorldTransform.c[eid] ?? 0, d: WorldTransform.d[eid] ?? 1,
- e: WorldTransform.e[eid] ?? 0, f: WorldTransform.f[eid] ?? 0,
- };
-}
-
-export function entityLocalMat(world: EngineWorld, eid: number): Mat2D {
- const LocalTransform = world.components.LocalTransform;
- return {
- a: LocalTransform.a[eid] ?? 1, b: LocalTransform.b[eid] ?? 0,
- c: LocalTransform.c[eid] ?? 0, d: LocalTransform.d[eid] ?? 1,
- e: LocalTransform.e[eid] ?? 0, f: LocalTransform.f[eid] ?? 0,
- };
-}
-
-export function entityOffset(world: EngineWorld, eid: number): Point {
- const Offset = world.components.Offset;
- return {
- x: Offset.x[eid] ?? 0,
- y: Offset.y[eid] ?? 0,
- };
-}
-
-export function entityAnchor(world: EngineWorld, eid: number): Point {
- const Anchor = world.components.Anchor;
- return {
- x: Anchor.x[eid] ?? 0.5,
- y: Anchor.y[eid] ?? 0.5,
- };
-}
-
-export function entityQuad(world: EngineWorld, eid: number): Quad {
- const Computed = world.components.Computed;
- const width = Computed.width[eid];
- const height = Computed.height[eid];
-
- const mat = multiply2D(entityWorldMat(world, eid), translate2D(Computed.originX[eid], Computed.originY[eid]));
-
- return rectToQuad(mat, width, height);
-}
diff --git a/apps/web/src/components/engine/utils/live-mounts.ts b/apps/web/src/components/engine/utils/live-mounts.ts
deleted file mode 100644
index 7caadf68..00000000
--- a/apps/web/src/components/engine/utils/live-mounts.ts
+++ /dev/null
@@ -1,54 +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/. */
-
-/**
- * JSX mounts keep their Solid reactive graph running after
- * the mount lands, so signals, effects, and timers keep driving the mounted
- * entities. A run stays alive until the world is
- * disposed or a later mount claims one of its root keys: the keyed root
- * replacement already deletes its entities, so the superseded graph is
- * disposed with them. Root keys are read live rather than snapshotted, so
- * roots a running graph adds or removes later still reconcile.
- */
-export type LiveMount = {
- /** Identity shared by every entity of this mount; unique per running graph. */
- mountId: string;
- rootKeys: () => string[];
- advance: () => void;
- dispose: () => void;
-};
-
-export type LiveMounts = ReturnType<typeof createLiveMounts>;
-
-export function createLiveMounts() {
- const mounts = new Set<LiveMount>();
- return {
- advance(): void {
- for (const mount of mounts) {
- mount.advance();
- }
- },
- hasMount(mountId: string): boolean {
- for (const mount of mounts) {
- if (mount.mountId === mountId) return true;
- }
- return false;
- },
- supersede(keys: string[]): void {
- for (const mount of mounts) {
- if (keys.some((key) => mount.rootKeys().includes(key))) {
- mount.dispose();
- mounts.delete(mount);
- }
- }
- },
- register(mount: LiveMount): void {
- mounts.add(mount);
- },
- disposeAll(): void {
- for (const mount of mounts) mount.dispose();
- mounts.clear();
- },
- };
-}
diff --git a/apps/web/src/components/engine/utils/sort.ts b/apps/web/src/components/engine/utils/sort.ts
deleted file mode 100644
index 517f751e..00000000
--- a/apps/web/src/components/engine/utils/sort.ts
+++ /dev/null
@@ -1,25 +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 type { EngineWorld } from "../api/world";
-
-export function sortByItemIndex(world: EngineWorld) {
- const ItemIndex = world.components.ItemIndex;
- return (a: number, b: number) => (ItemIndex[a] ?? 0) - (ItemIndex[b] ?? 0);
-}
-
-export function sortByStartTime(world: EngineWorld) {
- const Computed = world.components.Computed;
- return (a: number, b: number) => (Computed.start[a]) - (Computed.start[b]);
-}
-
-export function sortByOffset(world: EngineWorld) {
- const Computed = world.components.Computed;
- return (a: number, b: number) => Computed.stopOffset[a] - Computed.stopOffset[b];
-}
-
-export function sortByFrame(world: EngineWorld) {
- const Keyframe = world.components.Keyframe;
- return (a: number, b: number) => Keyframe.time[a] - Keyframe.time[b];
-}
diff --git a/apps/web/src/components/engine/utils/text-motion.ts b/apps/web/src/components/engine/utils/text-motion.ts
deleted file mode 100644
index 0d35f566..00000000
--- a/apps/web/src/components/engine/utils/text-motion.ts
+++ /dev/null
@@ -1,246 +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/. */
-
-// Text animation helpers used by the motion system.
-// All functions take the target text and a normalized ratio (0..1), returning
-// the partial display string for that progress.
-
-export function revealWords(chars: string, ratio: number): string {
- if (ratio >= 1) return chars;
- if (ratio <= 0) return '';
- const words = chars.split(/(\s+)/);
- const wordTokens = words.filter((_, i) => i % 2 === 0);
- const visibleCount = Math.floor(ratio * wordTokens.length);
- if (visibleCount <= 0) return '';
- let out = '';
- let shown = 0;
- for (let i = 0; i < words.length; i++) {
- if (i % 2 === 0) {
- if (shown >= visibleCount) break;
- out += words[i];
- shown++;
- } else {
- if (shown >= visibleCount) break;
- out += words[i];
- }
- }
- return out;
-}
-
-export function revealChars(chars: string, ratio: number): string {
- if (ratio >= 1) return chars;
- if (ratio <= 0) return '';
- const visibleCount = Math.floor(ratio * chars.length);
- return chars.slice(0, visibleCount);
-}
-
-// ─── Scramble Text ──────────────────────────────────────────
-// Adapted from anime.js scrambleText (src/text/scramble.js).
-// The motion system has no DOM target, prevTween, or wall-clock duration —
-// progress is the normalized ratio (0 = blank, 1 = settled). Real-time
-// concepts (revealRate/interval/settleDuration) collapse onto SCRAMBLE_SETTLE_RATIO
-// (per-char active window) and SCRAMBLE_SETTLE_RATE (refreshes across the animation).
-// Structural arrays are cached per text; scramble character lookups are stateless,
-// keyed by (seed, position, step) so timeline scrubbing stays deterministic.
-
-const SCRAMBLE_CHARSETS: Record<string, string> = {
- lowercase: 'a-z',
- uppercase: 'A-Z',
- numbers: '0-9',
- symbols: '!%#_|*+=',
- braille: '⠀-⣿',
- blocks: '▀-▟',
- shades: '░-▓',
-};
-
-const SCRAMBLE_DEFAULT_CHARS = 'a-zA-Z0-9!%#_';
-const SCRAMBLE_SETTLE_RATIO = 0.3;
-const SCRAMBLE_SETTLE_RATE = 30;
-const SCRAMBLE_FROM: 'left' | 'right' | 'center' | 'random' | number = 'left';
-const SCRAMBLE_REVERSED = false;
-const SCRAMBLE_PERTURBATION = 0;
-const SCRAMBLE_CURSOR = '';
-const SCRAMBLE_CACHE_LIMIT = 64;
-
-type ScrambleState = {
- animLength: number;
- endLength: number;
- characters: string;
- cursorChars: string;
- cursorLen: number;
- cursorZone: number;
- settleSpacing: number;
- stepRatio: number;
- charStarts: Float32Array;
- charEnds: Float32Array;
- seed: number;
-};
-
-const scrambleStateCache = new Map<string, ScrambleState>();
-
-function expandCharRanges(str: string): string {
- let result = '';
- for (let i = 0, l = str.length; i < l; i++) {
- if (i + 2 < l && str[i + 1] === '-' && str.charCodeAt(i) < str.charCodeAt(i + 2)) {
- const start = str.charCodeAt(i);
- const end = str.charCodeAt(i + 2);
- for (let c = start; c <= end; c++) result += String.fromCharCode(c);
- i += 2;
- } else {
- result += str[i];
- }
- }
- return result;
-}
-
-function hashTextToSeed(text: string): number {
- let h = 2166136261;
- for (let i = 0; i < text.length; i++) {
- h ^= text.charCodeAt(i);
- h = Math.imul(h, 16777619);
- }
- return h >>> 0;
-}
-
-function createSeededRandom(seed: number): (min: number, max: number) => number {
- let state = (seed >>> 0) || 1;
- return (min, max) => {
- state = (state + 0x6d2b79f5) | 0;
- let t = Math.imul(state ^ (state >>> 15), 1 | state);
- t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
- const r = ((t ^ (t >>> 14)) >>> 0) / 4294967296;
- return Math.floor(r * (max - min + 1)) + min;
- };
-}
-
-function pickScrambleChar(seed: number, position: number, step: number, charset: string): string {
- let h = (seed ^ Math.imul(position + 1, 0x9e3779b1) ^ Math.imul(step + 1, 0x85ebca6b)) >>> 0;
- h = Math.imul(h ^ (h >>> 16), 0x85ebca6b);
- h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35);
- h ^= h >>> 16;
- return charset[(h >>> 0) % charset.length];
-}
-
-function buildScrambleState(text: string): ScrambleState {
- const characters = expandCharRanges(SCRAMBLE_CHARSETS[SCRAMBLE_DEFAULT_CHARS] ?? SCRAMBLE_DEFAULT_CHARS);
- const cursorChars = SCRAMBLE_CURSOR;
- const cursorLen = cursorChars.length;
-
- const seed = hashTextToSeed(text);
- const rng = createSeededRandom(seed);
-
- const animLength = text.length;
- const endLength = text.length;
-
- const settleRatio = SCRAMBLE_SETTLE_RATIO;
- const settleSpacing = animLength > 0 ? (1 - settleRatio) / animLength : 0;
- const cursorZone = cursorLen * settleSpacing;
- const stepRatio = 1 / SCRAMBLE_SETTLE_RATE;
-
- const charOrder = new Int32Array(animLength);
- if (SCRAMBLE_FROM === 'random') {
- for (let i = 0; i < animLength; i++) charOrder[i] = i;
- for (let i = animLength - 1; i > 0; i--) {
- const j = rng(0, i);
- const tmp = charOrder[i];
- charOrder[i] = charOrder[j];
- charOrder[j] = tmp;
- }
- } else {
- const ref = SCRAMBLE_FROM === 'right' ? animLength - 1
- : SCRAMBLE_FROM === 'center' ? (animLength - 1) / 2
- : typeof SCRAMBLE_FROM === 'number' ? SCRAMBLE_FROM
- : 0;
- const indices = new Array<number>(animLength);
- for (let i = 0; i < animLength; i++) indices[i] = i;
- indices.sort((a, b) => Math.abs(a - ref) - Math.abs(b - ref));
- for (let i = 0; i < animLength; i++) charOrder[indices[i]] = i;
- }
- if (SCRAMBLE_REVERSED) {
- const last = animLength - 1;
- for (let i = 0; i < animLength; i++) charOrder[i] = last - charOrder[i];
- }
-
- const charStarts = new Float32Array(animLength);
- const charEnds = new Float32Array(animLength);
- const scale = SCRAMBLE_PERTURBATION > 0 ? SCRAMBLE_PERTURBATION * settleRatio : 0;
- for (let c = 0; c < animLength; c++) {
- const so = scale > 0 ? ((rng(0, 2000) - 1000) / 1000) * scale : 0;
- const eo = scale > 0 ? ((rng(0, 2000) - 1000) / 1000) * scale : 0;
- charStarts[c] = charOrder[c] * settleSpacing + so;
- charEnds[c] = Math.ceil((charStarts[c] + settleRatio + eo) / stepRatio) * stepRatio;
- }
-
- return {
- animLength,
- endLength,
- characters,
- cursorChars,
- cursorLen,
- cursorZone,
- settleSpacing,
- stepRatio,
- charStarts,
- charEnds,
- seed,
- };
-}
-
-function getScrambleState(text: string): ScrambleState {
- let state = scrambleStateCache.get(text);
- if (!state) {
- if (scrambleStateCache.size >= SCRAMBLE_CACHE_LIMIT) {
- const firstKey = scrambleStateCache.keys().next().value;
- if (firstKey !== undefined) scrambleStateCache.delete(firstKey);
- }
- state = buildScrambleState(text);
- scrambleStateCache.set(text, state);
- }
- return state;
-}
-
-export function scrambleChars(chars: string, ratio: number): string {
- if (chars.length === 0) return '';
- if (ratio >= 1) return chars;
-
- const {
- animLength, endLength, characters, charStarts, charEnds,
- cursorChars, cursorLen, cursorZone, settleSpacing, stepRatio, seed,
- } = getScrambleState(chars);
- const hasCursor = cursorLen > 0;
- const step = (ratio / stepRatio) | 0;
-
- let out = '';
- for (let c = 0; c < animLength; c++) {
- const charStart = charStarts[c];
- const charEnd = charEnds[c];
-
- // Settled: target glyph
- if (ratio >= charEnd) {
- if (c < endLength) out += chars[c];
- continue;
- }
-
- const isSpace = c < endLength && chars[c] === ' ';
- if (isSpace) {
- out += ' ';
- continue;
- }
-
- // Pre-transition: override scramble char (length stays constant — equivalent to
- // anime.js override=true with startLength == animLength)
- if (ratio < charStart) {
- out += pickScrambleChar(seed, c, step, characters);
- continue;
- }
-
- // Active zone: cursor sub-zone, then scramble
- if (hasCursor && ratio - charStart < cursorZone) {
- out += cursorChars[cursorLen - 1 - (((ratio - charStart) / settleSpacing) | 0)];
- } else {
- out += pickScrambleChar(seed, c, step, characters);
- }
- }
- return out;
-}
diff --git a/apps/web/src/components/engine/utils/text.ts b/apps/web/src/components/engine/utils/text.ts
deleted file mode 100644
index f9ec3981..00000000
--- a/apps/web/src/components/engine/utils/text.ts
+++ /dev/null
@@ -1,653 +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 { clamp } from "@/utils";
-import { colorToHex } from "@/utils/color";
-import { COMPOSITE_OPERATIONS, PaintType, FontStyle, StrokeCap, StrokeJoin, TextAlign, TextBaseline, TextCase } from "../components";
-import { createLinearGradient, createRadialGradient } from "../systems/render";
-import { hasComponent } from "bitecs";
-
-import type { EngineWorld } from "../api/world";
-
-type Ctx = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
-
-export type TokenOptions = {
- /**
- * Defines the characters to render
- */
- chars: string;
- /**
- * Defines the X offset of the token to the left of the line
- */
- offset: number;
- /**
- * Defines the metrics of the token
- */
- metrics: TextMetrics;
- /**
- * Defines the style of the token
- */
- ranges: number[];
-}
-
-export type Line = {
- offsetX: number;
- offsetY: number;
- baseline: number;
- height: number;
-};
-
-type RenderSplit = {
- words: string[];
- ranges: number[];
-};
-
-/** Global offscreen canvas used solely for text measurement. */
-let measureCanvas: OffscreenCanvas | null = null;
-let measureCtx: OffscreenCanvasRenderingContext2D | null = null;
-
-function getMeasureCtx(): OffscreenCanvasRenderingContext2D {
- if (!measureCtx) {
- measureCanvas = new OffscreenCanvas(1, 1);
- measureCtx = measureCanvas.getContext('2d', { willReadFrequently: true, alpha: true })!;
- }
- return measureCtx;
-}
-
-export class Token {
- public offset: number;
- public metrics: TextMetrics;
- public ranges: number[];
- public chars: string;
- public width: number;
- public height: number;
- public line = { offsetX: 0, offsetY: 0, baseline: 0, height: 0 };
-
- constructor(options: TokenOptions) {
- this.offset = options.offset;
- this.metrics = options.metrics;
- this.ranges = options.ranges;
- this.width = options.metrics.width;
- this.height = options.metrics.fontBoundingBoxAscent + options.metrics.fontBoundingBoxDescent;
- this.chars = options.chars;
- }
-
- public get x(): number {
- return (this.offset + this.line.offsetX) | 0;
- }
-
- public get y(): number {
- return (this.line.offsetY + this.line.baseline) | 0;
- }
-
- public get left(): number {
- return (
- this.offset +
- this.line.offsetX -
- this.metrics.actualBoundingBoxLeft
- ) | 0;
- }
-
- public get right(): number {
- return (
- this.offset +
- this.line.offsetX +
- this.metrics.actualBoundingBoxRight
- ) | 0;
- }
-
- public get top(): number {
- return (
- this.line.offsetY +
- this.line.baseline -
- this.metrics.actualBoundingBoxAscent
- ) | 0
- }
-
- public get bottom(): number {
- return (
- this.line.offsetY +
- this.line.baseline +
- this.metrics.actualBoundingBoxDescent
- ) | 0
- }
-
- public setLine(line: Line) {
- this.line = { ...line };
- }
-}
-
-function applyFont(ctx: Ctx, world: EngineWorld, eid: number, ranges: number[]) {
- const size = getFontSize(world, eid, ranges);
- const family = getFontFamily(world, eid, ranges);
- const weight = getFontWeight(world, eid, ranges);
- const style = getFontStyle(world, eid, ranges);
- const baseline = getTextBaseline(world, eid, ranges);
- const spacing = getLetterSpacing(world, eid, ranges);
-
- const mappedStyle = FontStyle[style].toLowerCase();
- const mappedBaseline = TextBaseline[baseline].toLowerCase() as CanvasTextBaseline;
-
- ctx.font = `${mappedStyle} ${weight.toLowerCase()} ${size}px ${family}`.trim();
- ctx.textBaseline = mappedBaseline;
- ctx.letterSpacing = `${spacing}px`;
-}
-
-function applyStroke(ctx: Ctx, world: EngineWorld, eid: number) {
- const Stroke = world.components.StrokeStyle;
-
- ctx.lineWidth = Stroke.width[eid] ?? 1;
- ctx.lineJoin = StrokeJoin[Stroke.join[eid] ?? 0].toLocaleLowerCase() as CanvasLineJoin;
- ctx.lineCap = StrokeCap[Stroke.cap[eid] ?? 0].toLocaleLowerCase() as CanvasLineCap;
- ctx.miterLimit = Stroke.miterLimit[eid] ?? 3;
-}
-
-function shapeTokens(world: EngineWorld, eid: number): void {
- const Computed = world.components.Computed;
- const Size = world.components.Size;
- const lines = world.components.TextCache.tokens[eid];
- const leading = world.components.TextStyle.leading[eid] ?? 1;
- const textAlign = world.components.TextStyle.textAlign[eid] ?? TextAlign.LEFT;
- const textBaseline = world.components.TextStyle.textBaseline[eid] ?? TextBaseline.TOP;
- if (!lines) return;
-
- // Measure each line's width by summing word widths and spaces
- const lineWidths: number[] = lines.map(line => line.reduce((acc, word) => acc + word.width, 0));
- const lineHeights: number[] = lines.map(line => Math.max(...line.map(word => word.height)));
-
- // Find the maximum line width
- const maxLineWidth = Math.max(...lineWidths);
-
- // Calculate total height based on leading
- const totalHeight = lineHeights.reduce((acc, height, i) => acc + height * (i < lineHeights.length - 1 ? leading : 1), 0);
-
- // When Size is assigned, use Computed for positioning; otherwise set it from text dimensions
- const hasSizeComponent = hasComponent(world, eid, Size);
- if (!hasSizeComponent || !Computed.width[eid] || !Computed.height[eid]) {
- Computed.width[eid] = Math.ceil(maxLineWidth);
- Computed.height[eid] = Math.ceil(totalHeight);
- }
-
- const containerWidth = Computed.width[eid];
- const containerHeight = Computed.height[eid];
-
- // Calculate initial vertical offset based on text baseline and container height
- let offsetY = 0;
- if (textBaseline === TextBaseline.MIDDLE) {
- offsetY = (containerHeight - totalHeight) / 2;
- } else if (textBaseline === TextBaseline.BOTTOM) {
- offsetY = containerHeight - totalHeight;
- }
-
- // Calculate coordinates for each word
- const line = {
- offsetX: 0,
- offsetY,
- baseline: 0,
- height: 0,
- };
-
- for (let l = 0; l < lines.length; l++) {
- line.height = lineHeights[l];
-
- if (textAlign === TextAlign.LEFT) {
- line.offsetX = 0;
- } else if (textAlign === TextAlign.CENTER) {
- line.offsetX = (containerWidth - lineWidths[l]) / 2;
- } else if (textAlign === TextAlign.RIGHT) {
- line.offsetX = containerWidth - lineWidths[l];
- }
-
- if (textBaseline === TextBaseline.TOP) {
- line.baseline = 0;
- } else if (textBaseline === TextBaseline.MIDDLE) {
- line.baseline = lineHeights[l] / 2;
- } else if (textBaseline === TextBaseline.BOTTOM) {
- line.baseline = lineHeights[l];
- } else if (textBaseline === TextBaseline.ALPHABETIC) {
- const maxAscent = Math.max(...lines[l].map(word => word.metrics.fontBoundingBoxAscent));
- line.baseline = maxAscent || lineHeights[l] * 0.75; // Fallback if no words in line
- }
-
- for (const word of lines[l]) {
- word.setLine(line);
- }
-
- line.offsetY += lineHeights[l] * leading;
- }
-}
-
-function tokenizeText(world: EngineWorld, eid: number) {
- const ctx = getMeasureCtx();
-
- // Split text into segments based on style overrides first
- const lines: Token[][] = [[]];
-
- let maxWidth = world.components.Size.width[eid] ?? Number.POSITIVE_INFINITY;
- if (typeof maxWidth === 'string') {
- maxWidth = world.components.Computed.width[eid] ?? Number.POSITIVE_INFINITY;
- }
-
- let offset = 0;
- for (const { words, ranges } of createRenderSplits(world, eid)) {
- applyFont(ctx, world, eid, ranges);
-
- for (const word of words) {
- const splits = word.split('\n');
-
- for (let i = 0; i < splits.length; i++) {
- const caseValue = getTextCase(world, eid, ranges);
- const chars = transformText(splits[i], caseValue);
-
- const metrics = ctx.measureText(chars);
-
- // check if word is too wide for the current line but not the first word
- if (offset + metrics.width > maxWidth && offset > 0) {
- lines.push([]);
- offset = 0;
- }
-
- // add word to current line
- lines[lines.length - 1].push(new Token({ chars, ranges, metrics, offset }));
- offset += metrics.width;
-
- // add new line if not the last line
- if (i < splits.length - 1) {
- lines.push([]);
- offset = 0;
- }
- }
- }
- }
-
- world.components.TextCache.tokens[eid] = lines;
-}
-
-/** Renders text tokens directly to the given canvas context. */
-function renderTokens(ctx: Ctx, world: EngineWorld, eid: number): void {
- const lines = world.components.TextCache.tokens[eid];
- if (!lines) return;
- const words = lines.flat();
-
- const savedAlpha = ctx.globalAlpha;
-
- // Draw all text shadows
- {
- ctx.save();
- ctx.textAlign = 'start';
- ctx.textBaseline = 'top';
-
- // ctx.shadowBlur/OffsetX/OffsetY are in device-pixel space and are not
- // affected by the current transform, so scale them up to match the
- // content transform (camera * resolution).
- const shadowScale = world.camera.a * world.resolution;
-
- for (const word of words) {
- applyFont(ctx, world, eid, word.ranges);
-
- const strokes = getStrokes(world, eid, word.ranges);
- const shadows = getShadows(world, eid, word.ranges);
-
- if (strokes.length) {
- applyStroke(ctx, world, eid);
- }
-
- // Draw shadows first (if any)
- for (const sid of shadows) {
- if (hasComponent(world, sid, world.components.Hidden)) continue;
-
- ctx.shadowOffsetX = (world.components.Offset.x[sid] ?? 0) * shadowScale;
- ctx.shadowOffsetY = (world.components.Offset.y[sid] ?? 0) * shadowScale;
- ctx.shadowBlur = (world.components.Blur[sid] ?? 0) * shadowScale;
- ctx.shadowColor = colorToHex(world.components.Color[sid] ?? 0x000000);
- ctx.fillStyle = colorToHex(world.components.Color[sid] ?? 0x000000);
- ctx.globalAlpha = savedAlpha * (world.components.Appearance.opacity[sid] ?? 1);
-
- if (strokes.length) {
- ctx.strokeText(word.chars, word.x, word.y);
- } else {
- ctx.fillText(word.chars, word.x, word.y);
- }
- }
-
- // Reset shadow properties if any shadows are applied
- if (shadows.length) {
- ctx.globalAlpha = savedAlpha;
- ctx.shadowColor = 'transparent';
- }
- }
- ctx.restore();
- }
-
- // Draw all text strokes
- {
- ctx.save();
- ctx.textAlign = 'start';
- ctx.textBaseline = 'top';
-
- for (const word of words) {
- const strokes = getStrokes(world, eid, word.ranges);
- if (!strokes.length) continue;
-
- applyFont(ctx, world, eid, word.ranges);
- applyStroke(ctx, world, eid);
-
- const w = world.components.Computed.width[eid];
- const h = world.components.Computed.height[eid];
-
- // Draw strokes (if any)
- for (const sid of strokes) {
- if (hasComponent(world, sid, world.components.Hidden)) continue;
-
- const savedCO = ctx.globalCompositeOperation;
- const blendMode = world.components.Appearance.blendMode[sid] ?? 0;
- if (blendMode !== 0) {
- ctx.globalCompositeOperation = COMPOSITE_OPERATIONS[blendMode];
- }
- ctx.globalAlpha = savedAlpha * (world.components.Appearance.opacity[sid] ?? 1);
-
- const paintType = world.components.Paint[sid];
- if (paintType === PaintType.LINEAR_GRADIENT) {
- ctx.strokeStyle = createLinearGradient(world, sid, ctx, w, h);
- } else if (paintType === PaintType.RADIAL_GRADIENT) {
- ctx.strokeStyle = createRadialGradient(world, sid, ctx, w, h);
- } else {
- ctx.strokeStyle = colorToHex(world.components.Color[sid] ?? 0x000000);
- }
- ctx.strokeText(word.chars, word.x, word.y);
- ctx.globalCompositeOperation = savedCO;
- }
- }
- ctx.restore();
- }
-
- // Draw all text fills
- {
- ctx.save();
- ctx.textAlign = 'start';
- ctx.textBaseline = 'top';
-
- for (const word of words) {
- const fills = getFills(world, eid, word.ranges);
- if (!fills.length) continue;
-
- applyFont(ctx, world, eid, word.ranges);
-
- const w = world.components.Computed.width[eid];
- const h = world.components.Computed.height[eid];
-
- for (const fid of fills) {
- if (hasComponent(world, fid, world.components.Hidden)) continue;
-
- const savedCO = ctx.globalCompositeOperation;
- const blendMode = world.components.Appearance.blendMode[fid] ?? 0;
- if (blendMode !== 0) {
- ctx.globalCompositeOperation = COMPOSITE_OPERATIONS[blendMode];
- }
- ctx.globalAlpha = savedAlpha * (world.components.Appearance.opacity[fid] ?? 1);
-
- const paintType = world.components.Paint[fid];
- if (paintType === PaintType.LINEAR_GRADIENT) {
- ctx.fillStyle = createLinearGradient(world, fid, ctx, w, h);
- } else if (paintType === PaintType.RADIAL_GRADIENT) {
- ctx.fillStyle = createRadialGradient(world, fid, ctx, w, h);
- } else {
- ctx.fillStyle = colorToHex(world.components.Color[fid] ?? 0x000000);
- }
- ctx.fillText(word.chars, word.x, word.y);
- ctx.globalCompositeOperation = savedCO;
- }
- }
- ctx.restore();
- }
-}
-
-/** Render text tokens directly to world.ctx. Call measureText first. */
-export function renderText(world: EngineWorld, eid: number) {
- const ctx = world.ctx;
- if (!ctx) return;
- tokenizeText(world, eid);
- shapeTokens(world, eid);
- renderTokens(ctx, world, eid);
-}
-
-/**
- * Splits text into segments based on overlapping text range eids.
- * Handles overlapping ranges by merging their eids.
- * @returns Array of RenderSplit objects with words, merged style, and contributing styleIds
- */
-function createRenderSplits(world: EngineWorld, eid: number): RenderSplit[] {
- const chars = world.components.Computed.chars[eid] ?? world.components.Chars[eid] ?? '';
-
- const textRangeEids = world.components.Cache.textRanges[eid];
-
- // Fast path: no styles
- if (textRangeEids.length === 0) {
- return [{
- words: tokenize(chars),
- ranges: [],
- }];
- }
-
- // Fast path: empty text
- if (chars.length === 0) {
- return [{
- words: [],
- ranges: [],
- }];
- }
-
- // Collect all unique boundary points
- const boundaries = new Set<number>();
- boundaries.add(0);
- boundaries.add(chars.length);
-
- for (const rid of textRangeEids) {
- let start = world.components.TextRange.start[rid] ?? 0;
- let end = world.components.TextRange.end[rid] ?? chars.length;
-
- // Clamp boundaries to valid text range
- start = clamp(start, 0, chars.length);
- end = clamp(end, 0, chars.length);
-
- boundaries.add(start);
- boundaries.add(end);
- }
-
- // Sort boundaries in ascending order
- const sortedBoundaries = Array.from(boundaries).sort((a, b) => a - b);
-
- // Create segments between each pair of boundaries
- const segments: RenderSplit[] = [];
-
- for (let i = 0; i < sortedBoundaries.length - 1; i++) {
- const segStart = sortedBoundaries[i];
- const segEnd = sortedBoundaries[i + 1];
-
- // Skip empty segments
- if (segStart >= segEnd) continue;
-
- // Find all active styles for this segment
- const ranges: number[] = [];
-
- for (const rid of textRangeEids) {
- let start = world.components.TextRange.start[rid] ?? 0;
- let end = world.components.TextRange.end[rid] ?? chars.length;
-
- // Clamp boundaries to valid text range
- start = clamp(start, 0, chars.length);
- end = clamp(end, 0, chars.length);
-
- // A style is active if segment falls within its range [start, end)
- // Using < for end to treat ranges as [start, end)
- if (start <= segStart && end > segStart) {
- ranges.push(rid);
- }
- }
-
- segments.push({
- words: tokenize(chars.slice(segStart, segEnd)),
- ranges: ranges,
- });
- }
-
- return segments;
-}
-
-function tokenize(input: string): string[] {
- // Fast path for inputs without spaces
- if (input.indexOf(' ') === -1) {
- return [input];
- }
-
- // Use regex to match:
- // 1. Any characters up to and including a space
- // 2. OR any remaining characters to the end
- return input.match(/[^]*? |[^]+$/g) || [input];
-}
-
-export function transformText(text: string, textCase?: number): string {
- if (textCase == TextCase.LOWER) {
- return text.toLocaleLowerCase();
- }
-
- if (textCase == TextCase.UPPER) {
- return text.toUpperCase();
- }
-
- return text;
-}
-
-
-function getFills(world: EngineWorld, eid: number, ranges: number[]) {
- let value = world.components.Cache.fills[eid];
-
- for (const rid of ranges) {
- const rangeValue = world.components.Cache.fills[rid];
- if (rangeValue.length) {
- value = rangeValue;
- }
- }
-
- return value;
-}
-
-
-function getStrokes(world: EngineWorld, eid: number, ranges: number[]) {
- let value = world.components.Cache.strokes[eid];
-
- for (const rid of ranges) {
- const rangeValue = world.components.Cache.strokes[rid];
- if (rangeValue.length) {
- value = rangeValue;
- }
- }
-
- return value;
-}
-
-function getShadows(world: EngineWorld, eid: number, ranges: number[]) {
- let value = world.components.Cache.shadows[eid];
-
- for (const rid of ranges) {
- const rangeValue = world.components.Cache.shadows[rid];
- if (rangeValue.length) {
- value = rangeValue;
- }
- }
-
- return value;
-}
-
-function getTextCase(world: EngineWorld, eid: number, ranges: number[]) {
- let value = world.components.TextStyle.textCase[eid] ?? 0;
-
- for (const rid of ranges) {
- const rangeValue = world.components.TextStyle.textCase[rid];
- if (rangeValue !== undefined) {
- value = rangeValue;
- }
- }
-
- return value;
-}
-
-function getFontSize(world: EngineWorld, eid: number, ranges: number[]) {
- let value = world.components.TextStyle.fontSize[eid] ?? 16;
-
- for (const rid of ranges) {
- const rangeValue = world.components.TextStyle.fontSize[rid];
- if (rangeValue !== undefined) {
- value = rangeValue;
- }
- }
-
- return value;
-}
-
-function getFontFamily(world: EngineWorld, eid: number, ranges: number[]) {
- let value = world.components.TextStyle.fontFamily[eid] ?? 'Inter';
-
- for (const rid of ranges) {
- const rangeValue = world.components.TextStyle.fontFamily[rid];
- if (rangeValue !== undefined) {
- value = rangeValue;
- }
- }
-
- return value;
-}
-
-function getFontWeight(world: EngineWorld, eid: number, ranges: number[]) {
- let value = world.components.TextStyle.fontWeight[eid] ?? '400';
-
- for (const rid of ranges) {
- const rangeValue = world.components.TextStyle.fontWeight[rid];
- if (rangeValue !== undefined) {
- value = rangeValue;
- }
- }
-
- return value;
-}
-
-
-function getFontStyle(world: EngineWorld, eid: number, ranges: number[]) {
- let value = world.components.TextStyle.fontStyle[eid] ?? FontStyle.NORMAL;
-
- for (const rid of ranges) {
- const rangeValue = world.components.TextStyle.fontStyle[rid];
- if (rangeValue !== undefined) {
- value = rangeValue;
- }
- }
-
- return value;
-}
-
-function getTextBaseline(world: EngineWorld, eid: number, ranges: number[]) {
- let value = world.components.TextStyle.textBaseline[eid] ?? TextBaseline.TOP;
-
- for (const rid of ranges) {
- const rangeValue = world.components.TextStyle.textBaseline[rid];
- if (rangeValue !== undefined) {
- value = rangeValue;
- }
- }
-
- return value;
-}
-
-function getLetterSpacing(world: EngineWorld, eid: number, ranges: number[]) {
- let value = world.components.TextStyle.letterSpacing[eid] ?? 0;
-
- for (const rid of ranges) {
- const rangeValue = world.components.TextStyle.letterSpacing[rid];
- if (rangeValue !== undefined) {
- value = rangeValue;
- }
- }
-
- return value;
-}
diff --git a/apps/web/src/components/engine/utils/time.ts b/apps/web/src/components/engine/utils/time.ts
deleted file mode 100644
index 5056c5ad..00000000
--- a/apps/web/src/components/engine/utils/time.ts
+++ /dev/null
@@ -1,97 +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 { hasComponent } from "bitecs";
-import { PaintType } from "../components";
-import { CONONICAL_TIME_BASE } from "../constants";
-
-import type { EngineWorld } from "../api/world";
-
-export function snapToMs(seconds: number) {
- return Math.round(seconds * CONONICAL_TIME_BASE) / CONONICAL_TIME_BASE;
-}
-
-export function snapToFps(seconds: number, fps: number = 30) {
- return Math.round(seconds * fps) / fps;
-}
-
-export function secondsToFrames(seconds: number = 0, fps: number = 30) {
- return Math.round(seconds * fps);
-}
-
-export function framesToSeconds(frames: number = 0, fps: number = 30) {
- return snapToMs(frames / fps);
-}
-
-/**
- * Computes the trim start frame (local) for a given entity,
- * based on the specified start frame (global) and playback rate.
- */
-export function computeTrimStart(world: EngineWorld, eid: number, start: number) {
- const c = world.components;
- const delay = c.Computed.delay[eid] ?? 0;
- const playbackRate = c.PlaybackRate[eid] ?? 1;
-
- return Math.round((start - delay) * playbackRate);
-}
-
-/**
- * Computes the trim end frame (local) for a given entity,
- * based on the specified end frame (global) and playback rate.
- */
-export function computeTrimEnd(world: EngineWorld, eid: number, end: number) {
- const c = world.components;
- const delay = c.Computed.delay[eid] ?? 0;
- const playbackRate = c.PlaybackRate[eid] ?? 1;
-
- return Math.round((end - delay) * playbackRate);
-}
-
-export function findGeometryAsset(world: EngineWorld, eid: number) {
- const c = world.components;
-
- if (hasComponent(world, eid, c.AssetId)) {
- const asset = world.assets.get(c.AssetId[eid] ?? '');
-
- if (asset) {
- return asset;
- }
- }
-
-
- for (const fid of c.Cache.fills[eid]) {
- if (hasComponent(world, fid, c.AssetId)) {
- const asset = world.assets.get(c.AssetId[fid] ?? '');
- if (asset) {
- return asset;
- }
- }
- }
-
- return null;
-}
-
-export function findAssetDuration(world: EngineWorld, eid: number) {
- const c = world.components;
-
- if (hasComponent(world, eid, c.Audio)) {
- const asset = world.assets.get(c.AssetId[eid] ?? '');
-
- if (asset?.type === 'AUDIO' || asset?.type === 'VIDEO') {
- return secondsToFrames(asset.duration, world.frameRate);
- }
- }
-
-
- for (const fid of c.Cache.fills[eid]) {
- if (c.Paint[fid] === PaintType.VIDEO || c.Paint[fid] === PaintType.SEQUENCE) {
- const asset = world.assets.get(c.AssetId[fid] ?? '');
- if (asset && (asset.type === 'VIDEO' || asset.type === 'SEQUENCE')) {
- return secondsToFrames(asset.duration, world.frameRate);
- }
- }
- }
-
- return null;
-}
diff --git a/apps/web/src/components/engine/utils/transcode.ts b/apps/web/src/components/engine/utils/transcode.ts
deleted file mode 100644
index 22db4fbf..00000000
--- a/apps/web/src/components/engine/utils/transcode.ts
+++ /dev/null
@@ -1,522 +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 { ALL_FORMATS, AudioSampleSink, BlobSource, BufferTarget, CanvasSink, Conversion, Input, InputAudioTrack, Mp4OutputFormat, OggOutputFormat, Output, StreamTarget } from 'mediabunny';
-import { assert } from '@/utils';
-import { getAssetFile } from '../api/assets';
-import type { StreamTargetChunk } from 'mediabunny';
-import type { Asset, VideoAsset, AudioAsset } from '../db';
-
-export async function transcodeForTranscription(asset: Asset): Promise<File> {
- const blob = await getAssetFile(asset);
- const input = new Input({ formats: ALL_FORMATS, source: new BlobSource(blob) });
- try {
- const output = new Output({ format: new OggOutputFormat(), target: new BufferTarget() });
- const conversion = await Conversion.init({
- input,
- output,
- video: { discard: true },
- audio: { codec: "opus", numberOfChannels: 1, sampleRate: 16000 },
- });
- if (!conversion.isValid || conversion.utilizedTracks.length === 0) {
- throw new Error("No audio found. The asset has no audio track to transcribe.");
- }
- await conversion.execute();
-
- const buffer = output.target.buffer;
- assert(buffer, "Transcoding produced no output.");
- return new File([buffer], `${crypto.randomUUID()}.ogg`, { type: "audio/ogg" });
- } finally {
- input.dispose();
- }
-}
-
-export async function transcodeForAnalysis(asset: VideoAsset | AudioAsset, window?: TimeWindow & { stripVideo?: boolean }) {
- const blob = await getAssetFile(asset);
- const hasWindow = window?.start !== undefined || window?.end !== undefined;
-
- const isVideo = asset.type === "VIDEO" && !window?.stripVideo;
- const trim = hasWindow ? resolveWindow(asset.duration, window) : undefined;
-
- const input = new Input({ formats: ALL_FORMATS, source: new BlobSource(blob) });
-
- const { writable, readable } = new TransformStream<StreamTargetChunk, Uint8Array<ArrayBuffer>>({
- transform: (chunk, controller) => controller.enqueue(chunk.data),
- });
-
- const target = new StreamTarget(writable);
- const format = isVideo
- ? new Mp4OutputFormat({ fastStart: "fragmented" })
- : new OggOutputFormat();
- const output = new Output({ format, target });
-
- const conversion = await Conversion.init({
- input,
- output,
- trim: (trim && { start: trim.start, end: trim.end }),
- video: { width: 640, frameRate: 24, bitrate: 500_000, discard: !isVideo },
- audio: { codec: isVideo ? undefined : "opus", numberOfChannels: 1, sampleRate: 16_000 },
- });
-
- if (!conversion.isValid || conversion.utilizedTracks.length === 0) {
- input.dispose();
- throw new Error("Nothing to analyze. The asset has no usable video or audio track.");
- }
-
- const run = async () => {
- try {
- await conversion.execute();
- } finally {
- input.dispose();
- }
- };
-
- return { readable, run };
-}
-
-const PATCH_SIZE = 28;
-const MAX_WIDTH_IN_TOKENS = 64; // 92
-const MAX_HEIGHT_IN_TOKENS = 52;
-const THUMBNAIL_HEIGHT_IN_TOKENS = 7;
-const RULER_HEIGHT_IN_TOKENS = 1;
-const AUDIO_WAVEFORM_HEIGHT_IN_TOKENS = 6;
-const AUDIO_COLUMN_WIDTH_IN_TOKENS = 6;
-const WAVEFORM_SAMPLE_WIDTH = 2; // pixels per peak; wider = less noisy
-const SILENCE_THRESHOLD = 12; // peak value (0-255) at/below which audio counts as silent
-const SILENCE_MIN_SECONDS = 0.4;
-const AUDIO_RULER_FRAME_RATE = 30;
-
-function splitTimecode(seconds: number, frameRate: number): [hours: number, minutes: number, seconds: number, frames: number] {
- const fps = Math.max(1, Math.round(frameRate));
- const totalFrames = Math.round(seconds * fps);
- const totalSeconds = Math.floor(totalFrames / fps);
- return [
- Math.floor(totalSeconds / 3600),
- Math.floor((totalSeconds % 3600) / 60),
- totalSeconds % 60,
- totalFrames % fps,
- ];
-}
-
-/** `HH:MM:SS:FF`, fixed width so ruler ticks line up. */
-export function formatTimestamp(seconds: number, frameRate: number): string {
- return splitTimecode(seconds, frameRate).map((v) => String(v).padStart(2, '0')).join(':');
-}
-
-/**
- * The same timecode with its zero segments dropped, for labels and filenames:
- * `08s10f`, `01m05s`, `0f`. Each segment carries its unit, so nothing is
- * ambiguous once the empty ones are gone.
- */
-export function formatTimecode(seconds: number, frameRate: number): string {
- const units = ['h', 'm', 's', 'f'];
- const stamp = splitTimecode(seconds, frameRate)
- .map((value, i) => (value === 0 ? '' : `${String(value).padStart(2, '0')}${units[i]}`))
- .join('');
- return stamp || '0f';
-}
-
-// Draws a timestamp label into the top-left corner of a canvas: white text with
-// a black outline, sized relative to the canvas so it reads at any resolution.
-export function stampTimestampLabel(
- ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
- height: number,
- label: string,
- options?: { minBandHeight?: number },
-): void {
- const bandHeight = Math.max(options?.minBandHeight ?? 20, Math.round(height * 0.06));
- const fontSize = Math.round(bandHeight * 0.72);
- const paddingLeft = Math.round(bandHeight * 0.4);
- const y = paddingLeft + bandHeight / 2;
-
- ctx.font = `normal ${fontSize}px sans-serif`;
- ctx.textBaseline = 'middle';
- ctx.textAlign = 'left';
-
- ctx.lineJoin = 'round';
- ctx.lineWidth = Math.max(2, Math.round(fontSize / 3));
- ctx.strokeStyle = '#000';
- ctx.strokeText(label, paddingLeft, y);
- ctx.fillStyle = '#FFF';
- ctx.fillText(label, paddingLeft, y);
-}
-
-export type RenderedPreview = {
- dataUrl: string;
-};
-
-export type RenderedWaveform = RenderedPreview & {
- silences: Array<{ start: number; end: number }>;
-};
-
-export type TimeWindow = { start?: number; end?: number };
-
-export type PreviewOptions = TimeWindow & { scale?: number };
-
-const MIN_SCALE = 0.25;
-const MAX_SCALE = 4;
-
-function resolveScale(scale?: number): number {
- if (scale === undefined) return 1;
- assert(Number.isFinite(scale) && scale > 0, `scale must be a positive number (got ${scale}).`);
- return Math.min(Math.max(scale, MIN_SCALE), MAX_SCALE);
-}
-
-// Clamp a requested [start, end] window (in seconds) to the asset's timeline.
-// `start` defaults to 0 and `end` to the full duration.
-function resolveWindow(duration: number, window?: TimeWindow): { start: number; end: number; duration: number } {
- const start = Math.min(Math.max(window?.start ?? 0, 0), duration);
- const end = Math.min(Math.max(window?.end ?? duration, start), duration);
- assert(end > start, `The requested window is empty; start (${start.toFixed(2)}s) is at or past the asset's end (${duration.toFixed(2)}s).`);
- return { start, end, duration: end - start };
-}
-
-// Render a video as a grid of thumbnails sampled across the window, each row
-// stamped with a timestamp ruler. Video only; no audio.
-export async function filmstripAsset(asset: Asset, options?: PreviewOptions): Promise<RenderedPreview> {
- assert(asset.type === "VIDEO", "The filmstrip needs a video asset; use `waveform` for audio.");
- const blob = await getAssetFile(asset);
- const scale = resolveScale(options?.scale);
- const input = new Input({ formats: ALL_FORMATS, source: new BlobSource(blob) });
- try {
- return await renderFilmstrip(asset, input, options, scale);
- } finally {
- input.dispose();
- }
-}
-
-async function renderFilmstrip(asset: VideoAsset, input: Input, window: TimeWindow | undefined, scale: number): Promise<RenderedPreview> {
- const videoTrack = await input.getPrimaryVideoTrack();
- assert(videoTrack, "Video track not found");
- const { start: windowStart, duration } = resolveWindow(asset.duration, window);
- const aspect = asset.width / asset.height;
- const frameRate = asset.frameRate;
-
- const thumbnailHeightInTokens = Math.min(
- Math.max(Math.round(THUMBNAIL_HEIGHT_IN_TOKENS * scale), 1),
- MAX_HEIGHT_IN_TOKENS - RULER_HEIGHT_IN_TOKENS,
- );
- const rowHeightInTokens = thumbnailHeightInTokens + RULER_HEIGHT_IN_TOKENS;
-
- const columnWidthInTokens = Math.min(
- Math.max(Math.floor(thumbnailHeightInTokens * aspect), 1),
- MAX_WIDTH_IN_TOKENS,
- );
-
- const thumbnailWidthInPixels = columnWidthInTokens * PATCH_SIZE;
- const thumbnailHeightInPixels = thumbnailHeightInTokens * PATCH_SIZE;
- const rowHeightInPixels = rowHeightInTokens * PATCH_SIZE;
-
- const rows = Math.floor(MAX_HEIGHT_IN_TOKENS / rowHeightInTokens);
- const columns = Math.floor(MAX_WIDTH_IN_TOKENS / columnWidthInTokens);
- const cells = rows * columns;
-
- const secondsPerColumn = Math.max(duration / cells, 1 / frameRate);
-
- const canvas = document.createElement('canvas');
- canvas.width = columnWidthInTokens * columns * PATCH_SIZE;
- canvas.height = rowHeightInTokens * rows * PATCH_SIZE;
- const ctx = canvas.getContext('2d')!;
-
- ctx.fillStyle = '#000';
- ctx.fillRect(0, 0, canvas.width, canvas.height);
-
- const sink = new CanvasSink(videoTrack, {
- width: thumbnailWidthInPixels,
- height: thumbnailHeightInPixels,
- fit: 'cover',
- poolSize: 1,
- });
-
- const rulerHeight = RULER_HEIGHT_IN_TOKENS * PATCH_SIZE;
-
- const timestamps = Array.from({ length: cells }, (_, i) => windowStart + i * secondsPerColumn);
- const interator = sink.canvasesAtTimestamps(timestamps);
- for (let i = 0; i < cells; i++) {
- const wrapped = await interator.next();
- if (!wrapped.value?.canvas) continue;
-
- const column = i % columns;
- const row = Math.floor(i / columns);
-
- ctx.drawImage(
- wrapped.value.canvas,
- column * columnWidthInTokens * PATCH_SIZE,
- rulerHeight + row * rowHeightInTokens * PATCH_SIZE,
- thumbnailWidthInPixels,
- thumbnailHeightInPixels
- );
- }
-
- drawRuler(ctx, {
- cells,
- columns,
- columnWidthInPixels: columnWidthInTokens * PATCH_SIZE,
- rowHeightInPixels,
- rulerHeight,
- secondsPerColumn,
- frameRate,
- startOffset: windowStart,
- });
-
- return { dataUrl: canvas.toDataURL('image/png') };
-}
-
-// Render the audio track of a video or audio file as a loudness-over-time
-// waveform with a timestamp ruler and silent stretches highlighted.
-export async function waveformAsset(asset: Asset, options?: PreviewOptions): Promise<RenderedWaveform> {
- assert(asset.type === "VIDEO" || asset.type === "AUDIO", "The waveform needs a video or audio asset.");
- const blob = await getAssetFile(asset);
- const scale = resolveScale(options?.scale);
- const input = new Input({ formats: ALL_FORMATS, source: new BlobSource(blob) });
- try {
- return await renderWaveform(asset, input, options, scale);
- } finally {
- input.dispose();
- }
-}
-
-async function renderWaveform(asset: VideoAsset | AudioAsset, input: Input, window: TimeWindow | undefined, scale: number): Promise<RenderedWaveform> {
- const audioTrack = await input.getPrimaryAudioTrack();
- assert(audioTrack, "No audio track found, so no waveform can be rendered.");
- assert(
- await audioTrack.canDecode(),
- `This browser's audio decoder can't handle the "${audioTrack.getCodec() ?? "unknown"}" codec, so no waveform can be rendered.`,
- );
- const { start: windowStart, duration } = resolveWindow(asset.duration, window);
- const rulerFrameRate = asset.type === "VIDEO" ? asset.frameRate : AUDIO_RULER_FRAME_RATE;
-
- const waveformHeightInTokens = Math.min(
- Math.max(Math.round(AUDIO_WAVEFORM_HEIGHT_IN_TOKENS * scale), 1),
- MAX_HEIGHT_IN_TOKENS - RULER_HEIGHT_IN_TOKENS,
- );
- const rowHeightInTokens = waveformHeightInTokens + RULER_HEIGHT_IN_TOKENS;
- const columnWidthInTokens = Math.min(
- Math.max(Math.round(AUDIO_COLUMN_WIDTH_IN_TOKENS * scale), 1),
- MAX_WIDTH_IN_TOKENS,
- );
-
- const rows = Math.floor(MAX_HEIGHT_IN_TOKENS / rowHeightInTokens);
- const columns = Math.floor(MAX_WIDTH_IN_TOKENS / columnWidthInTokens);
- const cells = rows * columns;
-
- const secondsPerColumn = duration / cells;
-
- const columnWidthInPixels = columnWidthInTokens * PATCH_SIZE;
- const rowHeightInPixels = rowHeightInTokens * PATCH_SIZE;
- const rulerHeight = RULER_HEIGHT_IN_TOKENS * PATCH_SIZE;
- const waveformHeight = waveformHeightInTokens * PATCH_SIZE;
- const rowWidth = columns * columnWidthInPixels;
-
- const canvas = document.createElement('canvas');
- canvas.width = rowWidth;
- canvas.height = rowHeightInPixels * rows;
- const ctx = canvas.getContext('2d')!;
-
- ctx.fillStyle = '#000';
- ctx.fillRect(0, 0, canvas.width, canvas.height);
-
- const peaksPerSecond = Math.round(columnWidthInPixels / secondsPerColumn / WAVEFORM_SAMPLE_WIDTH);
- const audioPeaks = await downsampleAudio(audioTrack, { peaksPerSecond, range: [windowStart, windowStart + duration] });
-
- const silentSpans = findSilentSpans(audioPeaks, peaksPerSecond);
- const silences = silentSpans.map((span) => ({
- start: windowStart + span.start / peaksPerSecond,
- end: windowStart + span.end / peaksPerSecond,
- }));
-
- drawWaveform(ctx, audioPeaks, silentSpans, {
- rows,
- rowWidth,
- rowHeightInPixels,
- waveformOffset: rulerHeight,
- waveformHeight,
- });
-
- drawRuler(ctx, {
- cells,
- columns,
- columnWidthInPixels,
- rowHeightInPixels,
- rulerHeight,
- secondsPerColumn,
- frameRate: rulerFrameRate,
- startOffset: windowStart,
- });
-
- return { dataUrl: canvas.toDataURL('image/png'), silences };
-}
-
-type WaveformLayout = {
- rows: number;
- rowWidth: number; // pixels spanning all columns of a row
- rowHeightInPixels: number;
- waveformOffset: number; // y offset within a row where the waveform begins
- waveformHeight: number;
-};
-
-// Scan the peaks for runs at/below the silence threshold that last at least
-// SILENCE_MIN_SECONDS, returned as [start, end) peak-index spans.
-function findSilentSpans(audioPeaks: Uint8ClampedArray, peaksPerSecond: number): Array<{ start: number; end: number }> {
- const minSilencePeaks = Math.round(SILENCE_MIN_SECONDS * peaksPerSecond);
- const silentSpans: Array<{ start: number; end: number }> = [];
- let runStart = -1;
- for (let i = 0; i <= audioPeaks.length; i++) {
- const silent = i < audioPeaks.length && audioPeaks[i] <= SILENCE_THRESHOLD;
- if (silent && runStart === -1) {
- runStart = i;
- } else if (!silent && runStart !== -1) {
- if (i - runStart >= minSilencePeaks) {
- silentSpans.push({ start: runStart, end: i });
- }
- runStart = -1;
- }
- }
- return silentSpans;
-}
-
-// Draws a center-aligned waveform, then overlays the given silent stretches in red.
-function drawWaveform(ctx: CanvasRenderingContext2D, audioPeaks: Uint8ClampedArray, silentSpans: Array<{ start: number; end: number }>, layout: WaveformLayout) {
- const { rows, rowWidth, rowHeightInPixels, waveformOffset, waveformHeight } = layout;
- const peaksPerRow = Math.floor(rowWidth / WAVEFORM_SAMPLE_WIDTH);
- const halfHeight = waveformHeight / 2;
-
- const waveformTopOf = (row: number) => row * rowHeightInPixels + waveformOffset;
-
- ctx.fillStyle = '#FFF';
- for (let row = 0; row < rows; row++) {
- const top = waveformTopOf(row);
-
- for (let col = 0; col < peaksPerRow; col++) {
- const peakIndex = row * peaksPerRow + col;
- if (peakIndex >= audioPeaks.length) break;
-
- const amplitude = (audioPeaks[peakIndex] / 255) * halfHeight;
- const x = col * WAVEFORM_SAMPLE_WIDTH;
- const centerY = top + halfHeight;
- ctx.fillRect(x, centerY - amplitude, WAVEFORM_SAMPLE_WIDTH, Math.max(amplitude * 2, 1));
- }
- }
-
- for (const span of silentSpans) {
- let i = span.start;
- while (i < span.end) {
- const row = Math.floor(i / peaksPerRow);
- const segEnd = Math.min(span.end, (row + 1) * peaksPerRow);
- const x = (i % peaksPerRow) * WAVEFORM_SAMPLE_WIDTH;
- const width = (segEnd - i) * WAVEFORM_SAMPLE_WIDTH;
- const top = waveformTopOf(row);
-
- ctx.fillStyle = 'rgba(255, 64, 64, 0.4)';
- ctx.fillRect(x, top, width, waveformHeight);
-
- i = segEnd;
- }
- }
-}
-
-type RulerLayout = {
- cells: number;
- columns: number;
- columnWidthInPixels: number;
- rowHeightInPixels: number;
- rulerHeight: number;
- secondsPerColumn: number;
- frameRate: number;
- startOffset?: number;
-};
-
-function drawRuler(ctx: CanvasRenderingContext2D, layout: RulerLayout) {
- const { cells, columns, columnWidthInPixels, rowHeightInPixels, rulerHeight, secondsPerColumn, frameRate, startOffset = 0 } = layout;
-
- const baseFontSize = Math.round(rulerHeight * 0.8);
- const leadFontSize = Math.round(rulerHeight * 0.9);
- const paddingLeft = 5;
- const paddingTop = 2;
- const tickHeight = Math.round(rulerHeight * 0.4);
- const labelGap = 10;
-
- ctx.fillStyle = '#FFF';
- ctx.strokeStyle = '#FFF';
- ctx.lineWidth = 2;
- ctx.textBaseline = 'middle';
- ctx.textAlign = 'left';
-
- const rowWidth = columns * columnWidthInPixels;
- let labelEndX = -Infinity;
- for (let i = 0; i < cells; i++) {
- const column = i % columns;
- const row = Math.floor(i / columns);
- const columnX = column * columnWidthInPixels;
- const rowTop = row * rowHeightInPixels;
-
- if (column === 0) labelEndX = -Infinity;
-
- const tickX = columnX + 0.5;
- if (tickX >= labelEndX) {
- ctx.beginPath();
- ctx.moveTo(tickX, rowTop + rulerHeight - tickHeight);
- ctx.lineTo(tickX, rowTop + rulerHeight);
- ctx.stroke();
- }
-
- ctx.font = column === 0
- ? `bold ${leadFontSize}px sans-serif`
- : `${baseFontSize}px sans-serif`;
- const label = formatTimestamp(startOffset + i * secondsPerColumn, frameRate);
- const labelX = columnX + paddingLeft;
- const labelWidth = ctx.measureText(label).width;
- if (labelX < labelEndX + labelGap || labelX + labelWidth > rowWidth) continue;
-
- ctx.fillText(label, labelX, (rowTop + rulerHeight / 2) + paddingTop);
- labelEndX = labelX + labelWidth;
- }
-}
-
-type SamplerOptions = {
- peaksPerSecond: number;
- range?: [start: number, end: number]; // seconds; omitted = whole track
-}
-
-async function downsampleAudio(track: InputAudioTrack, options: SamplerOptions) {
- const sink = new AudioSampleSink(track);
- const iterator = options.range ? sink.samples(...options.range) : sink.samples();
- const framesPerPeak = track.sampleRate / options.peaksPerSecond;
-
- const peaks: number[] = [];
- let frameCursor = 0; // frames consumed since the range start
- let currentPeak = 0; // index of the peak currently being accumulated
- let peakMax = 0;
-
- for await (const sample of iterator) {
- try {
- const size = sample.allocationSize({ format: 'f32', planeIndex: 0 });
- const floats = new Float32Array(size / Float32Array.BYTES_PER_ELEMENT);
- sample.copyTo(floats, { format: 'f32', planeIndex: 0 });
-
- const numChannels = sample.numberOfChannels;
- const numFrames = floats.length / numChannels;
-
- for (let frameIdx = 0; frameIdx < numFrames; frameIdx++) {
- while (currentPeak < Math.floor(frameCursor / framesPerPeak)) {
- peaks.push(Math.floor(255 * peakMax));
- peakMax = 0;
- currentPeak++;
- }
-
- for (let ch = 0; ch < numChannels; ch++) {
- peakMax = Math.max(peakMax, Math.sqrt(Math.abs(floats[frameIdx * numChannels + ch])));
- }
- frameCursor++;
- }
- } finally {
- sample.close();
- }
- }
-
- // Flush the final in-progress peak.
- if (frameCursor > 0) peaks.push(Math.floor(255 * peakMax));
-
- return Uint8ClampedArray.from(peaks);
-}
diff --git a/apps/web/src/components/engine/utils/transition.ts b/apps/web/src/components/engine/utils/transition.ts
deleted file mode 100644
index a6a20414..00000000
--- a/apps/web/src/components/engine/utils/transition.ts
+++ /dev/null
@@ -1,34 +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 type { EngineWorld } from '../api/world';
-
-export type TransitionWindow = { start: number; end: number; midpointFrame: number };
-
-/**
- * Compute the transition window between two adjacent clips.
- *
- * The left clip must have a Transition component.
- * Returns null if no transition is defined.
- */
-export function getTransitionWindow(
- world: EngineWorld,
- leftEid: number,
- rightEid: number,
-): TransitionWindow {
- const Transition = world.components.Transition;
- const Computed = world.components.Computed;
-
- const leftEnd = Computed.end[leftEid];
- const rightStart = Computed.start[rightEid];
-
- // Midpoint between the two clips, but never before the left clip ends
- const midpointFrame = Math.max(Math.floor((leftEnd + rightStart) / 2), leftEnd);
-
- const duration = Transition.duration[leftEid] ?? 1;
- const start = midpointFrame - duration / 2;
- const end = midpointFrame + duration / 2;
-
- return { start, end, midpointFrame };
-}
diff --git a/apps/web/src/components/genai/action-bar.tsx b/apps/web/src/components/genai/action-bar.tsx
index 59fc5a29..bae122e1 100644
--- a/apps/web/src/components/genai/action-bar.tsx
+++ b/apps/web/src/components/genai/action-bar.tsx
@@ -25,7 +25,7 @@ import { useModifiers } from "./use-modifiers";
import { createDefaultConfig } from "./prompt-input";
import { toast } from "somoto";
-import type { GenerationConfig } from "@/components/engine/db";
+import type { GenerationConfig } from "./schemas";
interface ActionBarProps {
openPromptInput?(config: GenerationConfig): void;
diff --git a/apps/web/src/components/genai/prompt-input.tsx b/apps/web/src/components/genai/prompt-input.tsx
index 1c456df4..d4440c32 100644
--- a/apps/web/src/components/genai/prompt-input.tsx
+++ b/apps/web/src/components/genai/prompt-input.tsx
@@ -56,7 +56,7 @@ import type {
VideoGenerationConfig,
VoiceGenerationConfig,
AudioGenerationConfig,
-} from "@/components/engine/db";
+} from "./schemas";
import { AssetThumbnail } from "@/components/ui/asset-thumbnail";
import { useLibrary } from "@/engine/library";
import { useEditor } from "@/engine/hooks";
diff --git a/apps/web/src/components/genai/schemas.ts b/apps/web/src/components/genai/schemas.ts
new file mode 100644
index 00000000..7df5a7e9
--- /dev/null
+++ b/apps/web/src/components/genai/schemas.ts
@@ -0,0 +1,60 @@
+/* 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 { z } from "zod";
+
+/**
+ * What a generation request says, in the vocabulary the prompt input speaks.
+ * The same shape the server-side adapters take, so a stored `usage_records`
+ * config parses back into one (see `use-generation-records.ts`).
+ */
+
+export const aspectRatioSchema = z.enum(["1:1", "4:3", "3:4", "16:9", "9:16"]);
+export type AspectRatio = z.infer<typeof aspectRatioSchema>;
+
+export const imageGenerationConfigSchema = z.object({
+ mode: z.literal("IMAGE"),
+ model: z.string(),
+ prompt: z.string(),
+ aspectRatio: aspectRatioSchema,
+ count: z.number().int().min(1).max(4),
+ imageRefIds: z.array(z.string()).optional(),
+});
+
+export const videoGenerationConfigSchema = z.object({
+ mode: z.literal("VIDEO"),
+ model: z.string(),
+ prompt: z.string(),
+ aspectRatio: aspectRatioSchema,
+ duration: z.number().int().min(1),
+ generateAudio: z.boolean().optional(),
+ startFrameImageId: z.string().optional(),
+ endFrameImageId: z.string().optional(),
+});
+
+export const voiceGenerationConfigSchema = z.object({
+ mode: z.literal("VOICE"),
+ model: z.string(),
+ prompt: z.string(),
+ voice: z.string(),
+});
+
+export const audioGenerationConfigSchema = z.object({
+ mode: z.literal("AUDIO"),
+ model: z.string(),
+ prompt: z.string(),
+});
+
+export const generationConfigSchema = z.discriminatedUnion("mode", [
+ imageGenerationConfigSchema,
+ videoGenerationConfigSchema,
+ voiceGenerationConfigSchema,
+ audioGenerationConfigSchema,
+]);
+
+export type ImageGenerationConfig = z.infer<typeof imageGenerationConfigSchema>;
+export type VideoGenerationConfig = z.infer<typeof videoGenerationConfigSchema>;
+export type VoiceGenerationConfig = z.infer<typeof voiceGenerationConfigSchema>;
+export type AudioGenerationConfig = z.infer<typeof audioGenerationConfigSchema>;
+export type GenerationConfig = z.infer<typeof generationConfigSchema>;
diff --git a/apps/web/src/components/genai/use-generate-audio.tsx b/apps/web/src/components/genai/use-generate-audio.tsx
index b5cc4cce..264bd33c 100644
--- a/apps/web/src/components/genai/use-generate-audio.tsx
+++ b/apps/web/src/components/genai/use-generate-audio.tsx
@@ -12,7 +12,7 @@ import { useEditor } from "@/engine/hooks";
import { AUDIO_SIZE } from "@/engine/insert-asset";
import { insertGenerated, randomSeed } from "./insert";
-import type { AudioGenerationConfig } from "@/components/engine/db";
+import type { AudioGenerationConfig } from "./schemas";
export function useGenerateAudio() {
const world = useWorld();
diff --git a/apps/web/src/components/genai/use-generate-image.tsx b/apps/web/src/components/genai/use-generate-image.tsx
index 2ce4f64f..ac5c8acf 100644
--- a/apps/web/src/components/genai/use-generate-image.tsx
+++ b/apps/web/src/components/genai/use-generate-image.tsx
@@ -17,7 +17,7 @@ import { useEditor } from "@/engine/hooks";
import { ASPECT_RATIO_DIMENSIONS } from "./config";
import { insertGenerated, randomSeed } from "./insert";
-import type { ImageGenerationConfig } from "@/components/engine/db";
+import type { ImageGenerationConfig } from "./schemas";
export function useGenerateImage() {
const world = useWorld();
diff --git a/apps/web/src/components/genai/use-generate-video.tsx b/apps/web/src/components/genai/use-generate-video.tsx
index 5d1b2c88..25e23bf5 100644
--- a/apps/web/src/components/genai/use-generate-video.tsx
+++ b/apps/web/src/components/genai/use-generate-video.tsx
@@ -15,7 +15,7 @@ import { useEditor } from "@/engine/hooks";
import { ASPECT_RATIO_DIMENSIONS } from "./config";
import { insertGenerated, randomSeed } from "./insert";
-import type { VideoGenerationConfig } from "@/components/engine/db";
+import type { VideoGenerationConfig } from "./schemas";
export function useGenerateVideo() {
const world = useWorld();
diff --git a/apps/web/src/components/genai/use-generate-voice.tsx b/apps/web/src/components/genai/use-generate-voice.tsx
index 9850ed34..a8e9591a 100644
--- a/apps/web/src/components/genai/use-generate-voice.tsx
+++ b/apps/web/src/components/genai/use-generate-voice.tsx
@@ -13,7 +13,7 @@ import { useEditor } from "@/engine/hooks";
import { AUDIO_SIZE } from "@/engine/insert-asset";
import { insertGenerated, randomSeed } from "./insert";
-import type { VoiceGenerationConfig } from "@/components/engine/db";
+import type { VoiceGenerationConfig } from "./schemas";
export function useGenerateVoice() {
const world = useWorld();
diff --git a/apps/web/src/components/genai/use-generation-records.ts b/apps/web/src/components/genai/use-generation-records.ts
index 0503377f..a6fb4822 100644
--- a/apps/web/src/components/genai/use-generation-records.ts
+++ b/apps/web/src/components/genai/use-generation-records.ts
@@ -16,7 +16,7 @@ import {
PROMPT_INPUT_VOICE_MODEL,
PROMPT_INPUT_VOICE_OPTIONS,
} from "./config";
-import { generationConfigSchema, type GenerationConfig } from "@/components/engine/db";
+import { generationConfigSchema, type GenerationConfig } from "./schemas";
import type { AssetInput, AssetSpecInput } from "@diffusionstudio/jsx";
import type { AssetLibrary } from "@diffusionstudio/assets";
diff --git a/apps/web/src/components/sidebar-left/project-menu/file-menu.tsx b/apps/web/src/components/sidebar-left/project-menu/file-menu.tsx
index c312d5e1..24ebd839 100644
--- a/apps/web/src/components/sidebar-left/project-menu/file-menu.tsx
+++ b/apps/web/src/components/sidebar-left/project-menu/file-menu.tsx
@@ -15,7 +15,7 @@ import {
import { useNavigate } from "@solidjs/router";
import { For, Show, createMemo } from "solid-js";
import { toast } from "somoto";
-import { forgetProjectBundle, generateProjectName } from "@/components/engine/db";
+import { forgetProjectBundle, generateProjectName } from "@/lib/db";
import { createProject, deleteProject, duplicateProject, ensureProjectsRoot } from "@/projects";
import { AssetId, ChildOf, Name, Root, Scene, getAssetFile, getActiveEntity, sortByItemIndex } from "@diffusionstudio/runtime";
import { assetName } from "@diffusionstudio/assets";
diff --git a/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx b/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx
index a4512a8d..fb61cd95 100644
--- a/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx
@@ -92,8 +92,7 @@ export function getFillTab(paint: PaintType): FillTab {
* property write but a swap: the new element is inserted where the old one
* stood and the old one removed. The picker therefore hands its caller the
* entity it ends up with, and what a swap carries over is what the new kind's
- * defaults say — the same values the bitecs panel seeded, since a color is
- * not a gradient and neither is a picture.
+ * defaults say, since a color is not a gradient and neither is a picture.
*/
export function FillPicker(props: FillPickerProps) {
const world = useWorld();
diff --git a/apps/web/src/components/ui/keyframe-bitecs.tsx b/apps/web/src/components/ui/keyframe-bitecs.tsx
deleted file mode 100644
index 949767a1..00000000
--- a/apps/web/src/components/ui/keyframe-bitecs.tsx
+++ /dev/null
@@ -1,74 +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 { useEngine } from "@/context/engine";
-import { findKeyframeTargetNode } from "@/components/engine/api";
-import { cx } from "@/lib/cva";
-import { createMemo, Show } from "solid-js";
-import { Tooltip, TooltipContent, TooltipTrigger } from "./tooltip";
-import { getParentEntity, toggleKeyframeTrack, useEntityState, type PropertyPath } from "../engine";
-
-type KeyframeProps = {
- target: number;
- property: PropertyPath;
- disabled?: boolean;
- class?: string;
-};
-
-export function Keyframe(props: KeyframeProps) {
- const { world } = useEngine();
- const c = world.components;
-
- const nodeEid = createMemo(() => findKeyframeTargetNode(world, props.target));
- const isAvailable = createMemo(() => getParentEntity(world, props.target) !== null && nodeEid() !== null);
-
- const tracks = useEntityState(c.Cache.keyframeTracks, nodeEid, []);
- const localFrame = useEntityState(c.Computed.localTime, nodeEid, 0);
-
- const matchedTrack = createMemo(() => {
- for (const tid of tracks()) {
- if (c.KeyframeTrack.property[tid] !== props.property) continue;
- if (c.KeyframeTrack.target[tid] !== props.target) continue;
- return tid;
- }
- return null;
- });
-
- const hasTrack = createMemo(() => matchedTrack() !== null);
-
- const keyframes = useEntityState(c.Cache.keyframes, matchedTrack, []);
-
- const isActive = createMemo(() => {
- return keyframes().some(kf => Math.round(c.Keyframe.time[kf]) === localFrame());
- });
-
- const toggleKeyframe = () => toggleKeyframeTrack(world, props.target, props.property);
-
- return (
- <Show when={isAvailable()}>
- <Tooltip openDelay={600} disabled={props.disabled}>
- <TooltipTrigger
- as="button"
- type="button"
- class={cx("flex items-center size-6 justify-center transition-colors group disabled:cursor-not-allowed disabled:opacity-50", props.class)}
- onClick={toggleKeyframe}
- disabled={props.disabled}
- >
- <div
- class={cx(
- "size-1.5 rotate-45 border transition-colors",
- isActive()
- ? "bg-primary"
- : "bg-transparent group-hover:border-foreground",
- hasTrack()
- ? "border-primary"
- : "border-muted-foreground"
- )}
- />
- </TooltipTrigger>
- <TooltipContent>{isActive() ? "Remove keyframe" : "Add keyframe"}</TooltipContent>
- </Tooltip>
- </Show>
- )
-}
diff --git a/apps/web/src/context/dapi/api.tsx b/apps/web/src/context/dapi/api.tsx
index 5daa51d7..c6b6e6af 100644
--- a/apps/web/src/context/dapi/api.tsx
+++ b/apps/web/src/context/dapi/api.tsx
@@ -3,8 +3,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { createEffect, createContext, useContext, onCleanup } from "solid-js";
-import { useEngine } from '@/context/engine';
import { useWorld } from '@diffusionstudio/koota-solid';
+import { Project } from '@diffusionstudio/runtime';
import { useProject } from '@/context/project';
import { useAuth } from '@/context/auth';
import { t, q, q0 } from "@/lib/cli-rpc";
@@ -22,7 +22,6 @@ import { useFullscreenState } from "@/hooks/use-fullscreen-state";
import type { JSX, Accessor } from 'solid-js';
import type { User } from '@supabase/supabase-js';
-import type { Engine } from '@/components/engine';
import type { World } from 'koota';
type EditorApiProviderProps = {
@@ -38,7 +37,6 @@ const EditorApiContext = createContext<EditorApiContextValue>();
export function EditorApiProvider(props: EditorApiProviderProps) {
const project = useProject();
- const engine = useEngine();
const auth = useAuth();
const isFullscreen = useFullscreenState();
@@ -53,14 +51,12 @@ export function EditorApiProvider(props: EditorApiProviderProps) {
return user;
};
- const getEngine = () => engine;
const world = useWorld();
createEffect(() => {
- if (!window.desktop || !engine.initialized() || project.id() !== engine.world.projectId) return;
+ if (!window.desktop || project.id() !== world.get(Project)?.id) return;
-
- const router = createAppRouter({ getEngine, world, getUser, requireAuth });
+ const router = createAppRouter({ world, getUser, requireAuth });
onCleanup(cliBridge.register(createRouterCaller(router)));
});
@@ -78,18 +74,17 @@ export function EditorApiProvider(props: EditorApiProviderProps) {
type AppRouterDeps = {
- getEngine: () => Engine;
world: World;
getUser: () => User;
requireAuth: <I, O>(fn: (data: I) => Promise<O>) => (data: I) => Promise<O>;
};
-function createAppRouter({ getEngine, world, getUser, requireAuth }: AppRouterDeps) {
+function createAppRouter({ world, getUser, requireAuth }: AppRouterDeps) {
return t.router({
ping: t.procedure.query(() => {}),
whoami: t.procedure.query(() => getUser()),
- context: q0(handleContextGet(getEngine)),
- capture: q(handleCapture(getEngine)),
+ context: q0(handleContextGet(world)),
+ capture: q(handleCapture(world)),
models: q(handleModels()),
logs: q(handleLogs()),
screenshot: q0(handleWindowScreenshot()),
diff --git a/apps/web/src/context/dapi/capture.ts b/apps/web/src/context/dapi/capture.ts
index 32ea9a11..6d40e7a5 100644
--- a/apps/web/src/context/dapi/capture.ts
+++ b/apps/web/src/context/dapi/capture.ts
@@ -2,64 +2,49 @@
* 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 { entityExists, hasComponent } from "bitecs";
-
-import { createImageEncoder } from "@/components/engine/encode/image-encoder";
-
+import { AdjustmentLayer, Geometry, Group, Scene } from "@diffusionstudio/runtime";
import {
- getSceneAncestor,
composeSheet,
+ createImageEncoder,
decodePngBase64,
planSheet,
planSheetSizes,
sheetTimecode,
-} from "@/components/engine";
+} from "@diffusionstudio/encoder";
+
+import { createCapture } from "@/engine/capture";
-import type { EngineWorld, Engine } from "@/components/engine";
import type { CaptureRequest, CaptureResult, TimecodedImage } from "@diffusionstudio/cli/channels";
-import type { Accessor } from "solid-js";
+import type { Entity, World } from "koota";
// Scenes are nodes too, so capture accepts them alongside geometry, groups,
// and adjustment layers.
-function isNode(world: EngineWorld, eid: number): boolean {
- const c = world.components;
- return (
- hasComponent(world, eid, c.Geometry) ||
- hasComponent(world, eid, c.Group) ||
- hasComponent(world, eid, c.AdjustmentLayer) ||
- hasComponent(world, eid, c.Scene)
- );
+function isNode(entity: Entity): boolean {
+ return entity.has(Geometry)
+ || entity.has(Group)
+ || entity.has(AdjustmentLayer)
+ || entity.has(Scene);
}
-function resolveNodeEid(world: EngineWorld, eid: number): number {
- if (
- !Number.isInteger(eid) ||
- !entityExists(world, eid) ||
- hasComponent(world, eid, world.components.Deleted) ||
- !isNode(world, eid)
- ) {
- throw new Error(`No such node: ${eid}`);
+/**
+ * The entity `id` names. Entities travel to the CLI as their koota values
+ * (`dapi context` hands them out), so an id is one straight back — it only
+ * has to still be alive and still be something worth drawing.
+ */
+function resolveNode(world: World, id: number): Entity {
+ const entity = id as Entity;
+ if (!Number.isInteger(id) || id < 0 || !world.has(entity) || !isNode(entity)) {
+ throw new Error(`No such node: ${id}`);
}
- return eid;
+ return entity;
}
// Ceiling on the height a sheet cell renders a node at.
const SHEET_CAPTURE_HEIGHT = 1080;
-export function handleCapture(engine: Accessor<Engine>) {
+export function handleCapture(world: World) {
return async ({ id, frames, combine = true, perSheet }: CaptureRequest): Promise<CaptureResult> => {
- const e = engine();
- const w = e.world;
-
- const eid = resolveNodeEid(w, id);
-
- if (!hasComponent(w, eid, w.components.Playback)) {
- const scene = getSceneAncestor(w, eid);
- const hint = scene !== null ? ` Capture its scene (id ${scene}) instead.` : "";
- throw new Error(
- `Node ${id} has no timeline clock and cannot be captured on its own.${hint}`,
- );
- }
+ const node = resolveNode(world, id);
// `undefined` means the node's first visible frame (the encoder's frame 0).
let shots = frames;
@@ -67,47 +52,54 @@ export function handleCapture(engine: Accessor<Engine>) {
shots = [0];
}
- const encoder = await createImageEncoder(w, {
- eid,
- frames: shots,
- resolution: 720,
- });
+ // The project re-rendered into a world of its own, reduced to this node:
+ // the same arrangement an export runs against, and the encoder's to draw.
+ const capture = await createCapture(world, node);
- // Sheets render at their cell size instead of the flat 720p: with a few
- // frames that is sharper than a standalone capture, never coarser. A node
- // is drawn, not decoded, so a small one is worth rendering past its own
- // bounds; beyond SHEET_CAPTURE_HEIGHT that only costs tokens.
- const aspect = encoder.bounds.width / encoder.bounds.height;
- const height = Math.max(encoder.bounds.height, SHEET_CAPTURE_HEIGHT);
- const sizes = combine ? planSheetSizes(shots.length, perSheet) : [];
- const plans = sizes.map((n) => planSheet(n, { width: height * aspect, height }));
- if (combine) {
- encoder.resize(Math.max(...plans.map((plan) => plan.cellHeight)));
- }
-
- const result = await encoder.render();
-
- if (result.type === "canceled") throw new Error("Capture canceled");
- if (result.type === "error") throw result.error;
-
- if (!combine) return result.data;
-
- const sheets: TimecodedImage[] = [];
- let offset = 0;
- for (const [sheet, size] of sizes.entries()) {
- const group = result.data.slice(offset, offset + size);
- const cells = group.map(({ timecode }, k) => ({ at: shots[offset + k], timecode }));
- offset += size;
- const images = await Promise.all(group.map((image) => decodePngBase64(image.base64)));
- sheets.push({
- timecode: sheetTimecode(cells),
- base64: composeSheet(
- images.map((image, k) => ({ image, label: group[k].timecode })),
- plans[sheet],
- ),
+ try {
+ const encoder = await createImageEncoder(capture.world, {
+ frames: shots,
+ resolution: 720,
});
- for (const image of images) image.close();
+
+ // Sheets render at their cell size instead of the flat 720p: with a few
+ // frames that is sharper than a standalone capture, never coarser. A node
+ // is drawn, not decoded, so a small one is worth rendering past its own
+ // bounds; beyond SHEET_CAPTURE_HEIGHT that only costs tokens.
+ const aspect = encoder.bounds.width / encoder.bounds.height;
+ const height = Math.max(encoder.bounds.height, SHEET_CAPTURE_HEIGHT);
+ const sizes = combine ? planSheetSizes(shots.length, perSheet) : [];
+ const plans = sizes.map((n) => planSheet(n, { width: height * aspect, height }));
+ if (combine) {
+ encoder.resize(Math.max(...plans.map((plan) => plan.cellHeight)));
+ }
+
+ const result = await encoder.render();
+
+ if (result.type === "canceled") throw new Error("Capture canceled");
+ if (result.type === "error") throw result.error;
+
+ if (!combine) return result.data;
+
+ const sheets: TimecodedImage[] = [];
+ let offset = 0;
+ for (const [sheet, size] of sizes.entries()) {
+ const group = result.data.slice(offset, offset + size);
+ const cells = group.map(({ timecode }, k) => ({ at: shots[offset + k], timecode }));
+ offset += size;
+ const images = await Promise.all(group.map((image) => decodePngBase64(image.base64)));
+ sheets.push({
+ timecode: sheetTimecode(cells),
+ base64: await composeSheet(
+ images.map((image, k) => ({ image, label: group[k].timecode })),
+ plans[sheet],
+ ),
+ });
+ for (const image of images) image.close();
+ }
+ return sheets;
+ } finally {
+ capture.dispose();
}
- return sheets;
};
}
diff --git a/apps/web/src/context/dapi/context.ts b/apps/web/src/context/dapi/context.ts
index 5077ff9e..dc8ce288 100644
--- a/apps/web/src/context/dapi/context.ts
+++ b/apps/web/src/context/dapi/context.ts
@@ -2,49 +2,36 @@
* 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 { ChildOf } from "@/components/engine/components";
-import { getAllEntities, hasComponent, Not, Or, query } from "bitecs";
-
-import type { Engine, EngineWorld } from "@/components/engine";
-import type { Accessor } from "solid-js";
-
-function selectedNodeIds(world: EngineWorld): number[] {
- const c = world.components;
- return [...query(world, [c.Selected, Or(c.Geometry, c.Group, c.AdjustmentLayer), Not(c.Deleted)])];
-}
-
-export function handleContextGet(engine: Accessor<Engine>) {
+import { Or } from "koota";
+import {
+ Active, AdjustmentLayer, Computed, Fonts, FrameRate, Geometry, Group,
+ Project, Scene, Selected, Workarea,
+} from "@diffusionstudio/runtime";
+
+import type { World } from "koota";
+
+/**
+ * What `dapi context` reports: enough of the open project for a caller with
+ * no window onto it to say what to look at next. Entities travel as their
+ * koota values, which is what `capture` takes back.
+ */
+export function handleContextGet(world: World) {
return async () => {
- const { world } = engine();
- const c = world.components;
-
- const scenes = [...query(world, [c.Geometry, c.Scene, Not(ChildOf('*')), Not(c.Deleted)])];
- const entityCount = getAllEntities(world).filter((eid) => !hasComponent(world, eid, c.Deleted)).length;
- const activeSceneId = world.selection.scene !== null ? world.selection.scene : null;
- const currentFrame = activeSceneId !== null ? (c.Computed.localTime[activeSceneId] ?? 0) : 0;
-
- const fps = world.frameRate;
- const workarea =
- activeSceneId !== null && hasComponent(world, activeSceneId, c.Workarea)
- ? {
- start: (c.Workarea.start[activeSceneId] ?? 0) / fps,
- end: (c.Workarea.end[activeSceneId] ?? 0) / fps,
- }
- : null;
+ const scenes = [...world.query(Scene)];
+ const active = world.queryFirst(Active) ?? null;
- const fontFamilies = [...new Set(["Inter", ...world.fonts.map((f) => f.family)])];
- const projectId = world.projectId;
- const selection = selectedNodeIds(world);
+ const fps = world.get(FrameRate)?.value ?? 30;
+ const workarea = active?.get(Workarea);
return {
- projectId,
- entityCount,
+ projectId: world.get(Project)?.id ?? '',
+ entityCount: world.entities.length,
scenes,
- activeSceneId,
- currentFrame,
- workarea,
- fontFamilies,
- selection,
+ activeSceneId: active,
+ currentFrame: active?.get(Computed)?.localTime ?? 0,
+ workarea: workarea ? { start: workarea.start / fps, end: workarea.end / fps } : null,
+ fontFamilies: [...new Set(["Inter", ...(world.get(Fonts)?.list ?? []).map((f) => f.family)])],
+ selection: [...world.query(Selected, Or(Geometry, Group, AdjustmentLayer))],
};
}
}
diff --git a/apps/web/src/context/dapi/media.ts b/apps/web/src/context/dapi/media.ts
index e0c953a1..0e6bdaf6 100644
--- a/apps/web/src/context/dapi/media.ts
+++ b/apps/web/src/context/dapi/media.ts
@@ -5,9 +5,10 @@
import { ALL_FORMATS, BlobSource, CanvasSink, Input } from 'mediabunny';
import { pickInformativeTimes } from './frame-triage';
import { trpc } from '@/lib/trpc';
-import { uploadBlob, composeSheet, planSheet, planSheetSizes, sheetTimecode } from '@/components/engine';
+import { uploadBlob } from '@/lib/uploads';
+import { composeSheet, planSheet, planSheetSizes, sheetTimecode } from '@diffusionstudio/encoder';
import { assert } from '@/utils';
-import { startResumableSession, uploadResumableStream } from '@/components/engine';
+import { startResumableSession, uploadResumableStream } from '@/lib/uploads';
import { filmstripAsset, formatTimecode, getAsset, getAssetFile, getLibrary, Project, transcodeForAnalysis, transcodeForTranscription, waveformAsset } from '@diffusionstudio/runtime';
import { assetName } from '@diffusionstudio/assets';
@@ -226,7 +227,7 @@ export function handleMediaFrame(world: World) {
const to = from + sizes[sheet];
result[sheet] = {
timecode: sheetTimecode(cells.slice(from, to)),
- base64: composeSheet(
+ base64: await composeSheet(
canvases.slice(from, to).map((canvas, k) => ({ image: canvas!, label: cells[from + k].timecode })),
plans[sheet],
),
diff --git a/apps/web/src/context/ecs.tsx b/apps/web/src/context/ecs.tsx
deleted file mode 100644
index 01d01313..00000000
--- a/apps/web/src/context/ecs.tsx
+++ /dev/null
@@ -1,69 +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 { hasComponent, Or } from 'bitecs';
-import { createContext, createMemo, useContext, type Accessor, type JSX } from 'solid-js';
-import { assert } from '@/utils';
-import { useEntityState, useQuery, useWorldState } from '@/components/engine/hooks';
-import { ToolType } from '@/components/engine';
-import { useEngine } from './engine';
-
-type ECSContextValue = {
- selectedTool: Accessor<ToolType>;
- selectedNodes: Accessor<Set<number>>;
- selectedKeyframes: Accessor<Set<number>>;
- now: Accessor<number>;
- playing: Accessor<number>;
- looping: Accessor<number>;
-};
-
-const ECSContext = createContext<ECSContextValue>();
-
-export function ECSProvider(props: { children: JSX.Element }) {
- const { world } = useEngine();
- const c = world.components;
-
- const scene = () => world.timelineIndex().root;
-
- // Node selection: filter Selected to scene nodes — geometries or groups
- // (excludes selected keyframes).
- const selection = useQuery([c.Selected, Or(c.Geometry, c.Group, c.AdjustmentLayer, c.Keyframe)]);
-
- const selectedNodes = createMemo(() => {
- const nodes = selection().filter(eid =>
- hasComponent(world, eid, c.Geometry)
- || hasComponent(world, eid, c.Group)
- || hasComponent(world, eid, c.AdjustmentLayer));
- return new Set(nodes);
- });
- const selectedKeyframes = createMemo(() => {
- const keyframes = selection().filter(eid => hasComponent(world, eid, c.Keyframe));
- return new Set(keyframes);
- });
-
- const selectedTool = useWorldState(w => w.selection.tool, ToolType.HAND);
- const now = useEntityState(c.Computed.localTime, scene, 0);
- const playing = useEntityState(c.Playback.playing, scene, 0);
- const looping = useEntityState(c.Playback.loop, scene, 0);
-
- return (
- <ECSContext.Provider
- value={{
- selectedTool,
- selectedNodes,
- selectedKeyframes,
- playing,
- looping,
- now,
- }}>
- {props.children}
- </ECSContext.Provider>
- );
-}
-
-export function useECS() {
- const ctx = useContext(ECSContext);
- assert(ctx, 'useECS must be used within ECSProvider');
- return ctx;
-}
diff --git a/apps/web/src/context/engine.tsx b/apps/web/src/context/engine.tsx
deleted file mode 100644
index 3cabd1de..00000000
--- a/apps/web/src/context/engine.tsx
+++ /dev/null
@@ -1,193 +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 { createEngine, invalidateAssets } from '@/components/engine';
-import { clearAudioPeaksCache, clearAudioTrackCache, clearKeyframeIndexCache, clearVideoTrackCache } from '@/components/engine/decoders';
-import { clearImageThumbnailCache } from '@/components/engine/timeline/render/image';
-import { assert } from '@/utils';
-import { createContext, onCleanup, onMount, useContext, createSignal, Show } from 'solid-js';
-import { verifyHandlePermissions } from "@/utils/browser";
-import { Button } from "@/components/ui/button";
-import {
- Dialog,
- DialogContent,
- DialogDescription,
- DialogFooter,
- DialogHeader,
- DialogPortal,
- DialogTitle,
-} from "@/components/ui/dialog";
-import { disposeDecoders } from '@/components/engine/api/utils';
-import { getAllEntities } from 'bitecs';
-
-import type { JSX } from 'solid-js';
-import type { ElectronFileHandle } from '@/lib/electron-file-handle';
-
-type EngineContextValue = ReturnType<typeof createEngine>;
-
-const EngineContext = createContext<EngineContextValue>();
-
-type EngineProviderProps = {
- children: JSX.Element;
- projectId: string;
-};
-
-type GrantableHandle = FileSystemDirectoryHandle | FileSystemFileHandle | ElectronFileHandle;;
-
-// Project-swap readiness barrier.
-const readyWaiters = new Map<string, Set<() => void>>();
-let readyProjectId: string | null = null;
-
-function markProjectReady(id: string): void {
- readyProjectId = id;
- const waiters = readyWaiters.get(id);
- if (!waiters) return;
- readyWaiters.delete(id);
- for (const resolve of waiters) resolve();
-}
-
-export function whenProjectReady(id: string, timeoutMs = 30_000): Promise<void> {
- if (readyProjectId === id) return Promise.resolve();
-
- const { promise, resolve } = Promise.withResolvers<void>();
- const waiters = readyWaiters.get(id) ?? new Set<() => void>();
- waiters.add(resolve);
- readyWaiters.set(id, waiters);
-
- // Best-effort: a project whose engine never initializes (init threw) must
- // not hang the caller forever.
- const timer = setTimeout(() => {
- readyWaiters.get(id)?.delete(resolve);
- console.warn(`[engine] timed out waiting for project ${id} to become ready`);
- resolve();
- }, timeoutMs);
-
- return promise.then(() => clearTimeout(timer));
-}
-
-export function EngineProvider(props: EngineProviderProps) {
- const engine = createEngine(props.projectId);
- const resizeObserver = new ResizeObserver(() => engine.resize());
-
- const [open, setOpen] = createSignal(false);
- const [handles, setHandles] = createSignal<GrantableHandle[]>([]);
- const [granting, setGranting] = createSignal(false);
- let resolveCurrent: (() => void) | null = null;
-
- const showGrantPermissionDialog = (pending: GrantableHandle[]) => {
- if (pending.length === 0) return Promise.resolve();
- // Resolve any prior pending call so callers don't deadlock if show()
- // is invoked again before the previous dialog closes.
- resolveCurrent?.();
- const { promise, resolve } = Promise.withResolvers<void>();
- resolveCurrent = resolve;
- setHandles(pending);
- setOpen(true);
- return promise;
- };
-
- const handleOpenChange = (next: boolean) => {
- setOpen(next);
- if (!next) {
- resolveCurrent?.();
- resolveCurrent = null;
- }
- };
-
- const handleGrant = async () => {
- if (granting()) return;
- setGranting(true);
- try {
- await verifyHandlePermissions(handles(), "read");
- } finally {
- setGranting(false);
- handleOpenChange(false);
- }
- };
-
- onMount(async () => {
- const canvas = document.getElementById('engine-canvas') as HTMLCanvasElement;
- resizeObserver.observe(canvas.parentElement!);
-
- await engine.init(canvas);
-
- markProjectReady(props.projectId);
-
- // Detect filesystem handles whose permission was lost across reloads, then
- // open a dialog so the user can re-grant in a real click handler — the
- // requestPermission API requires a transient user activation.
- const pending: GrantableHandle[] = [];
- for (const asset of engine.world.assets.values()) {
- if (asset.handle) {
- const state = await asset.handle.queryPermission({ mode: 'read' });
- if (state !== 'granted') pending.push(asset.handle);
- }
- if (asset.type === 'SEQUENCE') {
- const state = await asset.directoryHandle.queryPermission({ mode: 'read' });
- if (state !== 'granted') pending.push(asset.directoryHandle);
- }
- }
-
- if (pending.length > 0) {
- await showGrantPermissionDialog(pending);
-
- // Permission re-granted (or dialog dismissed): drop cached decoders/tracks
- // so they refetch with the new permission, and bump the assets version to
- // retrigger resource consumers (thumbnails, sidebar previews, etc).
- clearAudioPeaksCache();
- clearAudioTrackCache();
- clearVideoTrackCache();
- clearKeyframeIndexCache();
- clearImageThumbnailCache();
-
- for (const eid of getAllEntities(engine.world)) {
- disposeDecoders(engine.world, eid);
- }
-
- invalidateAssets(engine.world);
- }
- });
-
- onCleanup(() => {
- if (readyProjectId === props.projectId) readyProjectId = null;
-
- resizeObserver.disconnect();
- engine.dispose();
- });
-
- return (
- <EngineContext.Provider value={engine}>
- {props.children}
- <Dialog open={open()} onOpenChange={handleOpenChange}>
- <DialogPortal>
- <DialogContent class="sm:max-w-sm p-0 overflow-hidden" showCloseButton={false}>
- <img src="permission-dialog-header.png" alt="Restore access to your media" class="w-full h-auto" />
- <div class="px-4 pb-4">
- <DialogHeader>
- <DialogTitle class="text-[12px] font-450">Restore access to your files</DialogTitle>
- <DialogDescription class="text-xs text-muted-foreground leading-relaxed">
- This project references {handles().length} files from your filesystem. Grant access to load them. Enable <span class="font-450 text-foreground">Allow on every visit</span> in your browser settings to avoid this dialog in the future.
- </DialogDescription>
- </DialogHeader>
- <DialogFooter>
- <Button class="w-full mt-3" variant="secondary" disabled={granting()} onClick={handleGrant}>
- Grant access
- </Button>
- </DialogFooter>
- </div>
- </DialogContent>
- </DialogPortal>
- </Dialog>
- <Show when={!engine.initialized() && !open()}>
- <div class="fixed inset-0 z-50 bg-overlay animate-out fade-out-0 duration-200" />
- </Show>
- </EngineContext.Provider>
- );
-}
-
-export function useEngine() {
- const ctx = useContext(EngineContext);
- assert(ctx, 'useEngine must be used within EngineProvider');
- return ctx;
-}
diff --git a/apps/web/src/context/prompt-input.tsx b/apps/web/src/context/prompt-input.tsx
index 148e830d..7777b575 100644
--- a/apps/web/src/context/prompt-input.tsx
+++ b/apps/web/src/context/prompt-input.tsx
@@ -4,7 +4,7 @@
import { createContext, createSignal, useContext } from "solid-js";
import type { JSX } from "solid-js";
-import type { GenerationConfig } from "@/components/engine/db";
+import type { GenerationConfig } from "@/components/genai/schemas";
type PromptInputContextValue = {
openPromptInput: (config: GenerationConfig) => void;
diff --git a/apps/web/src/engine/capture.ts b/apps/web/src/engine/capture.ts
index 9f1a1eba..f3215425 100644
--- a/apps/web/src/engine/capture.ts
+++ b/apps/web/src/engine/capture.ts
@@ -27,7 +27,7 @@ import {
createRuntimeWorld, disposeDecoders, getParentNode, removeChild, resetCamera,
} from '@diffusionstudio/runtime';
-import { loadProjectBundle } from '@/components/engine/db';
+import { loadProjectBundle } from '@/lib/db';
import type { RuntimeMode } from '@diffusionstudio/runtime';
import type { Entity, World } from 'koota';
diff --git a/apps/web/src/engine/input/interactions.ts b/apps/web/src/engine/input/interactions.ts
index ce50f05c..ba98782d 100644
--- a/apps/web/src/engine/input/interactions.ts
+++ b/apps/web/src/engine/input/interactions.ts
@@ -11,9 +11,9 @@
* Every change goes through `DocumentEditor`, because the source is the
* document: dragging a node writes `x`/`y` back to the element it came from,
* and dropping it into a scene moves the element under that scene's, the same
- * way the inspector or a hand edit would. One bitecs behavior did not come
- * along, for want of a way to say it: alt-drag duplication, which would have
- * to author a copy of a subtree the JSX may well be generating.
+ * way the inspector or a hand edit would. Alt-drag duplication is the one
+ * gesture with no spelling here: it would have to author a copy of a subtree
+ * the JSX may well be generating.
*/
import {
diff --git a/apps/web/src/components/engine/db/global-db.ts b/apps/web/src/lib/db.ts
similarity index 100%
rename from apps/web/src/components/engine/db/global-db.ts
rename to apps/web/src/lib/db.ts
diff --git a/apps/web/src/lib/electron-file-writable.ts b/apps/web/src/lib/electron-file-writable.ts
index c6c2ecf6..9669fbfe 100644
--- a/apps/web/src/lib/electron-file-writable.ts
+++ b/apps/web/src/lib/electron-file-writable.ts
@@ -6,7 +6,7 @@ import { mainBridge } from "@/lib/ipc";
import { MAIN_CHANNELS } from "@desktop/main-channels";
import type { StreamTargetChunk } from "mediabunny";
-import type { WritableFileTarget } from "@/components/engine/encode/types";
+import type { WritableFileTarget } from "@diffusionstudio/encoder";
// A write-only, FileSystemFileHandle-shaped target backed by the main process.
// `createWritable()` returns a stream whose positioned chunks are forwarded to
diff --git a/apps/web/src/lib/uploads.ts b/apps/web/src/lib/uploads.ts
new file mode 100644
index 00000000..4a004561
--- /dev/null
+++ b/apps/web/src/lib/uploads.ts
@@ -0,0 +1,127 @@
+/* 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/. */
+
+/**
+ * Getting bytes into the cloud bucket the API resolves a `FileRef` against.
+ * A whole Blob goes in one signed PUT; anything produced as it is encoded
+ * (a transcode feeding a generation) goes through a GCS resumable session so
+ * the payload is never held in memory in full.
+ */
+
+import { trpc } from "@/lib/trpc";
+
+/**
+ * Upload a Blob to the cloud bucket via a signed URL under the asset id.
+ * Returns the FileRef the API can resolve.
+ */
+export async function uploadBlob(blob: Blob, id: string) {
+ const contentType = blob.type || "application/octet-stream";
+ const { uploadUrl, fileRef } = await trpc.getUploadUrl.mutate({ id, contentType });
+
+ if (uploadUrl) {
+ const res = await fetch(uploadUrl, {
+ method: "PUT",
+ headers: { "Content-Type": contentType },
+ body: blob,
+ });
+
+ if (!res.ok) {
+ console.error(`Failed to upload asset ${id}: ${res.status}`);
+ return null;
+ }
+ }
+
+ return fileRef;
+}
+
+/** GCS requires every resumable chunk except the last to be a multiple of 256 KiB. */
+const RESUMABLE_CHUNK_SIZE = 8 * 1024 * 1024;
+
+/**
+ * Open a GCS resumable upload session against a signed "resumable" URL and
+ * return the session URI that the bytes are streamed to.
+ */
+export async function startResumableSession(uploadUrl: string, mimeType: string): Promise<string> {
+ const res = await fetch(uploadUrl, {
+ method: "POST",
+ headers: { "x-goog-resumable": "start", "Content-Type": mimeType },
+ });
+ if (!res.ok) {
+ throw new Error(`Failed to start resumable upload session: ${res.status}`);
+ }
+ const sessionUrl = res.headers.get("Location");
+ if (!sessionUrl) {
+ throw new Error("Resumable upload session response is missing the Location header.");
+ }
+ return sessionUrl;
+}
+
+/**
+ * Stream a ReadableStream into an open GCS resumable session, PUTting
+ * fixed-size chunks as bytes arrive so the full payload is never buffered.
+ */
+export async function uploadResumableStream(
+ readable: ReadableStream<Uint8Array<ArrayBuffer>>,
+ sessionUrl: string,
+): Promise<void> {
+ const reader = readable.getReader();
+ let pending: Uint8Array<ArrayBuffer> = new Uint8Array(0);
+ let uploaded = 0;
+ let done = false;
+
+ while (!done) {
+ const { value, done: streamDone } = await reader.read();
+ if (streamDone) {
+ done = true;
+ } else if (value) {
+ pending = concatBytes(pending, value);
+ }
+
+ while (pending.length >= RESUMABLE_CHUNK_SIZE) {
+ await putResumableChunk(sessionUrl, pending.subarray(0, RESUMABLE_CHUNK_SIZE), uploaded, null);
+ uploaded += RESUMABLE_CHUNK_SIZE;
+ pending = pending.subarray(RESUMABLE_CHUNK_SIZE);
+ }
+ }
+
+ await putResumableChunk(sessionUrl, pending, uploaded, uploaded + pending.length);
+}
+
+async function putResumableChunk(
+ sessionUrl: string,
+ chunk: Uint8Array<ArrayBuffer>,
+ offset: number,
+ total: number | null,
+): Promise<void> {
+ const last = offset + chunk.length - 1;
+ const contentRange =
+ total === null
+ ? `bytes ${offset}-${last}/*`
+ : chunk.length === 0
+ ? `bytes */${total}`
+ : `bytes ${offset}-${last}/${total}`;
+
+ const res = await fetch(sessionUrl, {
+ method: "PUT",
+ headers: { "Content-Range": contentRange },
+ body: chunk,
+ });
+
+ // 308 "Resume Incomplete" is expected while more chunks remain; the final
+ // chunk finalizes the object and returns 200/201.
+ if (total === null) {
+ if (res.status !== 308) {
+ throw new Error(`Unexpected status during resumable upload: ${res.status}`);
+ }
+ } else if (!res.ok) {
+ throw new Error(`Failed to finalize resumable upload: ${res.status}`);
+ }
+}
+
+function concatBytes(a: Uint8Array<ArrayBuffer>, b: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer> {
+ const out = new Uint8Array(a.length + b.length);
+ out.set(a);
+ out.set(b, a.length);
+ return out;
+}
diff --git a/apps/web/src/pages/editor.tsx b/apps/web/src/pages/editor.tsx
index af3d1951..c4a9b47d 100644
--- a/apps/web/src/pages/editor.tsx
+++ b/apps/web/src/pages/editor.tsx
@@ -18,7 +18,7 @@ import { getDocumentEditor } from '@/engine/editor';
import { attachLibrary, isLibraryFile } from '@/engine/library';
import { attachAi } from '@/utils/gen-ai';
import { attachProjectConfig, isProjectConfigFile } from '@/engine/project-config';
-import { loadProjectBundle, rememberProjectBundle } from '@/components/engine/db';
+import { loadProjectBundle, rememberProjectBundle } from '@/lib/db';
import { isCacheFile } from '@diffusionstudio/assets';
import { createEditWriter } from '@/projects/edits';
import { compileProject, watchProject } from '@/projects/host';
diff --git a/apps/web/src/pages/project.tsx b/apps/web/src/pages/project.tsx
index 1a3ee338..f6cf3ada 100644
--- a/apps/web/src/pages/project.tsx
+++ b/apps/web/src/pages/project.tsx
@@ -7,15 +7,13 @@ import { Navigate, useNavigate } from '@solidjs/router';
import { EditorPage } from './editor';
import { LayoutProvider } from "@/context/layout";
import { PromptInputProvider } from "@/context/prompt-input";
-import { EngineProvider } from '@/context/engine';
import { EditorApiProvider } from '@/context/dapi';
import { ExportProvider } from '@/context/export';
import { ProjectProvider } from '@/context/project';
import { projectRoute, useProjectRef } from '@/hooks/use-project-route';
import { resolveProject } from '@/projects';
-import { ECSProvider } from '@/context/ecs';
import { TimelineProvider } from '@/context/timeline';
-import { EngineProvider as KootaEngineProvider } from '@/engine';
+import { EngineProvider } from '@/engine';
/** `/projects/*ref` — the editor, with the project `ref` names loaded. */
export function ProjectPage() {
@@ -56,23 +54,19 @@ export function ProjectPage() {
<Show when={project()} keyed fallback={<Navigate href="/" />}>
{(found) => (
<ProjectProvider project={found}>
- <KootaEngineProvider projectId={found.id}>
- <EngineProvider projectId={found.id}>
- <EditorApiProvider>
- <TimelineProvider>
- <ECSProvider>
- <ExportProvider>
- <PromptInputProvider>
- <LayoutProvider>
- <EditorPage />
- </LayoutProvider>
- </PromptInputProvider>
- </ExportProvider>
- </ECSProvider>
- </TimelineProvider>
- </EditorApiProvider>
- </EngineProvider>
- </KootaEngineProvider>
+ <EngineProvider projectId={found.id}>
+ <EditorApiProvider>
+ <TimelineProvider>
+ <ExportProvider>
+ <PromptInputProvider>
+ <LayoutProvider>
+ <EditorPage />
+ </LayoutProvider>
+ </PromptInputProvider>
+ </ExportProvider>
+ </TimelineProvider>
+ </EditorApiProvider>
+ </EngineProvider>
</ProjectProvider>
)}
</Show>
diff --git a/apps/web/src/projects/host.ts b/apps/web/src/projects/host.ts
index 13b7af37..44f4e62b 100644
--- a/apps/web/src/projects/host.ts
+++ b/apps/web/src/projects/host.ts
@@ -18,13 +18,13 @@ import { createSignal } from 'solid-js';
import { MAIN_CHANNELS } from '@desktop/main-channels';
import { mainBridge } from '@/lib/ipc';
-import { lastUsedProjectRoot, rememberProjectRoot } from '@/components/engine/db';
+import { lastUsedProjectRoot, rememberProjectRoot } from '@/lib/db';
import type { CompileResult, ProjectInfo, SourceEdit, WriteResult } from '@desktop/main-channels';
export type { CompileResult, ProjectInfo, SourceEdit, WriteResult };
-// The roots live in the app's IndexedDB (see @/components/engine/db) as a list
+// The roots live in the app's IndexedDB (see @/lib/db) as a list
// keyed by path. The app works against one of them — the one used last — but
// the store is already the list several roots will need, so growing into them
// is UI rather than a migration.
diff --git a/apps/web/src/utils/gen-ai.ts b/apps/web/src/utils/gen-ai.ts
index c3c951f5..8a5b5b3b 100644
--- a/apps/web/src/utils/gen-ai.ts
+++ b/apps/web/src/utils/gen-ai.ts
@@ -19,7 +19,7 @@ import {
PROMPT_INPUT_VOICE_OPTIONS,
} from "@/components/genai/config";
import { assert } from "@/utils";
-import { uploadBlob } from "@/components/engine";
+import { uploadBlob } from "@/lib/uploads";
import { track } from "@/lib/analytics";
import { trpc } from "@/lib/trpc";
import { toast } from "somoto";
diff --git a/package-lock.json b/package-lock.json
index 0e27a40e..8f643ed3 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -109,7 +109,6 @@
"@trpc/client": "^11.10.0",
"@trpc/server": "^11.10.0",
"animejs": "^4.4.1",
- "bitecs": "^0.4.0",
"cmdk-solid": "^1.1.2",
"colord": "^2.9.3",
"cva": "^1.0.0-beta.4",
@@ -5101,12 +5100,6 @@
"dev": true,
"license": "Apache-2.0"
},
- "node_modules/bitecs": {
- "version": "0.4.0",
- "resolved": "https://registry.npmjs.org/bitecs/-/bitecs-0.4.0.tgz",
- "integrity": "sha512-ho6Zop/L79DRTnBAfakPpGPuX7y0+lAjX06CpaAW+5tnAc7BH3L3RlSrWAXAqwnQGDZ10GsoxaxyTTsddlun3g==",
- "license": "MPL-2.0"
- },
"node_modules/bl": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz",
diff --git a/packages/encoder/src/encoder.ts b/packages/encoder/src/encoder.ts
index d6e6dac2..2f9950ec 100644
--- a/packages/encoder/src/encoder.ts
+++ b/packages/encoder/src/encoder.ts
@@ -466,8 +466,8 @@ export async function resolverSystem(world: World) {
/**
* Refresh every entity's accumulated delay + time bounds after raw store
* rewrites. propagateTimeRangeDown recomputes top-down and then re-derives
- * each parent after its children (bitecs used a two-pass Hierarchy(ChildOf)
- * walk; koota has no hierarchy-ordered queries).
+ * each parent after its children, since koota has no hierarchy-ordered
+ * queries to walk in one pass.
*/
export function recomputeAllTimeRanges(world: World): void {
const stage = world.get(Root)!;
diff --git a/packages/koota-solid/README.md b/packages/koota-solid/README.md
index ff79bd6f..ac22a504 100644
--- a/packages/koota-solid/README.md
+++ b/packages/koota-solid/README.md
@@ -1,6 +1,6 @@
# @diffusionstudio/koota-solid
-Solid bindings for [koota](https://github.com/pmndrs/koota): a port of `@koota/react` used while migrating the editor from bitecs to koota. The API mirrors `@koota/react` one to one, adapted to Solid's reactivity model.
+Solid bindings for [koota](https://github.com/pmndrs/koota): a port of `@koota/react`. The API mirrors `@koota/react` one to one, adapted to Solid's reactivity model.
## Usage
diff --git a/packages/koota-solid/package.json b/packages/koota-solid/package.json
index 31f3551b..4e57846c 100644
--- a/packages/koota-solid/package.json
+++ b/packages/koota-solid/package.json
@@ -1,7 +1,7 @@
{
"name": "@diffusionstudio/koota-solid",
"version": "0.1.0",
- "description": "Solid bindings for koota — a port of @koota/react used while migrating the editor from bitecs to koota.",
+ "description": "Solid bindings for koota — a port of @koota/react, adapted to Solid's reactivity model.",
"license": "MPL-2.0",
"type": "module",
"sideEffects": false,
diff --git a/packages/runtime/package.json b/packages/runtime/package.json
index df2ae955..6dfa3451 100644
--- a/packages/runtime/package.json
+++ b/packages/runtime/package.json
@@ -9,8 +9,7 @@
".": "./src/index.ts",
"./traits": "./src/traits/index.ts",
"./systems": "./src/systems/index.ts",
- "./media": "./src/media/index.ts",
- "./capture": "./src/capture/index.ts"
+ "./media": "./src/media/index.ts"
},
"scripts": {
"check": "tsc --noEmit"
diff --git a/packages/runtime/src/media/caption/utils.ts b/packages/runtime/src/media/caption/utils.ts
index a36db190..fe1bf7c9 100644
--- a/packages/runtime/src/media/caption/utils.ts
+++ b/packages/runtime/src/media/caption/utils.ts
@@ -68,7 +68,7 @@ export function findActiveGroup(groups: WordGroup[], relativeTime: number): numb
/**
* Playhead-driven caption text: a raw store write, no change events, so the
- * app never persists per-frame caption content (bitecs wrote the array too).
+ * app never persists per-frame caption content.
*/
export function setChars(world: World, entity: Entity, text: string) {
if (!entity.has(Chars)) entity.add(Chars);
diff --git a/packages/runtime/src/systems/transform.ts b/packages/runtime/src/systems/transform.ts
index bda0bfb0..410029f2 100644
--- a/packages/runtime/src/systems/transform.ts
+++ b/packages/runtime/src/systems/transform.ts
@@ -29,7 +29,7 @@ import type { Entity, World } from 'koota';
// This system writes derived per-frame data (LocalTransform, WorldTransform,
// WorldBounds, Computed group bounds) straight into trait stores: no
-// snapshots, no change events, matching the bitecs SoA hot path.
+// snapshots, no change events.
/**
* Compute the local 2D affine matrix from Offset, Rotation, Scale,
diff --git a/packages/runtime/src/traits/world.ts b/packages/runtime/src/traits/world.ts
index 87d50b50..e15325c0 100644
--- a/packages/runtime/src/traits/world.ts
+++ b/packages/runtime/src/traits/world.ts
@@ -11,10 +11,6 @@ import type { AssetLibrary } from '@diffusionstudio/assets';
import type { GenAi } from '../ai';
import type { Quad } from '../math/aabb';
-// Singleton state attached to the world itself (world.get/world.set).
-// Replaces the bitecs createWorld({...}) context bag. Only headless runtime
-// state lives here; editor state (input, HUD, history, persistence, asset
-// stores) is layered on by the app as its own world traits.
export const Project = trait({ id: '' });
diff --git a/packages/runtime/src/utils/sort.ts b/packages/runtime/src/utils/sort.ts
index 7f885697..b455dbd7 100644
--- a/packages/runtime/src/utils/sort.ts
+++ b/packages/runtime/src/utils/sort.ts
@@ -6,8 +6,6 @@ import { ItemIndex, Computed, Keyframe } from '../traits';
import type { Entity } from 'koota';
-// Plain comparators (the bitecs versions were world-bound factories; koota
-// entities carry their world, so these pass to sort() directly).
export function sortByItemIndex(a: Entity, b: Entity): number {
return (a.get(ItemIndex)?.value ?? 0) - (b.get(ItemIndex)?.value ?? 0);
diff --git a/packages/runtime/src/world/observers.ts b/packages/runtime/src/world/observers.ts
index 524694fe..fc749a57 100644
--- a/packages/runtime/src/world/observers.ts
+++ b/packages/runtime/src/world/observers.ts
@@ -2,20 +2,6 @@
* 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/. */
-// Runtime-owned invariants as koota event subscriptions (was the runtime half
-// of api/observers.ts): cache upkeep on ChildOf changes, authored-to-Computed
-// mirrors, and time-range reactions. Registered by createRuntimeWorld, so
-// deserialization and clones prime Cache/Computed through the same path as
-// actions. App-side duties of the bitecs observers (persistence, timeline
-// index, interactive state, selection styling, drag origins) and media
-// disposal hooks are layered on by their owners.
-//
-// Keyframe auto-sync on authored edits deliberately did not move here: koota
-// change events don't say which field changed, and syncing every field of a
-// trait would mint spurious keyframes on untouched properties. Editing
-// surfaces keep the track of the property they changed in step themselves —
-// in an app, through whatever owns the document, since a keyframe minted in
-// the world alone would never reach the project it was authored in.
import { store } from './store';
import {
@@ -279,8 +265,8 @@ export function observeWorld(world: World): () => void {
});
// Size flows into Computed via propagation (descendants owning a Size
- // re-derive too). bitecs restored sizes through resizeEntity; deserialize
- // is a plain set now, so the propagation rides on the event instead.
+ // re-derive too). Deserialize is a plain set, so the propagation rides on
+ // the event rather than on whatever restored the size.
mirror(Size, (entity) => {
propagateSize(world, entity);
}, false);
diff --git a/packages/runtime/src/world/store.ts b/packages/runtime/src/world/store.ts
index 7fb4a792..58be8191 100644
--- a/packages/runtime/src/world/store.ts
+++ b/packages/runtime/src/world/store.ts
@@ -9,8 +9,8 @@ import type { ExtractStore, Trait, World } from 'koota';
const registered = new WeakMap<World, WeakSet<Trait>>();
/**
- * Direct SoA store access for hot paths (bitecs-style array indexing by
- * entity.id(), no snapshots, no change events). koota registers a trait's
+ * Direct SoA store access for hot paths (array indexing by entity.id(), no
+ * snapshots, no change events). koota registers a trait's
* store lazily on first add/query and bare getStore throws before that, so
* this wrapper registers the trait once per world.
*/