Skip to content

Production-ready animations, transitions, backgrounds, and scenes for Remotion

feat: added htmlincanvas filters

kapishdima0ef34d9285d3
diff --git a/app/docs/layout.tsx b/app/docs/layout.tsx
index 85ba3d44..7c6d987f 100644
--- a/app/docs/layout.tsx
+++ b/app/docs/layout.tsx
@@ -12,7 +12,7 @@ export default async function Layout({ children }: { children: ReactNode }) {
// `withNewBadges`), then split it into the Components / Primitives tab trees
// (see `splitDocsTree`). Both run on the server; `DocsShell` picks the tree
// matching the active tab by pathname so each tab owns its own sidebar.
- const { components, primitives, shaders, icons } = splitDocsTree(
+ const { components, primitives, shaders, filters, icons } = splitDocsTree(
withNewBadges(source.pageTree),
);
@@ -31,6 +31,7 @@ export default async function Layout({ children }: { children: ReactNode }) {
componentsTree={components}
primitivesTree={primitives}
shadersTree={shaders}
+ filtersTree={filters}
iconsTree={icons}
>
{children}
diff --git a/app/llms-components.txt/route.ts b/app/llms-components.txt/route.ts
index a2d633d3..2e68acc2 100644
--- a/app/llms-components.txt/route.ts
+++ b/app/llms-components.txt/route.ts
@@ -58,7 +58,9 @@ export function GET() {
"is the value to pass to `linearTiming` / `springTiming`, for everything else it is the point at",
"which the animation has finished. It is a floor for the `Sequence`, not the whole beat: add hold",
"time on top when the element should stay on screen after it settles. `state-driven` means the",
- "component renders purely from its `state` prop and has no duration of its own.",
+ "component renders purely from its `state` prop and has no duration of its own. `sustained` means",
+ "the component wraps a scene and lasts exactly as long as that scene does — budget no frames for",
+ "it and pass it no `state`.",
"`Vibe` is the tonal tag; match it to the brand rather than mixing freely.",
];
diff --git a/components/docs/canvas-filter-note.tsx b/components/docs/canvas-filter-note.tsx
new file mode 100644
index 00000000..0d93e108
--- /dev/null
+++ b/components/docs/canvas-filter-note.tsx
@@ -0,0 +1,23 @@
+import { Warning } from "@/components/docs/callout";
+
+export function CanvasFilterNote() {
+ return (
+ <Warning title="Experimental — html-in-canvas">
+ <p>
+ This filter re-reads the real pixels of the scene it wraps through the
+ experimental html-in-canvas browser API. The preview above shows the
+ true effect in Chrome 149+ with{" "}
+ <code>chrome://flags/#canvas-draw-element</code> enabled (nested
+ canvases need 152.0.7944.0+). Every other browser — Safari, Firefox,
+ Chrome without the flag — falls back to a CSS approximation, so the page
+ and your video still play.
+ </p>
+ <p>
+ Rendering is supported through the CLI, Studio, Lambda and SSR with{" "}
+ <code>--gl=angle</code>, or <code>--gl=swangle</code> on a machine
+ without a GPU. Chrome may change or discontinue the API; the fallback is
+ what keeps your composition renderable if that happens.
+ </p>
+ </Warning>
+ );
+}
diff --git a/components/docs/docs-shell.tsx b/components/docs/docs-shell.tsx
index 0cb49b97..a6b2e5d7 100644
--- a/components/docs/docs-shell.tsx
+++ b/components/docs/docs-shell.tsx
@@ -41,26 +41,28 @@ export function DocsShell({
componentsTree,
primitivesTree,
shadersTree,
+ filtersTree,
iconsTree,
children,
}: {
componentsTree: Root;
primitivesTree: Root;
shadersTree: Root;
+ filtersTree: Root;
iconsTree: Root;
children: ReactNode;
}) {
const pathname = usePathname();
const activeTab = getActiveDocsTab(pathname);
const isIcons = activeTab === "icons";
- const tree =
- activeTab === "primitives"
- ? primitivesTree
- : activeTab === "shaders"
- ? shadersTree
- : isIcons
- ? iconsTree
- : componentsTree;
+ const trees: Record<typeof activeTab, Root> = {
+ components: componentsTree,
+ primitives: primitivesTree,
+ shaders: shadersTree,
+ filters: filtersTree,
+ icons: iconsTree,
+ };
+ const tree = trees[activeTab];
return (
<DocsLayout
diff --git a/components/docs/examples/vhs-filter-example.tsx b/components/docs/examples/vhs-filter-example.tsx
new file mode 100644
index 00000000..61727a76
--- /dev/null
+++ b/components/docs/examples/vhs-filter-example.tsx
@@ -0,0 +1,45 @@
+"use client";
+
+import { CanvasSceneA } from "@/components/docs/examples/canvas-scenes";
+import { VhsFilter } from "@/registry/remocn/vhs-filter";
+
+interface VhsFilterExampleProps {
+ bleed?: number;
+ wobble?: number;
+ noise?: number;
+ intensity?: number;
+}
+
+export function VhsFilterExampleScene({
+ bleed,
+ wobble,
+ noise,
+ intensity,
+}: VhsFilterExampleProps) {
+ return (
+ <VhsFilter
+ bleed={bleed}
+ wobble={wobble}
+ noise={noise}
+ intensity={intensity}
+ >
+ <CanvasSceneA />
+ </VhsFilter>
+ );
+}
+
+export const vhsFilterExampleCode = (
+ values: Record<string, unknown>,
+): string => {
+ const bleed = (values.bleed as number) ?? 1;
+ const wobble = (values.wobble as number) ?? 1;
+ const noise = (values.noise as number) ?? 1;
+ const intensity = (values.intensity as number) ?? 1;
+ return `import { VhsFilter } from "@/components/remocn/vhs-filter";
+
+export const MyScene = () => (
+ <VhsFilter bleed={${bleed}} wobble={${wobble}} noise={${noise}} intensity={${intensity}}>
+ <Scene />
+ </VhsFilter>
+);`;
+};
diff --git a/content/changelog/2026-08-02-filters.mdx b/content/changelog/2026-08-02-filters.mdx
new file mode 100644
index 00000000..7bcefb84
--- /dev/null
+++ b/content/changelog/2026-08-02-filters.mdx
@@ -0,0 +1,22 @@
+---
+title: Filters, a new category
+date: 2026-08-02
+---
+
+Canvas transitions gave the catalog effects that live at a cut. Filters are the other half of that idea — the same pixel-level treatment, held over a whole scene instead of a dozen frames. A filter is a wrapper: put a scene inside it and every frame of that scene is re-read and repainted, for as long as the scene runs. Where a CSS filter can only blend what is already under a pixel, these write each pixel from somewhere else entirely, which is the reason the category exists at all.
+
+<ChangelogPreview name="vhs-filter" />
+
+### New components
+
+- [VHS Filter](/docs/filters/components/vhs-filter) — a scene held under analog tape. Colour bleeds sideways off every edge while brightness stays sharp, which is what separates tape from a blur; rows wander out of line on a slow drift with per-line jitter on top; the bottom of the frame tears where a real deck switches heads; luma noise and scanlines settle over all of it. Every knob is a plain number, so `intensity` can be interpolated to let a scene surface out of the wear.
+
+### Improvements
+
+- Filters have their own docs tab, next to Shaders, with a Getting Started page covering what separates a filter from a transition, why the motion inside one never drifts, and how the browser requirement plays out.
+- Component metadata gained a third kind of length. Alongside a frame count and `state-driven`, a component can now declare `sustained` — it wraps a scene and lasts exactly as long as that scene does. Agents reading `/llms-components.txt` get the distinction spelled out, so a filter is never handed a `Sequence` budget or a `state` prop it has no use for.
+- Canvas Presentation now backs both categories. It grew a filter factory beside the transition one: a single-scene shader with the frame and elapsed time bound automatically, and a wrapper that holds each frame until the repaint has actually landed, so a filter's output can never be a frame behind the scene underneath it.
+
+### Fixes
+
+- The WebGL scaffolding shared by every canvas effect — program, quad, cached uniform lookups, texture binding, teardown — now exists once instead of once per factory.
diff --git a/content/docs/filters/components/meta.json b/content/docs/filters/components/meta.json
new file mode 100644
index 00000000..0a3ee9b3
--- /dev/null
+++ b/content/docs/filters/components/meta.json
@@ -0,0 +1,5 @@
+{
+ "title": "Filters",
+ "collapsible": false,
+ "pages": ["vhs-filter"]
+}
diff --git a/content/docs/filters/components/vhs-filter.mdx b/content/docs/filters/components/vhs-filter.mdx
new file mode 100644
index 00000000..9abbd9aa
--- /dev/null
+++ b/content/docs/filters/components/vhs-filter.mdx
@@ -0,0 +1,119 @@
+---
+title: VHS Filter
+description: Hold a scene under analog tape — chroma bleed, tape wobble, a head-switch tear and luma noise
+component: vhs-filter
+vibe: tech
+length: sustained
+useWhen:
+ - "A scene should read as recovered tape footage rather than a clean render — retro product spots, 'here is what we shipped in 2003' beats, lo-fi title cards."
+ - "You want the whole scene degraded for its full duration, not a one-frame artifact at a cut."
+ - "The scene is full-bleed and opaque, so the treatment has real pixels to work on edge to edge."
+avoidWhen:
+ - "The artifact should live only at the cut between two scenes — use `glitch-cut`."
+ - "Only a headline should break up while the scene behind it stays clean — use `rgb-glitch-text`."
+ - "You want digital corruption rather than analog wear — tape is soft and continuous, not blocky."
+---
+
+<ComponentPreview name="vhs-filter" />
+
+<CanvasFilterNote />
+
+## Installation
+
+<InstallBlock name="vhs-filter" />
+
+## Usage
+
+`VhsFilter` is a wrapper. Put a scene inside it and every frame of that scene is
+sampled and repainted as tape: the colour bleeds sideways off every edge while
+the brightness stays sharp, rows slide out of line on a slow wander, the bottom
+of the frame tears where a real deck switches heads, and noise settles over all
+of it.
+
+It has no duration of its own — it lasts exactly as long as the scene it wraps,
+so there is nothing to time and no `Sequence` to budget.
+
+```tsx
+import { VhsFilter } from "@/components/remocn/vhs-filter";
+
+export const MyScene = () => (
+ <VhsFilter>
+ <Scene />
+ </VhsFilter>
+);
+```
+
+## Driving it per frame
+
+Every knob is a plain number, so the tape can get worse on a beat and recover
+after it. `intensity` scales all four behaviours at once, which makes it the one
+to interpolate when you want the scene to surface out of the noise.
+
+```tsx
+import { interpolate, useCurrentFrame } from "remotion";
+import { VhsFilter } from "@/components/remocn/vhs-filter";
+
+export const MyScene = () => {
+ const frame = useCurrentFrame();
+ const intensity = interpolate(frame, [0, 24], [1, 0.15], {
+ extrapolateRight: "clamp",
+ });
+
+ return (
+ <VhsFilter intensity={intensity}>
+ <Scene />
+ </VhsFilter>
+ );
+};
+```
+
+## Props
+
+<PropsTable
+ rows={[
+ {
+ name: "bleed",
+ type: "number",
+ default: "1",
+ description:
+ "How far colour smears sideways from where it belongs. Brightness never smears with it, which is what makes the picture read as tape rather than as a blur.",
+ },
+ {
+ name: "wobble",
+ type: "number",
+ default: "1",
+ description:
+ "How far rows wander out of line. A slow drift across the frame plus per-line jitter, so edges never sit quite straight.",
+ },
+ {
+ name: "noise",
+ type: "number",
+ default: "1",
+ description: "Strength of the luma grain laid over the whole frame.",
+ },
+ {
+ name: "intensity",
+ type: "number",
+ default: "1",
+ description:
+ "Master amount, 0 to 1. Scales bleed, wobble, noise, scanlines and the head-switch tear together. At 0 the scene comes through untouched.",
+ },
+ {
+ name: "children",
+ type: "React.ReactNode",
+ description: "The scene the filter wraps and re-reads every frame.",
+ },
+ ]}
+/>
+
+## Notes
+
+The filter expects a full-bleed, opaque scene. It reads pixels from a displaced
+coordinate, so where the scene is transparent it has nothing to move and the
+untouched scene shows through underneath — put a background inside the wrapper
+rather than behind it.
+
+The wobble and the head-switch tear are functions of the current frame, not of a
+running clock, and they oscillate rather than accumulate. Two renders of the same
+frame produce the same pixels, and a filter left on for a minute looks the same
+at the end as at the start.
diff --git a/content/docs/filters/getting-started/introduction.mdx b/content/docs/filters/getting-started/introduction.mdx
new file mode 100644
index 00000000..cd44e815
--- /dev/null
+++ b/content/docs/filters/getting-started/introduction.mdx
@@ -0,0 +1,46 @@
+---
+title: Introduction
+description: Pixel-level treatments you hold over a whole scene, built on the experimental html-in-canvas browser API
+---
+
+A filter wraps a scene and re-reads its pixels every frame. Put your scene inside one and it comes out as tape footage, as a CRT display, as halftone print — the treatment stays on for as long as the scene runs.
+
+```tsx
+import { VhsFilter } from "@/components/remocn/vhs-filter";
+
+export const Scene = () => (
+ <VhsFilter>
+ <YourScene />
+ </VhsFilter>
+);
+```
+
+## Filters are not transitions
+
+Both categories sit on the same html-in-canvas foundation, but they answer different questions.
+
+A transition covers a cut. It has a progress, it runs between two scenes, and it is over in a dozen frames. A filter has no progress and no second scene: it is a look, and it lasts exactly as long as the scene it wraps. That is what `length: sustained` means in the docs metadata — budget no frames for a filter and pass it no `state`, just wrap the scene and let it run.
+
+Reach for a transition when the effect should live at the edit. Reach for a filter when the effect is the scene's material.
+
+## What a filter can do that CSS cannot
+
+A CSS `filter` can only work with the colour already under each pixel: blur it, tint it, raise its contrast. It cannot read somewhere else. Every filter here does exactly that — it writes each output pixel from a *different* source coordinate.
+
+That is the whole reason the category needs a browser API instead of a stylesheet. Chroma that bleeds sideways off an edge, a frame curved around a CRT's glass, a cell that collapses to the average colour of everything inside it, a picture seen through moving water — none of them are reachable by blending what is already in place.
+
+## Motion, and why it never drifts
+
+A filter has to be able to sit on a scene for its whole duration, so whatever moves inside it — tape wobble, ripple phase, noise — is a pure function of the current frame. It oscillates and never trends: come back at frame 600 and the look is the same as at frame 60, with the detail in a different place.
+
+That also makes the output deterministic. Nothing is simulated forward from a previous frame and no wall-clock time is read, so parallel and server-side renders produce identical pixels.
+
+## Browser support
+
+<CanvasFilterNote />
+
+## Composing filters
+
+Filters wrap, so they nest, and the innermost one is applied first. Each level costs another capture and another shader pass, so keep stacks shallow and put the filter that changes geometry — a warp, a curvature — outermost, where it can bend everything the inner ones produced.
+
+Browse the individual filters in the sidebar to preview each one and grab its install command.
diff --git a/content/docs/filters/getting-started/meta.json b/content/docs/filters/getting-started/meta.json
new file mode 100644
index 00000000..89d653a4
--- /dev/null
+++ b/content/docs/filters/getting-started/meta.json
@@ -0,0 +1,5 @@
+{
+ "title": "Getting Started",
+ "collapsible": false,
+ "pages": ["introduction"]
+}
diff --git a/content/docs/filters/meta.json b/content/docs/filters/meta.json
new file mode 100644
index 00000000..4282e092
--- /dev/null
+++ b/content/docs/filters/meta.json
@@ -0,0 +1,5 @@
+{
+ "title": "Filters",
+ "collapsible": false,
+ "pages": ["getting-started", "components"]
+}
diff --git a/content/docs/meta.json b/content/docs/meta.json
index 56c4ad36..34610cdc 100644
--- a/content/docs/meta.json
+++ b/content/docs/meta.json
@@ -8,6 +8,7 @@
"typography",
"ui",
"shaders",
+ "filters",
"icons",
"ui-blocks",
"ai",
diff --git a/lib/docs-meta.test.ts b/lib/docs-meta.test.ts
index c84ca3e4..75b76910 100644
--- a/lib/docs-meta.test.ts
+++ b/lib/docs-meta.test.ts
@@ -139,7 +139,7 @@ describe("component pages carry selection metadata", () => {
const offenders = componentPages
.filter((p) => {
const length = p.frontmatter.length;
- if (length === "state-driven") return false;
+ if (length === "state-driven" || length === "sustained") return false;
return !(
typeof length === "number" &&
Number.isInteger(length) &&
diff --git a/lib/docs-schema.ts b/lib/docs-schema.ts
index 3f211df3..a1375a15 100644
--- a/lib/docs-schema.ts
+++ b/lib/docs-schema.ts
@@ -15,7 +15,13 @@ export type Vibe = (typeof VIBES)[number];
export const componentMetaSchema = z.object({
component: z.string().optional(),
vibe: z.enum(VIBES).optional(),
- length: z.union([z.int().positive(), z.literal("state-driven")]).optional(),
+ length: z
+ .union([
+ z.int().positive(),
+ z.literal("state-driven"),
+ z.literal("sustained"),
+ ])
+ .optional(),
useWhen: z.array(z.string()).optional(),
avoidWhen: z.array(z.string()).optional(),
});
diff --git a/lib/docs-tabs.ts b/lib/docs-tabs.ts
index 23be9053..0efaad96 100644
--- a/lib/docs-tabs.ts
+++ b/lib/docs-tabs.ts
@@ -7,7 +7,12 @@ import type { Node, Root } from "fumadocs-core/page-tree";
* unchanged from the flat docs structure. No files move and no URLs change —
* the split is purely a view over the existing Fumadocs page tree.
*/
-export type DocsTabId = "components" | "primitives" | "shaders" | "icons";
+export type DocsTabId =
+ | "components"
+ | "primitives"
+ | "shaders"
+ | "filters"
+ | "icons";
export type DocsTab = {
id: DocsTabId;
@@ -28,6 +33,11 @@ export const DOCS_TABS: DocsTab[] = [
label: "Shaders",
href: "/docs/shaders/getting-started/introduction",
},
+ {
+ id: "filters",
+ label: "Filters",
+ href: "/docs/filters/getting-started/introduction",
+ },
{
id: "icons",
label: "Icons",
@@ -54,6 +64,14 @@ function isShadersPath(pathname: string): boolean {
);
}
+const FILTERS_PREFIX = "/docs/filters";
+
+function isFiltersPath(pathname: string): boolean {
+ return (
+ pathname === FILTERS_PREFIX || pathname.startsWith(`${FILTERS_PREFIX}/`)
+ );
+}
+
const ICONS_PREFIX = "/docs/icons";
function isIconsPath(pathname: string): boolean {
@@ -62,6 +80,7 @@ function isIconsPath(pathname: string): boolean {
export function getActiveDocsTab(pathname: string): DocsTabId {
if (isShadersPath(pathname)) return "shaders";
+ if (isFiltersPath(pathname)) return "filters";
if (isIconsPath(pathname)) return "icons";
if (isPrimitivesPath(pathname)) return "primitives";
return "components";
@@ -77,36 +96,32 @@ function isPrimitivesNode(node: Node): boolean {
);
}
-/** True if the node (or any descendant) is a shaders page — folders now nest. */
-function hasShadersDescendant(node: Node): boolean {
- if (node.type === "page") return node.url.startsWith(`${SHADERS_PREFIX}/`);
- if (node.type === "folder") {
- if (node.index?.url === SHADERS_PREFIX) return true;
- return node.children.some(hasShadersDescendant);
- }
- return false;
-}
-
-function isShadersNode(node: Node): boolean {
- if (node.type !== "folder") return false;
- if (node.index?.url === SHADERS_PREFIX) return true;
- return node.children.some(hasShadersDescendant);
-}
+/**
+ * True for the root-level folder node that owns `prefix`, matching a section
+ * however deeply its pages nest (shaders, filters and icons all group their
+ * pages under sub-folders). Kept separate from {@link isPrimitivesNode}, which
+ * only ever looks one level down.
+ */
+function sectionNodeMatcher(prefix: string): (node: Node) => boolean {
+ const hasDescendant = (node: Node): boolean => {
+ if (node.type === "page") return node.url.startsWith(`${prefix}/`);
+ if (node.type === "folder") {
+ if (node.index?.url === prefix) return true;
+ return node.children.some(hasDescendant);
+ }
+ return false;
+ };
-function hasIconsDescendant(node: Node): boolean {
- if (node.type === "page") return node.url.startsWith(`${ICONS_PREFIX}/`);
- if (node.type === "folder") {
- if (node.index?.url === ICONS_PREFIX) return true;
- return node.children.some(hasIconsDescendant);
- }
- return false;
+ return (node) => {
+ if (node.type !== "folder") return false;
+ if (node.index?.url === prefix) return true;
+ return node.children.some(hasDescendant);
+ };
}
-function isIconsNode(node: Node): boolean {
- if (node.type !== "folder") return false;
- if (node.index?.url === ICONS_PREFIX) return true;
- return node.children.some(hasIconsDescendant);
-}
+const isShadersNode = sectionNodeMatcher(SHADERS_PREFIX);
+const isFiltersNode = sectionNodeMatcher(FILTERS_PREFIX);
+const isIconsNode = sectionNodeMatcher(ICONS_PREFIX);
/**
* Hoists a matched folder's own index page and children up to the tab root, so
@@ -143,6 +158,7 @@ export function splitDocsTree(tree: Root): {
components: Root;
primitives: Root;
shaders: Root;
+ filters: Root;
icons: Root;
} {
return {
@@ -151,7 +167,10 @@ export function splitDocsTree(tree: Root): {
$id: "docs-tab-components",
children: tree.children.filter(
(node) =>
- !isPrimitivesNode(node) && !isShadersNode(node) && !isIconsNode(node),
+ !isPrimitivesNode(node) &&
+ !isShadersNode(node) &&
+ !isFiltersNode(node) &&
+ !isIconsNode(node),
),
},
primitives: {
@@ -164,6 +183,11 @@ export function splitDocsTree(tree: Root): {
$id: "docs-tab-shaders",
children: hoistPrimitives(tree.children.filter(isShadersNode)),
},
+ filters: {
+ ...tree,
+ $id: "docs-tab-filters",
+ children: hoistPrimitives(tree.children.filter(isFiltersNode)),
+ },
icons: {
...tree,
$id: "docs-tab-icons",
diff --git a/lib/get-llm-text.ts b/lib/get-llm-text.ts
index a3e9ad9b..91a8e742 100644
--- a/lib/get-llm-text.ts
+++ b/lib/get-llm-text.ts
@@ -6,8 +6,10 @@ export const SITE_URL = "https://remocn.dev";
type DocPage = InferPageType<typeof source>;
-export function formatLength(length: number | "state-driven"): string {
- return length === "state-driven" ? "state-driven" : `${length}f @ 30fps`;
+export function formatLength(
+ length: number | "state-driven" | "sustained",
+): string {
+ return typeof length === "string" ? length : `${length}f @ 30fps`;
}
export async function getLLMText(page: DocPage): Promise<string> {
diff --git a/mdx-components.tsx b/mdx-components.tsx
index 7b2ab9ce..a54aca95 100644
--- a/mdx-components.tsx
+++ b/mdx-components.tsx
@@ -3,6 +3,7 @@ import type { MDXComponents } from "mdx/types";
import type { ComponentProps } from "react";
import { BlockPreview } from "@/components/docs/block-preview";
import { Note, Warning } from "@/components/docs/callout";
+import { CanvasFilterNote } from "@/components/docs/canvas-filter-note";
import { CanvasTransitionNote } from "@/components/docs/canvas-transition-note";
import { getIconForLanguage } from "@/components/docs/code-icons";
import { ComponentCardGrid } from "@/components/docs/component-card-grid";
@@ -113,6 +114,7 @@ export function getMDXComponents(components?: MDXComponents): MDXComponents {
Note,
Warning,
CanvasTransitionNote,
+ CanvasFilterNote,
Dependencies,
ComponentCardGrid,
IconsGallery,
diff --git a/registry-artifacts/canvas-presentation.json b/registry-artifacts/canvas-presentation.json
index 4b2924d9..611a4709 100644
--- a/registry-artifacts/canvas-presentation.json
+++ b/registry-artifacts/canvas-presentation.json
@@ -2,12 +2,12 @@
"$schema": "https://ui.shadcn.com/schema/registry-item.json",
"name": "canvas-presentation",
"title": "Canvas Presentation",
- "description": "The html-in-canvas scaffolding the canvas transitions share: support detection, a full-screen WebGL2 scene shader over both sides of the cut, and routing to a CSS fallback where the browser lacks the API.",
+ "description": "The html-in-canvas scaffolding the canvas transitions and scene filters share: support detection, a full-screen WebGL2 shader fed either both sides of a cut or the single scene a filter wraps, and routing to a CSS fallback where the browser lacks the API.",
"dependencies": ["remotion", "@remotion/transitions"],
"files": [
{
"path": "registry/remocn/canvas-presentation/index.tsx",
- "content": "\"use client\";\n\nimport type {\n TransitionPresentation,\n TransitionPresentationComponentProps,\n} from \"@remotion/transitions\";\nimport React from \"react\";\nimport {\n AbsoluteFill,\n isHtmlInCanvasSupported,\n useDelayRender,\n} from \"remotion\";\n\nconst QUAD = new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]);\n\nconst VERTEX_SHADER = `#version 300 es\nin vec2 a_pos;\nout vec2 v_uv;\nvoid main() {\n v_uv = vec2(a_pos.x * 0.5 + 0.5, 0.5 - a_pos.y * 0.5);\n gl_Position = vec4(a_pos, 0.0, 1.0);\n}`;\n\nconst LAYER: React.CSSProperties = {\n position: \"absolute\",\n inset: 0,\n width: \"100%\",\n height: \"100%\",\n};\n\nconst OUTPUT_LAYER: React.CSSProperties = { ...LAYER, pointerEvents: \"none\" };\n\nexport const isCanvasTransitionSupported = (): boolean =>\n isHtmlInCanvasSupported();\n\nexport type SceneShaderFrame<Props> = {\n gl: WebGL2RenderingContext;\n uniform: (name: string) => WebGLUniformLocation | null;\n width: number;\n height: number;\n progress: number;\n passedProps: Props;\n};\n\nexport type SceneShaderDrawParams<Props> = {\n from: OffscreenCanvas | null;\n to: OffscreenCanvas | null;\n width: number;\n height: number;\n progress: number;\n passedProps: Props;\n};\n\nexport type SceneShaderInstance<Props> = {\n draw: (params: SceneShaderDrawParams<Props>) => void;\n clear: () => void;\n cleanup: () => void;\n};\n\nexport type SceneShader<Props> = (\n canvas: OffscreenCanvas,\n) => SceneShaderInstance<Props>;\n\nfunction compileShader(\n gl: WebGL2RenderingContext,\n source: string,\n type: GLenum,\n): WebGLShader {\n const shader = gl.createShader(type);\n if (!shader) {\n throw new Error(\"canvas-presentation: failed to create shader\");\n }\n gl.shaderSource(shader, source);\n gl.compileShader(shader);\n if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n const log = gl.getShaderInfoLog(shader);\n gl.deleteShader(shader);\n throw new Error(`canvas-presentation: failed to compile shader: ${log}`);\n }\n return shader;\n}\n\nfunction createProgram(\n gl: WebGL2RenderingContext,\n fragment: string,\n): WebGLProgram {\n const program = gl.createProgram();\n if (!program) {\n throw new Error(\"canvas-presentation: failed to create program\");\n }\n const vs = compileShader(gl, VERTEX_SHADER, gl.VERTEX_SHADER);\n const fs = compileShader(gl, fragment, gl.FRAGMENT_SHADER);\n gl.attachShader(program, vs);\n gl.attachShader(program, fs);\n gl.linkProgram(program);\n gl.deleteShader(vs);\n gl.deleteShader(fs);\n if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {\n const log = gl.getProgramInfoLog(program);\n gl.deleteProgram(program);\n throw new Error(`canvas-presentation: failed to link program: ${log}`);\n }\n return program;\n}\n\nfunction createSceneTexture(gl: WebGL2RenderingContext): WebGLTexture {\n const texture = gl.createTexture();\n if (!texture) {\n throw new Error(\"canvas-presentation: failed to create texture\");\n }\n gl.bindTexture(gl.TEXTURE_2D, texture);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n gl.texImage2D(\n gl.TEXTURE_2D,\n 0,\n gl.RGBA,\n 1,\n 1,\n 0,\n gl.RGBA,\n gl.UNSIGNED_BYTE,\n new Uint8Array([0, 0, 0, 0]),\n );\n return texture;\n}\n\nexport function makeSceneShader<Props extends Record<string, unknown>>(\n fragment: string,\n setUniforms?: (frame: SceneShaderFrame<Props>) => void,\n): SceneShader<Props> {\n return (canvas) => {\n const gl = canvas.getContext(\"webgl2\", { premultipliedAlpha: true });\n if (!gl) {\n throw new Error(\"canvas-presentation: WebGL2 is unavailable\");\n }\n\n const program = createProgram(gl, fragment);\n const fromTexture = createSceneTexture(gl);\n const toTexture = createSceneTexture(gl);\n const vao = gl.createVertexArray();\n gl.bindVertexArray(vao);\n const buffer = gl.createBuffer();\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer);\n gl.bufferData(gl.ARRAY_BUFFER, QUAD, gl.STATIC_DRAW);\n const aPos = gl.getAttribLocation(program, \"a_pos\");\n gl.enableVertexAttribArray(aPos);\n gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);\n\n const locations = new Map<string, WebGLUniformLocation | null>();\n const uniform = (name: string) => {\n const cached = locations.get(name);\n if (cached !== undefined) {\n return cached;\n }\n const location = gl.getUniformLocation(program, name);\n locations.set(name, location);\n return location;\n };\n\n const clear = () => {\n gl.clearColor(0, 0, 0, 0);\n gl.clear(gl.COLOR_BUFFER_BIT);\n };\n\n const bindScene = (\n image: OffscreenCanvas | null,\n texture: WebGLTexture,\n unit: number,\n name: string,\n ) => {\n gl.activeTexture(gl.TEXTURE0 + unit);\n gl.bindTexture(gl.TEXTURE_2D, texture);\n if (image) {\n gl.texImage2D(\n gl.TEXTURE_2D,\n 0,\n gl.RGBA,\n gl.RGBA,\n gl.UNSIGNED_BYTE,\n image,\n );\n }\n gl.uniform1i(uniform(name), unit);\n };\n\n return {\n clear,\n cleanup: () => {\n gl.deleteProgram(program);\n gl.deleteTexture(fromTexture);\n gl.deleteTexture(toTexture);\n gl.deleteBuffer(buffer);\n gl.deleteVertexArray(vao);\n },\n draw: ({ from, to, width, height, progress, passedProps }) => {\n gl.viewport(0, 0, width, height);\n clear();\n gl.useProgram(program);\n gl.bindVertexArray(vao);\n\n bindScene(from, fromTexture, 0, \"u_from\");\n bindScene(to, toTexture, 1, \"u_to\");\n\n gl.uniform1f(uniform(\"u_progress\"), progress);\n gl.uniform1f(uniform(\"u_aspect\"), width / height);\n setUniforms?.({ gl, uniform, width, height, progress, passedProps });\n\n gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);\n },\n };\n };\n}\n\nconst transferred = new WeakMap<HTMLCanvasElement, OffscreenCanvas>();\n\nfunction takeOffscreen(canvas: HTMLCanvasElement): OffscreenCanvas {\n const existing = transferred.get(canvas);\n if (existing) {\n return existing;\n }\n const created = canvas.transferControlToOffscreen();\n transferred.set(canvas, created);\n return created;\n}\n\nfunction makeCanvasComponent<Props extends Record<string, unknown>>(\n shader: SceneShader<Props>,\n): React.FC<TransitionPresentationComponentProps<Props>> {\n return function CanvasScenePresentation({\n children,\n presentationProgress,\n presentationDirection,\n passedProps,\n onElementImage,\n onUnmount,\n bothEnteringAndExiting,\n }) {\n const layoutRef = React.useRef<HTMLCanvasElement | null>(null);\n const outputRef = React.useRef<HTMLCanvasElement | null>(null);\n const captureRef = React.useRef<OffscreenCanvas | null>(null);\n const captureContextRef =\n React.useRef<OffscreenCanvasRenderingContext2D | null>(null);\n const outputSurfaceRef = React.useRef<OffscreenCanvas | null>(null);\n const instanceRef = React.useRef<SceneShaderInstance<Props> | null>(null);\n const passedPropsRef = React.useRef(passedProps);\n passedPropsRef.current = passedProps;\n const onElementImageRef = React.useRef(onElementImage);\n onElementImageRef.current = onElementImage;\n const onUnmountRef = React.useRef(onUnmount);\n onUnmountRef.current = onUnmount;\n\n const { delayRender, continueRender } = useDelayRender();\n const passThrough =\n bothEnteringAndExiting && presentationDirection === \"exiting\";\n\n const drawRef = React.useRef<\n (\n prevImage: OffscreenCanvas | null,\n nextImage: OffscreenCanvas | null,\n progress: number,\n ) => void\n >(() => undefined);\n drawRef.current = (prevImage, nextImage, progress) => {\n const instance = instanceRef.current;\n if (!instance) {\n return;\n }\n const handle = delayRender(\"canvas-presentation: paint\");\n const to = prevImage;\n const from = nextImage;\n const width = from?.width ?? to?.width ?? 0;\n const height = from?.height ?? to?.height ?? 0;\n if (width === 0 || height === 0) {\n instance.clear();\n continueRender(handle);\n return;\n }\n instance.draw({\n from,\n to,\n width,\n height,\n progress: !to ? 0 : !from ? 1 : progress,\n passedProps: passedPropsRef.current,\n });\n continueRender(handle);\n };\n\n const draw = React.useCallback(\n (\n prevImage: OffscreenCanvas | null,\n nextImage: OffscreenCanvas | null,\n progress: number,\n ) => drawRef.current(prevImage, nextImage, progress),\n [],\n );\n\n React.useLayoutEffect(() => {\n if (passThrough) {\n return;\n }\n const layout = layoutRef.current;\n const output = outputRef.current;\n if (!layout || !output) {\n return;\n }\n const capture = takeOffscreen(layout);\n const context = capture.getContext(\"2d\");\n if (!context) {\n throw new Error(\"canvas-presentation: failed to acquire a 2D context\");\n }\n const surface = takeOffscreen(output);\n captureRef.current = capture;\n captureContextRef.current = context;\n outputSurfaceRef.current = surface;\n instanceRef.current = shader(surface);\n\n return () => {\n instanceRef.current?.cleanup();\n instanceRef.current = null;\n captureRef.current = null;\n captureContextRef.current = null;\n outputSurfaceRef.current = null;\n };\n }, [passThrough, shader]);\n\n React.useLayoutEffect(() => {\n if (passThrough) {\n return;\n }\n const layout = layoutRef.current;\n if (!layout) {\n return;\n }\n layout.layoutSubtree = true;\n const handlePaint = () => {\n const scene = layout.firstElementChild;\n const capture = captureRef.current;\n const context = captureContextRef.current;\n if (!scene || !capture || !context) {\n return;\n }\n const elementImage = layout.captureElementImage(scene);\n try {\n context.reset();\n context.drawElementImage(elementImage, 0, 0);\n } finally {\n elementImage.close();\n }\n onElementImageRef.current(capture, draw);\n };\n layout.addEventListener(\"paint\", handlePaint);\n return () => layout.removeEventListener(\"paint\", handlePaint);\n }, [passThrough, draw]);\n\n React.useLayoutEffect(() => {\n if (passThrough) {\n return;\n }\n const layout = layoutRef.current;\n if (!layout) {\n return;\n }\n const observer = new ResizeObserver(([entry]) => {\n const capture = captureRef.current;\n const surface = outputSurfaceRef.current;\n if (!capture || !surface) {\n return;\n }\n const width = entry.devicePixelContentBoxSize[0].inlineSize;\n const height = entry.devicePixelContentBoxSize[0].blockSize;\n capture.width = width;\n capture.height = height;\n surface.width = width;\n surface.height = height;\n layout.requestPaint?.();\n });\n observer.observe(layout, { box: \"device-pixel-content-box\" });\n return () => observer.disconnect();\n }, [passThrough]);\n\n // biome-ignore lint/correctness/useExhaustiveDependencies: presentationProgress is the intentional trigger — a new progress value is what schedules the next paint\n React.useLayoutEffect(() => {\n if (passThrough) {\n return;\n }\n layoutRef.current?.requestPaint?.();\n }, [passThrough, presentationProgress]);\n\n React.useLayoutEffect(() => {\n if (passThrough) {\n return;\n }\n return () => onUnmountRef.current();\n }, [passThrough]);\n\n if (passThrough) {\n return <>{children}</>;\n }\n\n return (\n <AbsoluteFill>\n <canvas ref={layoutRef} style={LAYER}>\n {children}\n </canvas>\n <canvas ref={outputRef} style={OUTPUT_LAYER} />\n </AbsoluteFill>\n );\n };\n}\n\nexport type CanvasPresentationOptions<Props extends Record<string, unknown>> = {\n shader: SceneShader<Props>;\n fallback: React.FC<TransitionPresentationComponentProps<Props>>;\n};\n\nexport function makeCanvasPresentation<Props extends Record<string, unknown>>({\n shader,\n fallback: Fallback,\n}: CanvasPresentationOptions<Props>): (\n props: Props,\n) => TransitionPresentation<Props> {\n const Canvas = makeCanvasComponent(shader);\n\n const Presentation: React.FC<TransitionPresentationComponentProps<Props>> = (\n props,\n ) =>\n isCanvasTransitionSupported() ? (\n <Canvas {...props} />\n ) : (\n <Fallback {...props} />\n );\n\n return (props) => ({ component: Presentation, props });\n}\n",
+ "content": "\"use client\";\n\nimport type {\n TransitionPresentation,\n TransitionPresentationComponentProps,\n} from \"@remotion/transitions\";\nimport React from \"react\";\nimport {\n AbsoluteFill,\n isHtmlInCanvasSupported,\n useCurrentFrame,\n useDelayRender,\n useVideoConfig,\n} from \"remotion\";\n\nconst QUAD = new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]);\n\nconst VERTEX_SHADER = `#version 300 es\nin vec2 a_pos;\nout vec2 v_uv;\nvoid main() {\n v_uv = vec2(a_pos.x * 0.5 + 0.5, 0.5 - a_pos.y * 0.5);\n gl_Position = vec4(a_pos, 0.0, 1.0);\n}`;\n\nconst LAYER: React.CSSProperties = {\n position: \"absolute\",\n inset: 0,\n width: \"100%\",\n height: \"100%\",\n};\n\nconst OUTPUT_LAYER: React.CSSProperties = { ...LAYER, pointerEvents: \"none\" };\n\nexport const isCanvasTransitionSupported = (): boolean =>\n isHtmlInCanvasSupported();\n\nexport const isCanvasFilterSupported = (): boolean => isHtmlInCanvasSupported();\n\nexport type SceneShaderFrame<Props> = {\n gl: WebGL2RenderingContext;\n uniform: (name: string) => WebGLUniformLocation | null;\n width: number;\n height: number;\n progress: number;\n passedProps: Props;\n};\n\nexport type SceneShaderDrawParams<Props> = {\n from: OffscreenCanvas | null;\n to: OffscreenCanvas | null;\n width: number;\n height: number;\n progress: number;\n passedProps: Props;\n};\n\nexport type SceneShaderInstance<Props> = {\n draw: (params: SceneShaderDrawParams<Props>) => void;\n clear: () => void;\n cleanup: () => void;\n};\n\nexport type SceneShader<Props> = (\n canvas: OffscreenCanvas,\n) => SceneShaderInstance<Props>;\n\nexport type SceneFilterFrame<Props> = {\n gl: WebGL2RenderingContext;\n uniform: (name: string) => WebGLUniformLocation | null;\n width: number;\n height: number;\n frame: number;\n time: number;\n passedProps: Props;\n};\n\nexport type SceneFilterDrawParams<Props> = {\n scene: OffscreenCanvas | null;\n width: number;\n height: number;\n frame: number;\n time: number;\n passedProps: Props;\n};\n\nexport type SceneFilterInstance<Props> = {\n draw: (params: SceneFilterDrawParams<Props>) => void;\n clear: () => void;\n cleanup: () => void;\n};\n\nexport type SceneFilter<Props> = (\n canvas: OffscreenCanvas,\n) => SceneFilterInstance<Props>;\n\nfunction compileShader(\n gl: WebGL2RenderingContext,\n source: string,\n type: GLenum,\n): WebGLShader {\n const shader = gl.createShader(type);\n if (!shader) {\n throw new Error(\"canvas-presentation: failed to create shader\");\n }\n gl.shaderSource(shader, source);\n gl.compileShader(shader);\n if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n const log = gl.getShaderInfoLog(shader);\n gl.deleteShader(shader);\n throw new Error(`canvas-presentation: failed to compile shader: ${log}`);\n }\n return shader;\n}\n\nfunction createProgram(\n gl: WebGL2RenderingContext,\n fragment: string,\n): WebGLProgram {\n const program = gl.createProgram();\n if (!program) {\n throw new Error(\"canvas-presentation: failed to create program\");\n }\n const vs = compileShader(gl, VERTEX_SHADER, gl.VERTEX_SHADER);\n const fs = compileShader(gl, fragment, gl.FRAGMENT_SHADER);\n gl.attachShader(program, vs);\n gl.attachShader(program, fs);\n gl.linkProgram(program);\n gl.deleteShader(vs);\n gl.deleteShader(fs);\n if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {\n const log = gl.getProgramInfoLog(program);\n gl.deleteProgram(program);\n throw new Error(`canvas-presentation: failed to link program: ${log}`);\n }\n return program;\n}\n\nfunction createSceneTexture(gl: WebGL2RenderingContext): WebGLTexture {\n const texture = gl.createTexture();\n if (!texture) {\n throw new Error(\"canvas-presentation: failed to create texture\");\n }\n gl.bindTexture(gl.TEXTURE_2D, texture);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n gl.texImage2D(\n gl.TEXTURE_2D,\n 0,\n gl.RGBA,\n 1,\n 1,\n 0,\n gl.RGBA,\n gl.UNSIGNED_BYTE,\n new Uint8Array([0, 0, 0, 0]),\n );\n return texture;\n}\n\nfunction createQuadSurface(canvas: OffscreenCanvas, fragment: string) {\n const gl = canvas.getContext(\"webgl2\", { premultipliedAlpha: true });\n if (!gl) {\n throw new Error(\"canvas-presentation: WebGL2 is unavailable\");\n }\n\n const program = createProgram(gl, fragment);\n const vao = gl.createVertexArray();\n gl.bindVertexArray(vao);\n const buffer = gl.createBuffer();\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer);\n gl.bufferData(gl.ARRAY_BUFFER, QUAD, gl.STATIC_DRAW);\n const aPos = gl.getAttribLocation(program, \"a_pos\");\n gl.enableVertexAttribArray(aPos);\n gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);\n\n const locations = new Map<string, WebGLUniformLocation | null>();\n const uniform = (name: string) => {\n const cached = locations.get(name);\n if (cached !== undefined) {\n return cached;\n }\n const location = gl.getUniformLocation(program, name);\n locations.set(name, location);\n return location;\n };\n\n const clear = () => {\n gl.clearColor(0, 0, 0, 0);\n gl.clear(gl.COLOR_BUFFER_BIT);\n };\n\n const textures: WebGLTexture[] = [];\n const createTexture = () => {\n const texture = createSceneTexture(gl);\n textures.push(texture);\n return texture;\n };\n\n const begin = (width: number, height: number) => {\n gl.viewport(0, 0, width, height);\n clear();\n gl.useProgram(program);\n gl.bindVertexArray(vao);\n };\n\n const bindScene = (\n image: OffscreenCanvas | null,\n texture: WebGLTexture,\n unit: number,\n name: string,\n ) => {\n gl.activeTexture(gl.TEXTURE0 + unit);\n gl.bindTexture(gl.TEXTURE_2D, texture);\n if (image) {\n gl.texImage2D(\n gl.TEXTURE_2D,\n 0,\n gl.RGBA,\n gl.RGBA,\n gl.UNSIGNED_BYTE,\n image,\n );\n }\n gl.uniform1i(uniform(name), unit);\n };\n\n const draw = () => gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);\n\n const cleanup = () => {\n gl.deleteProgram(program);\n for (const texture of textures) {\n gl.deleteTexture(texture);\n }\n gl.deleteBuffer(buffer);\n gl.deleteVertexArray(vao);\n };\n\n return {\n gl,\n uniform,\n clear,\n createTexture,\n begin,\n bindScene,\n draw,\n cleanup,\n };\n}\n\nexport function makeSceneShader<Props extends Record<string, unknown>>(\n fragment: string,\n setUniforms?: (frame: SceneShaderFrame<Props>) => void,\n): SceneShader<Props> {\n return (canvas) => {\n const surface = createQuadSurface(canvas, fragment);\n const fromTexture = surface.createTexture();\n const toTexture = surface.createTexture();\n\n return {\n clear: surface.clear,\n cleanup: surface.cleanup,\n draw: ({ from, to, width, height, progress, passedProps }) => {\n surface.begin(width, height);\n surface.bindScene(from, fromTexture, 0, \"u_from\");\n surface.bindScene(to, toTexture, 1, \"u_to\");\n\n surface.gl.uniform1f(surface.uniform(\"u_progress\"), progress);\n surface.gl.uniform1f(surface.uniform(\"u_aspect\"), width / height);\n setUniforms?.({\n gl: surface.gl,\n uniform: surface.uniform,\n width,\n height,\n progress,\n passedProps,\n });\n\n surface.draw();\n },\n };\n };\n}\n\nexport function makeFilterShader<Props extends Record<string, unknown>>(\n fragment: string,\n setUniforms?: (frame: SceneFilterFrame<Props>) => void,\n): SceneFilter<Props> {\n return (canvas) => {\n const surface = createQuadSurface(canvas, fragment);\n const sceneTexture = surface.createTexture();\n\n return {\n clear: surface.clear,\n cleanup: surface.cleanup,\n draw: ({ scene, width, height, frame, time, passedProps }) => {\n surface.begin(width, height);\n surface.bindScene(scene, sceneTexture, 0, \"u_scene\");\n\n surface.gl.uniform1f(surface.uniform(\"u_frame\"), frame);\n surface.gl.uniform1f(surface.uniform(\"u_time\"), time);\n surface.gl.uniform1f(surface.uniform(\"u_aspect\"), width / height);\n setUniforms?.({\n gl: surface.gl,\n uniform: surface.uniform,\n width,\n height,\n frame,\n time,\n passedProps,\n });\n\n surface.draw();\n },\n };\n };\n}\n\nconst transferred = new WeakMap<HTMLCanvasElement, OffscreenCanvas>();\n\nfunction takeOffscreen(canvas: HTMLCanvasElement): OffscreenCanvas {\n const existing = transferred.get(canvas);\n if (existing) {\n return existing;\n }\n const created = canvas.transferControlToOffscreen();\n transferred.set(canvas, created);\n return created;\n}\n\nfunction makeCanvasComponent<Props extends Record<string, unknown>>(\n shader: SceneShader<Props>,\n): React.FC<TransitionPresentationComponentProps<Props>> {\n return function CanvasScenePresentation({\n children,\n presentationProgress,\n presentationDirection,\n passedProps,\n onElementImage,\n onUnmount,\n bothEnteringAndExiting,\n }) {\n const layoutRef = React.useRef<HTMLCanvasElement | null>(null);\n const outputRef = React.useRef<HTMLCanvasElement | null>(null);\n const captureRef = React.useRef<OffscreenCanvas | null>(null);\n const captureContextRef =\n React.useRef<OffscreenCanvasRenderingContext2D | null>(null);\n const outputSurfaceRef = React.useRef<OffscreenCanvas | null>(null);\n const instanceRef = React.useRef<SceneShaderInstance<Props> | null>(null);\n const passedPropsRef = React.useRef(passedProps);\n passedPropsRef.current = passedProps;\n const onElementImageRef = React.useRef(onElementImage);\n onElementImageRef.current = onElementImage;\n const onUnmountRef = React.useRef(onUnmount);\n onUnmountRef.current = onUnmount;\n\n const { delayRender, continueRender } = useDelayRender();\n const passThrough =\n bothEnteringAndExiting && presentationDirection === \"exiting\";\n\n const drawRef = React.useRef<\n (\n prevImage: OffscreenCanvas | null,\n nextImage: OffscreenCanvas | null,\n progress: number,\n ) => void\n >(() => undefined);\n drawRef.current = (prevImage, nextImage, progress) => {\n const instance = instanceRef.current;\n if (!instance) {\n return;\n }\n const handle = delayRender(\"canvas-presentation: paint\");\n const to = prevImage;\n const from = nextImage;\n const width = from?.width ?? to?.width ?? 0;\n const height = from?.height ?? to?.height ?? 0;\n if (width === 0 || height === 0) {\n instance.clear();\n continueRender(handle);\n return;\n }\n instance.draw({\n from,\n to,\n width,\n height,\n progress: !to ? 0 : !from ? 1 : progress,\n passedProps: passedPropsRef.current,\n });\n continueRender(handle);\n };\n\n const draw = React.useCallback(\n (\n prevImage: OffscreenCanvas | null,\n nextImage: OffscreenCanvas | null,\n progress: number,\n ) => drawRef.current(prevImage, nextImage, progress),\n [],\n );\n\n React.useLayoutEffect(() => {\n if (passThrough) {\n return;\n }\n const layout = layoutRef.current;\n const output = outputRef.current;\n if (!layout || !output) {\n return;\n }\n const capture = takeOffscreen(layout);\n const context = capture.getContext(\"2d\");\n if (!context) {\n throw new Error(\"canvas-presentation: failed to acquire a 2D context\");\n }\n const surface = takeOffscreen(output);\n captureRef.current = capture;\n captureContextRef.current = context;\n outputSurfaceRef.current = surface;\n instanceRef.current = shader(surface);\n\n return () => {\n instanceRef.current?.cleanup();\n instanceRef.current = null;\n captureRef.current = null;\n captureContextRef.current = null;\n outputSurfaceRef.current = null;\n };\n }, [passThrough, shader]);\n\n React.useLayoutEffect(() => {\n if (passThrough) {\n return;\n }\n const layout = layoutRef.current;\n if (!layout) {\n return;\n }\n layout.layoutSubtree = true;\n const handlePaint = () => {\n const scene = layout.firstElementChild;\n const capture = captureRef.current;\n const context = captureContextRef.current;\n if (!scene || !capture || !context) {\n return;\n }\n const elementImage = layout.captureElementImage(scene);\n try {\n context.reset();\n context.drawElementImage(elementImage, 0, 0);\n } finally {\n elementImage.close();\n }\n onElementImageRef.current(capture, draw);\n };\n layout.addEventListener(\"paint\", handlePaint);\n return () => layout.removeEventListener(\"paint\", handlePaint);\n }, [passThrough, draw]);\n\n React.useLayoutEffect(() => {\n if (passThrough) {\n return;\n }\n const layout = layoutRef.current;\n if (!layout) {\n return;\n }\n const observer = new ResizeObserver(([entry]) => {\n const capture = captureRef.current;\n const surface = outputSurfaceRef.current;\n if (!capture || !surface) {\n return;\n }\n const width = entry.devicePixelContentBoxSize[0].inlineSize;\n const height = entry.devicePixelContentBoxSize[0].blockSize;\n capture.width = width;\n capture.height = height;\n surface.width = width;\n surface.height = height;\n layout.requestPaint?.();\n });\n observer.observe(layout, { box: \"device-pixel-content-box\" });\n return () => observer.disconnect();\n }, [passThrough]);\n\n // biome-ignore lint/correctness/useExhaustiveDependencies: presentationProgress is the intentional trigger — a new progress value is what schedules the next paint\n React.useLayoutEffect(() => {\n if (passThrough) {\n return;\n }\n layoutRef.current?.requestPaint?.();\n }, [passThrough, presentationProgress]);\n\n React.useLayoutEffect(() => {\n if (passThrough) {\n return;\n }\n return () => onUnmountRef.current();\n }, [passThrough]);\n\n if (passThrough) {\n return <>{children}</>;\n }\n\n return (\n <AbsoluteFill>\n <canvas ref={layoutRef} style={LAYER}>\n {children}\n </canvas>\n <canvas ref={outputRef} style={OUTPUT_LAYER} />\n </AbsoluteFill>\n );\n };\n}\n\nexport type CanvasFilterProps = {\n children: React.ReactNode;\n};\n\nfunction makeFilterComponent<Props extends Record<string, unknown>>(\n shader: SceneFilter<Props>,\n): React.FC<Props & CanvasFilterProps> {\n return function CanvasSceneFilter(props) {\n const frame = useCurrentFrame();\n const { fps } = useVideoConfig();\n const { delayRender, continueRender } = useDelayRender();\n\n const layoutRef = React.useRef<HTMLCanvasElement | null>(null);\n const outputRef = React.useRef<HTMLCanvasElement | null>(null);\n const captureRef = React.useRef<OffscreenCanvas | null>(null);\n const captureContextRef =\n React.useRef<OffscreenCanvasRenderingContext2D | null>(null);\n const outputSurfaceRef = React.useRef<OffscreenCanvas | null>(null);\n const instanceRef = React.useRef<SceneFilterInstance<Props> | null>(null);\n\n const passedPropsRef = React.useRef(props);\n passedPropsRef.current = props;\n const frameRef = React.useRef(frame);\n frameRef.current = frame;\n const timeRef = React.useRef(frame / fps);\n timeRef.current = frame / fps;\n\n React.useLayoutEffect(() => {\n const layout = layoutRef.current;\n const output = outputRef.current;\n if (!layout || !output) {\n return;\n }\n const capture = takeOffscreen(layout);\n const context = capture.getContext(\"2d\");\n if (!context) {\n throw new Error(\"canvas-presentation: failed to acquire a 2D context\");\n }\n const surface = takeOffscreen(output);\n captureRef.current = capture;\n captureContextRef.current = context;\n outputSurfaceRef.current = surface;\n instanceRef.current = shader(surface);\n\n return () => {\n instanceRef.current?.cleanup();\n instanceRef.current = null;\n captureRef.current = null;\n captureContextRef.current = null;\n outputSurfaceRef.current = null;\n };\n }, [shader]);\n\n React.useLayoutEffect(() => {\n const layout = layoutRef.current;\n if (!layout) {\n return;\n }\n layout.layoutSubtree = true;\n const handlePaint = () => {\n const scene = layout.firstElementChild;\n const capture = captureRef.current;\n const context = captureContextRef.current;\n const instance = instanceRef.current;\n if (!scene || !capture || !context || !instance) {\n return;\n }\n const elementImage = layout.captureElementImage(scene);\n try {\n context.reset();\n context.drawElementImage(elementImage, 0, 0);\n } finally {\n elementImage.close();\n }\n if (capture.width === 0 || capture.height === 0) {\n instance.clear();\n return;\n }\n instance.draw({\n scene: capture,\n width: capture.width,\n height: capture.height,\n frame: frameRef.current,\n time: timeRef.current,\n passedProps: passedPropsRef.current,\n });\n };\n layout.addEventListener(\"paint\", handlePaint);\n return () => layout.removeEventListener(\"paint\", handlePaint);\n }, []);\n\n React.useLayoutEffect(() => {\n const layout = layoutRef.current;\n if (!layout) {\n return;\n }\n const observer = new ResizeObserver(([entry]) => {\n const capture = captureRef.current;\n const surface = outputSurfaceRef.current;\n if (!capture || !surface) {\n return;\n }\n const width = entry.devicePixelContentBoxSize[0].inlineSize;\n const height = entry.devicePixelContentBoxSize[0].blockSize;\n capture.width = width;\n capture.height = height;\n surface.width = width;\n surface.height = height;\n layout.requestPaint?.();\n });\n observer.observe(layout, { box: \"device-pixel-content-box\" });\n return () => observer.disconnect();\n }, []);\n\n React.useLayoutEffect(() => {\n const layout = layoutRef.current;\n if (!layout) {\n return;\n }\n const handle = delayRender(\"canvas-presentation: filter paint\");\n const settle = () => continueRender(handle);\n layout.addEventListener(\"paint\", settle, { once: true });\n layout.requestPaint?.();\n return () => {\n layout.removeEventListener(\"paint\", settle);\n continueRender(handle);\n };\n });\n\n return (\n <AbsoluteFill>\n <canvas ref={layoutRef} style={LAYER}>\n {props.children}\n </canvas>\n <canvas ref={outputRef} style={OUTPUT_LAYER} />\n </AbsoluteFill>\n );\n };\n}\n\nexport type CanvasPresentationOptions<Props extends Record<string, unknown>> = {\n shader: SceneShader<Props>;\n fallback: React.FC<TransitionPresentationComponentProps<Props>>;\n};\n\nexport function makeCanvasPresentation<Props extends Record<string, unknown>>({\n shader,\n fallback: Fallback,\n}: CanvasPresentationOptions<Props>): (\n props: Props,\n) => TransitionPresentation<Props> {\n const Canvas = makeCanvasComponent(shader);\n\n const Presentation: React.FC<TransitionPresentationComponentProps<Props>> = (\n props,\n ) =>\n isCanvasTransitionSupported() ? (\n <Canvas {...props} />\n ) : (\n <Fallback {...props} />\n );\n\n return (props) => ({ component: Presentation, props });\n}\n\nexport type CanvasFilterOptions<Props extends Record<string, unknown>> = {\n shader: SceneFilter<Props>;\n fallback: React.FC<Props & CanvasFilterProps>;\n};\n\nexport function makeCanvasFilter<Props extends Record<string, unknown>>({\n shader,\n fallback: Fallback,\n}: CanvasFilterOptions<Props>): React.FC<Props & CanvasFilterProps> {\n const Canvas = makeFilterComponent(shader);\n\n return function CanvasFilter(props) {\n return isCanvasFilterSupported() ? (\n <Canvas {...props} />\n ) : (\n <Fallback {...props} />\n );\n };\n}\n",
"type": "registry:lib",
"target": "lib/remocn/canvas-presentation.tsx"
}
diff --git a/registry-artifacts/registry.json b/registry-artifacts/registry.json
index 20e6fc13..e38d4af6 100644
--- a/registry-artifacts/registry.json
+++ b/registry-artifacts/registry.json
@@ -1633,7 +1633,7 @@
{
"name": "canvas-presentation",
"title": "Canvas Presentation",
- "description": "The html-in-canvas scaffolding the canvas transitions share: support detection, a full-screen WebGL2 scene shader over both sides of the cut, and routing to a CSS fallback where the browser lacks the API.",
+ "description": "The html-in-canvas scaffolding the canvas transitions and scene filters share: support detection, a full-screen WebGL2 shader fed either both sides of a cut or the single scene a filter wraps, and routing to a CSS fallback where the browser lacks the API.",
"dependencies": ["remotion", "@remotion/transitions"],
"files": [
{
@@ -1674,6 +1674,21 @@
],
"type": "registry:component"
},
+ {
+ "name": "vhs-filter",
+ "title": "VHS Filter",
+ "description": "Hold a scene under analog tape: chroma bleeds sideways off every edge, rows wobble out of line, the bottom of the frame tears on the head switch, and luma noise settles over all of it.",
+ "dependencies": ["remotion"],
+ "registryDependencies": ["@remocn/canvas-presentation"],
+ "files": [
+ {
+ "path": "registry/remocn/vhs-filter/index.tsx",
+ "type": "registry:component",
+ "target": "components/remocn/vhs-filter.tsx"
+ }
+ ],
+ "type": "registry:component"
+ },
{
"name": "grid-wave",
"title": "Grid Wave",
diff --git a/registry-artifacts/vhs-filter.json b/registry-artifacts/vhs-filter.json
new file mode 100644
index 00000000..3873fbef
--- /dev/null
+++ b/registry-artifacts/vhs-filter.json
@@ -0,0 +1,17 @@
+{
+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",
+ "name": "vhs-filter",
+ "title": "VHS Filter",
+ "description": "Hold a scene under analog tape: chroma bleeds sideways off every edge, rows wobble out of line, the bottom of the frame tears on the head switch, and luma noise settles over all of it.",
+ "dependencies": ["remotion"],
+ "registryDependencies": ["@remocn/canvas-presentation"],
+ "files": [
+ {
+ "path": "registry/remocn/vhs-filter/index.tsx",
+ "content": "\"use client\";\n\nimport type React from \"react\";\nimport { AbsoluteFill, random, useCurrentFrame } from \"remotion\";\nimport {\n type CanvasFilterProps,\n makeCanvasFilter,\n makeFilterShader,\n} from \"@/lib/remocn/canvas-presentation\";\n\nexport type VhsFilterProps = {\n bleed?: number;\n wobble?: number;\n noise?: number;\n intensity?: number;\n};\n\nconst DEFAULTS = {\n bleed: 1,\n wobble: 1,\n noise: 1,\n intensity: 1,\n};\n\nconst FRAGMENT_SHADER = `#version 300 es\nprecision highp float;\n\nuniform sampler2D u_scene;\nuniform float u_frame;\nuniform float u_time;\nuniform float u_bleed;\nuniform float u_wobble;\nuniform float u_noise;\nuniform float u_intensity;\n\nin vec2 v_uv;\nout vec4 outColor;\n\nconst float PI = 3.141592653589793;\nconst float LINES = 486.0;\nconst int TAPS = 6;\n\nfloat hash(vec2 p) {\n return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);\n}\n\nfloat valueNoise(float x) {\n float i = floor(x);\n float f = fract(x);\n float a = hash(vec2(i, 7.0));\n float b = hash(vec2(i + 1.0, 7.0));\n return mix(a, b, f * f * (3.0 - 2.0 * f));\n}\n\nvec3 rgb2yiq(vec3 c) {\n return vec3(\n dot(c, vec3(0.299, 0.587, 0.114)),\n dot(c, vec3(0.5959, -0.2746, -0.3213)),\n dot(c, vec3(0.2115, -0.5227, 0.3112))\n );\n}\n\nvec3 yiq2rgb(vec3 c) {\n return vec3(\n c.x + 0.956 * c.y + 0.619 * c.z,\n c.x - 0.272 * c.y - 0.647 * c.z,\n c.x - 1.106 * c.y + 1.703 * c.z\n );\n}\n\nvoid main() {\n float line = floor(v_uv.y * LINES);\n float clock = mod(u_frame, 64.0);\n\n float drift =\n sin(v_uv.y * 37.0 + u_time * 2.3) * 0.55 +\n sin(v_uv.y * 96.0 - u_time * 1.1) * 0.25;\n float jitter = valueNoise(line * 0.61 + u_time * 24.0) - 0.5;\n float wobble = (drift * 0.35 + jitter * 0.9) * 0.007 * u_wobble * u_intensity;\n\n float band = smoothstep(0.955, 0.994, v_uv.y);\n float tear = valueNoise(line * 2.7 + u_time * 43.0) - 0.5;\n float headSwitch = band * tear * 0.085 * u_intensity;\n\n vec2 uv = vec2(v_uv.x + wobble + headSwitch, v_uv.y);\n vec4 base = texture(u_scene, uv);\n float luma = rgb2yiq(base.rgb).x;\n\n float smear = u_bleed * 0.02 * u_intensity;\n vec2 chroma = vec2(0.0);\n float total = 0.0;\n for (int i = 0; i < TAPS; i++) {\n float t = float(i) / float(TAPS - 1);\n float weight = 1.0 - t * 0.72;\n vec3 tap = texture(u_scene, vec2(uv.x - smear * t, uv.y)).rgb;\n chroma += rgb2yiq(tap).yz * weight;\n total += weight;\n }\n\n vec3 color = yiq2rgb(vec3(luma, chroma / total));\n\n float scan = 0.94 + 0.06 * cos(v_uv.y * LINES * PI);\n color *= mix(1.0, scan, u_intensity);\n\n float grain =\n hash(vec2(v_uv.x * 640.0 + clock * 13.0, line + clock * 5.0)) - 0.5;\n color += grain * 0.07 * u_noise * u_intensity;\n\n color = mix(color, vec3(luma), band * 0.55 * u_intensity);\n color += band * (hash(vec2(line, clock * 3.0)) - 0.5) * 0.26 * u_intensity;\n\n outColor = vec4(clamp(color, 0.0, 1.0), base.a);\n}`;\n\nconst shader = makeFilterShader<VhsFilterProps>(\n FRAGMENT_SHADER,\n ({ gl, uniform, passedProps }) => {\n const {\n bleed = DEFAULTS.bleed,\n wobble = DEFAULTS.wobble,\n noise = DEFAULTS.noise,\n intensity = DEFAULTS.intensity,\n } = passedProps;\n gl.uniform1f(uniform(\"u_bleed\"), bleed);\n gl.uniform1f(uniform(\"u_wobble\"), wobble);\n gl.uniform1f(uniform(\"u_noise\"), noise);\n gl.uniform1f(uniform(\"u_intensity\"), intensity);\n },\n);\n\nconst VhsFilterFallback: React.FC<VhsFilterProps & CanvasFilterProps> = ({\n children,\n bleed = DEFAULTS.bleed,\n wobble = DEFAULTS.wobble,\n noise = DEFAULTS.noise,\n intensity = DEFAULTS.intensity,\n}) => {\n const frame = useCurrentFrame();\n const shift =\n (random(`vhs-wobble-${frame}`) - 0.5) * 3.4 * wobble * intensity;\n const flicker =\n 1 + (random(`vhs-flicker-${frame}`) - 0.5) * 0.07 * noise * intensity;\n const smear = 3 + bleed * 5;\n\n return (\n <AbsoluteFill>\n <AbsoluteFill\n style={{\n transform: `translateX(${shift}px)`,\n filter: `saturate(${1 + bleed * 0.3}) contrast(1.04) brightness(${flicker}) blur(${0.3 * intensity}px)`,\n }}\n >\n {children}\n </AbsoluteFill>\n <AbsoluteFill\n style={{\n transform: `translateX(${shift - smear}px)`,\n filter: \"saturate(2.2) blur(1.4px)\",\n mixBlendMode: \"screen\",\n opacity: 0.24 * bleed * intensity,\n }}\n >\n {children}\n </AbsoluteFill>\n <AbsoluteFill\n style={{\n background:\n \"repeating-linear-gradient(to bottom, rgba(0, 0, 0, 0.22) 0px, rgba(0, 0, 0, 0.22) 1px, transparent 1px, transparent 3px)\",\n opacity: intensity,\n }}\n />\n <div\n style={{\n position: \"absolute\",\n insetInline: 0,\n bottom: 0,\n height: \"3.6%\",\n transform: `translateX(${shift * 3}px)`,\n background: `rgba(228, 228, 228, ${0.13 * intensity})`,\n mixBlendMode: \"screen\",\n }}\n />\n </AbsoluteFill>\n );\n};\n\nexport const VhsFilter = makeCanvasFilter<VhsFilterProps>({\n shader,\n fallback: VhsFilterFallback,\n});\n",
+ "type": "registry:component",
+ "target": "components/remocn/vhs-filter.tsx"
+ }
+ ],
+ "type": "registry:component"
+}
diff --git a/registry/__index__.tsx b/registry/__index__.tsx
index 1ecb7601..56417807 100644
--- a/registry/__index__.tsx
+++ b/registry/__index__.tsx
@@ -111,6 +111,7 @@ import { trackingInConfig } from "@/registry/remocn/tracking-in/config";
import { typewriterConfig } from "@/registry/remocn/typewriter/config";
import { v0Config } from "@/registry/remocn/v0/config";
import { valueSwapConfig } from "@/registry/remocn/value-swap/config";
+import { vhsFilterConfig } from "@/registry/remocn/vhs-filter/config";
import { warpDissolveConfig } from "@/registry/remocn/warp-dissolve/config";
import { waveWipeConfig } from "@/registry/remocn/wave-wipe/config";
import { whipPanConfig } from "@/registry/remocn/whip-pan/config";
@@ -1457,6 +1458,13 @@ const registry: Record<string, RegistryEntry> = {
})),
config: displacementConfig,
},
+ "vhs-filter": {
+ load: () =>
+ import("@/components/docs/examples/vhs-filter-example").then((m) => ({
+ default: m.VhsFilterExampleScene,
+ })),
+ config: vhsFilterConfig,
+ },
"slide-swap": {
load: () =>
import("@/components/docs/examples/slide-swap-example").then((m) => ({
diff --git a/registry/remocn/canvas-presentation/index.tsx b/registry/remocn/canvas-presentation/index.tsx
index 7286f5f5..2a08da7d 100644
--- a/registry/remocn/canvas-presentation/index.tsx
+++ b/registry/remocn/canvas-presentation/index.tsx
@@ -8,7 +8,9 @@ import React from "react";
import {
AbsoluteFill,
isHtmlInCanvasSupported,
+ useCurrentFrame,
useDelayRender,
+ useVideoConfig,
} from "remotion";
const QUAD = new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]);
@@ -33,6 +35,8 @@ const OUTPUT_LAYER: React.CSSProperties = { ...LAYER, pointerEvents: "none" };
export const isCanvasTransitionSupported = (): boolean =>
isHtmlInCanvasSupported();
+export const isCanvasFilterSupported = (): boolean => isHtmlInCanvasSupported();
+
export type SceneShaderFrame<Props> = {
gl: WebGL2RenderingContext;
uniform: (name: string) => WebGLUniformLocation | null;
@@ -61,6 +65,35 @@ export type SceneShader<Props> = (
canvas: OffscreenCanvas,
) => SceneShaderInstance<Props>;
+export type SceneFilterFrame<Props> = {
+ gl: WebGL2RenderingContext;
+ uniform: (name: string) => WebGLUniformLocation | null;
+ width: number;
+ height: number;
+ frame: number;
+ time: number;
+ passedProps: Props;
+};
+
+export type SceneFilterDrawParams<Props> = {
+ scene: OffscreenCanvas | null;
+ width: number;
+ height: number;
+ frame: number;
+ time: number;
+ passedProps: Props;
+};
+
+export type SceneFilterInstance<Props> = {
+ draw: (params: SceneFilterDrawParams<Props>) => void;
+ clear: () => void;
+ cleanup: () => void;
+};
+
+export type SceneFilter<Props> = (
+ canvas: OffscreenCanvas,
+) => SceneFilterInstance<Props>;
+
function compileShader(
gl: WebGL2RenderingContext,
source: string,
@@ -127,88 +160,159 @@ function createSceneTexture(gl: WebGL2RenderingContext): WebGLTexture {
return texture;
}
-export function makeSceneShader<Props extends Record<string, unknown>>(
- fragment: string,
- setUniforms?: (frame: SceneShaderFrame<Props>) => void,
-): SceneShader<Props> {
- return (canvas) => {
- const gl = canvas.getContext("webgl2", { premultipliedAlpha: true });
- if (!gl) {
- throw new Error("canvas-presentation: WebGL2 is unavailable");
+function createQuadSurface(canvas: OffscreenCanvas, fragment: string) {
+ const gl = canvas.getContext("webgl2", { premultipliedAlpha: true });
+ if (!gl) {
+ throw new Error("canvas-presentation: WebGL2 is unavailable");
+ }
+
+ const program = createProgram(gl, fragment);
+ const vao = gl.createVertexArray();
+ gl.bindVertexArray(vao);
+ const buffer = gl.createBuffer();
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
+ gl.bufferData(gl.ARRAY_BUFFER, QUAD, gl.STATIC_DRAW);
+ const aPos = gl.getAttribLocation(program, "a_pos");
+ gl.enableVertexAttribArray(aPos);
+ gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);
+
+ const locations = new Map<string, WebGLUniformLocation | null>();
+ const uniform = (name: string) => {
+ const cached = locations.get(name);
+ if (cached !== undefined) {
+ return cached;
}
+ const location = gl.getUniformLocation(program, name);
+ locations.set(name, location);
+ return location;
+ };
- const program = createProgram(gl, fragment);
- const fromTexture = createSceneTexture(gl);
- const toTexture = createSceneTexture(gl);
- const vao = gl.createVertexArray();
+ const clear = () => {
+ gl.clearColor(0, 0, 0, 0);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+ };
+
+ const textures: WebGLTexture[] = [];
+ const createTexture = () => {
+ const texture = createSceneTexture(gl);
+ textures.push(texture);
+ return texture;
+ };
+
+ const begin = (width: number, height: number) => {
+ gl.viewport(0, 0, width, height);
+ clear();
+ gl.useProgram(program);
gl.bindVertexArray(vao);
- const buffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
- gl.bufferData(gl.ARRAY_BUFFER, QUAD, gl.STATIC_DRAW);
- const aPos = gl.getAttribLocation(program, "a_pos");
- gl.enableVertexAttribArray(aPos);
- gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);
-
- const locations = new Map<string, WebGLUniformLocation | null>();
- const uniform = (name: string) => {
- const cached = locations.get(name);
- if (cached !== undefined) {
- return cached;
- }
- const location = gl.getUniformLocation(program, name);
- locations.set(name, location);
- return location;
- };
+ };
- const clear = () => {
- gl.clearColor(0, 0, 0, 0);
- gl.clear(gl.COLOR_BUFFER_BIT);
- };
+ const bindScene = (
+ image: OffscreenCanvas | null,
+ texture: WebGLTexture,
+ unit: number,
+ name: string,
+ ) => {
+ gl.activeTexture(gl.TEXTURE0 + unit);
+ gl.bindTexture(gl.TEXTURE_2D, texture);
+ if (image) {
+ gl.texImage2D(
+ gl.TEXTURE_2D,
+ 0,
+ gl.RGBA,
+ gl.RGBA,
+ gl.UNSIGNED_BYTE,
+ image,
+ );
+ }
+ gl.uniform1i(uniform(name), unit);
+ };
- const bindScene = (
- image: OffscreenCanvas | null,
- texture: WebGLTexture,
- unit: number,
- name: string,
- ) => {
- gl.activeTexture(gl.TEXTURE0 + unit);
- gl.bindTexture(gl.TEXTURE_2D, texture);
- if (image) {
- gl.texImage2D(
- gl.TEXTURE_2D,
- 0,
- gl.RGBA,
- gl.RGBA,
- gl.UNSIGNED_BYTE,
- image,
- );
- }
- gl.uniform1i(uniform(name), unit);
- };
+ const draw = () => gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
+
+ const cleanup = () => {
+ gl.deleteProgram(program);
+ for (const texture of textures) {
+ gl.deleteTexture(texture);
+ }
+ gl.deleteBuffer(buffer);
+ gl.deleteVertexArray(vao);
+ };
+
+ return {
+ gl,
+ uniform,
+ clear,
+ createTexture,
+ begin,
+ bindScene,
+ draw,
+ cleanup,
+ };
+}
+
+export function makeSceneShader<Props extends Record<string, unknown>>(
+ fragment: string,
+ setUniforms?: (frame: SceneShaderFrame<Props>) => void,
+): SceneShader<Props> {
+ return (canvas) => {
+ const surface = createQuadSurface(canvas, fragment);
+ const fromTexture = surface.createTexture();
+ const toTexture = surface.createTexture();
return {
- clear,
- cleanup: () => {
- gl.deleteProgram(program);
- gl.deleteTexture(fromTexture);
- gl.deleteTexture(toTexture);
- gl.deleteBuffer(buffer);
- gl.deleteVertexArray(vao);
- },
+ clear: surface.clear,
+ cleanup: surface.cleanup,
draw: ({ from, to, width, height, progress, passedProps }) => {
- gl.viewport(0, 0, width, height);
- clear();
- gl.useProgram(program);
- gl.bindVertexArray(vao);
-
- bindScene(from, fromTexture, 0, "u_from");
- bindScene(to, toTexture, 1, "u_to");
+ surface.begin(width, height);
+ surface.bindScene(from, fromTexture, 0, "u_from");
+ surface.bindScene(to, toTexture, 1, "u_to");
+
+ surface.gl.uniform1f(surface.uniform("u_progress"), progress);
+ surface.gl.uniform1f(surface.uniform("u_aspect"), width / height);
+ setUniforms?.({
+ gl: surface.gl,
+ uniform: surface.uniform,
+ width,
+ height,
+ progress,
+ passedProps,
+ });
+
+ surface.draw();
+ },
+ };
+ };
+}
- gl.uniform1f(uniform("u_progress"), progress);
- gl.uniform1f(uniform("u_aspect"), width / height);
- setUniforms?.({ gl, uniform, width, height, progress, passedProps });
+export function makeFilterShader<Props extends Record<string, unknown>>(
+ fragment: string,
+ setUniforms?: (frame: SceneFilterFrame<Props>) => void,
+): SceneFilter<Props> {
+ return (canvas) => {
+ const surface = createQuadSurface(canvas, fragment);
+ const sceneTexture = surface.createTexture();
- gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
+ return {
+ clear: surface.clear,
+ cleanup: surface.cleanup,
+ draw: ({ scene, width, height, frame, time, passedProps }) => {
+ surface.begin(width, height);
+ surface.bindScene(scene, sceneTexture, 0, "u_scene");
+
+ surface.gl.uniform1f(surface.uniform("u_frame"), frame);
+ surface.gl.uniform1f(surface.uniform("u_time"), time);
+ surface.gl.uniform1f(surface.uniform("u_aspect"), width / height);
+ setUniforms?.({
+ gl: surface.gl,
+ uniform: surface.uniform,
+ width,
+ height,
+ frame,
+ time,
+ passedProps,
+ });
+
+ surface.draw();
},
};
};
@@ -412,6 +516,146 @@ function makeCanvasComponent<Props extends Record<string, unknown>>(
};
}
+export type CanvasFilterProps = {
+ children: React.ReactNode;
+};
+
+function makeFilterComponent<Props extends Record<string, unknown>>(
+ shader: SceneFilter<Props>,
+): React.FC<Props & CanvasFilterProps> {
+ return function CanvasSceneFilter(props) {
+ const frame = useCurrentFrame();
+ const { fps } = useVideoConfig();
+ const { delayRender, continueRender } = useDelayRender();
+
+ const layoutRef = React.useRef<HTMLCanvasElement | null>(null);
+ const outputRef = React.useRef<HTMLCanvasElement | null>(null);
+ const captureRef = React.useRef<OffscreenCanvas | null>(null);
+ const captureContextRef =
+ React.useRef<OffscreenCanvasRenderingContext2D | null>(null);
+ const outputSurfaceRef = React.useRef<OffscreenCanvas | null>(null);
+ const instanceRef = React.useRef<SceneFilterInstance<Props> | null>(null);
+
+ const passedPropsRef = React.useRef(props);
+ passedPropsRef.current = props;
+ const frameRef = React.useRef(frame);
+ frameRef.current = frame;
+ const timeRef = React.useRef(frame / fps);
+ timeRef.current = frame / fps;
+
+ React.useLayoutEffect(() => {
+ const layout = layoutRef.current;
+ const output = outputRef.current;
+ if (!layout || !output) {
+ return;
+ }
+ const capture = takeOffscreen(layout);
+ const context = capture.getContext("2d");
+ if (!context) {
+ throw new Error("canvas-presentation: failed to acquire a 2D context");
+ }
+ const surface = takeOffscreen(output);
+ captureRef.current = capture;
+ captureContextRef.current = context;
+ outputSurfaceRef.current = surface;
+ instanceRef.current = shader(surface);
+
+ return () => {
+ instanceRef.current?.cleanup();
+ instanceRef.current = null;
+ captureRef.current = null;
+ captureContextRef.current = null;
+ outputSurfaceRef.current = null;
+ };
+ }, [shader]);
+
+ React.useLayoutEffect(() => {
+ const layout = layoutRef.current;
+ if (!layout) {
+ return;
+ }
+ layout.layoutSubtree = true;
+ const handlePaint = () => {
+ const scene = layout.firstElementChild;
+ const capture = captureRef.current;
+ const context = captureContextRef.current;
+ const instance = instanceRef.current;
+ if (!scene || !capture || !context || !instance) {
+ return;
+ }
+ const elementImage = layout.captureElementImage(scene);
+ try {
+ context.reset();
+ context.drawElementImage(elementImage, 0, 0);
+ } finally {
+ elementImage.close();
+ }
+ if (capture.width === 0 || capture.height === 0) {
+ instance.clear();
+ return;
+ }
+ instance.draw({
+ scene: capture,
+ width: capture.width,
+ height: capture.height,
+ frame: frameRef.current,
+ time: timeRef.current,
+ passedProps: passedPropsRef.current,
+ });
+ };
+ layout.addEventListener("paint", handlePaint);
+ return () => layout.removeEventListener("paint", handlePaint);
+ }, []);
+
+ React.useLayoutEffect(() => {
+ const layout = layoutRef.current;
+ if (!layout) {
+ return;
+ }
+ const observer = new ResizeObserver(([entry]) => {
+ const capture = captureRef.current;
+ const surface = outputSurfaceRef.current;
+ if (!capture || !surface) {
+ return;
+ }
+ const width = entry.devicePixelContentBoxSize[0].inlineSize;
+ const height = entry.devicePixelContentBoxSize[0].blockSize;
+ capture.width = width;
+ capture.height = height;
+ surface.width = width;
+ surface.height = height;
+ layout.requestPaint?.();
+ });
+ observer.observe(layout, { box: "device-pixel-content-box" });
+ return () => observer.disconnect();
+ }, []);
+
+ React.useLayoutEffect(() => {
+ const layout = layoutRef.current;
+ if (!layout) {
+ return;
+ }
+ const handle = delayRender("canvas-presentation: filter paint");
+ const settle = () => continueRender(handle);
+ layout.addEventListener("paint", settle, { once: true });
+ layout.requestPaint?.();
+ return () => {
+ layout.removeEventListener("paint", settle);
+ continueRender(handle);
+ };
+ });
+
+ return (
+ <AbsoluteFill>
+ <canvas ref={layoutRef} style={LAYER}>
+ {props.children}
+ </canvas>
+ <canvas ref={outputRef} style={OUTPUT_LAYER} />
+ </AbsoluteFill>
+ );
+ };
+}
+
export type CanvasPresentationOptions<Props extends Record<string, unknown>> = {
shader: SceneShader<Props>;
fallback: React.FC<TransitionPresentationComponentProps<Props>>;
@@ -436,3 +680,23 @@ export function makeCanvasPresentation<Props extends Record<string, unknown>>({
return (props) => ({ component: Presentation, props });
}
+
+export type CanvasFilterOptions<Props extends Record<string, unknown>> = {
+ shader: SceneFilter<Props>;
+ fallback: React.FC<Props & CanvasFilterProps>;
+};
+
+export function makeCanvasFilter<Props extends Record<string, unknown>>({
+ shader,
+ fallback: Fallback,
+}: CanvasFilterOptions<Props>): React.FC<Props & CanvasFilterProps> {
+ const Canvas = makeFilterComponent(shader);
+
+ return function CanvasFilter(props) {
+ return isCanvasFilterSupported() ? (
+ <Canvas {...props} />
+ ) : (
+ <Fallback {...props} />
+ );
+ };
+}
diff --git a/registry/remocn/registry.json b/registry/remocn/registry.json
index 3251d7d4..d2241ebc 100644
--- a/registry/remocn/registry.json
+++ b/registry/remocn/registry.json
@@ -1634,7 +1634,7 @@
"name": "canvas-presentation",
"type": "registry:lib",
"title": "Canvas Presentation",
- "description": "The html-in-canvas scaffolding the canvas transitions share: support detection, a full-screen WebGL2 scene shader over both sides of the cut, and routing to a CSS fallback where the browser lacks the API.",
+ "description": "The html-in-canvas scaffolding the canvas transitions and scene filters share: support detection, a full-screen WebGL2 shader fed either both sides of a cut or the single scene a filter wraps, and routing to a CSS fallback where the browser lacks the API.",
"dependencies": ["remotion", "@remotion/transitions"],
"files": [
{
@@ -1674,6 +1674,21 @@
}
]
},
+ {
+ "name": "vhs-filter",
+ "type": "registry:component",
+ "title": "VHS Filter",
+ "description": "Hold a scene under analog tape: chroma bleeds sideways off every edge, rows wobble out of line, the bottom of the frame tears on the head switch, and luma noise settles over all of it.",
+ "dependencies": ["remotion"],
+ "registryDependencies": ["@remocn/canvas-presentation"],
+ "files": [
+ {
+ "path": "vhs-filter/index.tsx",
+ "type": "registry:component",
+ "target": "components/remocn/vhs-filter.tsx"
+ }
+ ]
+ },
{
"name": "grid-wave",
"type": "registry:component",
diff --git a/registry/remocn/vhs-filter/config.ts b/registry/remocn/vhs-filter/config.ts
new file mode 100644
index 00000000..a0b5b9d3
--- /dev/null
+++ b/registry/remocn/vhs-filter/config.ts
@@ -0,0 +1,47 @@
+import { vhsFilterExampleCode } from "@/components/docs/examples/vhs-filter-example";
+import { type ComponentConfig, FPS, H, W } from "@/lib/customizer-config";
+
+export const vhsFilterConfig: ComponentConfig = {
+ componentName: "VhsFilter",
+ importPath: "@/components/remocn/vhs-filter",
+ controls: {
+ bleed: {
+ type: "number",
+ default: 1,
+ min: 0,
+ max: 3,
+ step: 0.1,
+ label: "Chroma bleed",
+ },
+ wobble: {
+ type: "number",
+ default: 1,
+ min: 0,
+ max: 3,
+ step: 0.1,
+ label: "Tape wobble",
+ },
+ noise: {
+ type: "number",
+ default: 1,
+ min: 0,
+ max: 3,
+ step: 0.1,
+ label: "Noise",
+ },
+ intensity: {
+ type: "number",
+ default: 1,
+ min: 0,
+ max: 1,
+ step: 0.05,
+ label: "Intensity",
+ },
+ },
+ durationInFrames: 90,
+ fps: FPS,
+ compositionWidth: W,
+ compositionHeight: H,
+ previewBackdrop: { type: "color", value: "#0a0a0a" },
+ snippet: vhsFilterExampleCode,
+};
diff --git a/registry/remocn/vhs-filter/index.tsx b/registry/remocn/vhs-filter/index.tsx
new file mode 100644
index 00000000..babc06a7
--- /dev/null
+++ b/registry/remocn/vhs-filter/index.tsx
@@ -0,0 +1,190 @@
+"use client";
+
+import type React from "react";
+import { AbsoluteFill, random, useCurrentFrame } from "remotion";
+import {
+ type CanvasFilterProps,
+ makeCanvasFilter,
+ makeFilterShader,
+} from "@/lib/remocn/canvas-presentation";
+
+export type VhsFilterProps = {
+ bleed?: number;
+ wobble?: number;
+ noise?: number;
+ intensity?: number;
+};
+
+const DEFAULTS = {
+ bleed: 1,
+ wobble: 1,
+ noise: 1,
+ intensity: 1,
+};
+
+const FRAGMENT_SHADER = `#version 300 es
+precision highp float;
+
+uniform sampler2D u_scene;
+uniform float u_frame;
+uniform float u_time;
+uniform float u_bleed;
+uniform float u_wobble;
+uniform float u_noise;
+uniform float u_intensity;
+
+in vec2 v_uv;
+out vec4 outColor;
+
+const float PI = 3.141592653589793;
+const float LINES = 486.0;
+const int TAPS = 6;
+
+float hash(vec2 p) {
+ return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
+}
+
+float valueNoise(float x) {
+ float i = floor(x);
+ float f = fract(x);
+ float a = hash(vec2(i, 7.0));
+ float b = hash(vec2(i + 1.0, 7.0));
+ return mix(a, b, f * f * (3.0 - 2.0 * f));
+}
+
+vec3 rgb2yiq(vec3 c) {
+ return vec3(
+ dot(c, vec3(0.299, 0.587, 0.114)),
+ dot(c, vec3(0.5959, -0.2746, -0.3213)),
+ dot(c, vec3(0.2115, -0.5227, 0.3112))
+ );
+}
+
+vec3 yiq2rgb(vec3 c) {
+ return vec3(
+ c.x + 0.956 * c.y + 0.619 * c.z,
+ c.x - 0.272 * c.y - 0.647 * c.z,
+ c.x - 1.106 * c.y + 1.703 * c.z
+ );
+}
+
+void main() {
+ float line = floor(v_uv.y * LINES);
+ float clock = mod(u_frame, 64.0);
+
+ float drift =
+ sin(v_uv.y * 37.0 + u_time * 2.3) * 0.55 +
+ sin(v_uv.y * 96.0 - u_time * 1.1) * 0.25;
+ float jitter = valueNoise(line * 0.61 + u_time * 24.0) - 0.5;
+ float wobble = (drift * 0.35 + jitter * 0.9) * 0.007 * u_wobble * u_intensity;
+
+ float band = smoothstep(0.955, 0.994, v_uv.y);
+ float tear = valueNoise(line * 2.7 + u_time * 43.0) - 0.5;
+ float headSwitch = band * tear * 0.085 * u_intensity;
+
+ vec2 uv = vec2(v_uv.x + wobble + headSwitch, v_uv.y);
+ vec4 base = texture(u_scene, uv);
+ float luma = rgb2yiq(base.rgb).x;
+
+ float smear = u_bleed * 0.02 * u_intensity;
+ vec2 chroma = vec2(0.0);
+ float total = 0.0;
+ for (int i = 0; i < TAPS; i++) {
+ float t = float(i) / float(TAPS - 1);
+ float weight = 1.0 - t * 0.72;
+ vec3 tap = texture(u_scene, vec2(uv.x - smear * t, uv.y)).rgb;
+ chroma += rgb2yiq(tap).yz * weight;
+ total += weight;
+ }
+
+ vec3 color = yiq2rgb(vec3(luma, chroma / total));
+
+ float scan = 0.94 + 0.06 * cos(v_uv.y * LINES * PI);
+ color *= mix(1.0, scan, u_intensity);
+
+ float grain =
+ hash(vec2(v_uv.x * 640.0 + clock * 13.0, line + clock * 5.0)) - 0.5;
+ color += grain * 0.07 * u_noise * u_intensity;
+
+ color = mix(color, vec3(luma), band * 0.55 * u_intensity);
+ color += band * (hash(vec2(line, clock * 3.0)) - 0.5) * 0.26 * u_intensity;
+
+ outColor = vec4(clamp(color, 0.0, 1.0), base.a);
+}`;
+
+const shader = makeFilterShader<VhsFilterProps>(
+ FRAGMENT_SHADER,
+ ({ gl, uniform, passedProps }) => {
+ const {
+ bleed = DEFAULTS.bleed,
+ wobble = DEFAULTS.wobble,
+ noise = DEFAULTS.noise,
+ intensity = DEFAULTS.intensity,
+ } = passedProps;
+ gl.uniform1f(uniform("u_bleed"), bleed);
+ gl.uniform1f(uniform("u_wobble"), wobble);
+ gl.uniform1f(uniform("u_noise"), noise);
+ gl.uniform1f(uniform("u_intensity"), intensity);
+ },
+);
+
+const VhsFilterFallback: React.FC<VhsFilterProps & CanvasFilterProps> = ({
+ children,
+ bleed = DEFAULTS.bleed,
+ wobble = DEFAULTS.wobble,
+ noise = DEFAULTS.noise,
+ intensity = DEFAULTS.intensity,
+}) => {
+ const frame = useCurrentFrame();
+ const shift =
+ (random(`vhs-wobble-${frame}`) - 0.5) * 3.4 * wobble * intensity;
+ const flicker =
+ 1 + (random(`vhs-flicker-${frame}`) - 0.5) * 0.07 * noise * intensity;
+ const smear = 3 + bleed * 5;
+
+ return (
+ <AbsoluteFill>
+ <AbsoluteFill
+ style={{
+ transform: `translateX(${shift}px)`,
+ filter: `saturate(${1 + bleed * 0.3}) contrast(1.04) brightness(${flicker}) blur(${0.3 * intensity}px)`,
+ }}
+ >
+ {children}
+ </AbsoluteFill>
+ <AbsoluteFill
+ style={{
+ transform: `translateX(${shift - smear}px)`,
+ filter: "saturate(2.2) blur(1.4px)",
+ mixBlendMode: "screen",
+ opacity: 0.24 * bleed * intensity,
+ }}
+ >
+ {children}
+ </AbsoluteFill>
+ <AbsoluteFill
+ style={{
+ background:
+ "repeating-linear-gradient(to bottom, rgba(0, 0, 0, 0.22) 0px, rgba(0, 0, 0, 0.22) 1px, transparent 1px, transparent 3px)",
+ opacity: intensity,
+ }}
+ />
+ <div
+ style={{
+ position: "absolute",
+ insetInline: 0,
+ bottom: 0,
+ height: "3.6%",
+ transform: `translateX(${shift * 3}px)`,
+ background: `rgba(228, 228, 228, ${0.13 * intensity})`,
+ mixBlendMode: "screen",
+ }}
+ />
+ </AbsoluteFill>
+ );
+};
+
+export const VhsFilter = makeCanvasFilter<VhsFilterProps>({
+ shader,
+ fallback: VhsFilterFallback,
+});
diff --git a/src/remotion/canvas-spike-root.tsx b/src/remotion/canvas-spike-root.tsx
index ecf7d2a9..32eb82b4 100644
--- a/src/remotion/canvas-spike-root.tsx
+++ b/src/remotion/canvas-spike-root.tsx
@@ -11,12 +11,14 @@ import { EmberBurnExampleScene } from "@/components/docs/examples/ember-burn-exa
import { GlitchCutExampleScene } from "@/components/docs/examples/glitch-cut-example";
import { GridWaveExampleScene } from "@/components/docs/examples/grid-wave-example";
import { ParticleDissolveExampleScene } from "@/components/docs/examples/particle-dissolve-example";
+import { VhsFilterExampleScene } from "@/components/docs/examples/vhs-filter-example";
import type { ComponentConfig } from "@/lib/customizer-config";
import { displacementConfig } from "@/registry/remocn/displacement/config";
import { emberBurnConfig } from "@/registry/remocn/ember-burn/config";
import { glitchCutConfig } from "@/registry/remocn/glitch-cut/config";
import { gridWaveConfig } from "@/registry/remocn/grid-wave/config";
import { particleDissolveConfig } from "@/registry/remocn/particle-dissolve/config";
+import { vhsFilterConfig } from "@/registry/remocn/vhs-filter/config";
const { fontFamily: GEIST } = loadFont();
@@ -42,6 +44,7 @@ const SCENES: Record<
Scene: DisplacementExampleScene,
config: displacementConfig,
},
+ "vhs-filter": { Scene: VhsFilterExampleScene, config: vhsFilterConfig },
};
function makeStage(Scene: ComponentType) {