Prompt specification
Build or customize the Three.js 3D scene "Two Runners Matched Handoff" 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: Two Runners Matched Handoff
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/two-runners-matched-handoff-x3634
Scene Description Prompt:
I'd like to see this as a voxel Three.js scene: two runners matched handoff. Animate two stylized runners exchanging a baton within a marked fictional zone. Keep hand contact and speed transitions smooth while labeling the rules as simplified. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (two-runners-matched-handoff-x3634):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Relay — Matched Handoff</title>
<style>
:root {
color-scheme: dark;
--cyan: #34d8ee;
--coral: #ff716c;
--gold: #ffd166;
--panel: rgba(8, 17, 30, 0.84);
--border: rgba(163, 211, 255, 0.2);
--muted: #9eb1c8;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #08111f;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
position: absolute;
top: 20px;
left: 20px;
width: min(340px, calc(100vw - 40px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(12px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 31px);
line-height: 1.05;
letter-spacing: -0.04em;
}
.subtitle {
margin: 8px 0 16px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.rule-heading {
display: inline-flex;
align-items: center;
gap: 7px;
margin-bottom: 9px;
padding: 6px 9px;
border: 1px solid rgba(255, 209, 102, 0.35);
border-radius: 7px;
background: rgba(255, 209, 102, 0.09);
color: var(--gold);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
ol {
margin: 0;
padding-left: 20px;
color: #dbe8f8;
font-size: 12px;
line-height: 1.65;
}
li + li { margin-top: 3px; }
.disclaimer {
margin: 12px 0 0;
padding-top: 11px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: #8297b1;
font-size: 10px;
line-height: 1.45;
}
#status-wrap {
position: absolute;
left: 50%;
bottom: 22px;
width: min(620px, calc(100vw - 40px));
transform: translateX(-50%);
padding: 11px 14px 12px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(8, 17, 30, 0.82);
box-shadow: 0 12px 38px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(10px);
}
#status {
display: flex;
justify-content: space-between;
gap: 12px;
color: #eaf3ff;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#possession {
flex: 0 0 auto;
color: var(--gold);
}
#progress {
height: 3px;
margin-top: 9px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
#progress-fill {
width: 0;
height: 100%;
background: linear-gradient(90deg, var(--cyan), var(--gold), var(--coral));
box-shadow: 0 0 12px rgba(255, 209, 102, 0.5);
}
#controls {
position: absolute;
right: 20px;
bottom: 20px;
display: flex;
align-items: center;
gap: 8px;
pointer-events: auto;
}
.control-button,
.control-copy {
border: 1px solid var(--border);
border-radius: 9px;
background: rgba(8, 17, 30, 0.82);
color: #c8d8ea;
font: inherit;
font-size: 10px;
}
.control-button {
min-width: 70px;
padding: 9px 11px;
cursor: pointer;
}
.control-button:hover {
border-color: rgba(52, 216, 238, 0.55);
color: white;
}
.control-copy {
padding: 9px 11px;
color: #8297b1;
}
.runner-label,
#baton-label {
position: absolute;
top: 0;
left: 0;
white-space: nowrap;
transform: translate(-50%, -100%);
will-change: transform;
}
.runner-label {
padding: 6px 9px;
border: 1px solid currentColor;
border-radius: 7px;
background: rgba(7, 14, 25, 0.78);
box-shadow: 0 7px 18px rgba(0, 0, 0, 0.24);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
}
#label-a { color: var(--cyan); }
#label-b { color: var(--coral); }
#baton-label {
padding: 4px 7px;
border-radius: 5px;
background: var(--gold);
color: #382b0e;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.12em;
box-shadow: 0 0 20px rgba(255, 209, 102, 0.28);
}
@media (max-width: 760px) {
.panel {
top: 10px;
left: 10px;
width: 270px;
padding: 13px;
}
.subtitle, ol, .disclaimer { display: none; }
.rule-heading { margin-bottom: 0; }
#controls { right: 10px; bottom: 10px; }
.control-copy { display: none; }
#status-wrap { bottom: 62px; }
#status { font-size: 9px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Animated voxel relay handoff scene"></canvas>
<div id="hud">
<section class="panel">
<div class="eyebrow">Voxel Motion Study</div>
<h1>Matched Relay Handoff</h1>
<p class="subtitle">
Two stylized runners exchange a baton at a synchronized center-line contact.
</p>
<div class="rule-heading">Simplified Rules</div>
<ol>
<li>Enter the marked fictional relay zone.</li>
<li>Approach the center line with matched timing.</li>
<li>Inner hands must touch for a valid transfer.</li>
<li>Speed eases in and out; possession alternates.</li>
</ol>
<p class="disclaimer">
Simplified training visualization only. This is a fictional rule set,
not an official athletics rule set.
</p>
</section>
<div id="label-a" class="runner-label">RUNNER CYAN</div>
<div id="label-b" class="runner-label">RUNNER CORAL</div>
<div id="baton-label">BATON</div>
<div id="status-wrap">
<div id="status">
<span id="phase">Approaching the zone</span>
<span id="possession">CYAN HAS THE BATON</span>
</div>
<div id="progress"><div id="progress-fill"></div></div>
</div>
<div id="controls">
<button id="pause" class="control-button" type="button">Pause</button>
<button id="restart" class="control-button" type="button">Restart</button>
<div class="control-copy">Drag to orbit · Wheel to zoom · Space to pause</div>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const phaseEl = document.querySelector("#phase");
const possessionEl = document.querySelector("#possession");
const progressEl = document.querySelector("#progress-fill");
const pauseButton = document.querySelector("#pause");
const restartButton = document.querySelector("#restart");
const labelA = document.querySelector("#label-a");
const labelB = document.querySelector("#label-b");
const batonLabel = document.querySelector("#baton-label");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08111f);
scene.fog = new THREE.Fog(0x08111f, 19, 39);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
// Custom camera controls: no addons are used.
const cameraTarget = new THREE.Vector3(0, 1.05, 0);
let azimuth = 0.62;
let polar = 1.08;
let radius = 15.5;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
const sinPolar = Math.sin(polar);
camera.position.set(
cameraTarget.x + radius * sinPolar * Math.sin(azimuth),
cameraTarget.y + radius * Math.cos(polar),
cameraTarget.z + radius * sinPolar * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
azimuth -= dx * 0.006;
polar = THREE.MathUtils.clamp(polar - dy * 0.005, 0.48, 1.38);
updateCamera();
});
const stopDragging = () => { dragging = false; };
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radius = THREE.MathUtils.clamp(
radius * Math.exp(event.deltaY * 0.001),
9,
24
);
updateCamera();
},
{ passive: false }
);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.76,
metalness: 0.02,
flatShading: true,
...options
});
}
function addBox(parent, w, h, d, mat, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Lighting.
scene.add(new THREE.HemisphereLight(0xaddfff, 0x111826, 1.7));
const sun = new THREE.DirectionalLight(0xfff0d5, 3.1);
sun.position.set(7, 13, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 11;
sun.shadow.camera.bottom = -11;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0008;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x4cc9ff, 1.25);
rimLight.position.set(-9, 5, -8);
scene.add(rimLight);
// Ground and grid.
const groundMat = material(0x0b1826);
addBox(scene, 34, 0.12, 20, groundMat, 0, -0.09, -1);
const grid = new THREE.GridHelper(34, 34, 0x284158, 0x172a3b);
grid.position.set(0, -0.018, -1);
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
// Fictional marked zone.
const zoneFloorMat = material(0x102a35, {
transparent: true,
opacity: 0.96,
emissive: 0x06141b,
emissiveIntensity: 0.4
});
addBox(scene, 9.2, 0.045, 4.7, zoneFloorMat, 0, 0, 0);
const zoneLineMat = material(0xffd166, {
emissive: 0xa66f00,
emissiveIntensity: 0.85
});
for (let x = -4.25; x <= 4.25; x += 0.68) {
addBox(scene, 0.43, 0.045, 0.09, zoneLineMat, x, 0.055, -2.22);
addBox(scene, 0.43, 0.045, 0.09, zoneLineMat, x, 0.055, 2.22);
}
for (let z = -1.82; z <= 1.82; z += 0.68) {
addBox(scene, 0.09, 0.045, 0.43, zoneLineMat, -4.48, 0.055, z);
addBox(scene, 0.09, 0.045, 0.43, zoneLineMat, 4.48, 0.055, z);
}
// Voxel contact ring, centered under the touching hands.
const contactRingMat = material(0x58e6f3, {
emissive: 0x0e7180,
emissiveIntensity: 0.8
});
for (let i = 0; i < 28; i++) {
const angle = (i / 28) * Math.PI * 2;
const segment = addBox(
scene,
0.25,
0.045,
0.09,
contactRingMat,
Math.cos(angle) * 1.13,
0.064,
0.24 + Math.sin(angle) * 1.13
);
segment.rotation.y = -(angle + Math.PI / 2);
}
const centerMat = material(0xffd166, {
emissive: 0x8b5a00,
emissiveIntensity: 1.1
});
for (let z = -1.45; z <= 1.45; z += 0.58) {
addBox(scene, 0.07, 0.045, 0.34, centerMat, 0, 0.057, z);
}
// Direction markers.
const arrowMat = material(0x507a91);
for (const x of [-3.3, 3.3]) {
addBox(scene, 0.14, 0.035, 0.68, arrowMat, x, 0.045, 1.58);
addBox(scene, 0.14, 0.035, 0.38, arrowMat, x - 0.11, 0.045, 1.87);
addBox(scene, 0.14, 0.035, 0.38, arrowMat, x + 0.11, 0.045, 1.87);
}
function createRunner(theme) {
const root = new THREE.Group();
const shirt = material(theme.shirt);
const shorts = material(theme.shorts);
const skin = material(theme.skin);
const shoe = material(theme.shoe);
const hair = material(theme.hair);
const white = material(0xf4fbff);
const dark = material(0x142333);
// Hips and torso.
addBox(root, 0.54, 0.27, 0.62, shorts, 0, 1.08, 0);
addBox(root, 0.58, 0.66, 0.62, shirt, 0, 1.48, 0);
addBox(root, 0.035, 0.13, 0.51, white, 0.307, 1.49, 0);
addBox(root, 0.05, 0.08, 0.18, dark, 0.325, 1.49, 0);
// Neck and head.
addBox(root, 0.22, 0.18, 0.24, skin, 0, 1.91, 0);
addBox(root, 0.47, 0.45, 0.45, skin, 0, 2.15, 0);
addBox(root, 0.49, 0.13, 0.47, hair, -0.015, 2.38, 0);
addBox(root, 0.18, 0.11, 0.47, hair, -0.16, 2.28, 0);
// Face points toward local +X.
addBox(root, 0.035, 0.055, 0.055, dark, 0.241, 2.19, 0.105);
addBox(root, 0.035, 0.055, 0.055, dark, 0.241, 2.19, -0.105);
addBox(root, 0.06, 0.07, 0.09, skin, 0.255, 2.08, 0);
function createLeg(z) {
const hip = new THREE.Group();
hip.position.set(0, 1.04, z);
root.add(hip);
addBox(hip, 0.21, 0.44, 0.21, skin, 0, -0.22, 0);
const knee = new THREE.Group();
knee.position.y = -0.44;
hip.add(knee);
addBox(knee, 0.19, 0.42, 0.19, skin, 0, -0.21, 0);
addBox(knee, 0.34, 0.15, 0.23, shoe, 0.07, -0.44, 0);
return { hip, knee };
}
function createArm(z, handoff) {
const shoulder = new THREE.Group();
shoulder.position.set(0, 1.68, z);
root.add(shoulder);
addBox(shoulder, 0.19, 0.32, 0.19, shirt, 0, -0.16, 0);
const elbow = new THREE.Group();
elbow.position.y = -0.32;
shoulder.add(elbow);
addBox(elbow, 0.17, 0.30, 0.17, skin, 0, -0.15, 0);
const handMarker = new THREE.Object3D();
handMarker.position.y = -0.32;
elbow.add(handMarker);
addBox(handMarker, 0.19, 0.18, 0.19, skin);
return { shoulder, elbow, handMarker, handoff };
}
const legs = [
createLeg(0.18),
createLeg(-0.18)
];
// Both handoff hands use the same world Z lane so they can meet exactly.
const handoffArm = createArm(0.24, true);
const runningArm = createArm(-0.24, false);
const labelAnchor = new THREE.Object3D();
labelAnchor.position.set(0, 2.76, 0);
root.add(labelAnchor);
scene.add(root);
return {
root,
legs,
handoffArm,
runningArm,
labelAnchor
};
}
const runnerA = createRunner({
shirt: 0x20c9df,
shorts: 0x0b6679,
skin: 0xd99a70,
shoe: 0xe8fbff,
hair: 0x172332
});
const runnerB = createRunner({
shirt: 0xff675f,
shorts: 0x8f2f3a,
skin: 0x8b583c,
shoe: 0xffe6d7,
hair: 0x201713
});
// Freestanding voxel sign.
const signGroup = new THREE.Group();
signGroup.position.set(0, 0, -4.15);
scene.add(signGroup);
const signPostMat = material(0x294052);
addBox(signGroup, 0.17, 3.05, 0.17, signPostMat, -2.35, 1.52, 0);
addBox(signGroup, 0.17, 3.05, 0.17, signPostMat, 2.35, 1.52, 0);
const boardMat = material(0x14283b, {
emissive: 0x07131e,
emissiveIntensity: 0.7
});
addBox(signGroup, 6.25, 1.75, 0.18, boardMat, 0, 2.75, 0);
const signCanvas = document.createElement("canvas");
signCanvas.width = 1024;
signCanvas.height = 288;
const ctx = signCanvas.getContext("2d");
ctx.fillStyle = "#102438";
ctx.fillRect(0, 0, 1024, 288);
ctx.strokeStyle = "#ffd166";
ctx.lineWidth = 12;
ctx.strokeRect(8, 8, 1008, 272);
ctx.textAlign = "center";
ctx.fillStyle = "#f2fbff";
ctx.font = "900 57px system-ui, sans-serif";
ctx.fillText("FICTIONAL RELAY ZONE", 512, 104);
ctx.fillStyle = "#ffd166";
ctx.font = "800 30px system-ui, sans-serif";
ctx.fillText("SIMPLIFIED RULES • HANDS MUST MEET", 512, 170);
ctx.fillStyle = "#9eb1c8";
ctx.font = "500 23px system-ui, sans-serif";
ctx.fillText("Match the approach • transfer at contact • ease the speed", 512, 226);
const signTexture = new THREE.CanvasTexture(signCanvas);
signTexture.colorSpace = THREE.SRGBColorSpace;
signTexture.minFilter = THREE.LinearFilter;
const signFace = new THREE.Mesh(
new THREE.PlaneGeometry(6.05, 1.57),
new THREE.MeshBasicMaterial({ map: signTexture })
);
signFace.position.set(0, 2.75, 0.101);
signGroup.add(signFace);
// Background voxel pylons.
const buildingMats = [
material(0x102337),
material(0x142b40),
material(0x183246)
];
for (let i = 0; i < 11; i++) {
const h = 1.4 + ((i * 7) % 5) * 0.55;
const w = 0.8 + ((i * 3) % 4) * 0.18;
addBox(
scene,
w,
h,
1.0,
buildingMats[i % buildingMats.length],
-11 + i * 2.15,
h / 2 - 0.03,
-7.7 - (i % 2) * 0.35
);
if (i % 2 === 0) {
addBox(
scene,
0.12,
0.12,
0.12,
i % 4 === 0 ? zoneLineMat : contactRingMat,
-11 + i * 2.15,
h + 0.05,
-7.05 - (i % 2) * 0.35
);
}
}
// Baton: deliberately rectangular to retain the voxel language.
const baton = new THREE.Group();
scene.add(baton);
const batonCoreMat = material(0xffa62b, {
emissive: 0xa44700,
emissiveIntensity: 0.85,
roughness: 0.55
});
const batonCapMat = material(0xfff2bd, {
emissive: 0x8c6a00,
emissiveIntensity: 0.35
});
addBox(baton, 0.12, 0.12, 0.68, batonCoreMat);
addBox(baton, 0.16, 0.16, 0.12, batonCapMat, 0, 0, -0.35);
addBox(baton, 0.16, 0.16, 0.12, batonCapMat, 0, 0, 0.35);
const batonLight = new THREE.PointLight(0xffb84d, 1.1, 2.3);
baton.add(batonLight);
// Transfer particles.
const particles = [];
function spawnTransferParticles() {
const origin = new THREE.Vector3(0, 1.68, 0.24);
for (let i = 0; i < 18; i++) {
const particleMat = new THREE.MeshBasicMaterial({
color: i % 3 === 0 ? 0xffffff : 0xffd166,
transparent: true,
opacity: 1
});
const particle = new THREE.Mesh(
new THREE.BoxGeometry(0.075, 0.075, 0.075),
particleMat
);
particle.position.copy(origin);
particle.castShadow = false;
const angle = (i / 18) * Math.PI * 2;
const speed = 0.9 + (i % 5) * 0.16;
particle.userData.velocity = new THREE.Vector3(
Math.cos(angle) * speed,
0.75 + (i % 4) * 0.14,
Math.sin(angle) * speed
);
particle.userData.life = 0.75 + (i % 3) * 0.12;
particle.userData.maxLife = particle.userData.life;
scene.add(particle);
particles.push(particle);
}
}
// Animation state.
const CYCLE = 9.0;
const CONTACT_DISTANCE = 0.64;
const clock = new THREE.Clock();
let elapsed = 0;
let paused = false;
let holder = "a";
let lastCycle = -1;
let transferDone = false;
let transferFlashUntil = -1;
let gaitPhase = 0;
let previousDistance = CONTACT_DISTANCE;
const clamp01 = (x) => Math.max(0, Math.min(1, x));
const lerp = (a, b, t) => a + (b - a) * t;
// Fifth-order smoothstep: velocity and acceleration are continuous at phase ends.
function smoother(t) {
t = clamp01(t);
return t * t * t * (t * (t * 6 - 15) + 10);
}
function getMotion(localTime) {
let distance;
let leftRotation;
let rightRotation;
let handoffExtension;
if (localTime < 0.75) {
distance = CONTACT_DISTANCE;
leftRotation = 0;
rightRotation = Math.PI;
handoffExtension = 1;
} else if (localTime < 1.85) {
const u = smoother((localTime - 0.75) / 1.10);
distance = lerp(CONTACT_DISTANCE, 1.55, u);
leftRotation = Math.PI * u;
rightRotation = Math.PI * (1 - u);
handoffExtension = 1 - u;
} else if (localTime < 4.15) {
const u = smoother((localTime - 1.85) / 2.30);
distance = lerp(1.55, 3.8, u);
leftRotation = Math.PI;
rightRotation = 0;
handoffExtension = 0;
} else if (localTime < 5.05) {
const u = smoother((localTime - 4.15) / 0.90);
distance = 3.8;
leftRotation = Math.PI * (1 - u);
rightRotation = Math.PI * u;
handoffExtension = 0;
} else if (localTime < 8.35) {
const u = smoother((localTime - 5.05) / 3.30);
distance = lerp(3.8, CONTACT_DISTANCE, u);
leftRotation = 0;
rightRotation = Math.PI;
handoffExtension = smoother(u);
} else {
distance = CONTACT_DISTANCE;
leftRotation = 0;
rightRotation = Math.PI;
handoffExtension = 1;
}
return {
distance,
leftRotation,
rightRotation,
handoffExtension
};
}
function updateRunner(runner, x, rotation, speed, dt, extension) {
runner.root.position.x = x;
runner.root.rotation.y = rotation;
const amplitude = Math.min(1, speed / 3.1);
const bob = Math.abs(Math.sin(gaitPhase * 2)) * 0.045 * amplitude;
runner.root.position.y = bob;
const leftSwing = Math.sin(gaitPhase);
const rightSwing = Math.sin(gaitPhase + Math.PI);
runner.legs[0].hip.rotation.z = leftSwing * 0.72 * amplitude;
runner.legs[1].hip.rotation.z = rightSwing * 0.72 * amplitude;
runner.legs[0].knee.rotation.z =
-Math.max(0, -leftSwing) * 0.92 * amplitude;
runner.legs[1].knee.rotation.z =
-Math.max(0, -rightSwing) * 0.92 * amplitude;
// The free arm counter-swings.
runner.runningArm.shoulder.rotation.z =
-Math.sin(gaitPhase) * 0.54 * amplitude;
runner.runningArm.elbow.rotation.z =
-0.18 - 0.22 * amplitude * Math.max(0, Math.sin(gaitPhase));
// At full extension the arm is exactly horizontal. The same local
// geometry is used by both runners, guaranteeing a matched hand contact.
runner.handoffArm.shoulder.rotation.z =
extension * Math.PI * 0.5 +
(1 - extension) * Math.sin(gaitPhase + 0.4) * 0.22;
runner.handoffArm.elbow.rotation.z =
(1 - extension) * (-0.25 + Math.sin(gaitPhase) * 0.12);
}
function updateParticles(dt) {
for (let i = particles.length - 1; i >= 0; i--) {
const p = particles[i];
p.userData.life -= dt;
if (p.userData.life <= 0) {
scene.remove(p);
p.geometry.dispose();
p.material.dispose();
particles.splice(i, 1);
continue;
}
p.userData.velocity.y -= 2.1 * dt;
p.position.addScaledVector(p.userData.velocity, dt);
p.rotation.x += dt * 4;
p.rotation.y += dt * 5;
const lifeRatio = p.userData.life / p.userData.maxLife;
p.material.opacity = lifeRatio;
p.scale.setScalar(0.55 + lifeRatio * 0.7);
}
}
function holderName(id) {
return id === "a" ? "CYAN" : "CORAL";
}
function updateStatus(localTime) {
let phase;
if (localTime < 0.75) {
phase = "Contact window • synchronized handoff";
} else if (localTime < 1.85) {
phase = "Easing apart • runners turning";
} else if (localTime < 4.15) {
phase = "Returning to outer marks";
} else if (localTime < 5.05) {
phase = "Turning into the marked zone";
} else if (localTime < 8.35) {
phase = "Approaching • speed eases in";
} else {
phase = "Hands meet • transfer window";
}
if (elapsed < transferFlashUntil) {
const previousHolder = holder === "a" ? "b" : "a";
phase = `Baton transferred • ${holderName(previousHolder)} → ${holderName(holder)}`;
}
phaseEl.textContent = phase;
possessionEl.textContent = `${holderName(holder)} HAS THE BATON`;
progressEl.style.width = `${(localTime / CYCLE) * 100}%`;
}
const projected = new THREE.Vector3();
function projectLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
element.style.transform =
`translate(${x}px, ${y}px) translate(-50%, -100%)`;
}
function setPaused(value) {
paused = value;
pauseButton.textContent = paused ? "Resume" : "Pause";
}
pauseButton.addEventListener("click", () => setPaused(!paused));
function restart() {
elapsed = 0;
holder = "a";
lastCycle = -1;
transferDone = false;
transferFlashUntil = -1;
previousDistance = CONTACT_DISTANCE;
gaitPhase = 0;
for (const p of particles) {
scene.remove(p);
p.geometry.dispose();
p.material.dispose();
}
particles.length = 0;
}
restartButton.addEventListener("click", restart);
addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
setPaused(!paused);
}
if (event.key.toLowerCase() === "r") {
restart();
}
if (event.key.toLowerCase() === "c") {
azimuth = 0.62;
polar = 1.08;
radius = 15.5;
updateCamera();
}
});
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateCamera();
function animate() {
const rawDt = Math.min(clock.getDelta(), 0.05);
const dt = paused ? 0 : rawDt;
elapsed += dt;
const cycleIndex = Math.floor(elapsed / CYCLE);
const localTime = elapsed % CYCLE;
if (cycleIndex !== lastCycle) {
lastCycle = cycleIndex;
transferDone = false;
}
const motion = getMotion(localTime);
const speed = dt > 0
? Math.abs(motion.distance - previousDistance) / dt
: 0;
previousDistance = motion.distance;
gaitPhase += speed * 2.85 * dt;
updateRunner(
runnerA,
-motion.distance,
motion.leftRotation,
speed,
dt,
motion.handoffExtension
);
updateRunner(
runnerB,
motion.distance,
motion.rightRotation,
speed,
dt,
motion.handoffExtension
);
// The transfer occurs while both hands are exactly coincident.
if (localTime >= 8.55 && !transferDone) {
transferDone = true;
holder = holder === "a" ? "b" : "a";
transferFlashUntil = elapsed + 1.05;
spawnTransferParticles();
}
scene.updateMatrixWorld(true);
const activeRunner = holder === "a" ? runnerA : runnerB;
const handPosition = new THREE.Vector3();
activeRunner.handoffArm.handMarker.getWorldPosition(handPosition);
baton.position.copy(handPosition);
// A tiny pulse makes the possession change easier to read.
const pulse = 1 + Math.max(0, 1 - (elapsed - (transferFlashUntil - 1.05)) * 3) * 0.16;
baton.scale.setScalar(pulse);
updateParticles(dt);
updateStatus(localTime);
const anchorA = runnerA.labelAnchor.getWorldPosition(new THREE.Vector3());
const anchorB = runnerB.labelAnchor.getWorldPosition(new THREE.Vector3());
const batonAnchor = baton.position.clone().add(new THREE.Vector3(0, 0.24, 0));
projectLabel(labelA, anchorA);
projectLabel(labelB, anchorB);
projectLabel(batonLabel, batonAnchor);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```