Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance runtime package with new world traits and font utilities

konstantin-paulusb7d19ba61526
diff --git a/packages/runtime/src/fonts/index.ts b/packages/runtime/src/fonts/index.ts
index 072e60d0..a8e32130 100644
--- a/packages/runtime/src/fonts/index.ts
+++ b/packages/runtime/src/fonts/index.ts
@@ -1 +1,2 @@
-// Font loading and fixtures (moves wholesale from engine/font/).
+// Font loading; fixtures and utils follow from engine/font/.
+export * from './types';
diff --git a/packages/runtime/src/fonts/types.ts b/packages/runtime/src/fonts/types.ts
new file mode 100644
index 00000000..6fdb58f9
--- /dev/null
+++ b/packages/runtime/src/fonts/types.ts
@@ -0,0 +1,41 @@
+/* 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 '../constants';
+
+/**
+ * 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/packages/runtime/src/index.ts b/packages/runtime/src/index.ts
index a2fcc25c..4334875a 100644
--- a/packages/runtime/src/index.ts
+++ b/packages/runtime/src/index.ts
@@ -1,4 +1,6 @@
export * from './constants';
export * from './traits';
+export * from './world/create-world';
export * from './capture';
+export * from './fonts';
export * from './utils';
diff --git a/packages/runtime/src/traits/world.ts b/packages/runtime/src/traits/world.ts
index 07d08824..d8d20265 100644
--- a/packages/runtime/src/traits/world.ts
+++ b/packages/runtime/src/traits/world.ts
@@ -1 +1,69 @@
-// Singleton world traits: Camera, PlaybackState, Mode, Background. App-injected resources (canvas, resolution) also land here.
+/* 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 { trait, type Entity } from 'koota';
+
+import { createLiveMounts } from '../utils/live-mounts';
+
+import type { FontSource } from '../fonts/types';
+
+// 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: '' });
+
+export type RuntimeMode = 'realtime' | 'offline-video' | 'offline-audio';
+
+export const Mode = trait({ value: 'realtime' as RuntimeMode });
+
+// Scene the playhead, timeline, and capture operate on (was selection.scene).
+export const ActiveScene = trait({ entity: null as Entity | null });
+
+/**
+ * 2D affine camera transform in CSS pixel space (before DPR scaling). The
+ * render system multiplies this by RenderSurface.resolution to derive the
+ * canvas transform.
+ */
+export const Camera = trait({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 });
+
+export type Camera2D = { a: number; b: number; c: number; d: number; e: number; f: number };
+
+export const DEFAULT_BACKGROUND = 0x161616;
+
+export const Background = trait({ value: DEFAULT_BACKGROUND });
+
+// Frame clock (was timestamp).
+export const Time = trait({ now: 0, delta: 0 });
+
+// Project frame rate (frames per second).
+export const FrameRate = trait({ value: 30 });
+
+// Render target injected by the host: the editor canvas in the app, an
+// OffscreenCanvas during capture. resolution is the device pixel ratio.
+export const RenderSurface = trait({
+ canvas: () => null as HTMLCanvasElement | OffscreenCanvas | null,
+ ctx: () => null as CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D | null,
+ resolution: 1,
+});
+
+// Audio output injected by the host: a realtime AudioContext in the editor,
+// an OfflineAudioContext during capture.
+export const AudioEngine = trait({
+ context: () => null as AudioContext | OfflineAudioContext | null,
+});
+
+// Fonts registered for text layout.
+export const Fonts = trait({ list: () => [] as FontSource[] });
+
+// Live JSX mount registry; running graphs advance once per frame.
+export const Mounts = trait(() => createLiveMounts());
+
+// Async barrier for offline capture: systems push decoder/host readiness
+// promises here; the encoder awaits and clears them before sampling a frame.
+// null in realtime mode (nothing collects).
+export const FramePromises = trait({
+ list: () => null as (Promise<unknown> | null)[] | null,
+});
diff --git a/packages/runtime/src/utils/index.ts b/packages/runtime/src/utils/index.ts
index e86991a6..9be421e1 100644
--- a/packages/runtime/src/utils/index.ts
+++ b/packages/runtime/src/utils/index.ts
@@ -1,2 +1,3 @@
// Pure helpers only: time, sort, transition, captions follow. DOM-touching utils (cursor, interaction) stay in apps/web.
export * from './text';
+export * from './live-mounts';
diff --git a/packages/runtime/src/utils/live-mounts.ts b/packages/runtime/src/utils/live-mounts.ts
new file mode 100644
index 00000000..97961708
--- /dev/null
+++ b/packages/runtime/src/utils/live-mounts.ts
@@ -0,0 +1,54 @@
+/* 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 (`dapi mount`) 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/packages/runtime/src/world/create-world.ts b/packages/runtime/src/world/create-world.ts
index b71b6baa..b2775db2 100644
--- a/packages/runtime/src/world/create-world.ts
+++ b/packages/runtime/src/world/create-world.ts
@@ -1 +1,45 @@
-// createWorld() with all runtime traits. No AudioContext or window access; the app injects those.
+/* 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 'koota';
+
+import {
+ Project,
+ Mode,
+ ActiveScene,
+ Camera,
+ Background,
+ Time,
+ FrameRate,
+ RenderSurface,
+ AudioEngine,
+ Fonts,
+ Mounts,
+ FramePromises,
+} from '../traits/world';
+
+/**
+ * World with the headless runtime singletons attached. The host injects its
+ * surfaces after creation (world.set(RenderSurface, ...) and
+ * world.set(AudioEngine, ...)); the editor app layers its own world traits on
+ * top (input, history, persistence, asset stores).
+ */
+export function createRuntimeWorld(projectId: string) {
+ return createWorld(
+ Project({ id: projectId }),
+ Mode,
+ ActiveScene,
+ Camera,
+ Background,
+ Time,
+ FrameRate,
+ RenderSurface,
+ AudioEngine,
+ Fonts,
+ Mounts,
+ FramePromises,
+ );
+}
+
+export type RuntimeWorld = ReturnType<typeof createRuntimeWorld>;