Skip to content

Write HTML. Render video. Built for agents.

13.7 KB
Raw Blame
{"html":"<!doctype html>\n<!--\n  Product Turntable: one product turns a full revolution on a studio plinth, a seamless loop.\n  model_url takes a .glb/.gltf; empty uses the built-in procedural drink can.\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\":\"model_url\",\"type\":\"string\",\"role\":\"content\",\"label\":\"Model URL (.glb or .gltf)\",\"description\":\"Leave empty for the built-in drink can.\",\"default\":\"\"},{\"id\":\"product_name\",\"type\":\"string\",\"role\":\"content\",\"label\":\"Label wordmark\",\"default\":\"YUZU\",\"maxLength\":10},{\"id\":\"flavor\",\"type\":\"string\",\"role\":\"content\",\"label\":\"Label line\",\"default\":\"Sparkling Citrus\",\"maxLength\":28},{\"id\":\"caption\",\"type\":\"string\",\"role\":\"content\",\"label\":\"Caption\",\"default\":\"Sparkling Citrus · 355 ml\",\"maxLength\":40},{\"id\":\"label_color\",\"type\":\"color\",\"role\":\"style\",\"label\":\"Label color\",\"default\":\"#f2c230\"},{\"id\":\"ink_color\",\"type\":\"color\",\"role\":\"style\",\"label\":\"Label ink\",\"default\":\"#1d2a24\"},{\"id\":\"background\",\"type\":\"color\",\"role\":\"style\",\"label\":\"Backdrop\",\"default\":\"#e8e4dc\"},{\"id\":\"turns\",\"type\":\"number\",\"role\":\"motion\",\"label\":\"Turns per clip\",\"default\":1,\"min\":1,\"max\":4,\"step\":1}]'\n>\n  <head>\n    <meta charset=\"UTF-8\" />\n    <meta name=\"viewport\" content=\"width=1920, height=1080\" />\n    <title>Product Turntable</title>\n    <script src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js\"></script>\n    <style>\n      @font-face {\n        font-family: \"PT 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: #e8e4dc;\n      }\n      #pt-root {\n        position: relative;\n        width: 1920px;\n        height: 1080px;\n        overflow: hidden;\n        background:\n          radial-gradient(1100px 760px at 50% 38%, rgba(255, 255, 255, 0.72), transparent 70%),\n          var(--pt-bg, #e8e4dc);\n      }\n      #pt-canvas {\n        position: absolute;\n        inset: 0;\n        width: 1920px;\n        height: 1080px;\n        display: block;\n      }\n      #pt-caption {\n        position: absolute;\n        left: 96px;\n        bottom: 96px;\n        font:\n          500 30px/1 \"PT Caption\",\n          system-ui,\n          sans-serif;\n        letter-spacing: 0.01em;\n        color: rgba(20, 22, 24, 0.62);\n      }\n    </style>\n  </head>\n  <body>\n    <div\n      id=\"pt-root\"\n      data-composition-id=\"product-turntable\"\n      data-start=\"0\"\n      data-duration=\"6\"\n      data-width=\"1920\"\n      data-height=\"1080\"\n    >\n      <canvas\n        id=\"pt-canvas\"\n        class=\"clip\"\n        data-start=\"0\"\n        data-duration=\"6\"\n        data-track-index=\"1\"\n      ></canvas>\n      <div id=\"pt-caption\"></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(\"pt-root\");\n        var duration = Math.max(0.001, parseFloat(root.dataset.duration) || 6);\n        root.style.setProperty(\"--pt-bg\", vars.background || \"#e8e4dc\");\n        document.getElementById(\"pt-caption\").textContent = vars.caption || \"\";\n\n        var clock = { t: 0 };\n        var scene = { vars: vars, duration: duration, clock: clock, render: null };\n        window.__productTurntable = scene;\n        window.__hf = window.__hf || {};\n        window.__hf.buildReady = window.__hf.buildReady || {};\n        window.__hf.buildReady[\"product-turntable\"] = 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        window.__timelines = window.__timelines || {};\n        window.__timelines[\"product-turntable\"] = tl;\n      })();\n    </script>\n\n    <script type=\"module\">\n      import * as THREE from \"https://cdn.jsdelivr.net/npm/[email protected]/+esm\";\n      import { RoomEnvironment } from \"https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/environments/RoomEnvironment.js/+esm\";\n      import { GLTFLoader } from \"https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/loaders/GLTFLoader.js/+esm\";\n\n      const host = window.__productTurntable;\n      const V = host.vars;\n      const hex = (v, fallback) => (/^#[0-9a-f]{6}$/i.test(String(v)) ? String(v) : fallback);\n      const turns = Math.max(1, Math.min(4, Math.round(Number(V.turns) || 1)));\n\n      const renderer = new THREE.WebGLRenderer({\n        canvas: document.getElementById(\"pt-canvas\"),\n        antialias: true,\n        alpha: true,\n        preserveDrawingBuffer: true,\n      });\n      renderer.setPixelRatio(1);\n      renderer.setSize(1920, 1080, false);\n      renderer.outputColorSpace = THREE.SRGBColorSpace;\n      renderer.toneMapping = THREE.ACESFilmicToneMapping;\n      renderer.toneMappingExposure = 1.3;\n\n      const scene = new THREE.Scene();\n      const pmrem = new THREE.PMREMGenerator(renderer);\n      // Studio: two tall softbox strips (key 35 deg left, rim 120 deg right at 1.6x) over a dimmed room fill.\n      const studio = new THREE.Scene();\n      const room = new RoomEnvironment();\n      room.traverse((o) => {\n        if (o.isLight) o.intensity *= 0.3;\n        if (o.material && o.material.emissiveIntensity) o.material.emissiveIntensity *= 0.3;\n      });\n      studio.add(room);\n      for (const [deg, intensity] of [\n        [-35, 5],\n        [120, 8],\n      ]) {\n        const strip = new THREE.Mesh(\n          new THREE.PlaneGeometry(1.2, 8),\n          new THREE.MeshLambertMaterial({\n            color: 0x000000,\n            emissive: 0xffffff,\n            emissiveIntensity: intensity,\n            side: THREE.DoubleSide,\n          }),\n        );\n        const a = THREE.MathUtils.degToRad(deg);\n        strip.position.set(6 * Math.sin(a), 1, 6 * Math.cos(a));\n        strip.lookAt(0, 1, 0);\n        studio.add(strip);\n      }\n      scene.environment = pmrem.fromScene(studio, 0.02).texture;\n\n      // Can and plinth vertically centred, about 140 px clear above and below.\n      const camera = new THREE.PerspectiveCamera(24, 1920 / 1080, 0.1, 100);\n      camera.position.set(0, 2.1, 9.4);\n      camera.lookAt(0, 0.9, 0);\n\n      const key = new THREE.DirectionalLight(0xffffff, 1.6);\n      key.position.set(-3.5, 5, 4);\n      scene.add(key);\n      const rim = new THREE.DirectionalLight(0xfff1dc, 2.2);\n      rim.position.set(4, 3, -4);\n      scene.add(rim);\n\n      // Plinth with a soft baked contact shadow: no shadow maps, no per-frame cost.\n      const plinth = new THREE.Mesh(\n        new THREE.CylinderGeometry(1.42, 1.46, 0.2, 96),\n        new THREE.MeshStandardMaterial({ color: 0xf4f1ec, roughness: 0.55, metalness: 0 }),\n      );\n      plinth.position.y = -0.1;\n      scene.add(plinth);\n\n      function radialTexture(inner, outer) {\n        const c = document.createElement(\"canvas\");\n        c.width = c.height = 256;\n        const g = c.getContext(\"2d\");\n        const grad = g.createRadialGradient(128, 128, 0, 128, 128, 128);\n        grad.addColorStop(0, inner);\n        grad.addColorStop(1, outer);\n        g.fillStyle = grad;\n        g.fillRect(0, 0, 256, 256);\n        const tex = new THREE.CanvasTexture(c);\n        tex.colorSpace = THREE.SRGBColorSpace;\n        return tex;\n      }\n      const shadow = new THREE.Mesh(\n        new THREE.PlaneGeometry(2.4, 2.4),\n        new THREE.MeshBasicMaterial({\n          map: radialTexture(\"rgba(0,0,0,0.55)\", \"rgba(0,0,0,0)\"),\n          transparent: true,\n          depthWrite: false,\n        }),\n      );\n      shadow.rotation.x = -Math.PI / 2;\n      shadow.position.y = 0.002;\n      scene.add(shadow);\n\n      const turntable = new THREE.Group();\n      scene.add(turntable);\n\n      async function labelTexture() {\n        try {\n          const face = new FontFace(\n            \"PT Inter\",\n            \"url(https://cdn.jsdelivr.net/fontsource/fonts/inter@latest/latin-800-normal.woff2)\",\n            { weight: \"800\" },\n          );\n          document.fonts.add(await face.load());\n        } catch (error) {\n          console.error(\"[product-turntable] label font failed to load, using system sans\", error);\n        }\n        const c = document.createElement(\"canvas\");\n        // 2.35:1 matches the label's circumference to height, so type is not stretched.\n        c.width = 2048;\n        c.height = 872;\n        const g = c.getContext(\"2d\");\n        const label = hex(V.label_color, \"#f2c230\");\n        const ink = hex(V.ink_color, \"#1d2a24\");\n        g.fillStyle = label;\n        g.fillRect(0, 0, 2048, 872);\n        g.fillStyle = ink;\n        g.fillRect(0, 70, 2048, 12);\n        g.fillRect(0, 790, 2048, 12);\n        const name = String(V.product_name || \"YUZU\").toUpperCase();\n        const flavor = String(V.flavor || \"\").toUpperCase();\n        // Two faces, front and back, so the wordmark reads twice per revolution.\n        for (const cx of [512, 1536]) {\n          g.save();\n          g.textAlign = \"center\";\n          g.textBaseline = \"middle\";\n          let size = 280;\n          g.font = `800 ${size}px \"PT Inter\", system-ui, sans-serif`;\n          while (g.measureText(name).width > 600 && size > 80) {\n            size -= 10;\n            g.font = `800 ${size}px \"PT Inter\", system-ui, sans-serif`;\n          }\n          g.fillText(name, cx, 400);\n          g.font = `800 50px \"PT Inter\", system-ui, sans-serif`;\n          g.globalAlpha = 0.8;\n          g.fillText(flavor, cx, 590, 540);\n          g.restore();\n        }\n        g.save();\n        g.textAlign = \"center\";\n        g.font = `800 34px \"PT Inter\", system-ui, sans-serif`;\n        g.globalAlpha = 0.7;\n        for (const cx of [0, 1024, 2048]) g.fillText(\"355 ML\", cx, 450);\n        g.restore();\n        const tex = new THREE.CanvasTexture(c);\n        tex.colorSpace = THREE.SRGBColorSpace;\n        tex.anisotropy = renderer.capabilities.getMaxAnisotropy();\n        return tex;\n      }\n\n      async function builtInCan() {\n        const can = new THREE.Group();\n        const metal = new THREE.MeshStandardMaterial({\n          color: 0xd4d7dc,\n          metalness: 1,\n          roughness: 0.24,\n        });\n        // Lathe profile (radius, height): domed base, straight wall, necked shoulder, rolled lip.\n        const profile = [\n          [0.0, 0.1],\n          [0.46, 0.06],\n          [0.6, 0.0],\n          [0.66, 0.06],\n          [0.67, 0.16],\n          [0.67, 2.02],\n          [0.63, 2.14],\n          [0.56, 2.24],\n          [0.555, 2.29],\n          [0.575, 2.31],\n          [0.56, 2.33],\n          [0.5, 2.3],\n          [0.0, 2.27],\n        ].map(([x, y]) => new THREE.Vector2(x, y));\n        can.add(new THREE.Mesh(new THREE.LatheGeometry(profile, 128), metal));\n        const labelMat = new THREE.MeshPhysicalMaterial({\n          map: await labelTexture(),\n          metalness: 0.45,\n          roughness: 0.32,\n          clearcoat: 1,\n          clearcoatRoughness: 0.12,\n        });\n        const label = new THREE.Mesh(\n          new THREE.CylinderGeometry(0.674, 0.674, 1.8, 128, 1, true),\n          labelMat,\n        );\n        label.position.y = 1.1;\n        can.add(label);\n        // Pull tab: a thin rounded plate on the lid.\n        const tab = new THREE.Mesh(new THREE.CapsuleGeometry(0.07, 0.22, 4, 12), metal);\n        tab.rotation.set(Math.PI / 2, 0, Math.PI / 2);\n        tab.scale.set(1, 1, 0.25);\n        tab.position.set(0.1, 2.285, 0);\n        can.add(tab);\n        return can;\n      }\n\n      function fitModel(object) {\n        const box = new THREE.Box3().setFromObject(object);\n        const size = box.getSize(new THREE.Vector3());\n        const center = box.getCenter(new THREE.Vector3());\n        const scale = Math.min(2.3 / size.y, 2.6 / Math.max(size.x, size.z));\n        object.scale.setScalar(scale);\n        object.position.set(-center.x * scale, -box.min.y * scale, -center.z * scale);\n        const wrapper = new THREE.Group();\n        wrapper.add(object);\n        return wrapper;\n      }\n\n      // A model_url that fails to load fails the render; it never swaps in a different product.\n      async function product() {\n        const url = String(V.model_url || \"\").trim();\n        if (!url) return builtInCan();\n        const gltf = await new GLTFLoader().loadAsync(url);\n        return fitModel(gltf.scene);\n      }\n\n      turntable.add(await product());\n\n      host.render = (t) => {\n        turntable.rotation.y = (t / host.duration) * turns * Math.PI * 2;\n        renderer.render(scene, camera);\n      };\n      host.render(host.clock.t);\n      host.ready();\n    </script>\n  </body>\n</html>\n"}