Skip to content

Write HTML. Render video. Built for agents.

10.2 KB
Raw Blame
{"html":"<!doctype html>\n<!--\n  Glass Hero: a glass object rises in front of a giant headline and turns, bending the type.\n  The headline is drawn inside the scene because transmission only refracts what is rendered.\n  Seek-safe: every frame is render(t), driven by one linear GSAP clock.\n-->\n<html\n  lang=\"en\"\n  data-composition-variables='[{\"id\":\"headline\",\"type\":\"string\",\"role\":\"content\",\"label\":\"Headline behind the glass\",\"default\":\"CLARITY\",\"maxLength\":12},{\"id\":\"subline\",\"type\":\"string\",\"role\":\"content\",\"label\":\"Subline\",\"default\":\"See every detail, nothing in the way.\",\"maxLength\":48},{\"id\":\"shape\",\"type\":\"enum\",\"role\":\"style\",\"label\":\"Glass shape\",\"default\":\"lens\",\"options\":[{\"value\":\"lens\",\"label\":\"Lens\"},{\"value\":\"ring\",\"label\":\"Ring\"}]},{\"id\":\"glass_tint\",\"type\":\"color\",\"role\":\"style\",\"label\":\"Glass tint\",\"default\":\"#cfe9ff\"},{\"id\":\"type_color\",\"type\":\"color\",\"role\":\"style\",\"label\":\"Headline color\",\"default\":\"#f4efe4\"},{\"id\":\"background\",\"type\":\"color\",\"role\":\"style\",\"label\":\"Backdrop\",\"default\":\"#0b0b0c\"}]'\n>\n  <head>\n    <meta charset=\"UTF-8\" />\n    <meta name=\"viewport\" content=\"width=1920, height=1080\" />\n    <title>Glass Hero</title>\n    <script src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js\"></script>\n    <style>\n      @font-face {\n        font-family: \"GH Caption\";\n        font-weight: 500;\n        src: url(https://cdn.jsdelivr.net/fontsource/fonts/inter@latest/latin-500-normal.woff2)\n          format(\"woff2\");\n      }\n      html,\n      body {\n        margin: 0;\n        width: 1920px;\n        height: 1080px;\n        overflow: hidden;\n        background: #0b0b0c;\n      }\n      #gh-root {\n        position: relative;\n        width: 1920px;\n        height: 1080px;\n        overflow: hidden;\n        background: #0b0b0c;\n      }\n      #gh-canvas {\n        position: absolute;\n        inset: 0;\n        width: 1920px;\n        height: 1080px;\n        display: block;\n      }\n      #gh-subline {\n        position: absolute;\n        left: 0;\n        right: 0;\n        bottom: 118px;\n        text-align: center;\n        font:\n          500 36px/1.2 \"GH Caption\",\n          system-ui,\n          sans-serif;\n        letter-spacing: 0.01em;\n        color: rgba(244, 239, 228, 0.78);\n        opacity: 0;\n      }\n    </style>\n  </head>\n  <body>\n    <div\n      id=\"gh-root\"\n      data-composition-id=\"glass-hero\"\n      data-start=\"0\"\n      data-duration=\"5\"\n      data-width=\"1920\"\n      data-height=\"1080\"\n    >\n      <canvas\n        id=\"gh-canvas\"\n        class=\"clip\"\n        data-start=\"0\"\n        data-duration=\"5\"\n        data-track-index=\"1\"\n      ></canvas>\n      <div id=\"gh-subline\"></div>\n    </div>\n\n    <script>\n      (function () {\n        var html = document.documentElement;\n        var defaults = {};\n        try {\n          JSON.parse(html.getAttribute(\"data-composition-variables\") || \"[]\").forEach(function (v) {\n            defaults[v.id] = v.default;\n          });\n        } catch (error) {\n          /* malformed declaration: the scene module falls back to its own defaults */\n        }\n        var scoped =\n          (window.__hyperframes &&\n            window.__hyperframes.getVariables &&\n            window.__hyperframes.getVariables()) ||\n          {};\n        var vars = Object.assign({}, defaults, window.__hfVariables || {}, scoped);\n        var root = document.getElementById(\"gh-root\");\n        var duration = Math.max(0.001, parseFloat(root.dataset.duration) || 5);\n        var bg = /^#[0-9a-f]{6}$/i.test(String(vars.background)) ? vars.background : \"#0b0b0c\";\n        root.style.background = bg;\n        var subline = document.getElementById(\"gh-subline\");\n        subline.textContent = vars.subline || \"\";\n\n        var clock = { t: 0 };\n        var scene = { vars: vars, duration: duration, clock: clock, background: bg, render: null };\n        window.__glassHero = scene;\n        window.__hf = window.__hf || {};\n        window.__hf.buildReady = window.__hf.buildReady || {};\n        window.__hf.buildReady[\"glass-hero\"] = new Promise(function (resolve) {\n          scene.ready = resolve;\n        });\n\n        var tl = gsap.timeline({ paused: true });\n        tl.to(clock, {\n          t: duration,\n          duration: duration,\n          ease: \"none\",\n          onUpdate: function () {\n            if (scene.render) scene.render(clock.t);\n          },\n        });\n        tl.fromTo(\n          subline,\n          { opacity: 0, y: 18 },\n          { opacity: 1, y: 0, duration: 0.7, ease: \"power2.out\" },\n          Math.min(1.5, duration * 0.3),\n        );\n        window.__timelines = window.__timelines || {};\n        window.__timelines[\"glass-hero\"] = tl;\n      })();\n    </script>\n\n    <script type=\"module\">\n      import * as THREE from \"https://cdn.jsdelivr.net/npm/[email protected]/+esm\";\n\n      const host = window.__glassHero;\n      const V = host.vars;\n      const hex = (v, fallback) => (/^#[0-9a-f]{6}$/i.test(String(v)) ? String(v) : fallback);\n\n      const renderer = new THREE.WebGLRenderer({\n        canvas: document.getElementById(\"gh-canvas\"),\n        antialias: true,\n        preserveDrawingBuffer: true,\n      });\n      renderer.setPixelRatio(1);\n      renderer.setSize(1920, 1080, false);\n      renderer.outputColorSpace = THREE.SRGBColorSpace;\n      // No tone mapping: the glass passes the ground and type through at their true values.\n\n      const scene = new THREE.Scene();\n      scene.background = new THREE.Color(host.background);\n      const pmrem = new THREE.PMREMGenerator(renderer);\n      // Studio light for the reflections: a soft key box overhead and two tall strips at the sides.\n      const studio = new THREE.Scene();\n      studio.background = new THREE.Color(0.08, 0.08, 0.08);\n      const softbox = (w, h, x, y, z, level) => {\n        const m = new THREE.Mesh(\n          new THREE.PlaneGeometry(w, h),\n          new THREE.MeshBasicMaterial({\n            color: new THREE.Color(level, level, level),\n            side: THREE.DoubleSide,\n          }),\n        );\n        m.position.set(x, y, z);\n        m.lookAt(0, 0, 0);\n        studio.add(m);\n      };\n      softbox(8, 3, -1, 6, 2, 2.5);\n      softbox(1.2, 9, -7, 0, -1, 4);\n      softbox(1.2, 9, 7, 0, -1, 4);\n      scene.environment = pmrem.fromScene(studio, 0.02).texture;\n\n      const camera = new THREE.PerspectiveCamera(30, 1920 / 1080, 0.1, 100);\n      camera.position.set(0, 0, 10);\n\n      async function headlineTexture() {\n        try {\n          const face = new FontFace(\n            \"GH Inter\",\n            \"url(https://cdn.jsdelivr.net/fontsource/fonts/inter@latest/latin-900-normal.woff2)\",\n            { weight: \"900\" },\n          );\n          document.fonts.add(await face.load());\n        } catch (error) {\n          console.error(\"[glass-hero] headline font failed to load, using system sans\", error);\n        }\n        const c = document.createElement(\"canvas\");\n        c.width = 3840;\n        c.height = 2160;\n        const g = c.getContext(\"2d\");\n        g.fillStyle = host.background;\n        g.fillRect(0, 0, c.width, c.height);\n        const text = String(V.headline || \"CLARITY\").toUpperCase();\n        let size = 1000;\n        g.font = `900 ${size}px \"GH Inter\", system-ui, sans-serif`;\n        while (g.measureText(text).width > 3100 && size > 200) {\n          size -= 20;\n          g.font = `900 ${size}px \"GH Inter\", system-ui, sans-serif`;\n        }\n        g.textAlign = \"center\";\n        g.textBaseline = \"middle\";\n        g.fillStyle = hex(V.type_color, \"#f4efe4\");\n        g.fillText(text, c.width / 2, c.height * 0.47);\n        const tex = new THREE.CanvasTexture(c);\n        tex.colorSpace = THREE.SRGBColorSpace;\n        tex.anisotropy = renderer.capabilities.getMaxAnisotropy();\n        return tex;\n      }\n\n      // The backdrop plane fills the frustum at z = -2, so it reads as the page itself.\n      const planeHeight = 2 * Math.tan(THREE.MathUtils.degToRad(camera.fov / 2)) * 12;\n      const backdrop = new THREE.Mesh(\n        new THREE.PlaneGeometry(planeHeight * (1920 / 1080), planeHeight),\n        new THREE.MeshBasicMaterial({ map: await headlineTexture() }),\n      );\n      backdrop.position.z = -2;\n      scene.add(backdrop);\n\n      const geometries = {\n        ring: () => new THREE.TorusGeometry(1.25, 0.46, 96, 200),\n        lens: () => {\n          const g = new THREE.SphereGeometry(1.6, 128, 64);\n          g.scale(1, 1, 0.42);\n          return g;\n        },\n      };\n      const glass = new THREE.Mesh(\n        (geometries[V.shape] || geometries.lens)(),\n        new THREE.MeshPhysicalMaterial({\n          color: 0xffffff,\n          transmission: 1,\n          thickness: 0.6,\n          ior: 1.3,\n          roughness: 0.03,\n          metalness: 0,\n          dispersion: 0.6,\n          attenuationColor: new THREE.Color(hex(V.glass_tint, \"#cfe9ff\")),\n          attenuationDistance: 2.2,\n          specularIntensity: 1,\n          clearcoat: 1,\n          clearcoatRoughness: 0.04,\n        }),\n      );\n      scene.add(glass);\n\n      const key = new THREE.DirectionalLight(0xffffff, 2.4);\n      key.position.set(-4, 5, 6);\n      scene.add(key);\n\n      const easeOut = (x) => 1 - Math.pow(1 - Math.min(1, Math.max(0, x)), 3);\n      const lens = V.shape !== \"ring\";\n\n      host.render = (t) => {\n        // Rise and settle over the first 1.6 s, then keep turning; the turn never stops,\n        // so a held frame still shows the type moving through the glass.\n        const rise = easeOut(t / 1.8);\n        glass.position.set(\n          Math.sin(t * 0.55) * 0.35,\n          -3.2 * (1 - rise) + Math.sin(t * 0.9) * 0.06,\n          1.2,\n        );\n        glass.scale.setScalar(0.62 + 0.14 * rise);\n        glass.rotation.set(\n          lens ? Math.sin(t * 0.7) * 0.35 : 0.5 + t * 0.22,\n          lens ? Math.sin(t * 0.5) * 0.5 + 0.2 : t * 0.34,\n          lens ? 0 : 0.2 + t * 0.08,\n        );\n        renderer.render(scene, camera);\n      };\n      host.render(host.clock.t);\n      host.ready();\n    </script>\n  </body>\n</html>\n"}