Skip to content

Write HTML. Render video. Built for agents.

20.4 KB
Raw Blame
{"html":"<!doctype html>\n<html lang=\"en\">\n  <head>\n    <meta charset=\"UTF-8\" />\n    <meta name=\"viewport\" content=\"width=1920, height=1080\" />\n    <title>Code Shader Dissolve</title>\n    <script src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js\"></script>\n    <script src=\"https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js\"></script>\n    <style>\n      * {\n        margin: 0;\n        padding: 0;\n        box-sizing: border-box;\n      }\n      html,\n      body {\n        width: 1920px;\n        height: 1080px;\n        overflow: hidden;\n        background: #05070b;\n      }\n      #root {\n        position: relative;\n        width: 1920px;\n        height: 1080px;\n      }\n      #gl {\n        position: absolute;\n        inset: 0;\n        width: 1920px;\n        height: 1080px;\n        display: block;\n      }\n    </style>\n  </head>\n  <body>\n    <div\n      id=\"root\"\n      data-composition-id=\"code-shader-dissolve\"\n      data-start=\"0\"\n      data-duration=\"7\"\n      data-width=\"1920\"\n      data-height=\"1080\"\n    >\n      <canvas id=\"gl\" width=\"1920\" height=\"1080\"></canvas>\n    </div>\n    <script>\n      window.__TOKENS = {\n        compile: {\n          lang: \"js\",\n          theme: \"github-dark\",\n          bg: \"#24292e\",\n          fg: \"#e1e4e8\",\n          states: [\n            {\n              code: \"function compile(src) {\\n  const ast = parse(src)\\n  const ir = optimize(ast)\\n  return emit(ir)\\n}\",\n              tokens: [\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-0\",\n                  content: \"function\",\n                  color: \"#F97583\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-1\",\n                  content: \" \",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-2\",\n                  content: \"compile\",\n                  color: \"#B392F0\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-3\",\n                  content: \"(\",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-4\",\n                  content: \"src\",\n                  color: \"#FFAB70\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-5\",\n                  content: \") {\",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-6\",\n                  content: \"\\n\",\n                  color: \"#e1e4e8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-7\",\n                  content: \"  \",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-8\",\n                  content: \"const\",\n                  color: \"#F97583\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-9\",\n                  content: \" \",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-10\",\n                  content: \"ast\",\n                  color: \"#79B8FF\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-11\",\n                  content: \" \",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-12\",\n                  content: \"=\",\n                  color: \"#F97583\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-13\",\n                  content: \" \",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-14\",\n                  content: \"parse\",\n                  color: \"#B392F0\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-15\",\n                  content: \"(src)\",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-16\",\n                  content: \"\\n\",\n                  color: \"#e1e4e8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-17\",\n                  content: \"  \",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-18\",\n                  content: \"const\",\n                  color: \"#F97583\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-19\",\n                  content: \" \",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-20\",\n                  content: \"ir\",\n                  color: \"#79B8FF\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-21\",\n                  content: \" \",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-22\",\n                  content: \"=\",\n                  color: \"#F97583\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-23\",\n                  content: \" \",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-24\",\n                  content: \"optimize\",\n                  color: \"#B392F0\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-25\",\n                  content: \"(ast)\",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-26\",\n                  content: \"\\n\",\n                  color: \"#e1e4e8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-27\",\n                  content: \"  \",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-28\",\n                  content: \"return\",\n                  color: \"#F97583\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-29\",\n                  content: \" \",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-30\",\n                  content: \"emit\",\n                  color: \"#B392F0\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-31\",\n                  content: \"(ir)\",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-32\",\n                  content: \"\\n\",\n                  color: \"#e1e4e8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-33\",\n                  content: \"}\",\n                  color: \"#E1E4E8\",\n                  fontStyle: 0,\n                },\n                {\n                  key: \"zGtBjd8ajYlOewpKFJD53Oc7W94UowxIfV0186O1l8A-34\",\n                  content: \"\\n\",\n                  color: \"#e1e4e8\",\n                  fontStyle: 0,\n                },\n              ],\n            },\n          ],\n        },\n      };\n      window.__BLOCK = {\n        id: \"code-shader-dissolve\",\n        effect: \"dissolve\",\n        seq: \"compile\",\n        duration: 7,\n        seed: 11,\n      };\n    </script>\n    <script>\n      // hf-code-anim-gl.js — WebGL code-animation engine for the \"crazy\" Code Animations blocks.\n      //\n      // Bridges the baked Shiki tokens (window.__TOKENS) into Three.js: the tokens are drawn once\n      // to an offscreen 2D canvas (a CanvasTexture), which then drives a lit 3D slab, a dissolve\n      // shader, or a GPU particle system. Determinism per the project contract:\n      //   - render only via tl.eventCallback(\"onUpdate\", …) + one initial render() — never rAF\n      //   - all randomness from a seeded mulberry32 — never Math.random / Date.now\n      //   - canvas drawn once after document.fonts.ready (final glyph metrics)\n      //   - paused GSAP timeline registered on window.__timelines[id]; HF seeks it per frame\n      //\n      // Each block sets window.__BLOCK = { id, effect, seq, duration, seed } and includes this engine.\n      (function () {\n        \"use strict\";\n        var T = window.__TOKENS || {};\n\n        // ---- seeded PRNG (no Math.random) -----------------------------------------\n        function mulberry32(a) {\n          return function () {\n            a |= 0;\n            a = (a + 0x6d2b79f5) | 0;\n            var t = Math.imul(a ^ (a >>> 15), 1 | a);\n            t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;\n            return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n          };\n        }\n\n        // ---- split a token state into lines of {content,color} --------------------\n        function splitLines(state) {\n          var lines = [[]];\n          for (var i = 0; i < state.tokens.length; i++) {\n            var t = state.tokens[i];\n            if (t.content === \"\\n\") {\n              lines.push([]);\n              continue;\n            }\n            lines[lines.length - 1].push(t);\n          }\n          while (lines.length && lines[lines.length - 1].length === 0) lines.pop();\n          return lines;\n        }\n\n        // ---- THE BRIDGE: draw baked tokens to an offscreen 2D canvas ---------------\n        // Returns { canvas, W, H, lines, lineH, padY, fontPx } so effects can sample it.\n        function drawCodeToCanvas(state, opts) {\n          opts = opts || {};\n          var fontPx = opts.fontPx || 36;\n          var lineH = opts.lineH || 54;\n          var padX = opts.padX || 70;\n          var padY = opts.padY || 60;\n          var weight = opts.weight || 500;\n          var font = weight + \" \" + fontPx + 'px \"JetBrains Mono\", monospace';\n\n          var lines = splitLines(state);\n          var measure = document.createElement(\"canvas\").getContext(\"2d\");\n          measure.font = font;\n          var maxW = 0;\n          for (var i = 0; i < lines.length; i++) {\n            var w = 0;\n            for (var j = 0; j < lines[i].length; j++)\n              w += measure.measureText(lines[i][j].content).width;\n            if (w > maxW) maxW = w;\n          }\n          var W = Math.max(2, Math.ceil(maxW + padX * 2));\n          var H = Math.max(2, Math.ceil(lines.length * lineH + padY * 2));\n\n          var canvas = document.createElement(\"canvas\");\n          canvas.width = W;\n          canvas.height = H;\n          var ctx = canvas.getContext(\"2d\");\n          if (opts.transparent) {\n            ctx.clearRect(0, 0, W, H);\n          } else {\n            ctx.fillStyle = opts.bg || \"#0b0f17\";\n            ctx.fillRect(0, 0, W, H);\n          }\n          ctx.textBaseline = \"top\";\n          ctx.font = font;\n          for (var li = 0; li < lines.length; li++) {\n            var x = padX;\n            var y = padY + li * lineH;\n            for (var k = 0; k < lines[li].length; k++) {\n              var tok = lines[li][k];\n              ctx.fillStyle = tok.color;\n              ctx.fillText(tok.content, x, y);\n              x += ctx.measureText(tok.content).width;\n            }\n          }\n          return {\n            canvas: canvas,\n            W: W,\n            H: H,\n            lines: lines.length,\n            lineH: lineH,\n            padY: padY,\n            fontPx: fontPx,\n          };\n        }\n\n        function fxDissolve(THREE, scene, camera, renderer, state, st, tl, dur, seed) {\n          scene.background = new THREE.Color(0x05070b);\n          var code = drawCodeToCanvas(state, {\n            fontPx: 40,\n            lineH: 60,\n            padX: 90,\n            padY: 80,\n            weight: 600,\n          });\n          var tex = new THREE.CanvasTexture(code.canvas);\n          tex.minFilter = THREE.LinearFilter;\n          tex.magFilter = THREE.LinearFilter;\n          tex.generateMipmaps = false;\n\n          var aspect = code.W / code.H;\n          var planeH = 6.6;\n          var planeW = planeH * aspect;\n\n          var uniforms = {\n            uTex: { value: tex },\n            uProgress: { value: 0 },\n            uSeed: { value: (seed || 1) * 13.37 },\n          };\n          var mat = new THREE.ShaderMaterial({\n            uniforms: uniforms,\n            transparent: true,\n            vertexShader: [\n              \"varying vec2 vUv;\",\n              \"void main(){ vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }\",\n            ].join(\"\\n\"),\n            fragmentShader: [\n              \"precision highp float;\",\n              \"uniform sampler2D uTex;\",\n              \"uniform float uProgress;\",\n              \"uniform float uSeed;\",\n              \"varying vec2 vUv;\",\n              \"float hash(vec2 p){ p=fract(p*vec2(123.34,456.21)); p+=dot(p,p+45.32); return fract(p.x*p.y); }\",\n              \"float noise(vec2 p){ vec2 i=floor(p),f=fract(p); float a=hash(i),b=hash(i+vec2(1.,0.)),c=hash(i+vec2(0.,1.)),d=hash(i+vec2(1.,1.)); vec2 u=f*f*(3.-2.*f); return mix(mix(a,b,u.x),mix(c,d,u.x),u.y); }\",\n              \"void main(){\",\n              \"  vec2 uv = vUv;\",\n              // multi-octave seeded noise field\n              \"  float n = noise(uv*16.0+uSeed)*0.6 + noise(uv*48.0+uSeed*1.7)*0.3 + noise(uv*120.0+uSeed*2.3)*0.1;\",\n              \"  float edge = 0.12;\",\n              \"  float reveal = smoothstep(n-edge, n+edge, uProgress);\", // 0 hidden -> 1 shown\n              // chromatic fringe strongest at the dissolving edge\n              \"  float band = reveal*(1.0-reveal)*4.0;\",\n              \"  float off = band*0.010;\",\n              \"  float r = texture2D(uTex, uv+vec2(off,0.0)).r;\",\n              \"  float g = texture2D(uTex, uv).g;\",\n              \"  float b = texture2D(uTex, uv-vec2(off,0.0)).b;\",\n              \"  vec3 codeCol = vec3(r,g,b);\",\n              // additive cyan edge glow along the dissolve front\n              \"  vec3 glow = vec3(0.30,0.72,1.0) * band * 0.9;\",\n              \"  vec3 outc = codeCol*reveal + glow;\",\n              \"  float a = max(reveal, band*0.8);\",\n              \"  gl_FragColor = vec4(outc, a);\",\n              \"}\",\n            ].join(\"\\n\"),\n          });\n\n          var plane = new THREE.Mesh(new THREE.PlaneGeometry(planeW, planeH), mat);\n          scene.add(plane);\n\n          camera.position.set(0, 0, 10);\n          var vfov = (camera.fov * Math.PI) / 180;\n          var screenAspect = 1920 / 1080;\n          var fitH = planeH / 2 / Math.tan(vfov / 2) / 0.82;\n          var fitW = planeW / 2 / (Math.tan(vfov / 2) * screenAspect) / 0.82;\n          camera.position.z = Math.max(fitH, fitW);\n          camera.lookAt(0, 0, 0);\n\n          // compile in, then hold crisp. Overshoot to 1.15 so even max-noise pixels\n          // (n up to ~1.0, edge 0.12) fully resolve — no leftover specks at rest.\n          tl.to(uniforms.uProgress, { value: 1.15, duration: dur * 0.62, ease: \"power1.inOut\" }, 0);\n\n          // uProgress is tweened by GSAP; the boot loop's render() draws once per frame.\n          return function () {};\n        }\n\n        // ---- dispatch + boot ------------------------------------------------------\n        function buildEffect(THREE, scene, camera, renderer, spec, st, tl) {\n          var state = T[spec.seq].states[0];\n          var dur = spec.duration;\n          if (spec.effect === \"dissolve\")\n            return fxDissolve(THREE, scene, camera, renderer, state, st, tl, dur, spec.seed);\n          return function () {};\n        }\n\n        // Free GPU resources (renderer, geometries, materials, textures) on page teardown.\n        // One-shot renders exit per job so nothing leaks there; this guards long-lived\n        // Studio/player sessions that reload. Fires only on unload — never during capture.\n        function disposeGL(renderer, scene) {\n          scene.traverse(function (o) {\n            if (o.geometry && o.geometry.dispose) o.geometry.dispose();\n            var mats = o.material ? (Array.isArray(o.material) ? o.material : [o.material]) : [];\n            mats.forEach(function (m) {\n              if (m.map && m.map.dispose) m.map.dispose();\n              if (\n                m.uniforms &&\n                m.uniforms.uTex &&\n                m.uniforms.uTex.value &&\n                m.uniforms.uTex.value.dispose\n              ) {\n                m.uniforms.uTex.value.dispose();\n              }\n              if (m.dispose) m.dispose();\n            });\n          });\n          if (renderer.dispose) renderer.dispose();\n        }\n\n        function go() {\n          var spec = window.__BLOCK;\n          var root = document.getElementById(\"root\");\n          var glcanvas = document.getElementById(\"gl\");\n          var W = 1920;\n          var H = 1080;\n          window.__timelines = window.__timelines || {};\n          var tl = gsap.timeline({ paused: true });\n\n          var THREE = window.THREE;\n          var renderer = new THREE.WebGLRenderer({\n            canvas: glcanvas,\n            antialias: true,\n            alpha: false,\n            preserveDrawingBuffer: true,\n          });\n          renderer.setPixelRatio(1);\n          renderer.setSize(W, H, false);\n          var scene = new THREE.Scene();\n          var camera = new THREE.PerspectiveCamera(45, W / H, 0.1, 200);\n          var st = {};\n\n          document.fonts.ready.then(function () {\n            var renderFn = buildEffect(THREE, scene, camera, renderer, spec, st, tl);\n            function render() {\n              renderFn();\n              renderer.render(scene, camera);\n            }\n            tl.eventCallback(\"onUpdate\", render);\n            var dur = parseFloat(root.dataset.duration) || tl.duration();\n            tl.to({}, { duration: dur }, 0); // pad to full composition length\n            render(); // paint frame 0\n            window.addEventListener(\"beforeunload\", function () {\n              disposeGL(renderer, scene);\n            });\n            window.__timelines[spec.id] = tl; // register AFTER setup completes\n            if (typeof window.__hfForceTimelineRebind === \"function\") {\n              window.__hfForceTimelineRebind(); // re-nest now that we are populated\n            }\n          });\n        }\n\n        if (document.readyState === \"loading\") document.addEventListener(\"DOMContentLoaded\", go);\n        else go();\n      })();\n    </script>\n  </body>\n</html>\n"}