The VS Code of video editing, built for agents.
TypeScript
Refactor HTML and Surface host sharing logic and introduce rendering update handling
konstantin-paulusd335b3d0f4aa
diff --git a/apps/web/src/components/engine/decoders/html.ts b/apps/web/src/components/engine/decoders/html.tsindex 3b305dc2..4cb231e9 100644--- a/apps/web/src/components/engine/decoders/html.ts+++ b/apps/web/src/components/engine/decoders/html.ts@@ -2,8 +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/. */-import { addComponent } from '../api/events';-import type { EngineWorld } from '../api/world';type Ctx2D = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;@@ -20,6 +18,29 @@ export function isHtmlInCanvasSupported(): boolean {&& 'drawElementImage' in CanvasRenderingContext2D.prototype;}+/** Whether `world` currently owns or shares any live HTML paint host. */+export function hasLiveHtmlHosts(world: EngineWorld): boolean {+ for (const host of world.components.HtmlHost) {+ if (host) return true;+ }+ return false;+}++/**+ * Resolves after the browser's next rendering update (style/layout/paint) has+ * completed. drawElementImage samples paint records that are only refreshed+ * while the host canvas paints, so an offline render loop that mutates the+ * paint's DOM must yield one rendering update before rasterizing — otherwise+ * every frame in the same task samples the records of the last real paint.+ * The task queued from inside rAF runs after that frame's paint, so this+ * costs one vsync, not two.+ */+export function nextRenderingUpdate(): Promise<void> {+ return new Promise(resolve => {+ requestAnimationFrame(() => setTimeout(resolve, 0));+ });+}+/*** DOM host for one HTML paint: a div inside the host's own layoutsubtree* canvas. The mount document inserts the paint's JSX children into `element`@@ -103,23 +124,3 @@ export class HtmlHost {this.canvas.remove();}}--/**- * Shares <HtmlPaint> hosts with a cloned (offline) world. Host DOM is- * runtime state serializeEntity cannot carry, so export/capture worlds- * reference the live hosts directly and sample whatever the mount's reactive- * graph shows while encoding. The source world keeps ownership; offline- * worlds must not dispose them.- */-export function shareHtmlHosts(- sourceWorld: EngineWorld,- world: EngineWorld,- eidMap: Map<number, number>,-) {- for (const [sourceEid, targetEid] of eidMap) {- const host = sourceWorld.components.HtmlHost[sourceEid];- if (!host) continue;- addComponent(world, targetEid, world.components.HtmlHost, false);- world.components.HtmlHost[targetEid] = host;- }-}diff --git a/apps/web/src/components/engine/decoders/surface.ts b/apps/web/src/components/engine/decoders/surface.tsindex d1294040..355ee2dd 100644--- a/apps/web/src/components/engine/decoders/surface.ts+++ b/apps/web/src/components/engine/decoders/surface.ts@@ -41,23 +41,3 @@ export class SurfaceHost {this.canvas.height = 0;}}--/**- * Shares <SurfacePaint> hosts with a cloned (offline) world. The backing- * canvas is runtime state serializeEntity cannot carry, so export/capture- * worlds reference the live hosts directly and sample whatever the mount's- * ref last drew while encoding. The source world keeps ownership; offline- * worlds must not dispose them.- */-export function shareSurfaceHosts(- sourceWorld: EngineWorld,- world: EngineWorld,- eidMap: Map<number, number>,-) {- for (const [sourceEid, targetEid] of eidMap) {- const host = sourceWorld.components.SurfaceHost[sourceEid];- if (!host) continue;- addComponent(world, targetEid, world.components.SurfaceHost, false);- world.components.SurfaceHost[targetEid] = host;- }-}diff --git a/apps/web/src/components/engine/encode/encoder.ts b/apps/web/src/components/engine/encode/encoder.tsindex 15dc1fad..64203fae 100644--- a/apps/web/src/components/engine/encode/encoder.ts+++ b/apps/web/src/components/engine/encode/encoder.ts@@ -27,6 +27,7 @@ import { renderSystem } from '../systems/render';import { cloneFromRecords, serializeEntity } from '../api/serialize';import { getEntityTree } from '../api/query';import { AudioBus } from '../services/audio-bus';+import { hasLiveHtmlHosts, nextRenderingUpdate } from '../decoders/html';import { realizeMounts } from '@/utils/mount';import type { EngineWorld } from '../api/world';@@ -243,6 +244,7 @@ export async function createEncoder(sourceWorld: EngineWorld, config: EncoderConconst start = performance.now();const startTime = start;const totalFrames = Math.floor(duration * frameRate);+ const waitForHtmlHosts = hasLiveHtmlHosts(world);let audioRenderingDone = false;let audioRenderingCompleted: Promise<AudioBuffer> | null = null;@@ -276,6 +278,10 @@ export async function createEncoder(sourceWorld: EngineWorld, config: EncoderConmotionSystem(world);}+ if (waitForHtmlHosts) {+ await nextRenderingUpdate();+ }+if (videoEnabled) {// only visual systemstransformSystem(world);diff --git a/apps/web/src/components/engine/encode/image-encoder.ts b/apps/web/src/components/engine/encode/image-encoder.tsindex 2c74ad28..bcc79bb8 100644--- a/apps/web/src/components/engine/encode/image-encoder.ts+++ b/apps/web/src/components/engine/encode/image-encoder.ts@@ -15,8 +15,7 @@ import { motionSystem } from '../systems/motion';import { transformSystem } from '../systems/transform';import { renderSystem } from '../systems/render';import { cloneFromRecords, serializeEntity } from '../api/serialize';-import { shareHtmlHosts } from '../decoders/html';-import { shareSurfaceHosts } from '../decoders/surface';+import { hasLiveHtmlHosts, nextRenderingUpdate } from '../decoders/html';import { getEntityTree } from '../api/query';import { realizeMounts } from '@/utils/mount';import { framesToSeconds, formatTimestamp, stampTimestampLabel } from '../utils';@@ -85,8 +84,6 @@ export async function createImageEncoder(sourceWorld: EngineWorld, config: Imageconst rootEid = eidMap.get(config.eid);assert(rootEid !== undefined, 'Failed to clone entity subtree');- shareHtmlHosts(sourceWorld, world, eidMap);- shareSurfaceHosts(sourceWorld, world, eidMap);const mounts = await realizeMounts(world);const clonedEids = [...eidMap.values()];@@ -181,6 +178,7 @@ export async function createImageEncoder(sourceWorld: EngineWorld, config: Imageconst render = async (): Promise<ImageExportResult> => {try {const images: string[] = [];+ const waitForHtmlHosts = hasLiveHtmlHosts(world);for (const frame of config.frames) {if (canceled) {@@ -191,6 +189,9 @@ export async function createImageEncoder(sourceWorld: EngineWorld, config: Imageawait resolverSystem(world);motionSystem(world);transformSystem(world);+ if (waitForHtmlHosts) {+ await nextRenderingUpdate();+ }renderSystem(world);if (config.timestamp) {