Prompt specification
Build or customize the Three.js 3D scene "Opposite changes returning to zero" 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: Opposite changes returning to zero
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/opposite-changes-returning-to-zero-x1702
Scene Description Prompt:
Use Three.js from cdnjs to animate paired positive and negative moves on the same line; show cancellation of signed changes without confusing a negative position with backward time. Deliver one self-contained HTML file with stylized solid shapes.
Working Three.js Starter (opposite-changes-returning-to-zero-x1702):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Paired Signed Moves</title>
<style>
:root {
--ink: #17253b;
--muted: #657086;
--paper: #f3eee5;
--panel: rgba(255, 252, 247, 0.92);
--positive: #269b68;
--positive-soft: #d9f2e5;
--negative: #e8645c;
--negative-soft: #fbe0dc;
--token: #f3b63f;
--time: #2e83bd;
--net: #7255aa;
--line: rgba(23, 37, 59, 0.13);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, input { font: inherit; }
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active { cursor: grabbing; }
.brand {
position: fixed;
z-index: 3;
top: 24px;
left: 26px;
max-width: 420px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--time);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.brand p {
max-width: 370px;
margin: 11px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.legend {
position: fixed;
z-index: 3;
top: 24px;
right: 24px;
width: 205px;
padding: 13px 14px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(255, 252, 247, 0.72);
box-shadow: 0 12px 35px rgba(34, 44, 61, 0.06);
backdrop-filter: blur(10px);
pointer-events: none;
}
.legend-title {
margin-bottom: 9px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 7px;
color: #344057;
font-size: 12px;
font-weight: 650;
}
.swatch {
width: 18px;
height: 8px;
flex: 0 0 auto;
border-radius: 99px;
}
.swatch.token {
width: 13px;
height: 13px;
border-radius: 4px;
background: var(--token);
transform: rotate(8deg);
}
.swatch.time {
width: 13px;
height: 13px;
border-radius: 50%;
background: var(--time);
}
.story {
position: fixed;
z-index: 4;
left: 50%;
bottom: 18px;
width: min(790px, calc(100% - 32px));
padding: 15px 17px 14px;
border: 1px solid rgba(23, 37, 59, 0.13);
border-radius: 21px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(29, 39, 54, 0.13);
backdrop-filter: blur(16px);
transform: translateX(-50%);
}
.story-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
}
.readouts {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 7px;
}
.badge {
display: inline-flex;
align-items: center;
min-height: 27px;
padding: 5px 9px;
border-radius: 9px;
background: #eef1f5;
color: #425068;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.055em;
text-transform: uppercase;
white-space: nowrap;
}
.badge.position {
background: #fff1cc;
color: #805b10;
}
.badge.time {
background: #dcecf8;
color: #236b99;
}
.controls {
display: flex;
align-items: center;
gap: 7px;
flex: 0 0 auto;
}
.controls button {
height: 32px;
padding: 0 11px;
border: 1px solid rgba(23, 37, 59, 0.16);
border-radius: 9px;
background: #fff;
color: var(--ink);
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: transform 120ms ease, background 120ms ease;
}
.controls button:hover {
background: #f0f4f7;
transform: translateY(-1px);
}
.controls button.primary {
border-color: var(--ink);
background: var(--ink);
color: #fff;
}
.speed {
display: flex;
align-items: center;
gap: 5px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
}
.speed input {
width: 68px;
accent-color: var(--time);
}
.motion-grid {
display: grid;
grid-template-columns: 1fr auto 1fr;
align-items: center;
gap: 12px;
margin-top: 12px;
}
.motion-card {
min-width: 0;
padding: 9px 11px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.62);
}
.motion-card span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.motion-card strong {
display: block;
overflow: hidden;
color: var(--ink);
font-size: 15px;
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
white-space: nowrap;
}
.motion-card.space { border-left: 4px solid var(--token); }
.motion-card.clock { border-left: 4px solid var(--time); }
.rail-note {
color: #7c8494;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
text-align: center;
text-transform: uppercase;
}
.observation {
min-height: 35px;
margin-top: 10px;
color: #354157;
font-size: 13px;
line-height: 1.35;
}
.equations {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 11px;
}
.equation-box {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
min-width: 0;
padding: 9px 11px;
border-radius: 12px;
background: #f2f3f4;
}
.equation-label {
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.equation {
white-space: nowrap;
font-size: 18px;
font-weight: 850;
font-variant-numeric: tabular-nums;
}
.term {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 36px;
height: 28px;
padding: 0 7px;
border-radius: 8px;
transition: color 180ms ease, background 180ms ease, transform 180ms ease;
}
.term.positive {
background: var(--positive-soft);
color: #16764b;
}
.term.negative {
background: var(--negative-soft);
color: #b4423b;
}
.term.net {
background: #e8e0f4;
color: #65469d;
}
.term.active {
transform: translateY(-2px);
box-shadow: 0 5px 12px rgba(33, 43, 58, 0.1);
}
.hint {
position: fixed;
z-index: 3;
right: 25px;
bottom: 255px;
color: rgba(23, 37, 59, 0.48);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
pointer-events: none;
}
@media (max-width: 900px) {
.legend { display: none; }
.hint { display: none; }
}
@media (max-width: 680px) {
.brand {
top: 14px;
left: 16px;
max-width: calc(100% - 32px);
}
.brand p { display: none; }
h1 { font-size: 27px; }
.story {
bottom: 9px;
width: calc(100% - 18px);
padding: 12px;
border-radius: 17px;
}
.story-top {
align-items: flex-start;
flex-direction: column;
gap: 8px;
}
.controls { width: 100%; }
.speed { margin-left: auto; }
.speed input { width: 55px; }
.motion-grid {
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.rail-note { display: none; }
.motion-card { padding: 7px 9px; }
.motion-card strong { font-size: 13px; }
.observation {
min-height: 48px;
font-size: 12px;
}
.equations {
grid-template-columns: 1fr;
gap: 6px;
}
.equation-box { padding: 7px 9px; }
.equation { font-size: 16px; }
}
</style>
</head>
<body>
<div id="stage" aria-label="Animated three-dimensional demonstration of signed moves"></div>
<header class="brand">
<div class="eyebrow">Space and time use different rails</div>
<h1>Paired signed moves</h1>
<p>
A +4 move and a −4 move share one position line. Their signed changes cancel,
even though the final position remains negative.
</p>
</header>
<aside class="legend" aria-hidden="true">
<div class="legend-title">Scene key</div>
<div class="legend-row"><span class="swatch token"></span>Position token</div>
<div class="legend-row"><span class="swatch" style="background:var(--positive)"></span>Positive change</div>
<div class="legend-row"><span class="swatch" style="background:var(--negative)"></span>Negative change</div>
<div class="legend-row"><span class="swatch time"></span>Forward time cursor</div>
</aside>
<section class="story" aria-label="Animation explanation">
<div class="story-top">
<div class="readouts">
<span id="phaseTag" class="badge">1 / Positive move</span>
<span id="positionValue" class="badge position">Position x = −2.0</span>
<span id="timeValue" class="badge time">Time → step 1 / 4</span>
</div>
<div class="controls">
<button id="pauseBtn" class="primary" type="button">Pause</button>
<button id="replayBtn" type="button">Replay</button>
<button id="viewBtn" type="button">Reset view</button>
<label class="speed" for="speed">
Speed
<input id="speed" type="range" min="0.5" max="1.5" step="0.25" value="1">
</label>
</div>
</div>
<div class="motion-grid">
<div class="motion-card space">
<span>Space / position</span>
<strong id="spaceMotion">−2 → +2</strong>
</div>
<div class="rail-note">Separate<br>rails</div>
<div class="motion-card clock">
<span>Time / order</span>
<strong id="clockMotion">always forward →</strong>
</div>
</div>
<div id="observation" class="observation" aria-live="polite"></div>
<div class="equations">
<div class="equation-box">
<span class="equation-label">Signed change</span>
<div class="equation" aria-label="Plus four plus negative four equals zero">
<span id="posTerm" class="term positive active">+4</span>
<span>+</span>
<span id="negTerm" class="term negative">−4</span>
<span>=</span>
<span id="netTerm" class="term net">0</span>
</div>
</div>
<div class="equation-box">
<span class="equation-label">Final position</span>
<div class="equation" aria-label="Negative two plus four minus four equals negative two">
<span class="term">−2</span>
<span>+</span>
<span class="term positive">4</span>
<span>−</span>
<span class="term negative">4</span>
<span>=</span>
<span class="term">−2</span>
</div>
</div>
</div>
</section>
<div class="hint">Drag to orbit · Scroll to zoom</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const COLORS = {
ink: 0x17253b,
rail: 0x5c687c,
positive: 0x269b68,
negative: 0xe8645c,
token: 0xf3b63f,
time: 0x2e83bd,
net: 0x7255aa,
paper: 0xf3eee5,
floorLeft: 0xf2dfd7,
floorRight: 0xdfeee4
};
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 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.08;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.paper);
scene.fog = new THREE.Fog(COLORS.paper, 19, 32);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.05, 0);
let cameraAzimuth = 0;
let cameraPolar = 1.08;
let cameraZoom = 1;
const hemi = new THREE.HemisphereLight(0xffffff, 0x8793a5, 2.15);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff6e8, 3.4);
key.position.set(-5, 9, 7);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -10;
key.shadow.camera.right = 10;
key.shadow.camera.top = 8;
key.shadow.camera.bottom = -6;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 28;
key.shadow.bias = -0.0004;
scene.add(key);
const fill = new THREE.DirectionalLight(0xbdd7ee, 1.15);
fill.position.set(7, 4, -5);
scene.add(fill);
const standard = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.68,
metalness: 0.02,
...options
});
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 20),
standard(0xf1ece3, { roughness: 0.98 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.05;
floor.receiveShadow = true;
scene.add(floor);
const leftZone = new THREE.Mesh(
new THREE.BoxGeometry(5.55, 0.045, 2.9),
standard(COLORS.floorLeft, { roughness: 0.95 })
);
leftZone.position.set(-2.78, -1.015, 0.18);
leftZone.receiveShadow = true;
scene.add(leftZone);
const rightZone = new THREE.Mesh(
new THREE.BoxGeometry(5.55, 0.045, 2.9),
standard(COLORS.floorRight, { roughness: 0.95 })
);
rightZone.position.set(2.78, -1.015, 0.18);
rightZone.receiveShadow = true;
scene.add(rightZone);
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeLabel(
text,
{
worldWidth = 1.4,
color = "#17253b",
background = null,
border = null,
fontSize = 42
} = {}
) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
if (background) {
roundedRect(ctx, 9, 12, 494, 104, 25);
ctx.fillStyle = background;
ctx.fill();
if (border) {
ctx.lineWidth = 4;
ctx.strokeStyle = border;
ctx.stroke();
}
}
ctx.fillStyle = color;
ctx.font = `800 ${fontSize}px Inter, system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 66, 470);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(worldWidth, worldWidth * 0.25, 1);
sprite.renderOrder = 20;
return sprite;
}
function setLabelOpacity(sprite, opacity) {
sprite.visible = opacity > 0.01;
sprite.material.opacity = opacity;
}
function horizontalCylinder(length, radius, material, segments = 24) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, segments),
material
);
mesh.rotation.z = Math.PI / 2;
return mesh;
}
// Lower rail: this is a spatial coordinate line, not a clock.
const positionRail = horizontalCylinder(
10.75,
0.055,
standard(COLORS.ink, { roughness: 0.5 })
);
positionRail.position.set(0, -0.08, 0);
positionRail.castShadow = true;
scene.add(positionRail);
const axisHeadMaterial = standard(COLORS.ink, { roughness: 0.5 });
const rightAxisHead = new THREE.Mesh(
new THREE.ConeGeometry(0.19, 0.48, 24),
axisHeadMaterial
);
rightAxisHead.rotation.z = -Math.PI / 2;
rightAxisHead.position.set(5.48, -0.08, 0);
rightAxisHead.castShadow = true;
scene.add(rightAxisHead);
const leftAxisHead = new THREE.Mesh(
new THREE.ConeGeometry(0.19, 0.48, 24),
axisHeadMaterial
);
leftAxisHead.rotation.z = Math.PI / 2;
leftAxisHead.position.set(-5.48, -0.08, 0);
leftAxisHead.castShadow = true;
scene.add(leftAxisHead);
const tickMaterial = standard(0x445168, { roughness: 0.72 });
const originTickMaterial = standard(COLORS.token, { roughness: 0.55 });
for (const value of [-4, -2, 0, 2, 4]) {
const tick = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 0.29, 0.17),
value === 0 ? originTickMaterial : tickMaterial
);
tick.position.set(value, -0.09, 0);
tick.castShadow = true;
scene.add(tick);
const text = value === 0 ? "0" : value < 0 ? `−${Math.abs(value)}` : `${value}`;
const label = makeLabel(text, {
worldWidth: value === 0 ? 0.43 : 0.62,
color: value === 0 ? "#8a6116" : "#48556b",
fontSize: 46
});
label.position.set(value, -0.53, 0.05);
scene.add(label);
}
const negativeZoneLabel = makeLabel("x < 0", {
worldWidth: 1.35,
color: "#a35049",
background: "rgba(255,248,244,0.90)",
border: "#e6b7af",
fontSize: 44
});
negativeZoneLabel.position.set(-4.12, 0.83, -0.55);
scene.add(negativeZoneLabel);
const positiveZoneLabel = makeLabel("x > 0", {
worldWidth: 1.35,
color: "#267450",
background: "rgba(246,252,248,0.92)",
border: "#a9d6bf",
fontSize: 44
});
positiveZoneLabel.position.set(4.12, 0.83, -0.55);
scene.add(positiveZoneLabel);
const positionTitle = makeLabel("SPACE / POSITION", {
worldWidth: 2.95,
color: "#46536a",
fontSize: 39
});
positionTitle.position.set(0, -0.73, 0.65);
scene.add(positionTitle);
// Upper rail: time always advances from left to right.
const timeRail = horizontalCylinder(
10.45,
0.055,
standard(COLORS.rail, { roughness: 0.58 })
);
timeRail.position.set(0, 2.9, -1.18);
timeRail.castShadow = true;
scene.add(timeRail);
const timeArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.19, 0.48, 24),
standard(COLORS.rail, { roughness: 0.55 })
);
timeArrow.rotation.z = -Math.PI / 2;
timeArrow.position.set(5.34, 2.9, -1.18);
scene.add(timeArrow);
for (const x of [-4.82, 4.82]) {
const support = new THREE.Mesh(
new THREE.CylinderGeometry(0.028, 0.028, 3.88, 12),
standard(0xa9afb8, { roughness: 0.85 })
);
support.position.set(x, 0.91, -1.18);
support.castShadow = true;
scene.add(support);
}
const timeTitle = makeLabel("TIME / CHRONOLOGY →", {
worldWidth: 3.85,
color: "#ffffff",
background: "#17253b",
border: "#17253b",
fontSize: 38
});
timeTitle.position.set(0, 3.66, -1.18);
scene.add(timeTitle);
const timeNodeXs = [-4.45, -1.48, 1.48, 4.45];
const timeNodeLabels = [
{ text: "START", bg: "#ffffff", fg: "#4a566c", border: "#cbd1da" },
{ text: "+4 MOVE", bg: "#d9f2e5", fg: "#16764b", border: "#94cfb0" },
{ text: "−4 MOVE", bg: "#fbe0dc", fg: "#b4423b", border: "#e9aaa4" },
{ text: "Δx = 0", bg: "#e8e0f4", fg: "#65469d", border: "#b8a5d7" }
];
const timeNodes = [];
timeNodeXs.forEach((x, index) => {
const node = new THREE.Mesh(
new THREE.SphereGeometry(0.125, 20, 14),
standard(index === 0 ? 0x526078 : 0x8994a5, { roughness: 0.55 })
);
node.position.set(x, 2.9, -1.18);
node.castShadow = true;
scene.add(node);
timeNodes.push(node);
const config = timeNodeLabels[index];
const label = makeLabel(config.text, {
worldWidth: index === 0 ? 1.15 : 1.38,
color: config.fg,
background: config.bg,
border: config.border,
fontSize: 39
});
label.position.set(x, 2.32, -1.13);
scene.add(label);
});
const timeProgress = horizontalCylinder(
1,
0.072,
standard(COLORS.time, {
roughness: 0.45,
transparent: true,
opacity: 0.7
})
);
timeProgress.position.set(-4.45, 2.92, -1.13);
scene.add(timeProgress);
const timeCursor = new THREE.Mesh(
new THREE.SphereGeometry(0.19, 24, 16),
standard(COLORS.time, { roughness: 0.38 })
);
timeCursor.position.set(timeNodeXs[0], 2.94, -1.05);
timeCursor.castShadow = true;
scene.add(timeCursor);
const cursorHalo = new THREE.Mesh(
new THREE.TorusGeometry(0.28, 0.025, 10, 40),
new THREE.MeshBasicMaterial({
color: COLORS.time,
transparent: true,
opacity: 0.65,
depthWrite: false
})
);
cursorHalo.position.copy(timeCursor.position);
cursorHalo.position.z += 0.03;
scene.add(cursorHalo);
// The moving token is the only object whose x-coordinate changes.
const token = new THREE.Group();
token.position.set(-2, 0, 0.25);
const tokenBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.43, 0.48, 0.18, 32),
standard(0x324158, { roughness: 0.58 })
);
tokenBase.position.y = 0.08;
tokenBase.castShadow = true;
tokenBase.receiveShadow = true;
token.add(tokenBase);
const tokenBody = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.46, 1),
standard(COLORS.token, { roughness: 0.52 })
);
tokenBody.position.y = 0.56;
tokenBody.castShadow = true;
token.add(tokenBody);
const tokenBand = new THREE.Mesh(
new THREE.TorusGeometry(0.37, 0.035, 10, 36),
standard(0xfff1c7, { roughness: 0.42 })
);
tokenBand.rotation.x = Math.PI / 2;
tokenBand.position.y = 0.48;
token.add(tokenBand);
scene.add(token);
function createArrow(color) {
const group = new THREE.Group();
const length = 4;
const materials = [];
const shaftMaterial = standard(color, {
roughness: 0.5,
transparent: true,
opacity: 0,
depthWrite: false
});
const headMaterial = standard(color, {
roughness: 0.5,
transparent: true,
opacity: 0,
depthWrite: false
});
const shaft = horizontalCylinder(length - 0.38, 0.085, shaftMaterial, 20);
shaft.position.x = (length - 0.38) / 2;
group.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.25, 0.48, 24),
headMaterial
);
head.rotation.z = -Math.PI / 2;
head.position.x = length - 0.24;
group.add(head);
materials.push(shaftMaterial, headMaterial);
group.userData.materials = materials;
return group;
}
function setGroupOpacity(group, opacity) {
group.visible = opacity > 0.01;
for (const material of group.userData.materials) {
material.opacity = opacity;
}
}
const positiveArrow = createArrow(COLORS.positive);
positiveArrow.position.set(-2, 0.015, 0.24);
scene.add(positiveArrow);
const negativeArrow = createArrow(COLORS.negative);
negativeArrow.position.set(2, 0.015, 0.24);
negativeArrow.rotation.y = Math.PI;
scene.add(negativeArrow);
const positiveChangeLabel = makeLabel("+4 CHANGE", {
worldWidth: 1.72,
color: "#16764b",
background: "#d9f2e5",
border: "#8ec9ab",
fontSize: 39
});
positiveChangeLabel.position.set(0, 1.12, 0.25);
scene.add(positiveChangeLabel);
const negativeChangeLabel = makeLabel("−4 CHANGE", {
worldWidth: 1.72,
color: "#b4423b",
background: "#fbe0dc",
border: "#e6a39d",
fontSize: 39
});
negativeChangeLabel.position.set(0, 1.12, 0.25);
scene.add(negativeChangeLabel);
const resultRing = new THREE.Mesh(
new THREE.TorusGeometry(0.61, 0.037, 12, 64),
new THREE.MeshBasicMaterial({
color: COLORS.net,
transparent: true,
opacity: 0,
depthWrite: false
})
);
resultRing.rotation.x = Math.PI / 2;
resultRing.position.set(-2, -0.005, 0.27);
scene.add(resultRing);
const resultLabel = makeLabel("NET CHANGE Δx = 0", {
worldWidth: 2.45,
color: "#65469d",
background: "#eee8f7",
border: "#b8a5d7",
fontSize: 36
});
resultLabel.position.set(-2, 1.34, 0.25);
scene.add(resultLabel);
const phaseTag = document.getElementById("phaseTag");
const positionValue = document.getElementById("positionValue");
const timeValue = document.getElementById("timeValue");
const spaceMotion = document.getElementById("spaceMotion");
const clockMotion = document.getElementById("clockMotion");
const observation = document.getElementById("observation");
const posTerm = document.getElementById("posTerm");
const negTerm = document.getElementById("negTerm");
const netTerm = document.getElementById("netTerm");
const pauseBtn = document.getElementById("pauseBtn");
const replayBtn = document.getElementById("replayBtn");
const viewBtn = document.getElementById("viewBtn");
const speedInput = document.getElementById("speed");
const copy = [
{
tag: "1 / Positive move",
time: "Time → step 1 / 4",
space: "−2 → +2",
text: "Positive move: +4. The token travels from x = −2 to x = +2 on the lower line while the blue clock advances right."
},
{
tag: "2 / Negative move",
time: "Time → step 2 / 4",
space: "+2 → −2",
text: "Negative move: −4. The token travels from x = +2 back to x = −2. Position moves left; time still advances right."
},
{
tag: "3 / Cancellation",
time: "Time → step 3 / 4",
space: "+2 → −2",
text: "The paired signed changes cancel: +4 + (−4) = 0. The token returns to its starting point."
},
{
tag: "4 / Zero change ≠ zero position",
time: "Time → step 4 / 4",
space: "final x = −2",
text: "Net signed change is 0, but final position is x = −2. A negative coordinate is not backward time."
}
];
const segmentDurations = [1.5, 1.5, 1.35, 1.55];
const totalDuration = segmentDurations.reduce((sum, value) => sum + value, 0);
const START_X = -2;
const POSITION_X = 2;
const phaseBoundaries = [0];
segmentDurations.forEach(duration => {
phaseBoundaries.push(phaseBoundaries[phaseBoundaries.length - 1] + duration);
});
let elapsed = 0;
let playing = true;
let speed = 1;
let previousPhase = -1;
let previousPositionText = "";
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
const ease = t => {
t = clamp(t, 0, 1);
return t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
};
function formatPosition(value) {
return `Position x = ${value.toFixed(1).replace("-", "−")}`;
}
function updatePhase(phase) {
if (phase === previousPhase) return;
previousPhase = phase;
const item = copy[phase];
phaseTag.textContent = item.tag;
timeValue.textContent = item.time;
spaceMotion.textContent = item.space;
clockMotion.textContent = "always forward →";
observation.textContent = item.text;
posTerm.classList.toggle("active", phase === 0 || phase >= 2);
negTerm.classList.toggle("active", phase === 1 || phase >= 2);
netTerm.classList.toggle("active", phase >= 2);
}
function applyState(time) {
let phase = 0;
let local = 0;
for (let index = 0; index < segmentDurations.length; index++) {
const end = phaseBoundaries[index + 1];
if (time < end || index === segmentDurations.length - 1) {
phase = index;
local = clamp(
(time - phaseBoundaries[index]) / segmentDurations[index],
0,
1
);
break;
}
}
let tokenX = START_X;
let cursorX = timeNodeXs[0];
let positiveOpacity = 0;
let negativeOpacity = 0;
let positiveLabelOpacity = 0;
let negativeLabelOpacity = 0;
let resultOpacity = 0;
if (phase === 0) {
const p = ease(local);
tokenX = lerp(START_X, POSITION_X, p);
cursorX = lerp(timeNodeXs[0], timeNodeXs[1], p);
positiveOpacity = 0.2 + 0.8 * Math.min(1, local * 4);
positiveLabelOpacity = Math.min(1, local * 5);
} else if (phase === 1) {
const p = ease(local);
tokenX = lerp(POSITION_X, START_X, p);
cursorX = lerp(timeNodeXs[1], timeNodeXs[2], p);
positiveOpacity = 1 - 0.35 * local;
negativeOpacity = Math.min(1, local * 5);
positiveLabelOpacity = 1 - local;
negativeLabelOpacity = Math.min(1, local * 5);
} else if (phase === 2) {
const p = ease(local);
tokenX = START_X;
cursorX = lerp(timeNodeXs[2], timeNodeXs[3], p);
positiveOpacity = 0.65 * (1 - p);
negativeOpacity = 1 * (1 - p);
resultOpacity = p;
} else {
tokenX = START_X;
cursorX = timeNodeXs[3];
resultOpacity = 1;
}
token.position.x = tokenX;
timeCursor.position.x = cursorX;
cursorHalo.position.x = cursorX;
const pulse = 1 + Math.sin(time * 4.2) * 0.08;
cursorHalo.scale.setScalar(pulse);
const progressLength = Math.max(0.001, cursorX - timeNodeXs[0]);
timeProgress.scale.y = progressLength;
timeProgress.position.x = (timeNodeXs[0] + cursorX) / 2;
timeNodes.forEach((node, index) => {
const proximity = Math.max(0, 1 - Math.abs(cursorX - timeNodeXs[index]) / 0.8);
node.scale.setScalar(1 + proximity * 0.36);
});
setGroupOpacity(positiveArrow, positiveOpacity);
setGroupOpacity(negativeArrow, negativeOpacity);
setLabelOpacity(positiveChangeLabel, positiveLabelOpacity);
setLabelOpacity(negativeChangeLabel, negativeLabelOpacity);
const resultScale = phase === 2
? lerp(0.45, 1, ease(local))
: 1 + Math.sin(time * 3.4) * 0.045;
resultRing.scale.setScalar(resultScale);
resultRing.material.opacity = resultOpacity * 0.9;
resultRing.visible = resultOpacity > 0.01;
setLabelOpacity(resultLabel, resultOpacity);
updatePhase(phase);
const positionText = formatPosition(tokenX);
if (positionText !== previousPositionText) {
positionValue.textContent = positionText;
previousPositionText = positionText;
}
}
function updatePauseButton() {
if (elapsed >= totalDuration) {
pauseBtn.textContent = "Replay";
} else {
pauseBtn.textContent = playing ? "Pause" : "Play";
}
}
function resetAnimation(play = true) {
elapsed = 0;
playing = play;
previousPhase = -1;
applyState(elapsed);
updatePauseButton();
}
pauseBtn.addEventListener("click", () => {
if (elapsed >= totalDuration) {
resetAnimation(true);
} else {
playing = !playing;
updatePauseButton();
}
});
replayBtn.addEventListener("click", () => resetAnimation(true));
speedInput.addEventListener("input", () => {
speed = Number(speedInput.value);
});
function updateCamera() {
const radius = 16;
camera.position.set(
cameraTarget.x + radius * Math.sin(cameraPolar) * Math.sin(cameraAzimuth),
cameraTarget.y + radius * Math.cos(cameraPolar),
cameraTarget.z + radius * Math.sin(cameraPolar) * Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
viewBtn.addEventListener("click", () => {
cameraAzimuth = 0;
cameraPolar = 1.08;
cameraZoom = 1;
updateCamera();
});
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraAzimuth = clamp(cameraAzimuth - dx * 0.004, -0.5, 0.5);
cameraPolar = clamp(cameraPolar + dy * 0.003, 0.82, 1.29);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraZoom = clamp(cameraZoom * (1 - event.deltaY * 0.001), 0.75, 1.65);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", event => {
if (event.code === "Space" && event.target.tagName !== "INPUT") {
event.preventDefault();
pauseBtn.click();
}
if (event.key.toLowerCase() === "r" && event.target.tagName !== "INPUT") {
resetAnimation(true);
}
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(8.9, 13.6 / aspect);
camera.left = -viewHeight * aspect / 2;
camera.right = viewHeight * aspect / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
renderer.setSize(width, height);
updateCamera();
}
window.addEventListener("resize", resize);
resize();
applyState(0);
updatePauseButton();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
elapsed = Math.min(totalDuration, elapsed + delta * speed);
if (elapsed >= totalDuration) {
playing = false;
updatePauseButton();
}
}
applyState(elapsed);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```