Prompt specification
Build or customize the Three.js 3D scene "Moving cart and stacked graph panels" 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: Moving cart and stacked graph panels
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/moving-cart-and-stacked-graph-panels-x1405
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Place a cart on a one-dimensional track beside synchronized position, velocity, and acceleration plots; use one consistent trajectory and a shared time cursor.
Working Three.js Starter (moving-cart-and-stacked-graph-panels-x1405):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>One-Dimensional Motion Lab</title>
<style>
:root {
--ink: #17211f;
--muted: #6d7772;
--paper: #f4f1e9;
--card: #fffefa;
--line: #dcded7;
--orange: #f0643a;
--orange-dark: #bd4225;
--blue: #3977d3;
--green: #169a79;
--purple: #8b5bd7;
--shadow: 0 14px 38px rgba(31, 43, 39, 0.09);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
}
body {
color: var(--ink);
background: var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
overflow: hidden;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
display: grid;
grid-template-columns: minmax(0, 1.18fr) minmax(460px, 0.82fr);
width: 100%;
height: 100vh;
}
/* ---------- 3D scene ---------- */
.scene-panel {
position: relative;
min-width: 0;
overflow: hidden;
background: #dce7e2;
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.scene-shade {
position: absolute;
inset: 0;
pointer-events: none;
background:
linear-gradient(180deg, rgba(237, 244, 240, 0.52), transparent 30%),
linear-gradient(0deg, rgba(20, 36, 31, 0.15), transparent 28%);
}
.scene-header {
position: absolute;
top: 30px;
left: 32px;
z-index: 2;
max-width: 460px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 13px;
color: #53645e;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 25px;
height: 2px;
background: var(--orange);
content: "";
}
.scene-header h1 {
max-width: 430px;
margin: 0;
font-size: clamp(30px, 4vw, 54px);
font-weight: 750;
line-height: 0.98;
letter-spacing: -0.055em;
}
.scene-header p {
max-width: 380px;
margin: 14px 0 0;
color: #53645e;
font-size: 14px;
line-height: 1.55;
}
.view-reset {
position: absolute;
top: 28px;
right: 28px;
z-index: 4;
display: inline-flex;
align-items: center;
gap: 7px;
padding: 9px 13px;
border: 1px solid rgba(36, 54, 48, 0.14);
border-radius: 999px;
color: #40504a;
background: rgba(255, 255, 255, 0.64);
box-shadow: 0 5px 18px rgba(31, 49, 43, 0.08);
backdrop-filter: blur(12px);
cursor: pointer;
transition: background 160ms ease, transform 160ms ease;
}
.view-reset:hover {
background: rgba(255, 255, 255, 0.9);
transform: translateY(-1px);
}
.scene-hud {
position: absolute;
left: 28px;
bottom: 28px;
z-index: 3;
display: grid;
grid-template-columns: repeat(3, minmax(92px, 1fr));
gap: 8px;
width: min(440px, calc(100% - 56px));
padding: 8px;
border: 1px solid rgba(255, 255, 255, 0.58);
border-radius: 16px;
background: rgba(250, 252, 248, 0.72);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
pointer-events: none;
}
.hud-item {
padding: 9px 11px;
border-right: 1px solid rgba(47, 66, 59, 0.12);
}
.hud-item:last-child {
border-right: 0;
}
.hud-label {
display: block;
margin-bottom: 4px;
color: #718079;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.hud-value {
font-variant-numeric: tabular-nums;
font-size: 17px;
font-weight: 750;
letter-spacing: -0.03em;
}
.hud-value small {
color: #74817b;
font-size: 10px;
font-weight: 650;
letter-spacing: 0;
}
.orbit-hint {
position: absolute;
right: 28px;
bottom: 30px;
z-index: 3;
padding: 8px 11px;
border-radius: 10px;
color: rgba(39, 58, 51, 0.7);
background: rgba(255, 255, 255, 0.52);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
pointer-events: none;
backdrop-filter: blur(10px);
}
/* ---------- Dashboard ---------- */
.dashboard {
min-width: 0;
padding: 22px 24px 18px;
overflow-y: auto;
border-left: 1px solid rgba(23, 33, 31, 0.07);
background:
radial-gradient(circle at 100% 0%, rgba(240, 100, 58, 0.07), transparent 28%),
var(--paper);
scrollbar-width: thin;
scrollbar-color: #c6cbc4 transparent;
}
.dash-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
margin-bottom: 16px;
}
.dash-kicker {
margin: 0 0 5px;
color: var(--orange-dark);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.dash-head h2 {
margin: 0;
font-size: 25px;
font-weight: 780;
letter-spacing: -0.04em;
}
.live-badge {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid #d8ddd6;
border-radius: 999px;
color: #5b6862;
background: rgba(255, 255, 255, 0.58);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.live-badge::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(22, 154, 121, 0.12);
content: "";
}
.stats-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 9px;
margin-bottom: 17px;
}
.stat-card {
position: relative;
min-width: 0;
padding: 11px 12px 12px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 254, 250, 0.9);
box-shadow: 0 6px 20px rgba(42, 51, 47, 0.035);
}
.stat-card::before {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 3px;
background: var(--metric-color);
content: "";
}
.stat-name {
display: flex;
align-items: center;
gap: 7px;
color: #758079;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.metric-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--metric-color);
}
.stat-number {
margin-top: 7px;
overflow: hidden;
font-size: clamp(18px, 2vw, 24px);
font-weight: 770;
font-variant-numeric: tabular-nums;
letter-spacing: -0.045em;
text-overflow: ellipsis;
white-space: nowrap;
}
.stat-unit {
color: #8b948f;
font-size: 10px;
font-weight: 650;
letter-spacing: 0;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: 0 0 10px;
}
.section-title h3 {
margin: 0;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.015em;
}
.derived-badge {
padding: 5px 8px;
border-radius: 7px;
color: #66736d;
background: #e6e9e2;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.plot-stack {
position: relative;
display: grid;
gap: 9px;
}
.chart-card {
position: relative;
z-index: 1;
padding: 10px 11px 7px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 254, 250, 0.94);
box-shadow: 0 6px 20px rgba(42, 51, 47, 0.035);
}
.chart-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
height: 22px;
padding: 0 4px;
}
.chart-name {
display: flex;
align-items: center;
gap: 7px;
font-size: 10px;
font-weight: 800;
}
.chart-name .metric-dot {
width: 8px;
height: 8px;
}
.chart-reading {
color: #6c7771;
font-size: 10px;
font-weight: 650;
font-variant-numeric: tabular-nums;
}
.chart-wrap {
position: relative;
}
.chart-svg {
display: block;
width: 100%;
height: 106px;
overflow: visible;
cursor: crosshair;
}
.cursor-line {
position: absolute;
z-index: 5;
width: 1px;
pointer-events: none;
background: var(--orange);
box-shadow: 0 0 0 1px rgba(240, 100, 58, 0.04);
}
.cursor-line::before {
position: absolute;
top: -4px;
left: -4px;
width: 9px;
height: 9px;
border: 2px solid var(--paper);
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 0 1px rgba(240, 100, 58, 0.25);
content: "";
}
.cursor-label {
position: absolute;
top: -20px;
left: 0;
padding: 3px 6px;
border-radius: 5px;
color: white;
background: var(--orange);
font-size: 8px;
font-weight: 800;
font-variant-numeric: tabular-nums;
white-space: nowrap;
transform: translateX(-50%);
}
.source-note {
display: flex;
align-items: center;
gap: 10px;
margin-top: 10px;
padding: 9px 11px;
border: 1px dashed #cfd4cc;
border-radius: 10px;
color: #748079;
background: rgba(255, 255, 255, 0.36);
font-size: 9px;
line-height: 1.4;
}
.source-note strong {
flex: 0 0 auto;
color: #59655f;
font-size: 8px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.source-note code {
color: #4e5c56;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 9px;
}
/* ---------- Transport ---------- */
.transport {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 13px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid #d9ddd5;
}
.play-button {
display: grid;
width: 42px;
height: 42px;
padding: 0;
place-items: center;
border: 0;
border-radius: 50%;
color: white;
background: var(--orange);
box-shadow: 0 7px 18px rgba(240, 100, 58, 0.27);
cursor: pointer;
transition: transform 150ms ease, background 150ms ease;
}
.play-button:hover {
background: var(--orange-dark);
transform: translateY(-1px);
}
.play-button svg {
width: 17px;
height: 17px;
fill: currentColor;
}
.timeline {
min-width: 0;
}
.timeline-top {
display: flex;
justify-content: space-between;
gap: 10px;
margin-bottom: 7px;
color: #758079;
font-size: 9px;
font-weight: 750;
font-variant-numeric: tabular-nums;
}
.timeline-top strong {
color: var(--ink);
font-size: 10px;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 99px;
outline: none;
background: linear-gradient(
to right,
var(--orange) 0%,
var(--orange) var(--progress, 0%),
#d8ddd6 var(--progress, 0%),
#d8ddd6 100%
);
cursor: pointer;
appearance: none;
-webkit-appearance: none;
}
input[type="range"]::-webkit-slider-thumb {
width: 14px;
height: 14px;
border: 3px solid white;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 0 1px rgba(240, 100, 58, 0.28), 0 2px 6px rgba(0, 0, 0, 0.15);
appearance: none;
-webkit-appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid white;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 0 1px rgba(240, 100, 58, 0.28);
}
.speed-control {
display: flex;
gap: 3px;
padding: 3px;
border: 1px solid #d6dad3;
border-radius: 9px;
background: rgba(255, 255, 255, 0.55);
}
.speed-control button {
min-width: 35px;
padding: 6px 7px;
border: 0;
border-radius: 6px;
color: #6c7771;
background: transparent;
font-size: 9px;
font-weight: 800;
cursor: pointer;
}
.speed-control button.active {
color: white;
background: var(--ink);
}
@media (max-width: 980px) {
body {
overflow: auto;
}
.app {
grid-template-columns: 1fr;
grid-template-rows: minmax(510px, 62vh) auto;
height: auto;
}
.dashboard {
overflow: visible;
border-top: 1px solid rgba(23, 33, 31, 0.07);
border-left: 0;
}
.scene-header {
top: 24px;
left: 24px;
}
.view-reset {
top: 22px;
right: 22px;
}
.scene-hud {
left: 20px;
bottom: 20px;
width: min(440px, calc(100% - 40px));
}
.orbit-hint {
display: none;
}
}
@media (max-width: 580px) {
.app {
grid-template-rows: 500px auto;
}
.scene-header h1 {
max-width: 300px;
font-size: 36px;
}
.scene-header p {
display: none;
}
.view-reset {
padding: 8px 10px;
font-size: 10px;
}
.scene-hud {
grid-template-columns: repeat(3, 1fr);
gap: 2px;
}
.hud-item {
padding: 8px 7px;
}
.hud-value {
font-size: 14px;
}
.dashboard {
padding: 20px 16px;
}
.stats-grid {
gap: 6px;
}
.stat-card {
padding: 10px 8px;
}
.stat-number {
font-size: 17px;
}
.stat-unit {
display: block;
margin-top: 2px;
}
.transport {
grid-template-columns: auto 1fr;
}
.speed-control {
grid-column: 1 / -1;
justify-self: end;
}
.source-note {
align-items: flex-start;
flex-direction: column;
}
}
</style>
</head>
<body>
<main class="app">
<section class="scene-panel" aria-label="Three-dimensional cart on a one-dimensional track">
<div id="scene"></div>
<div class="scene-shade"></div>
<header class="scene-header">
<div class="eyebrow">Kinematics lab · Track 01</div>
<h1>Motion, in one dimension.</h1>
<p>
One cart, one trajectory, and one shared time axis. Position, velocity,
and acceleration are derived from the same motion.
</p>
</header>
<button class="view-reset" id="resetView" type="button" aria-label="Reset camera view">
<span aria-hidden="true">↺</span>
Reset view
</button>
<div class="scene-hud" aria-label="Current motion values">
<div class="hud-item">
<span class="hud-label">Position</span>
<span class="hud-value" id="hudX">−4.40 <small>m</small></span>
</div>
<div class="hud-item">
<span class="hud-label">Velocity</span>
<span class="hud-value" id="hudV">+1.62 <small>m/s</small></span>
</div>
<div class="hud-item">
<span class="hud-label">Acceleration</span>
<span class="hud-value" id="hudA">+0.00 <small>m/s²</small></span>
</div>
</div>
<div class="orbit-hint">DRAG TO ORBIT · SCROLL TO ZOOM</div>
</section>
<aside class="dashboard" aria-label="Synchronized motion plots">
<header class="dash-head">
<div>
<p class="dash-kicker">Trajectory monitor</p>
<h2>Cart 01 / Live state</h2>
</div>
<span class="live-badge">Synchronized</span>
</header>
<section class="stats-grid" aria-label="Current measurements">
<article class="stat-card" style="--metric-color: var(--blue)">
<div class="stat-name"><span class="metric-dot"></span>Position</div>
<div class="stat-number" id="statX">−4.40</div>
<span class="stat-unit">metres</span>
</article>
<article class="stat-card" style="--metric-color: var(--green)">
<div class="stat-name"><span class="metric-dot"></span>Velocity</div>
<div class="stat-number" id="statV">+1.62</div>
<span class="stat-unit">metres / second</span>
</article>
<article class="stat-card" style="--metric-color: var(--purple)">
<div class="stat-name"><span class="metric-dot"></span>Acceleration</div>
<div class="stat-number" id="statA">+0.00</div>
<span class="stat-unit">metres / second²</span>
</article>
</section>
<div class="section-title">
<h3>Shared time axis</h3>
<span class="derived-badge">All curves derived from x(t)</span>
</div>
<section class="plot-stack" id="plotStack" aria-label="Position, velocity, and acceleration graphs">
<article class="chart-card">
<div class="chart-head">
<div class="chart-name" style="color: var(--blue)">
<span class="metric-dot"></span> Position · x(t)
</div>
<div class="chart-reading"><span id="chartX">−4.40</span> m</div>
</div>
<div class="chart-wrap">
<svg class="chart-svg" id="positionChart" viewBox="0 0 420 108"
preserveAspectRatio="none" role="img" aria-label="Position over time"></svg>
</div>
</article>
<article class="chart-card">
<div class="chart-head">
<div class="chart-name" style="color: var(--green)">
<span class="metric-dot"></span> Velocity · v(t)
</div>
<div class="chart-reading"><span id="chartV">+1.62</span> m/s</div>
</div>
<div class="chart-wrap">
<svg class="chart-svg" id="velocityChart" viewBox="0 0 420 108"
preserveAspectRatio="none" role="img" aria-label="Velocity over time"></svg>
</div>
</article>
<article class="chart-card">
<div class="chart-head">
<div class="chart-name" style="color: var(--purple)">
<span class="metric-dot"></span> Acceleration · a(t)
</div>
<div class="chart-reading"><span id="chartA">+0.00</span> m/s²</div>
</div>
<div class="chart-wrap">
<svg class="chart-svg" id="accelerationChart" viewBox="0 0 420 108"
preserveAspectRatio="none" role="img" aria-label="Acceleration over time"></svg>
</div>
</article>
<div class="cursor-line" id="cursorLine" aria-hidden="true">
<span class="cursor-label" id="cursorLabel">t = 0.00 s</span>
</div>
</section>
<div class="source-note">
<strong>Single source</strong>
<code>x(t) = −4.4 + (8.8/12)t + 1.7 sin(2πt/12)</code>
</div>
<section class="transport" aria-label="Playback controls">
<button class="play-button" id="playButton" type="button" aria-label="Pause animation"></button>
<div class="timeline">
<div class="timeline-top">
<span>Time cursor</span>
<span><strong id="currentTime">0.00</strong> / 12.00 s</span>
</div>
<input id="timeSlider" type="range" min="0" max="12" step="0.01" value="0"
aria-label="Simulation time">
</div>
<div class="speed-control" aria-label="Playback speed">
<button type="button" data-speed="0.5">½×</button>
<button type="button" data-speed="1" class="active">1×</button>
<button type="button" data-speed="2">2×</button>
</div>
</section>
</aside>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Shared physical model. All views use these same three functions.
// -------------------------------------------------------------------------
const DURATION = 12;
const TWO_PI = Math.PI * 2;
const state = {
time: 0,
playing: !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
speed: 1
};
function motionAt(t) {
const w = TWO_PI / DURATION;
return {
x: -4.4 + (8.8 * t / DURATION) + 1.7 * Math.sin(w * t),
v: 8.8 / DURATION + 1.7 * w * Math.cos(w * t),
a: -1.7 * w * w * Math.sin(w * t)
};
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function signed(value, digits = 2) {
if (Math.abs(value) < 0.5 * Math.pow(10, -digits)) value = 0;
const sign = value > 0 ? "+" : value < 0 ? "−" : "";
return sign + Math.abs(value).toFixed(digits);
}
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce7e2);
scene.fog = new THREE.Fog(0xdce7e2, 19, 42);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-10, 10, 7, -7, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.55, 0);
const cameraOrbit = {
radius: 19,
theta: 0.55,
phi: 1.04
};
function applyCamera() {
const { radius, theta, phi } = cameraOrbit;
camera.position.set(
radius * Math.sin(phi) * Math.cos(theta),
cameraTarget.y + radius * Math.cos(phi),
radius * Math.sin(phi) * Math.sin(theta)
);
camera.lookAt(cameraTarget);
camera.updateProjectionMatrix();
}
function resetCamera() {
cameraOrbit.radius = 19;
cameraOrbit.theta = 0.55;
cameraOrbit.phi = 1.04;
camera.zoom = 1;
applyCamera();
resizeScene();
}
const hemiLight = new THREE.HemisphereLight(0xf7fff9, 0x63736c, 2.2);
scene.add(hemiLight);
const sun = new THREE.DirectionalLight(0xfff4dc, 3.1);
sun.position.set(-7, 13, 8);
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 = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0005;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8d8ff, 0.8);
fill.position.set(10, 5, -9);
scene.add(fill);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.85,
metalness: 0,
flatShading: true,
...options
});
}
function addBox(parent, size, position, material, cast = true, receive = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), material);
mesh.position.set(...position);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
parent.add(mesh);
return mesh;
}
// Ground and faceted landscape.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 32),
standardMaterial(0xc7d2c7)
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.16;
ground.receiveShadow = true;
scene.add(ground);
const hillMaterials = [
standardMaterial(0x8fa99a),
standardMaterial(0x789487),
standardMaterial(0xa4b8a8)
];
const hillData = [
[-10, 1.9, -9, 4.7, 5],
[-4.6, 2.6, -11, 5.8, 6],
[2.2, 2.2, -10.2, 5.0, 5],
[8.3, 3.0, -10.8, 6.4, 6],
[13.2, 1.7, -8.5, 4.1, 5]
];
hillData.forEach(([x, y, z, radius, sides], index) => {
const height = radius * (0.75 + (index % 2) * 0.18);
const hill = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, sides),
hillMaterials[index % hillMaterials.length]
);
hill.position.set(x, height / 2 - 0.18, z);
hill.rotation.y = index * 0.73;
hill.receiveShadow = true;
scene.add(hill);
});
function addTree(x, z, scale = 1) {
const tree = new THREE.Group();
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.08 * scale, 0.12 * scale, 0.8 * scale, 6),
standardMaterial(0x695c45)
);
trunk.position.y = 0.24 * scale;
trunk.castShadow = true;
tree.add(trunk);
const crown = new THREE.Mesh(
new THREE.ConeGeometry(0.48 * scale, 1.25 * scale, 7),
standardMaterial(0x527b67)
);
crown.position.y = 0.92 * scale;
crown.castShadow = true;
tree.add(crown);
tree.position.set(x, -0.13, z);
tree.rotation.y = x * 0.47;
scene.add(tree);
}
[
[-8.8, -3.8, 0.9],
[-6.6, -5.0, 0.72],
[8.5, -4.5, 0.85],
[10.2, -2.8, 0.65],
[-12, 1.2, 0.8],
[12, 1.0, 0.7]
].forEach(([x, z, scale]) => addTree(x, z, scale));
// Track bed, sleepers, rails, and end stops.
const trackGroup = new THREE.Group();
scene.add(trackGroup);
const ballast = addBox(
trackGroup,
[17.4, 0.16, 1.42],
[0, -0.02, 0],
standardMaterial(0x777c73)
);
ballast.receiveShadow = true;
const sleeperMaterial = standardMaterial(0x61584c);
for (let x = -8.1; x <= 8.1; x += 0.55) {
addBox(
trackGroup,
[0.16, 0.12, 1.16],
[x, 0.105, 0],
sleeperMaterial,
false,
true
);
}
const railMaterial = standardMaterial(0xb9c4c2, {
metalness: 0.55,
roughness: 0.35
});
addBox(trackGroup, [17.3, 0.09, 0.095], [0, 0.22, -0.36], railMaterial);
addBox(trackGroup, [17.3, 0.09, 0.095], [0, 0.22, 0.36], railMaterial);
const stopMaterial = standardMaterial(0xd55736);
[-8.35, 8.35].forEach((x) => {
addBox(trackGroup, [0.18, 0.62, 1.05], [x, 0.3, 0], stopMaterial);
addBox(trackGroup, [0.42, 0.13, 1.18], [x, 0.64, 0], railMaterial);
});
function makeLabelSprite(text) {
const canvas = document.createElement("canvas");
canvas.width = 192;
canvas.height = 72;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgba(248,250,244,0.92)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "rgba(48,67,59,0.22)";
ctx.lineWidth = 4;
ctx.strokeRect(2, 2, canvas.width - 4, canvas.height - 4);
ctx.fillStyle = "#34443d";
ctx.font = "700 34px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({ map: texture, transparent: true })
);
sprite.scale.set(0.66, 0.25, 1);
return sprite;
}
for (let x = -6; x <= 6; x += 2) {
const label = makeLabelSprite(String(x));
label.position.set(x, 0.72, 0.88);
scene.add(label);
addBox(
scene,
[0.045, 0.5, 0.045],
[x, 0.34, 0.88],
standardMaterial(0x66736c),
false,
true
);
}
// Low-poly utility cart.
const cart = new THREE.Group();
scene.add(cart);
const wheelMaterial = standardMaterial(0x252d2b, {
roughness: 0.72,
metalness: 0.08
});
const hubMaterial = standardMaterial(0xafb8b3, {
metalness: 0.45,
roughness: 0.42
});
const cartOrange = standardMaterial(0xf0643a);
const cartOrangeDark = standardMaterial(0xb84225);
const cartCream = standardMaterial(0xf0d7a6);
const cartDark = standardMaterial(0x394541);
addBox(cart, [1.34, 0.2, 0.88], [0, 0.48, 0], cartDark);
addBox(cart, [1.18, 0.12, 0.76], [0, 0.63, 0], cartOrangeDark);
addBox(cart, [1.15, 0.32, 0.08], [0, 0.81, -0.37], cartOrange);
addBox(cart, [1.15, 0.32, 0.08], [0, 0.81, 0.37], cartOrange);
const rearWall = addBox(
cart,
[0.08, 0.32, 0.72],
[-0.57, 0.81, 0],
cartOrange
);
rearWall.rotation.z = -0.08;
const frontWall = addBox(
cart,
[0.08, 0.32, 0.72],
[0.57, 0.81, 0],
cartOrange
);
frontWall.rotation.z = 0.08;
const payload = addBox(
cart,
[0.55, 0.34, 0.48],
[-0.12, 0.86, 0],
cartCream
);
payload.rotation.y = 0.12;
addBox(
cart,
[0.34, 0.25, 0.38],
[0.31, 0.81, 0.02],
standardMaterial(0xd8b96f)
);
const wheelPivots = [];
[-0.45, 0.45].forEach((x) => {
[-0.48, 0.48].forEach((z) => {
const pivot = new THREE.Group();
pivot.position.set(x, 0.3, z);
const wheel = new THREE.Mesh(
new THREE.CylinderGeometry(0.19, 0.19, 0.13, 10),
wheelMaterial
);
wheel.rotation.x = Math.PI / 2;
wheel.castShadow = true;
pivot.add(wheel);
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.145, 8),
hubMaterial
);
hub.rotation.x = Math.PI / 2;
pivot.add(hub);
cart.add(pivot);
wheelPivots.push(pivot);
});
});
const beaconBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.12, 0.14, 0.06, 8),
cartDark
);
beaconBase.position.set(0.42, 1.04, 0);
cart.add(beaconBase);
const beacon = new THREE.Mesh(
new THREE.CylinderGeometry(0.085, 0.105, 0.16, 8),
standardMaterial(0xffb44f, {
emissive: 0x8b3d00,
emissiveIntensity: 0.22
})
);
beacon.position.set(0.42, 1.14, 0);
beacon.castShadow = true;
cart.add(beacon);
// Direct camera interaction, implemented without external controls.
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 dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
cameraOrbit.theta -= dx * 0.006;
cameraOrbit.phi = clamp(cameraOrbit.phi + dy * 0.004, 0.58, 1.36);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
applyCamera();
});
function endDrag(event) {
dragging = false;
if (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();
camera.zoom = clamp(camera.zoom * Math.exp(-event.deltaY * 0.001), 0.78, 1.55);
camera.updateProjectionMatrix();
}, { passive: false });
document.getElementById("resetView").addEventListener("click", resetCamera);
function resizeScene() {
const width = sceneHost.clientWidth;
const height = sceneHost.clientHeight;
if (!width || !height) return;
const aspect = width / height;
const desiredWidth = 18.8;
const desiredHeight = 10.8;
const frustumHeight = Math.max(desiredHeight, desiredWidth / aspect);
camera.left = (-frustumHeight * aspect) / 2;
camera.right = (frustumHeight * aspect) / 2;
camera.top = frustumHeight / 2;
camera.bottom = -frustumHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
new ResizeObserver(resizeScene).observe(sceneHost);
resizeScene();
applyCamera();
// -------------------------------------------------------------------------
// Plot rendering
// -------------------------------------------------------------------------
const SVG_NS = "http://www.w3.org/2000/svg";
const PLOT = {
width: 420,
height: 108,
left: 48,
right: 14,
top: 10,
bottom: 19
};
const chartDefinitions = [
{
key: "x",
svg: document.getElementById("positionChart"),
color: "#3977d3",
label: "x(t)",
unit: "m",
range: [-4.8, 4.8]
},
{
key: "v",
svg: document.getElementById("velocityChart"),
color: "#169a79",
label: "v(t)",
unit: "m/s",
range: [-0.38, 1.82]
},
{
key: "a",
svg: document.getElementById("accelerationChart"),
color: "#8b5bd7",
label: "a(t)",
unit: "m/s²",
range: [-0.29, 0.29]
}
];
const plotWidth = PLOT.width - PLOT.left - PLOT.right;
const plotHeight = PLOT.height - PLOT.top - PLOT.bottom;
function plotX(t) {
return PLOT.left + (t / DURATION) * plotWidth;
}
function plotY(value, range) {
const fraction = (value - range[0]) / (range[1] - range[0]);
return PLOT.top + (1 - fraction) * plotHeight;
}
function createSvgElement(tag, attributes = {}) {
const element = document.createElementNS(SVG_NS, tag);
Object.entries(attributes).forEach(([key, value]) => {
element.setAttribute(key, value);
});
return element;
}
function formatAxis(value) {
const absolute = Math.abs(value);
if (absolute < 0.01) return "0";
if (absolute >= 10) return value.toFixed(0);
if (absolute >= 1) return value.toFixed(1);
return value.toFixed(2);
}
chartDefinitions.forEach((chart) => {
const svg = chart.svg;
const [minValue, maxValue] = chart.range;
// Grid.
for (let i = 0; i <= 3; i++) {
const y = PLOT.top + (i / 3) * plotHeight;
const value = maxValue - (i / 3) * (maxValue - minValue);
svg.appendChild(createSvgElement("line", {
x1: PLOT.left,
y1: y,
x2: PLOT.width - PLOT.right,
y2: y,
stroke: i === 3 ? "#cfd5ce" : "#e7e9e3",
"stroke-width": 1
}));
const label = createSvgElement("text", {
x: PLOT.left - 8,
y: y + 3,
fill: "#82908a",
"font-size": 9,
"font-family": "system-ui, sans-serif",
"text-anchor": "end"
});
label.textContent = formatAxis(value);
svg.appendChild(label);
}
// Shared vertical time grid.
[0, 3, 6, 9, 12].forEach((time) => {
const x = plotX(time);
svg.appendChild(createSvgElement("line", {
x1: x,
y1: PLOT.top,
x2: x,
y2: PLOT.height - PLOT.bottom,
stroke: "#e5e8e2",
"stroke-width": 1,
"stroke-dasharray": time === 0 ? "0" : "3 4"
}));
const label = createSvgElement("text", {
x,
y: PLOT.height - 5,
fill: "#89938e",
"font-size": 8,
"font-family": "system-ui, sans-serif",
"text-anchor": time === 0 ? "start" : time === 12 ? "end" : "middle"
});
label.textContent = `${time}s`;
svg.appendChild(label);
});
// Zero line.
if (minValue < 0 && maxValue > 0) {
const zeroY = plotY(0, chart.range);
svg.appendChild(createSvgElement("line", {
x1: PLOT.left,
y1: zeroY,
x2: PLOT.width - PLOT.right,
y2: zeroY,
stroke: "#bcc5be",
"stroke-width": 1.1
}));
}
// Analytic curve sampled from the shared trajectory.
let pathData = "";
for (let i = 0; i <= 300; i++) {
const time = (i / 300) * DURATION;
const value = motionAt(time)[chart.key];
const x = plotX(time);
const y = plotY(value, chart.range);
pathData += `${i === 0 ? "M" : "L"}${x.toFixed(2)},${y.toFixed(2)} `;
}
svg.appendChild(createSvgElement("path", {
d: pathData,
fill: "none",
stroke: chart.color,
"stroke-width": 2.4,
"stroke-linecap": "round",
"stroke-linejoin": "round",
"vector-effect": "non-scaling-stroke"
}));
chart.marker = createSvgElement("circle", {
cx: plotX(0),
cy: plotY(motionAt(0)[chart.key], chart.range),
r: 4.2,
fill: chart.color,
stroke: "#fffefa",
"stroke-width": 2,
"vector-effect": "non-scaling-stroke"
});
svg.appendChild(chart.marker);
svg.addEventListener("click", (event) => {
const rect = svg.getBoundingClientRect();
const localX = ((event.clientX - rect.left) / rect.width) * PLOT.width;
const ratio = clamp(
(localX - PLOT.left) / plotWidth,
0,
1
);
setTime(ratio * DURATION);
});
});
// The cursor is one DOM element crossing all three SVG plots.
const plotStack = document.getElementById("plotStack");
const cursorLine = document.getElementById("cursorLine");
const cursorLabel = document.getElementById("cursorLabel");
let cursorGeometry = null;
function measureCursor() {
const stackRect = plotStack.getBoundingClientRect();
const chartRects = chartDefinitions.map((chart) => chart.svg.getBoundingClientRect());
const first = chartRects[0];
const last = chartRects[chartRects.length - 1];
cursorGeometry = {
left: first.left - stackRect.left,
top: first.top - stackRect.top,
width: first.width,
height: last.bottom - first.top
};
updateCursorPosition();
}
function updateCursorPosition() {
if (!cursorGeometry) return;
const ratio = state.time / DURATION;
const x = cursorGeometry.left +
((PLOT.left + ratio * plotWidth) / PLOT.width) * cursorGeometry.width;
cursorLine.style.left = `${x}px`;
cursorLine.style.top = `${cursorGeometry.top}px`;
cursorLine.style.height = `${cursorGeometry.height}px`;
cursorLabel.textContent = `t = ${state.time.toFixed(2)} s`;
}
new ResizeObserver(measureCursor).observe(plotStack);
window.addEventListener("resize", measureCursor);
// -------------------------------------------------------------------------
// DOM synchronization
// -------------------------------------------------------------------------
const hudX = document.getElementById("hudX");
const hudV = document.getElementById("hudV");
const hudA = document.getElementById("hudA");
const statX = document.getElementById("statX");
const statV = document.getElementById("statV");
const statA = document.getElementById("statA");
const chartX = document.getElementById("chartX");
const chartV = document.getElementById("chartV");
const chartA = document.getElementById("chartA");
const slider = document.getElementById("timeSlider");
const currentTime = document.getElementById("currentTime");
const playButton = document.getElementById("playButton");
const playIcon = `
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M8 5.5v13l11-6.5z"></path>
</svg>`;
const pauseIcon = `
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M7 5h4v14H7zm6 0h4v14h-4z"></path>
</svg>`;
function updatePlayButton() {
playButton.innerHTML = state.playing ? pauseIcon : playIcon;
playButton.setAttribute(
"aria-label",
state.playing ? "Pause animation" : "Play animation"
);
}
function setTime(time) {
state.time = clamp(time, 0, DURATION);
const motion = motionAt(state.time);
cart.position.x = motion.x;
wheelPivots.forEach((wheel) => {
wheel.rotation.z = -motion.x / 0.19;
});
const xText = signed(motion.x);
const vText = signed(motion.v);
const aText = signed(motion.a);
hudX.innerHTML = `${xText} <small>m</small>`;
hudV.innerHTML = `${vText} <small>m/s</small>`;
hudA.innerHTML = `${aText} <small>m/s²</small>`;
statX.textContent = xText;
statV.textContent = vText;
statA.textContent = aText;
chartX.textContent = xText;
chartV.textContent = vText;
chartA.textContent = aText;
slider.value = state.time.toFixed(2);
slider.style.setProperty(
"--progress",
`${(state.time / DURATION) * 100}%`
);
currentTime.textContent = state.time.toFixed(2);
chartDefinitions.forEach((chart) => {
const value = motion[chart.key];
chart.marker.setAttribute("cx", plotX(state.time));
chart.marker.setAttribute("cy", plotY(value, chart.range));
});
updateCursorPosition();
}
function togglePlayback() {
state.playing = !state.playing;
updatePlayButton();
}
playButton.addEventListener("click", togglePlayback);
slider.addEventListener("input", () => {
setTime(Number(slider.value));
});
document.querySelectorAll("[data-speed]").forEach((button) => {
button.addEventListener("click", () => {
state.speed = Number(button.dataset.speed);
document.querySelectorAll("[data-speed]").forEach((other) => {
other.classList.toggle("active", other === button);
});
});
});
window.addEventListener("keydown", (event) => {
const tag = document.activeElement?.tagName;
if (tag === "INPUT" || tag === "BUTTON") return;
if (event.code === "Space") {
event.preventDefault();
togglePlayback();
} else if (event.code === "ArrowLeft") {
setTime(state.time - 0.1);
} else if (event.code === "ArrowRight") {
setTime(state.time + 0.1);
}
});
updatePlayButton();
setTime(0);
measureCursor();
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
let lastFrame = performance.now();
function animate(now) {
const delta = Math.min((now - lastFrame) / 1000, 0.05);
lastFrame = now;
if (state.playing) {
let nextTime = state.time + delta * state.speed;
if (nextTime > DURATION) nextTime -= DURATION;
setTime(nextTime);
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```