Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor HTML rendering logic and remove unused functions

konstantin-paulus0d20ff6b58b5
diff --git a/apps/web/src/components/engine/decoders/html.ts b/apps/web/src/components/engine/decoders/html.ts
index 42c002bf..402706c5 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 type { EngineWorld } from '../api/world';
-
type Ctx2D = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
// html-in-canvas (https://github.com/WICG/html-in-canvas). Chromium only,
@@ -18,14 +16,6 @@ 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
@@ -34,11 +24,21 @@ export function hasLiveHtmlHosts(world: EngineWorld): boolean {
* 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.
+ *
+ * Memoized while in flight: every host forwarded in the same tick shares one
+ * rendering update, so a scene with many HTML paints still waits a single rAF
+ * per frame rather than scheduling one per host.
*/
+let pendingRenderingUpdate: Promise<void> | null = null;
+
export function nextRenderingUpdate(): Promise<void> {
- return new Promise(resolve => {
- requestAnimationFrame(() => setTimeout(resolve, 0));
+ pendingRenderingUpdate ??= new Promise(resolve => {
+ requestAnimationFrame(() => setTimeout(() => {
+ pendingRenderingUpdate = null;
+ resolve();
+ }, 0));
});
+ return pendingRenderingUpdate;
}
let htmlCanvas: HTMLCanvasElement | null = null;
diff --git a/apps/web/src/components/engine/encode/encoder.ts b/apps/web/src/components/engine/encode/encoder.ts
index 64203fae..15dc1fad 100644
--- a/apps/web/src/components/engine/encode/encoder.ts
+++ b/apps/web/src/components/engine/encode/encoder.ts
@@ -27,7 +27,6 @@ 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';
@@ -244,7 +243,6 @@ export async function createEncoder(sourceWorld: EngineWorld, config: EncoderCon
const 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;
@@ -278,10 +276,6 @@ export async function createEncoder(sourceWorld: EngineWorld, config: EncoderCon
motionSystem(world);
}
- if (waitForHtmlHosts) {
- await nextRenderingUpdate();
- }
-
if (videoEnabled) {
// only visual systems
transformSystem(world);
diff --git a/apps/web/src/components/engine/encode/image-encoder.ts b/apps/web/src/components/engine/encode/image-encoder.ts
index bcc79bb8..5d29493d 100644
--- a/apps/web/src/components/engine/encode/image-encoder.ts
+++ b/apps/web/src/components/engine/encode/image-encoder.ts
@@ -15,7 +15,6 @@ import { motionSystem } from '../systems/motion';
import { transformSystem } from '../systems/transform';
import { renderSystem } from '../systems/render';
import { cloneFromRecords, serializeEntity } from '../api/serialize';
-import { hasLiveHtmlHosts, nextRenderingUpdate } from '../decoders/html';
import { getEntityTree } from '../api/query';
import { realizeMounts } from '@/utils/mount';
import { framesToSeconds, formatTimestamp, stampTimestampLabel } from '../utils';
@@ -178,7 +177,6 @@ export async function createImageEncoder(sourceWorld: EngineWorld, config: Image
const render = async (): Promise<ImageExportResult> => {
try {
const images: string[] = [];
- const waitForHtmlHosts = hasLiveHtmlHosts(world);
for (const frame of config.frames) {
if (canceled) {
@@ -189,9 +187,6 @@ export async function createImageEncoder(sourceWorld: EngineWorld, config: Image
await resolverSystem(world);
motionSystem(world);
transformSystem(world);
- if (waitForHtmlHosts) {
- await nextRenderingUpdate();
- }
renderSystem(world);
if (config.timestamp) {
diff --git a/apps/web/src/components/engine/systems/playback.ts b/apps/web/src/components/engine/systems/playback.ts
index 926b09a9..8388d629 100644
--- a/apps/web/src/components/engine/systems/playback.ts
+++ b/apps/web/src/components/engine/systems/playback.ts
@@ -5,6 +5,7 @@
import { hasComponent, Not, Or, query } from 'bitecs';
import { ChildOf, PaintType } from '../components';
import { resolveAudioDecoder, resolveCaptionDecoder, resolveImageDecoder, resolveSequenceDecoder, resolveShaderHost, resolveVideoDecoder } from '../decoders';
+import { nextRenderingUpdate } from '../decoders/html';
import { getParentEntity } from '../api';
import { getTransitionWindow } from '../utils/transition';
import { AudioBus } from '../services/audio-bus';
@@ -207,6 +208,12 @@ function forwardAudioDecoder(world: EngineWorld, sid: number, eid: number, aid?:
}
}
+function forwardHtmlHost(world: EngineWorld, 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(nextRenderingUpdate());
+}
+
function forwardImageDecoder(world: EngineWorld, _sid: number, _eid: number, fid: number): void {
const resolvedDecoder = resolveImageDecoder(world, fid);
if (!resolvedDecoder) return;
@@ -250,6 +257,10 @@ function forwardDecoders(world: EngineWorld, sid: number, eid: number): void {
forwardImageDecoder(world, sid, eid, fid);
}
+ if (c.Paint[fid] === PaintType.HTML && visualsEnabled) {
+ forwardHtmlHost(world, eid, fid);
+ }
+
if (c.Paint[fid] === PaintType.SHADER && visualsEnabled) {
const ready = resolveShaderHost(world, fid)?.whenReady();
if (ready) world.promises?.push(ready);