Prompt specification
Build or customize the Three.js 3D scene "Expression Changes During a Surprise Reveal" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Expression Changes During a Surprise Reveal
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/expression-changes-during-a-surprise-reveal-x3718
Scene Description Prompt:
A stylized fictional character reacts as a surprise toy emerges from a box. Animate the reveal in Three.js from cdnjs, one HTML file.
Working Three.js Starter (expression-changes-during-a-surprise-reveal-x3718):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pocket Surprise — Three.js</title>
<style>
:root {
color-scheme: light;
--ink: #34264f;
--cream: #fff8ec;
--yellow: #ffc95c;
--purple: #8f72e8;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #f7eadb;
font-family: Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas:active { cursor: grabbing; }
#reveal {
position: fixed;
left: 50%;
top: 7%;
z-index: 5;
width: min(90vw, 620px);
text-align: center;
pointer-events: none;
opacity: 0;
transform: translate(-50%, -25%) scale(.65) rotate(-3deg);
will-change: opacity, transform;
}
#reveal h1 {
margin: 0;
color: var(--ink);
font-size: clamp(2.5rem, 8vw, 6.5rem);
font-weight: 950;
letter-spacing: -.07em;
line-height: .88;
text-shadow:
0 4px 0 #fff,
0 9px 0 rgba(52, 38, 79, .14);
}
#reveal h1 span { color: #ed6c91; }
#reveal p {
display: inline-block;
margin: 1rem 0 0;
padding: .55rem 1rem;
border-radius: 999px;
background: rgba(255, 255, 255, .8);
color: #665680;
font-weight: 800;
font-size: clamp(.75rem, 2vw, 1rem);
box-shadow: 0 8px 30px rgba(69, 46, 91, .1);
backdrop-filter: blur(8px);
}
.badge {
position: fixed;
left: 22px;
top: 20px;
z-index: 6;
padding: .65rem .9rem;
border: 2px solid rgba(52, 38, 79, .12);
border-radius: 999px;
background: rgba(255, 255, 255, .72);
color: var(--ink);
font-size: .78rem;
font-weight: 900;
letter-spacing: .08em;
text-transform: uppercase;
box-shadow: 0 10px 30px rgba(69, 46, 91, .08);
backdrop-filter: blur(8px);
pointer-events: none;
}
.hint {
position: fixed;
right: 20px;
bottom: 18px;
z-index: 6;
padding: .55rem .75rem;
border-radius: 999px;
background: rgba(52, 38, 79, .78);
color: white;
font-size: .72rem;
font-weight: 750;
pointer-events: none;
opacity: .82;
}
button {
position: fixed;
left: 50%;
bottom: 18px;
z-index: 8;
transform: translateX(-50%);
border: 0;
border-radius: 999px;
padding: .72rem 1.15rem;
background: var(--ink);
color: white;
font: inherit;
font-size: .78rem;
font-weight: 850;
cursor: pointer;
box-shadow: 0 9px 24px rgba(52, 38, 79, .22);
transition: transform .2s ease, background .2s ease;
}
button:hover {
transform: translateX(-50%) translateY(-2px);
background: #49356d;
}
button:active { transform: translateX(-50%) scale(.96); }
#error {
position: fixed;
inset: 0;
z-index: 20;
display: none;
place-items: center;
padding: 2rem;
background: #f7eadb;
color: var(--ink);
text-align: center;
font-weight: 800;
}
@media (max-width: 600px) {
.badge { left: 12px; top: 12px; }
.hint { display: none; }
button { bottom: 12px; }
}
</style>
</head>
<body>
<div class="badge">A Pocket-Sized Surprise</div>
<div id="reveal" aria-live="polite">
<h1>Meet <span>Pockit!</span></h1>
<p>A tiny robot. A very BIG surprise.</p>
</div>
<div class="hint">Drag to orbit · Scroll to zoom</div>
<button id="replay" type="button">↻ Replay surprise</button>
<div id="error">This scene needs WebGL to run in your browser.</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const errorElement = document.querySelector("#error");
const revealElement = document.querySelector("#reveal");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
} catch (error) {
errorElement.style.display = "grid";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf7eadb);
scene.fog = new THREE.Fog(0xf7eadb, 10, 22);
const camera = new THREE.PerspectiveCamera(
37,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(0, 1.25, 0);
let cameraYaw = 0.17;
let cameraElevation = 0.23;
let cameraRadius = 7.35;
let desiredYaw = cameraYaw;
let desiredElevation = cameraElevation;
let desiredRadius = cameraRadius;
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
desiredYaw -= deltaX * 0.006;
desiredElevation = THREE.MathUtils.clamp(
desiredElevation + deltaY * 0.004,
0.06,
0.65
);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
desiredRadius = THREE.MathUtils.clamp(
desiredRadius + event.deltaY * 0.005,
5.1,
10.5
);
}, { passive: false });
const hemisphere = new THREE.HemisphereLight(0xfff6e8, 0x77658f, 2.25);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff3d8, 4.1);
keyLight.position.set(-4.5, 7.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.0002;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0xdab7ff, 18, 12, 2);
rimLight.position.set(4.5, 4.2, -3.2);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb968, 10, 9, 2);
warmLight.position.set(-3.8, 2.8, 3);
scene.add(warmLight);
const palette = {
ink: 0x34264f,
cream: 0xfff8ec,
yellow: 0xffc95c,
orange: 0xf28b52,
coral: 0xed6c91,
purple: 0x8f72e8,
mint: 0x72cdb5,
blue: 0x4e87c9,
skin: 0xf4b88d,
hair: 0x4b3151
};
function standard(color, roughness = 0.72, metalness = 0) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness
});
}
function mesh(geometry, material, parent, position, scale) {
const object = new THREE.Mesh(geometry, material);
if (position) object.position.set(...position);
if (scale) object.scale.set(...scale);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
// Stage
const floor = mesh(
new THREE.PlaneGeometry(40, 40),
standard(0xf7eadb),
scene,
[0, -0.08, 0]
);
floor.rotation.x = -Math.PI / 2;
floor.castShadow = false;
const stage = mesh(
new THREE.CylinderGeometry(3.35, 3.5, 0.18, 64),
standard(0xffe4a6, 0.86),
scene,
[0, 0, 0]
);
stage.castShadow = false;
const stageTop = mesh(
new THREE.CylinderGeometry(3.05, 3.22, 0.055, 64),
standard(0xfff1cf, 0.9),
scene,
[0, 0.105, 0]
);
stageTop.castShadow = false;
const backgroundDecor = new THREE.Group();
scene.add(backgroundDecor);
const decorMaterials = [
standard(0xf5c2d5, 0.9),
standard(0xb6a1eb, 0.9),
standard(0x9ed9c8, 0.9),
standard(0xffd477, 0.9)
];
const decorObjects = [];
const decorData = [
[-3.8, 2.8, -3.3, .42],
[-2.8, 4.2, -4.1, .18],
[3.7, 3.5, -3.8, .31],
[4.3, 1.9, -2.8, .16],
[.1, 4.5, -5.0, .13],
[-4.6, 4.8, -4.6, .09]
];
decorData.forEach((data, index) => {
const object = mesh(
new THREE.SphereGeometry(data[3], 24, 18),
decorMaterials[index % decorMaterials.length],
backgroundDecor,
[data[0], data[1], data[2]]
);
decorObjects.push({ object, baseY: data[1], phase: index * 1.37 });
});
const backdropRing = mesh(
new THREE.TorusGeometry(2.6, .12, 14, 72),
standard(0xe6d8fa, 0.9),
backgroundDecor,
[0, 2.9, -4.8]
);
backdropRing.castShadow = false;
// Character
const character = new THREE.Group();
character.position.set(-1.18, 0, 0);
scene.add(character);
const shirtMaterial = standard(palette.orange);
const overallMaterial = standard(palette.blue);
const skinMaterial = standard(palette.skin);
const hairMaterial = standard(palette.hair);
const shoeMaterial = standard(palette.ink);
const whiteMaterial = standard(0xffffff, 0.45);
const pupilMaterial = standard(0x241a34, 0.35);
const body = mesh(
new THREE.SphereGeometry(1, 32, 24),
shirtMaterial,
character,
[0, 1.08, 0],
[.57, .68, .38]
);
const bib = mesh(
new THREE.BoxGeometry(.58, .53, .08),
overallMaterial,
character,
[0, 1.02, .34]
);
mesh(
new THREE.BoxGeometry(.12, .66, .1),
overallMaterial,
character,
[-.23, 1.38, .29]
);
mesh(
new THREE.BoxGeometry(.12, .66, .1),
overallMaterial,
character,
[.23, 1.38, .29]
);
[-.19, .19].forEach(x => {
const button = mesh(
new THREE.CylinderGeometry(.055, .055, .025, 16),
standard(palette.yellow, .5),
character,
[x, 1.25, .395]
);
button.rotation.x = Math.PI / 2;
});
mesh(
new THREE.CylinderGeometry(.14, .16, .25, 18),
skinMaterial,
character,
[0, 1.68, 0]
);
const head = new THREE.Group();
head.position.set(0, 2.03, 0);
character.add(head);
const headMesh = mesh(
new THREE.SphereGeometry(.62, 36, 28),
skinMaterial,
head,
[0, 0, 0],
[1, 1.04, .91]
);
const hairCap = mesh(
new THREE.SphereGeometry(.64, 36, 20, 0, Math.PI * 2, 0, Math.PI * .38),
hairMaterial,
head,
[0, .015, -.015],
[1, 1.04, .93]
);
mesh(
new THREE.SphereGeometry(.22, 24, 18),
hairMaterial,
head,
[-.33, .39, .01],
[1.25, .78, 1]
);
mesh(
new THREE.SphereGeometry(.21, 24, 18),
hairMaterial,
head,
[.05, .48, -.02],
[1.2, .8, 1]
);
mesh(
new THREE.SphereGeometry(.18, 24, 18),
hairMaterial,
head,
[.39, .32, -.01],
[1.1, .85, 1]
);
const eyeWhiteLeft = mesh(
new THREE.SphereGeometry(.14, 24, 18),
whiteMaterial,
head,
[-.22, .105, .515],
[1, 1.12, .48]
);
const eyeWhiteRight = mesh(
new THREE.SphereGeometry(.14, 24, 18),
whiteMaterial,
head,
[.22, .105, .515],
[1, 1.12, .48]
);
const pupilLeft = mesh(
new THREE.SphereGeometry(.065, 20, 16),
pupilMaterial,
head,
[-.22, .105, .6],
[1, 1, .5]
);
const pupilRight = mesh(
new THREE.SphereGeometry(.065, 20, 16),
pupilMaterial,
head,
[.22, .105, .6],
[1, 1, .5]
);
const smileCurve = new THREE.QuadraticBezierCurve3(
new THREE.Vector3(-.15, -.14, .55),
new THREE.Vector3(0, -.22, .59),
new THREE.Vector3(.15, -.14, .55)
);
const smile = mesh(
new THREE.TubeGeometry(smileCurve, 24, .022, 8, false),
pupilMaterial,
head,
[0, 0, 0]
);
const surprisedMouth = mesh(
new THREE.SphereGeometry(.11, 24, 18),
pupilMaterial,
head,
[0, -.16, .56],
[1, 1.35, .38]
);
surprisedMouth.visible = false;
const eyebrowMaterial = hairMaterial;
const leftBrow = mesh(
new THREE.BoxGeometry(.18, .025, .025),
eyebrowMaterial,
head,
[-.22, .31, .55]
);
const rightBrow = mesh(
new THREE.BoxGeometry(.18, .025, .025),
eyebrowMaterial,
head,
[.22, .31, .55]
);
function makeCharacterArm(side) {
const arm = new THREE.Group();
arm.position.set(side * .54, 1.47, 0);
character.add(arm);
mesh(
new THREE.CylinderGeometry(.14, .12, .42, 18),
shirtMaterial,
arm,
[0, -.22, 0]
);
mesh(
new THREE.CylinderGeometry(.105, .09, .38, 18),
skinMaterial,
arm,
[0, -.61, 0]
);
mesh(
new THREE.SphereGeometry(.13, 20, 16),
skinMaterial,
arm,
[0, -.83, 0],
[1, 1.05, .82]
);
return arm;
}
const leftArm = makeCharacterArm(-1);
const rightArm = makeCharacterArm(1);
[-1, 1].forEach(side => {
mesh(
new THREE.CylinderGeometry(.14, .12, .51, 18),
overallMaterial,
character,
[side * .23, .52, 0]
);
const shoe = mesh(
new THREE.SphereGeometry(.22, 24, 18),
shoeMaterial,
character,
[side * .23, .17, .1],
[1, .62, 1.35]
);
shoe.rotation.x = -.08;
});
// Surprise box
const box = new THREE.Group();
box.position.set(.92, 0, 0);
scene.add(box);
const boxMaterial = standard(palette.yellow, .68);
const boxInsideMaterial = standard(0x6e5687, .9);
const ribbonMaterial = standard(palette.coral, .6);
const innerFloor = mesh(
new THREE.BoxGeometry(1.94, .12, 1.35),
boxInsideMaterial,
box,
[0, .19, 0]
);
const boxTop = 1.38;
const boxHeight = 1.26;
const boxWidth = 2.04;
const boxDepth = 1.42;
function makeFrontPanel() {
const panel = new THREE.Group();
panel.position.set(0, boxTop, boxDepth / 2);
box.add(panel);
mesh(
new THREE.BoxGeometry(boxWidth, boxHeight, .1),
boxMaterial,
panel,
[0, -boxHeight / 2, 0]
);
const interior = mesh(
new THREE.PlaneGeometry(boxWidth - .05, boxHeight - .05),
boxInsideMaterial,
panel,
[0, -boxHeight / 2, -.056]
);
interior.rotation.y = Math.PI;
interior.castShadow = false;
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 320;
const context = labelCanvas.getContext("2d");
context.fillStyle = "#fff7df";
context.fillRect(0, 0, 512, 320);
context.strokeStyle = "#34264f";
context.lineWidth = 18;
context.strokeRect(9, 9, 494, 302);
context.fillStyle = "#8f72e8";
context.font = "900 220px system-ui";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText("?", 256, 172);
context.fillStyle = "#ed6c91";
context.font = "900 36px system-ui";
context.fillText("SURPRISE!", 256, 50);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const label = mesh(
new THREE.PlaneGeometry(.72, .45),
new THREE.MeshBasicMaterial({ map: texture }),
panel,
[0, -.67, .057]
);
label.castShadow = false;
[-.76, .76].forEach(x => {
const dot = mesh(
new THREE.SphereGeometry(.045, 16, 12),
ribbonMaterial,
panel,
[x, -1.08, .061]
);
dot.scale.z = .35;
});
return panel;
}
function makeBackPanel() {
const panel = new THREE.Group();
panel.position.set(0, boxTop, -boxDepth / 2);
box.add(panel);
mesh(
new THREE.BoxGeometry(boxWidth, boxHeight, .1),
boxMaterial,
panel,
[0, -boxHeight / 2, 0]
);
return panel;
}
function makeSidePanel(side) {
const panel = new THREE.Group();
panel.position.set(side * boxWidth / 2, boxTop, 0);
box.add(panel);
mesh(
new THREE.BoxGeometry(.1, boxHeight, boxDepth),
boxMaterial,
panel,
[0, -boxHeight / 2, 0]
);
mesh(
new THREE.BoxGeometry(.012, boxHeight - .12, .3),
ribbonMaterial,
panel,
[side * .057, -boxHeight / 2, 0]
);
return panel;
}
const frontPanel = makeFrontPanel();
const backPanel = makeBackPanel();
const leftPanel = makeSidePanel(-1);
const rightPanel = makeSidePanel(1);
// Robot toy
const robot = new THREE.Group();
robot.position.set(.92, .2, 0);
robot.scale.setScalar(.72);
scene.add(robot);
const robotBodyMaterial = standard(palette.purple, .48);
const robotHeadMaterial = standard(palette.coral, .5);
const robotCreamMaterial = standard(0xfff4d7, .55);
const robotDarkMaterial = standard(0x302344, .35);
const robotMintMaterial = standard(palette.mint, .5);
const robotEyeMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: 0x86ffe1,
emissiveIntensity: .65,
roughness: .3
});
const robotBody = mesh(
new THREE.SphereGeometry(1, 32, 24),
robotBodyMaterial,
robot,
[0, 0, 0],
[.48, .55, .35]
);
const robotBelly = mesh(
new THREE.SphereGeometry(.31, 24, 18),
robotCreamMaterial,
robot,
[0, -.03, .34],
[1, 1.18, .25]
);
const robotHeart = mesh(
new THREE.SphereGeometry(.075, 18, 14),
robotMintMaterial,
robot,
[0, .03, .425],
[1, 1.15, .4]
);
const robotHead = new THREE.Group();
robotHead.position.set(0, .67, 0);
robot.add(robotHead);
mesh(
new THREE.SphereGeometry(.5, 32, 24),
robotHeadMaterial,
robotHead,
[0, 0, 0],
[1, .95, .9]
);
[-1, 1].forEach(side => {
const eye = mesh(
new THREE.SphereGeometry(.115, 22, 16),
robotEyeMaterial,
robotHead,
[side * .17, .055, .405],
[1, 1.16, .5]
);
eye.castShadow = false;
});
const robotSmile = mesh(
new THREE.TorusGeometry(.1, .018, 8, 24, Math.PI),
robotDarkMaterial,
robotHead,
[0, -.12, .42]
);
robotSmile.rotation.z = Math.PI;
[-1, 1].forEach(side => {
const ear = mesh(
new THREE.CylinderGeometry(.12, .12, .13, 18),
robotMintMaterial,
robotHead,
[side * .5, .03, 0]
);
ear.rotation.z = Math.PI / 2;
});
mesh(
new THREE.CylinderGeometry(.025, .025, .28, 12),
robotDarkMaterial,
robot,
[0, 1.16, 0]
);
const antennaBall = mesh(
new THREE.SphereGeometry(.09, 18, 14),
robotEyeMaterial,
robot,
[0, 1.34, 0]
);
function makeRobotArm(side) {
const arm = new THREE.Group();
arm.position.set(side * .5, .17, 0);
robot.add(arm);
mesh(
new THREE.CylinderGeometry(.09, .075, .43, 16),
robotBodyMaterial,
arm,
[0, -.22, 0]
);
mesh(
new THREE.SphereGeometry(.105, 18, 14),
robotCreamMaterial,
arm,
[0, -.48, 0]
);
return arm;
}
const robotLeftArm = makeRobotArm(-1);
const robotRightArm = makeRobotArm(1);
[-1, 1].forEach(side => {
const leg = new THREE.Group();
leg.position.set(side * .23, -.43, 0);
robot.add(leg);
mesh(
new THREE.CylinderGeometry(.1, .09, .32, 16),
robotBodyMaterial,
leg,
[0, -.16, 0]
);
const foot = mesh(
new THREE.SphereGeometry(.16, 20, 14),
robotCreamMaterial,
leg,
[0, -.34, .08],
[1, .55, 1.25]
);
foot.castShadow = true;
});
// Spring connecting the toy to the box
const spring = new THREE.Group();
spring.position.x = .92;
scene.add(spring);
const springMaterial = standard(0xd8d1e8, .35, .5);
const springRings = [];
for (let i = 0; i < 15; i++) {
const ring = mesh(
new THREE.TorusGeometry(1, .105, 8, 32),
springMaterial,
spring,
[0, 0, 0],
[.25, .25, .25]
);
ring.rotation.x = Math.PI / 2;
ring.castShadow = false;
springRings.push(ring);
}
// Star burst around the robot
function createStarShape(outerRadius, innerRadius, points = 5) {
const shape = new THREE.Shape();
for (let i = 0; i < points * 2; i++) {
const radius = i % 2 === 0 ? outerRadius : innerRadius;
const angle = i * Math.PI / points - Math.PI / 2;
const x = Math.cos(angle) * radius;
const y = Math.sin(angle) * radius;
if (i === 0) shape.moveTo(x, y);
else shape.lineTo(x, y);
}
shape.closePath();
return shape;
}
const burstGroup = new THREE.Group();
burstGroup.position.set(.92, 1.65, 0);
scene.add(burstGroup);
const starGeometry = new THREE.ShapeGeometry(createStarShape(.17, .075));
const starMaterials = [
new THREE.MeshBasicMaterial({ color: 0xffc95c }),
new THREE.MeshBasicMaterial({ color: 0xed6c91 }),
new THREE.MeshBasicMaterial({ color: 0x8f72e8 })
];
const stars = [];
for (let i = 0; i < 9; i++) {
const angle = i / 9 * Math.PI * 2;
const star = mesh(
starGeometry,
starMaterials[i % starMaterials.length],
burstGroup,
[Math.cos(angle) * .92, Math.sin(angle) * .72, -.05 + (i % 3) * .06]
);
star.castShadow = false;
stars.push({ object: star, angle, phase: i * .7 });
}
const revealRingMaterial = new THREE.MeshBasicMaterial({
color: 0xffca5e,
transparent: true,
opacity: 0,
depthWrite: false
});
const revealRing = mesh(
new THREE.TorusGeometry(.7, .025, 10, 72),
revealRingMaterial,
burstGroup,
[0, 0, -.1]
);
revealRing.rotation.x = Math.PI / 2;
revealRing.castShadow = false;
// Confetti
const confetti = [];
const confettiColors = [
0xffc95c, 0xed6c91, 0x8f72e8, 0x72cdb5, 0x4e87c9, 0xffffff
];
for (let i = 0; i < 76; i++) {
const color = confettiColors[i % confettiColors.length];
const object = mesh(
new THREE.BoxGeometry(.035, .085, .014),
new THREE.MeshBasicMaterial({ color }),
scene,
[0, -10, 0]
);
object.castShadow = false;
confetti.push({
object,
start: 3.95 + Math.random() * 1.8,
duration: 2.5 + Math.random() * 1.3,
x: .92 + (Math.random() - .5) * .65,
z: (Math.random() - .5) * .7,
vx: (Math.random() - .5) * 2.25,
vy: 2.1 + Math.random() * 2.1,
rx: (Math.random() - .5) * 9,
ry: (Math.random() - .5) * 9,
phase: Math.random() * Math.PI * 2
});
}
// Timeline helpers
const clamp01 = value => THREE.MathUtils.clamp(value, 0, 1);
const lerp = THREE.MathUtils.lerp;
function smooth(start, end, value) {
const x = clamp01((value - start) / (end - start));
return x * x * (3 - 2 * x);
}
function smoother(start, end, value) {
const x = clamp01((value - start) / (end - start));
return x * x * x * (x * (x * 6 - 15) + 10);
}
function easeOutBack(value) {
const x = clamp01(value);
const c1 = 1.70158;
const c3 = c1 + 1;
return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);
}
let elapsed = 0;
const cycleDuration = 10.25;
document.querySelector("#replay").addEventListener("click", () => {
elapsed = 0;
});
function updateAnimation(time) {
const t = time % cycleDuration;
// The box opens with a tiny overshoot.
const opening = smooth(1.25, 2.05, t);
const closing = 1 - smooth(8.9, 9.72, t);
const openAmount = Math.min(opening, closing);
const flapAngle = openAmount * 1.88 + Math.sin(openAmount * Math.PI) * .13;
frontPanel.rotation.x = -flapAngle;
backPanel.rotation.x = flapAngle;
leftPanel.rotation.z = -flapAngle;
rightPanel.rotation.z = flapAngle;
const anticipation = smooth(.65, 1.25, t) * (1 - smooth(1.7, 2.15, t));
const boxShake = t > 1.05 && t < 1.5 ? Math.sin(t * 65) * .035 * (1 - smooth(1.3, 1.5, t)) : 0;
box.position.x = .92 + boxShake;
box.rotation.z = boxShake * .08;
// Robot rises out of the box, then settles back before the lid closes.
let robotY;
if (t < 8.15) {
robotY = .2 + 1.45 * easeOutBack((t - 2.0) / 1.55);
} else {
robotY = lerp(1.65, .2, smooth(8.15, 9.02, t));
}
robot.position.y = robotY;
const robotVisible = t > 1.95;
robot.visible = robotVisible;
const springTop = robotY - .37;
const springBottom = .3;
const springLength = Math.max(0, springTop - springBottom);
spring.visible = t > 2.0 && t < 3.85 && springLength > .06;
springRings.forEach((ring, index) => {
const fraction = index / (springRings.length - 1);
ring.position.y = springBottom + fraction * springLength;
const radius = .245 + Math.sin(fraction * Math.PI) * .035;
ring.scale.set(radius, radius, radius);
});
robot.rotation.y = -.18 + smooth(2.0, 3.7, t) * .33 + Math.sin(t * 2.1) * .035;
// Character reaction: lean, jump, wide eyes, raised arms, O-shaped mouth.
const surprise = smooth(2.52, 3.12, t) * (1 - smooth(7.8, 8.68, t));
const jumpAge = (t - 2.82) / 1.45;
let jump = 0;
if (jumpAge > 0 && jumpAge < 1) {
jump = Math.sin(jumpAge * Math.PI) * .38;
jump += Math.sin(jumpAge * Math.PI * 2) * .035;
}
character.position.y = jump;
character.rotation.y = anticipation * .23 + surprise * .06;
character.rotation.z = Math.sin(t * 2.5) * .012 + surprise * Math.sin(t * 8) * .025;
character.position.x = -1.18 + surprise * .035;
body.scale.y = .68 * (1 + Math.sin(t * 2.2) * .012 + surprise * .025);
leftArm.rotation.z = -.13 - surprise * 2.23;
rightArm.rotation.z = .13 + surprise * 2.23;
leftArm.rotation.x = surprise * -.28;
rightArm.rotation.x = surprise * -.28;
head.rotation.y = anticipation * .28 - surprise * .07;
head.rotation.z = surprise * Math.sin(t * 10) * .035;
head.position.y = 2.03 + Math.sin(t * 2.1) * .012;
const blink =
(t > .68 && t < .79) ||
(t > 6.35 && t < 6.43)
? .12
: 1;
const eyeWide = 1 + surprise * .28;
eyeWhiteLeft.scale.set(eyeWide, 1.12 * blink * eyeWide, .48);
eyeWhiteRight.scale.set(eyeWide, 1.12 * blink * eyeWide, .48);
pupilLeft.scale.set(1 - surprise * .25, (1 - surprise * .25) * blink, .5);
pupilRight.scale.set(1 - surprise * .25, (1 - surprise * .25) * blink, .5);
pupilLeft.position.x = -.22 + anticipation * .035;
pupilRight.position.x = .22 + anticipation * .035;
smile.visible = surprise < .45;
surprisedMouth.visible = surprise > .38;
surprisedMouth.scale.set(
1 + surprise * .18,
(1 + surprise * .18) * (1 + surprise * .28),
.38
);
leftBrow.rotation.z = .12 + surprise * .22;
rightBrow.rotation.z = -.12 - surprise * .22;
leftBrow.position.y = .31 + surprise * .08;
rightBrow.position.y = .31 + surprise * .08;
// Robot celebrates and waves.
const robotPop = smooth(2.9, 3.65, t);
robot.scale.setScalar(.72 * (1 + Math.sin(robotPop * Math.PI) * .09));
robotLeftArm.rotation.z = -.12 - robotPop * .92;
robotRightArm.rotation.z = .12 + robotPop * .92;
const wave = smooth(3.85, 4.4, t) * (1 - smooth(7.6, 8.25, t));
robotRightArm.rotation.z = lerp(robotRightArm.rotation.z, 2.35, wave);
robotRightArm.rotation.x = wave * Math.sin(t * 9) * .42 - wave * .25;
robotRightArm.rotation.y = wave * .18;
robotHead.rotation.z = Math.sin(t * 3.1) * .025 + wave * Math.sin(t * 8) * .035;
antennaBall.scale.setScalar(1 + Math.sin(t * 7) * .08 + wave * .12);
// Graphic title
const titleIn = smooth(3.75, 4.35, t);
const titleOut = 1 - smooth(7.75, 8.55, t);
const titleAmount = titleIn * titleOut;
revealElement.style.opacity = titleAmount.toFixed(3);
revealElement.style.transform =
`translate(-50%, -25%) scale(${(.68 + titleAmount * .32).toFixed(3)}) rotate(${(1 - titleAmount) * -4}deg)`;
// Stars and expanding ring
const burst = smooth(3.45, 4.05, t) * (1 - smooth(7.4, 8.2, t));
burstGroup.visible = burst > .01;
stars.forEach(star => {
star.object.visible = burst > .1;
const pulse = 1 + Math.sin(t * 7 + star.phase) * .12;
star.object.scale.setScalar(pulse);
star.object.rotation.z = t * 1.4 + star.phase;
star.object.position.y =
Math.sin(star.angle) * .72 + Math.sin(t * 3 + star.phase) * .06;
});
const ringAge = (t - 3.0) / .9;
if (ringAge > 0 && ringAge < 1) {
revealRingMaterial.opacity = (1 - ringAge) * .7;
revealRing.scale.setScalar(.5 + ringAge * 1.7);
} else {
revealRingMaterial.opacity = 0;
}
// Confetti follows deterministic ballistic paths.
confetti.forEach(piece => {
const age = t - piece.start;
if (age > 0 && age < piece.duration && t < 8.8) {
piece.object.visible = true;
piece.object.position.set(
piece.x + piece.vx * age,
3.45 + piece.vy * age - 1.8 * age * age,
piece.z + Math.sin(age * 2 + piece.phase) * .3
);
piece.object.rotation.set(
piece.rx * age,
piece.ry * age,
piece.phase + age * 7
);
} else {
piece.object.visible = false;
}
});
// Background floating shapes
decorObjects.forEach(item => {
item.object.position.y = item.baseY + Math.sin(t * .75 + item.phase) * .13;
item.object.rotation.y = t * .18 + item.phase;
});
backdropRing.rotation.z = Math.sin(t * .22) * .06;
}
function updateCamera(dt) {
const damping = 1 - Math.exp(-dt * 7);
cameraYaw = lerp(cameraYaw, desiredYaw, damping);
cameraElevation = lerp(cameraElevation, desiredElevation, damping);
cameraRadius = lerp(cameraRadius, desiredRadius, damping);
const horizontalRadius = Math.cos(cameraElevation) * cameraRadius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontalRadius,
cameraTarget.y + Math.sin(cameraElevation) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontalRadius
);
camera.lookAt(cameraTarget);
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), .05);
elapsed += dt;
updateAnimation(elapsed);
updateCamera(dt);
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate();
</script>
</body>
</html>
```