mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-14 13:27:13 +00:00
* feat(catalog): put the variables panel back, on payloads The panel drove its preview by loading an .html from docs/public, a type the host does not publish, so it showed an empty frame in production and was parked when the catalog was re-landed. It now mounts the same JSON payload the plain player uses and re-mounts it as values change, injecting them as window.__hfVariables into the composition head before any of its scripts run, which is where the runtime reads overrides from. Doing it in the markup rather than after load is what stops the composition initialising with the wrong values first. 172 items with variables get the panel back; the playhead carries across a change so a tweak mid-shot does not jump back to frame zero. * fix(docs): drop the unused url form and the needless escapes * fix(docs): the panel cannot reference a binding beside the export * feat(catalog): make importing an SVG the obvious move A reader arrives at this control with a shape, not with path data, and the panel asked for the coordinates first. Import is now the primary action in a drop target you can see is a drop target, and the raw path sits behind a disclosure for anyone who wants it. * feat(cli): let a fruitless catalog search report the gap An agent that searches by meaning and finds nothing worth installing knows something we do not: the name of a move the catalog is missing. There was no way to tell us, so that knowledge was lost at the end of every run. hyperframes feedback --search-miss "<query>" --wanted "<the move>" records it. It carries no rating, so it never lands in the rating metric, and it is a separate deliberate command rather than something catalog --query does on its own: plain search still sends nothing, which is what the CLI promises. --rating stops being required at the arg level, since a miss has no rating to give. The check moved into the run body, where an absent one is now handled rather than crashing on undefined. * feat(cli): carry tuned variable values into the install snippet Someone who tunes a block on its catalog page had no way to keep those values: the install command was the same one everybody gets, and the tuning stayed on the page. hyperframes add <item> --vars '<json>' now prints a mount element carrying data-variable-values, so the values land where the block is used. They ride on the host rather than being written into the installed file. That keeps the composition on disk byte-identical to the registry's, so a later reinstall can still tell an edit from an update, and it lets two mounts of the same block carry different values. * fix(catalog): serve the item's own directory so runtime paths resolve Some compositions assemble their asset URLs at run time — "compositions/components/" + texture + ".png" for the texture masks, a font the compiler pulled into _remote_media — and no scan of the markup can see a string that does not exist until a script concatenates it. Those items either rendered black or were dropped to a video that had never been uploaded. Each item that needs it now has its prepared directory published, and its payload carries a <base> pointing at it, so any relative path the composition invents resolves. caption-texture renders its masks again, and variable-font-flex has a preview at all for the first time: its MP4 and poster are both 403. Both layouts are published, because which one a composition asks for differs per item, and a directory only earns that if it is under 2 MB. The 12 MB texture sheet keeps the recorded video it already had. * fix(catalog): let the variables panel actually drive the composition Every control on the panel was inert. The values reached the composition and nothing repainted, because the payload had already been compiled: compiling inlines a mounted component and resolves its variables into the markup and CSS, so by the time a reader turns a knob there is nothing left to change. An item that declares variables now ships uncompiled, keeping the mount the runtime loads at run time, which is the only state where data-variable-values still means anything. The component travels inline as a data URI rather than a sibling file, because .html is the one type the docs host will not publish. The demo's own pinned values come off, so the reader's choices reach the mount instead of losing to the values the demo picked to show itself off. Measured on the rendered frame rather than the DOM: green rgb(98,207,144), blue rgb(6,6,199), violet rgb(177,147,230), and back to green. docs/public/catalog drops from 48 MB to 35 MB along the way, since an uncompiled payload carries far less than an inlined one. * feat(catalog): keep variable changes in the url A reader who tuned a piece lost it on reload, and had nothing to send anyone. The values now live in the query string, scoped by composition id so two links never read each other,and only the ones that differ from the defaults are written, so changing one knob gives a short URL rather than every variable spelled out. replaceState rather than pushState: dragging a slider should not leave a trail of history entries. An unreadable value is ignored rather than thrown, so a truncated or hand-edited link opens the piece at its defaults. * fix(catalog): only rewrite the url when a value actually changed * refactor(catalog): memoise the declared defaults on their content * feat(catalog): offer an install command carrying the tuned values The Install block is generated before anyone touches a knob, so it can only ever print the plain command. Someone who spent a minute tuning a piece copied it and got the defaults back. The panel now carries its own command in the Snippet tab, with --vars holding exactly the values that differ. An untouched piece still offers the same short command, so nothing gets noisier for the common case. * fix(catalog): a piece with nothing to render is a skip, not a failure caption-blend-difference is a stylesheet and a paragraph of prose — a class you add to your own captions, with no standalone scene to show. The generator treated that as a build failure, so every run ended by reporting something broken when nothing was. It now reports the shape it is and keeps its recorded video, which is the only honest preview such an item has. A genuine render failure still throws. * fix(catalog): restore variables from the url on a cold load A shared link opened at the defaults. The first render happens on the server, where there is no window to read the query string from, and React then hydrates against that markup and never revisits it — so the values only appeared once you touched a control. The URL is read again after mount, which is the first moment it exists. The value is also escaped once now rather than twice: URLSearchParams already decodes on the way out, and decoding a second time turned an SVG path full of percent-escapes into something that no longer parsed, besides doubling the length of every link. * fix(catalog): mount the preview with the values a link carried The frame was built from the declared defaults and the shared values were posted to it afterwards, which is too late for anything the composition reads once at init: a path arrived after the mark had already been drawn from the default one, so a link looked right in the panel and wrong on screen. * feat(catalog): the install command follows the values you tuned Copying the Install line gave the plain command back, because that block is generated before anyone touches a knob and had no way to know what changed. The tuned command only existed in the panel Snippet tab, which is not where anyone looks for it. The line now reads the same query string the panel writes, so the two agree without either component knowing the other exists, and a shared link carries the right command too. replaceState fires no event, so the panel announces its own writes. * fix(catalog): send a text variable to the preview once it is finished Every other control in the explorer reports a whole value on every event: a slider at any position is a position, a swatch is a colour. A text field is not. Typing v3 into a badge posted v first, so the preview remounted and rendered a composition built from half a word. The post now waits while a text field has focus and goes out when the edit is committed, with Enter or by clicking away. The field itself is unchanged and still tracks every keystroke. --------- Co-authored-by: Miguel Angel Simon Sierra <miguelangelsi07@gmail.com>
660 lines
24 KiB
Plaintext
660 lines
24 KiB
Plaintext
---
|
|
title: "Code 3D Extrude"
|
|
description: "Syntax-highlighted code on a lit, beveled 3D slab that rotates through real space and settles to a readable rest — true WebGL depth and lighting, not a 2D transform."
|
|
---
|
|
|
|
import { InstallCommand } from "/snippets/install-command.jsx";
|
|
|
|
<iframe
|
|
className="w-full aspect-video rounded-xl border-0 bg-zinc-100 dark:bg-zinc-800"
|
|
title="code-3d-extrude preview"
|
|
loading="lazy"
|
|
srcDoc={`<!doctype html><html><head><meta charset="utf-8"><style>html,body{margin:0;height:100%;overflow:hidden;background:transparent}hyperframes-player{display:block;width:100%;height:100%}</style><script src="https://cdn.jsdelivr.net/npm/@hyperframes/player@0.7/dist/hyperframes-player.global.js"><\/script></head><body><script>fetch("/public/catalog/blocks/code-3d-extrude.json").then(function(r){return r.json()}).then(function(d){var p=document.createElement("hyperframes-player");p.setAttribute("srcdoc",d.html);p.setAttribute("controls","");p.setAttribute("autoplay","");p.setAttribute("loop","");p.setAttribute("muted","");p.setAttribute("poster","https://static.heygen.ai/hyperframes-oss/docs/images/catalog/blocks/code-3d-extrude.png");document.body.appendChild(p)});<\/script></body></html>`}
|
|
/>
|
|
|
|
## Install
|
|
|
|
<InstallCommand command="npx hyperframes add code-3d-extrude" item="code-3d-extrude" />
|
|
|
|
That writes one file: `compositions/code-3d-extrude.html`.
|
|
|
|
## Source
|
|
|
|
<Accordion title={`code-3d-extrude.html`}>
|
|
|
|
```html
|
|
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=1920, height=1080" />
|
|
<title>Code 3D Extrude</title>
|
|
<script src="https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/gsap.min.js"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/three@0.147.0/build/three.min.js"></script>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
html,
|
|
body {
|
|
width: 1920px;
|
|
height: 1080px;
|
|
overflow: hidden;
|
|
background: #05070b;
|
|
}
|
|
#root {
|
|
position: relative;
|
|
width: 1920px;
|
|
height: 1080px;
|
|
}
|
|
#gl {
|
|
position: absolute;
|
|
inset: 0;
|
|
width: 1920px;
|
|
height: 1080px;
|
|
display: block;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div
|
|
id="root"
|
|
data-composition-id="code-3d-extrude"
|
|
data-start="0"
|
|
data-duration="8"
|
|
data-width="1920"
|
|
data-height="1080"
|
|
>
|
|
<canvas id="gl" width="1920" height="1080"></canvas>
|
|
</div>
|
|
<script>
|
|
window.__TOKENS = {
|
|
feature: {
|
|
lang: "js",
|
|
theme: "github-dark",
|
|
bg: "#24292e",
|
|
fg: "#e1e4e8",
|
|
states: [
|
|
{
|
|
code: 'async function loadConfig(path) {\n const raw = await readFile(path, "utf8")\n const config = JSON.parse(raw)\n return validate(config)\n}',
|
|
tokens: [
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-0",
|
|
content: "async",
|
|
color: "#F97583",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-1",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-2",
|
|
content: "function",
|
|
color: "#F97583",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-3",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-4",
|
|
content: "loadConfig",
|
|
color: "#B392F0",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-5",
|
|
content: "(",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-6",
|
|
content: "path",
|
|
color: "#FFAB70",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-7",
|
|
content: ") {",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-8",
|
|
content: "\n",
|
|
color: "#e1e4e8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-9",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-10",
|
|
content: "const",
|
|
color: "#F97583",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-11",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-12",
|
|
content: "raw",
|
|
color: "#79B8FF",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-13",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-14",
|
|
content: "=",
|
|
color: "#F97583",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-15",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-16",
|
|
content: "await",
|
|
color: "#F97583",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-17",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-18",
|
|
content: "readFile",
|
|
color: "#B392F0",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-19",
|
|
content: "(path,",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-20",
|
|
content: " ",
|
|
color: "#e1e4e8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-21",
|
|
content: '"utf8"',
|
|
color: "#9ECBFF",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-22",
|
|
content: ")",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-23",
|
|
content: "\n",
|
|
color: "#e1e4e8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-24",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-25",
|
|
content: "const",
|
|
color: "#F97583",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-26",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-27",
|
|
content: "config",
|
|
color: "#79B8FF",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-28",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-29",
|
|
content: "=",
|
|
color: "#F97583",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-30",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-31",
|
|
content: "JSON",
|
|
color: "#79B8FF",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-32",
|
|
content: ".",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-33",
|
|
content: "parse",
|
|
color: "#B392F0",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-34",
|
|
content: "(raw)",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-35",
|
|
content: "\n",
|
|
color: "#e1e4e8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-36",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-37",
|
|
content: "return",
|
|
color: "#F97583",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-38",
|
|
content: " ",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-39",
|
|
content: "validate",
|
|
color: "#B392F0",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-40",
|
|
content: "(config)",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-41",
|
|
content: "\n",
|
|
color: "#e1e4e8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-42",
|
|
content: "}",
|
|
color: "#E1E4E8",
|
|
fontStyle: 0,
|
|
},
|
|
{
|
|
key: "SPx9u4TyzfMqSHpbjPHnWFQU8lPVEPieF6W5Vtu-bNA-43",
|
|
content: "\n",
|
|
color: "#e1e4e8",
|
|
fontStyle: 0,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
};
|
|
window.__BLOCK = {
|
|
id: "code-3d-extrude",
|
|
effect: "extrude",
|
|
seq: "feature",
|
|
duration: 8,
|
|
seed: 7,
|
|
};
|
|
</script>
|
|
<script>
|
|
// hf-code-anim-gl.js — WebGL code-animation engine for the "crazy" Code Animations blocks.
|
|
//
|
|
// Bridges the baked Shiki tokens (window.__TOKENS) into Three.js: the tokens are drawn once
|
|
// to an offscreen 2D canvas (a CanvasTexture), which then drives a lit 3D slab, a dissolve
|
|
// shader, or a GPU particle system. Determinism per the project contract:
|
|
// - render only via tl.eventCallback("onUpdate", …) + one initial render() — never rAF
|
|
// - all randomness from a seeded mulberry32 — never Math.random / Date.now
|
|
// - canvas drawn once after document.fonts.ready (final glyph metrics)
|
|
// - paused GSAP timeline registered on window.__timelines[id]; HF seeks it per frame
|
|
//
|
|
// Each block sets window.__BLOCK = { id, effect, seq, duration, seed } and includes this engine.
|
|
(function () {
|
|
"use strict";
|
|
var T = window.__TOKENS || {};
|
|
|
|
// ---- seeded PRNG (no Math.random) -----------------------------------------
|
|
function mulberry32(a) {
|
|
return function () {
|
|
a |= 0;
|
|
a = (a + 0x6d2b79f5) | 0;
|
|
var t = Math.imul(a ^ (a >>> 15), 1 | a);
|
|
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
};
|
|
}
|
|
|
|
// ---- split a token state into lines of {content,color} --------------------
|
|
function splitLines(state) {
|
|
var lines = [[]];
|
|
for (var i = 0; i < state.tokens.length; i++) {
|
|
var t = state.tokens[i];
|
|
if (t.content === "\n") {
|
|
lines.push([]);
|
|
continue;
|
|
}
|
|
lines[lines.length - 1].push(t);
|
|
}
|
|
while (lines.length && lines[lines.length - 1].length === 0) lines.pop();
|
|
return lines;
|
|
}
|
|
|
|
// ---- THE BRIDGE: draw baked tokens to an offscreen 2D canvas ---------------
|
|
// Returns { canvas, W, H, lines, lineH, padY, fontPx } so effects can sample it.
|
|
function drawCodeToCanvas(state, opts) {
|
|
opts = opts || {};
|
|
var fontPx = opts.fontPx || 36;
|
|
var lineH = opts.lineH || 54;
|
|
var padX = opts.padX || 70;
|
|
var padY = opts.padY || 60;
|
|
var weight = opts.weight || 500;
|
|
var font = weight + " " + fontPx + 'px "JetBrains Mono", monospace';
|
|
|
|
var lines = splitLines(state);
|
|
var measure = document.createElement("canvas").getContext("2d");
|
|
measure.font = font;
|
|
var maxW = 0;
|
|
for (var i = 0; i < lines.length; i++) {
|
|
var w = 0;
|
|
for (var j = 0; j < lines[i].length; j++)
|
|
w += measure.measureText(lines[i][j].content).width;
|
|
if (w > maxW) maxW = w;
|
|
}
|
|
var W = Math.max(2, Math.ceil(maxW + padX * 2));
|
|
var H = Math.max(2, Math.ceil(lines.length * lineH + padY * 2));
|
|
|
|
var canvas = document.createElement("canvas");
|
|
canvas.width = W;
|
|
canvas.height = H;
|
|
var ctx = canvas.getContext("2d");
|
|
if (opts.transparent) {
|
|
ctx.clearRect(0, 0, W, H);
|
|
} else {
|
|
ctx.fillStyle = opts.bg || "#0b0f17";
|
|
ctx.fillRect(0, 0, W, H);
|
|
}
|
|
ctx.textBaseline = "top";
|
|
ctx.font = font;
|
|
for (var li = 0; li < lines.length; li++) {
|
|
var x = padX;
|
|
var y = padY + li * lineH;
|
|
for (var k = 0; k < lines[li].length; k++) {
|
|
var tok = lines[li][k];
|
|
ctx.fillStyle = tok.color;
|
|
ctx.fillText(tok.content, x, y);
|
|
x += ctx.measureText(tok.content).width;
|
|
}
|
|
}
|
|
return {
|
|
canvas: canvas,
|
|
W: W,
|
|
H: H,
|
|
lines: lines.length,
|
|
lineH: lineH,
|
|
padY: padY,
|
|
fontPx: fontPx,
|
|
};
|
|
}
|
|
|
|
function fxExtrude(THREE, scene, camera, renderer, state, st, tl, dur) {
|
|
scene.background = new THREE.Color(0x05070b);
|
|
var code = drawCodeToCanvas(state, {
|
|
fontPx: 38,
|
|
lineH: 56,
|
|
padX: 80,
|
|
padY: 70,
|
|
weight: 600,
|
|
});
|
|
var tex = new THREE.CanvasTexture(code.canvas);
|
|
tex.minFilter = THREE.LinearFilter;
|
|
tex.magFilter = THREE.LinearFilter;
|
|
tex.generateMipmaps = false;
|
|
|
|
var aspect = code.W / code.H;
|
|
var slabH = 6.2;
|
|
var slabW = slabH * aspect;
|
|
var depth = 0.5;
|
|
|
|
// beveled slab: rounded extruded rectangle
|
|
var hw = slabW / 2;
|
|
var hh = slabH / 2;
|
|
var r = 0.18;
|
|
var shape = new THREE.Shape();
|
|
shape.moveTo(-hw + r, -hh);
|
|
shape.lineTo(hw - r, -hh);
|
|
shape.quadraticCurveTo(hw, -hh, hw, -hh + r);
|
|
shape.lineTo(hw, hh - r);
|
|
shape.quadraticCurveTo(hw, hh, hw - r, hh);
|
|
shape.lineTo(-hw + r, hh);
|
|
shape.quadraticCurveTo(-hw, hh, -hw, hh - r);
|
|
shape.lineTo(-hw, -hh + r);
|
|
shape.quadraticCurveTo(-hw, -hh, -hw + r, -hh);
|
|
var geo = new THREE.ExtrudeGeometry(shape, {
|
|
depth: depth,
|
|
bevelEnabled: true,
|
|
bevelThickness: 0.12,
|
|
bevelSize: 0.1,
|
|
bevelSegments: 3,
|
|
steps: 1,
|
|
});
|
|
geo.center();
|
|
// UVs from ExtrudeGeometry don't map the texture nicely to the face; remap front-face UVs from XY bounds.
|
|
geo.computeBoundingBox();
|
|
var bb = geo.boundingBox;
|
|
var sizeX = bb.max.x - bb.min.x;
|
|
var sizeY = bb.max.y - bb.min.y;
|
|
var pos = geo.attributes.position;
|
|
var uv = geo.attributes.uv;
|
|
for (var vi = 0; vi < pos.count; vi++) {
|
|
uv.setXY(vi, (pos.getX(vi) - bb.min.x) / sizeX, (pos.getY(vi) - bb.min.y) / sizeY);
|
|
}
|
|
uv.needsUpdate = true;
|
|
|
|
// Face is an unlit "screen" — shows the exact vivid Shiki colors (no PBR/emissive wash).
|
|
// The 3D read comes from the lit, beveled EDGES + perspective, like a glowing monitor.
|
|
var faceMat = new THREE.MeshBasicMaterial({ map: tex });
|
|
var edgeMat = new THREE.MeshStandardMaterial({
|
|
color: 0x141d2b,
|
|
roughness: 0.3,
|
|
metalness: 0.4,
|
|
});
|
|
// ExtrudeGeometry groups: 0 = front+back faces, 1 = sides. Front shows the code.
|
|
var slab = new THREE.Mesh(geo, [faceMat, edgeMat]);
|
|
scene.add(slab);
|
|
|
|
scene.add(new THREE.AmbientLight(0xffffff, 0.5));
|
|
var key = new THREE.DirectionalLight(0xbcd4ff, 1.25);
|
|
key.position.set(5, 7, 9);
|
|
scene.add(key);
|
|
var rim = new THREE.DirectionalLight(0x4fffa0, 0.45);
|
|
rim.position.set(-7, -3, 5);
|
|
scene.add(rim);
|
|
|
|
camera.position.set(0, 0, 10);
|
|
|
|
// fit camera distance so the WHOLE slab fits (width-limited for wide code), ~78% fill
|
|
var vfov = (camera.fov * Math.PI) / 180;
|
|
var screenAspect = 1920 / 1080;
|
|
var fitH = slabH / 2 / Math.tan(vfov / 2) / 0.78;
|
|
var fitW = slabW / 2 / (Math.tan(vfov / 2) * screenAspect) / 0.78;
|
|
var fitZ = Math.max(fitH, fitW);
|
|
|
|
st.rotY = -1.0;
|
|
st.rotX = 0.4;
|
|
st.camZ = fitZ + 6;
|
|
tl.to(st, { rotY: 0, rotX: 0, camZ: fitZ, duration: dur * 0.6, ease: "power3.out" }, 0);
|
|
// gentle breathing drift at rest (subtle — stays readable)
|
|
tl.to(
|
|
st,
|
|
{ rotY: 0.06, rotX: -0.02, duration: dur * 0.4, ease: "sine.inOut" },
|
|
dur * 0.6,
|
|
);
|
|
|
|
return function () {
|
|
slab.rotation.y = st.rotY;
|
|
slab.rotation.x = st.rotX;
|
|
camera.position.z = st.camZ;
|
|
camera.lookAt(0, 0, 0);
|
|
// boot loop's render() issues the single renderer.render() — don't double-render here
|
|
};
|
|
}
|
|
|
|
// ---- dispatch + boot ------------------------------------------------------
|
|
function buildEffect(THREE, scene, camera, renderer, spec, st, tl) {
|
|
var state = T[spec.seq].states[0];
|
|
var dur = spec.duration;
|
|
if (spec.effect === "extrude")
|
|
return fxExtrude(THREE, scene, camera, renderer, state, st, tl, dur);
|
|
return function () {};
|
|
}
|
|
|
|
// Free GPU resources (renderer, geometries, materials, textures) on page teardown.
|
|
// One-shot renders exit per job so nothing leaks there; this guards long-lived
|
|
// Studio/player sessions that reload. Fires only on unload — never during capture.
|
|
function disposeGL(renderer, scene) {
|
|
scene.traverse(function (o) {
|
|
if (o.geometry && o.geometry.dispose) o.geometry.dispose();
|
|
var mats = o.material ? (Array.isArray(o.material) ? o.material : [o.material]) : [];
|
|
mats.forEach(function (m) {
|
|
if (m.map && m.map.dispose) m.map.dispose();
|
|
if (
|
|
m.uniforms &&
|
|
m.uniforms.uTex &&
|
|
m.uniforms.uTex.value &&
|
|
m.uniforms.uTex.value.dispose
|
|
) {
|
|
m.uniforms.uTex.value.dispose();
|
|
}
|
|
if (m.dispose) m.dispose();
|
|
});
|
|
});
|
|
if (renderer.dispose) renderer.dispose();
|
|
}
|
|
|
|
function go() {
|
|
var spec = window.__BLOCK;
|
|
var root = document.getElementById("root");
|
|
var glcanvas = document.getElementById("gl");
|
|
var W = 1920;
|
|
var H = 1080;
|
|
window.__timelines = window.__timelines || {};
|
|
var tl = gsap.timeline({ paused: true });
|
|
window.__timelines[spec.id] = tl; // register synchronously
|
|
|
|
var THREE = window.THREE;
|
|
var renderer = new THREE.WebGLRenderer({
|
|
canvas: glcanvas,
|
|
antialias: true,
|
|
alpha: false,
|
|
preserveDrawingBuffer: true,
|
|
});
|
|
renderer.setPixelRatio(1);
|
|
renderer.setSize(W, H, false);
|
|
var scene = new THREE.Scene();
|
|
var camera = new THREE.PerspectiveCamera(45, W / H, 0.1, 200);
|
|
var st = {};
|
|
|
|
document.fonts.ready.then(function () {
|
|
var renderFn = buildEffect(THREE, scene, camera, renderer, spec, st, tl);
|
|
function render() {
|
|
renderFn();
|
|
renderer.render(scene, camera);
|
|
}
|
|
tl.eventCallback("onUpdate", render);
|
|
var dur = parseFloat(root.dataset.duration) || tl.duration();
|
|
tl.to({}, { duration: dur }, 0); // pad to full composition length
|
|
render(); // paint frame 0
|
|
window.addEventListener("beforeunload", function () {
|
|
disposeGL(renderer, scene);
|
|
});
|
|
});
|
|
}
|
|
|
|
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", go);
|
|
else go();
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|
|
```
|
|
|
|
</Accordion>
|
|
|
|
## Usage
|
|
|
|
After installing, add the block to your host composition:
|
|
|
|
```html
|
|
<div data-composition-id="code-3d-extrude" data-composition-src="compositions/code-3d-extrude.html" data-start="0" data-duration="8" data-track-index="1" data-width="1920" data-height="1080"></div>
|
|
```
|
|
|
|
{/* hf:generated-footer */}
|
|
|
|
Tagged `code` `code-animation` `3d` `webgl` `developer`.
|
|
|
|
## Related topics
|
|
|
|
- [Browse the complete Catalog](/catalog)
|
|
- [Add assets and Catalog items in Studio](/studio/assets-and-blocks)
|
|
- [Build a richer composition](/go-further)
|