Prompt specification
Build or customize the Three.js 3D scene "Force-time pulse sculpture" 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: Force-time pulse sculpture
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/force-time-pulse-sculpture-x1397
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Build two equal-area force-versus-time shapes as raised ribbons beside a cart; label equal impulse while showing their different peak force and duration.
Working Three.js Starter (force-time-pulse-sculpture-x1397):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Equal Impulse — Low-Poly Force vs. Time</title>
<style>
:root {
color-scheme: light;
--ink: #193747;
--muted: #60717a;
--paper: #f6f0e4;
--teal: #168b87;
--orange: #ef6a3a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--ink);
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#stage canvas {
touch-action: none;
cursor: grab;
}
#stage canvas.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 10;
pointer-events: none;
}
.intro {
top: 22px;
left: 24px;
width: min(430px, calc(100vw - 48px));
padding: 18px 20px 17px;
border: 1px solid rgba(25, 55, 71, 0.12);
border-radius: 18px;
background: rgba(255, 252, 245, 0.82);
box-shadow: 0 12px 32px rgba(40, 55, 58, 0.09);
backdrop-filter: blur(10px);
}
.eyebrow {
margin-bottom: 6px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 2.4vw, 34px);
line-height: 1.04;
letter-spacing: -0.035em;
}
.intro p {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.intro b {
color: var(--ink);
}
.controls {
top: 22px;
right: 22px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
border: 1px solid rgba(25, 55, 71, 0.16);
border-radius: 12px;
padding: 10px 13px;
color: var(--ink);
background: rgba(255, 252, 245, 0.88);
box-shadow: 0 8px 20px rgba(40, 55, 58, 0.08);
font: 700 12px/1 Inter, ui-sans-serif, system-ui, sans-serif;
cursor: pointer;
}
button:hover {
background: #fffdf8;
transform: translateY(-1px);
}
.hint {
right: 22px;
bottom: 18px;
padding: 9px 12px;
border-radius: 999px;
color: #536771;
background: rgba(255, 252, 245, 0.76);
font-size: 11px;
letter-spacing: 0.02em;
backdrop-filter: blur(8px);
}
@media (max-width: 700px) {
.intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
.intro p {
display: none;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="stage" aria-label="Interactive 3D force-versus-time scene"></div>
<div class="hud intro">
<div class="eyebrow">Physics / Impulse</div>
<h1>Same area.<br>Different force history.</h1>
<p>
Both raised force–time shapes enclose the same area:
<b>J = 12 N·s</b>. A sharper push uses more peak force for less time.
</p>
</div>
<div class="hud controls">
<button id="reset" type="button">Reset view</button>
</div>
<div class="hud 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.querySelector("#stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Low-poly cart with two equal-area force-time graphs"
);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf6f0e4);
scene.fog = new THREE.Fog(0xf6f0e4, 24, 42);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xfff8e8, 0x7e7568, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d5, 3.2);
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 = 14;
sun.shadow.camera.bottom = -9;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0005;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0x8dc8d0, 0.75);
fillLight.position.set(8, 6, -8);
scene.add(fillLight);
const COLORS = {
ink: 0x193747,
teal: 0x168b87,
orange: 0xef6a3a,
gold: 0xf2c45e,
cream: 0xfffaf0,
floor: 0xe8decc,
rail: 0x87979c,
wheel: 0x27343a
};
const material = (color, extra = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
flatShading: true,
...extra
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(42, 30),
material(COLORS.floor)
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.13;
ground.receiveShadow = true;
scene.add(ground);
function addBox(size, position, mat, parent = scene, edges = false) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
mat
);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (edges) {
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
new THREE.LineBasicMaterial({
color: COLORS.ink,
transparent: true,
opacity: 0.32
})
);
outline.position.copy(mesh.position);
parent.add(outline);
}
return mesh;
}
function cylinderBetween(a, b, radius, mat, radialSegments = 8) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments
),
mat
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
scene.add(mesh);
return mesh;
}
function coneAt(position, direction, size, mat) {
const cone = new THREE.Mesh(
new THREE.ConeGeometry(size, size * 2.7, 7),
mat
);
cone.position.copy(position);
cone.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
cone.castShadow = true;
scene.add(cone);
return cone;
}
function roundedRect(ctx, x, y, w, h, r) {
const radius = Math.min(r, w * 0.5, h * 0.5);
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.arcTo(x + w, y, x + w, y + h, radius);
ctx.arcTo(x + w, y + h, x, y + h, radius);
ctx.arcTo(x, y + h, x, y, radius);
ctx.arcTo(x, y, x + w, y, radius);
ctx.closePath();
}
function createPanel(lines, accent, worldWidth) {
const canvas = document.createElement("canvas");
const width = 1024;
const padding = 34;
const lineGap = 12;
const height = Math.ceil(
padding * 2 +
lines.reduce((sum, line) => sum + line.size * 1.22, 0) +
lineGap * (lines.length - 1)
);
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, width, height);
ctx.save();
ctx.shadowColor = "rgba(30, 47, 53, 0.16)";
ctx.shadowBlur = 22;
ctx.shadowOffsetY = 8;
roundedRect(ctx, 8, 8, width - 16, height - 16, 24);
ctx.fillStyle = "rgba(255, 252, 245, 0.96)";
ctx.fill();
ctx.restore();
roundedRect(ctx, 8, 8, width - 16, height - 16, 24);
ctx.strokeStyle = accent;
ctx.lineWidth = 6;
ctx.stroke();
ctx.fillStyle = accent;
roundedRect(ctx, 8, 8, 20, height - 16, 10);
ctx.fill();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
let y = padding;
for (const line of lines) {
const fontWeight = line.bold ? 800 : 550;
ctx.font = `${fontWeight} ${line.size}px Inter, ui-sans-serif, system-ui, sans-serif`;
ctx.fillStyle = line.color || COLORS.ink;
ctx.fillText(line.text, width * 0.52, y + line.size * 0.61);
y += line.size * 1.22 + lineGap;
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
sprite.scale.set(worldWidth, worldWidth * height / width, 1);
sprite.renderOrder = 30;
return sprite;
}
function createText(
text,
color = "#193747",
worldWidth = 1.2,
fontSize = 48
) {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const font = `700 ${fontSize}px Inter, ui-sans-serif, system-ui, sans-serif`;
ctx.font = font;
canvas.width = Math.ceil(ctx.measureText(text).width + 30);
canvas.height = Math.ceil(fontSize * 1.65);
const context = canvas.getContext("2d");
context.font = font;
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = color;
context.fillText(text, canvas.width / 2, canvas.height / 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
sprite.scale.set(worldWidth, worldWidth * canvas.height / canvas.width, 1);
sprite.renderOrder = 28;
return sprite;
}
const graphBase = 0.68;
const graphZ = -1.75;
const graphFrontZ = -1.39;
const forceScaleMax = 8.8;
const specs = [
{
id: "A",
center: -3.35,
width: 3,
height: 8,
duration: 3,
peak: 8,
accent: COLORS.teal,
accentCSS: "#168b87",
shape: "triangle",
title: "A · SHORT, SHARP PUSH",
equation: "Area = ½ × 3 s × 8 N = 12 N·s"
},
{
id: "B",
center: 3.25,
width: 5,
height: 2.4,
duration: 5,
peak: 2.4,
accent: COLORS.orange,
accentCSS: "#ef6a3a",
shape: "rectangle",
title: "B · LONGER, GENTLER PUSH",
equation: "Area = 5 s × 2.4 N = 12 N·s"
}
];
const chartObjects = [];
const movingMarkers = [];
function addGrid(spec) {
const left = spec.center - spec.width / 2;
const right = spec.center + spec.width / 2;
const positions = [];
for (let force = 2; force <= 8; force += 2) {
positions.push(
left, graphBase + force, -2.12,
right, graphBase + force, -2.12
);
}
const divisions = spec.shape === "triangle" ? 4 : 5;
for (let i = 1; i < divisions; i++) {
const x = left + (spec.width * i) / divisions;
positions.push(
x, graphBase, -2.12,
x, graphBase + forceScaleMax, -2.12
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const grid = new THREE.LineSegments(
geometry,
new THREE.LineBasicMaterial({
color: 0x718188,
transparent: true,
opacity: 0.23
})
);
scene.add(grid);
}
function addAxes(spec) {
const left = spec.center - spec.width / 2;
const right = spec.center + spec.width / 2;
const axisX = left - 0.34;
const axisMat = material(COLORS.ink, { roughness: 0.65 });
cylinderBetween(
new THREE.Vector3(axisX, graphBase, graphFrontZ),
new THREE.Vector3(axisX, graphBase + forceScaleMax, graphFrontZ),
0.021,
axisMat,
6
);
coneAt(
new THREE.Vector3(axisX, graphBase + forceScaleMax + 0.10, graphFrontZ),
new THREE.Vector3(0, 1, 0),
0.075,
axisMat
);
cylinderBetween(
new THREE.Vector3(axisX, graphBase, graphFrontZ),
new THREE.Vector3(right + 0.38, graphBase, graphFrontZ),
0.021,
axisMat,
6
);
coneAt(
new THREE.Vector3(right + 0.46, graphBase, graphFrontZ),
new THREE.Vector3(1, 0, 0),
0.075,
axisMat
);
for (const force of [2, 4, 6, 8]) {
const label = createText(String(force), "#60717a", 0.30, 44);
label.position.set(axisX - 0.24, graphBase + force, graphFrontZ + 0.03);
scene.add(label);
}
const forceLabel = createText("FORCE (N)", "#193747", 1.35, 44);
forceLabel.position.set(axisX - 0.04, graphBase + 5.45, graphFrontZ + 0.04);
scene.add(forceLabel);
const timeLabel = createText("TIME (s)", "#193747", 1.20, 44);
timeLabel.position.set(right - 0.10, graphBase + 0.29, graphFrontZ + 0.04);
scene.add(timeLabel);
const dimensionPositions = [
left, 0.44, graphFrontZ,
right, 0.44, graphFrontZ,
left, 0.34, graphFrontZ,
left, 0.54, graphFrontZ,
right, 0.34, graphFrontZ,
right, 0.54, graphFrontZ
];
const dimensionGeometry = new THREE.BufferGeometry();
dimensionGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dimensionPositions, 3)
);
scene.add(
new THREE.LineSegments(
dimensionGeometry,
new THREE.LineBasicMaterial({ color: spec.accent, opacity: 0.8 })
)
);
const durationLabel = createText(
`${spec.duration} s duration`,
spec.accentCSS,
1.55,
42
);
durationLabel.position.set(spec.center, 0.20, graphFrontZ + 0.06);
scene.add(durationLabel);
}
function addRaisedRibbon(spec) {
const half = spec.width / 2;
const shape = new THREE.Shape();
if (spec.shape === "triangle") {
shape.moveTo(-half, 0);
shape.lineTo(half, 0);
shape.lineTo(0, spec.height);
shape.closePath();
} else {
shape.moveTo(-half, 0);
shape.lineTo(half, 0);
shape.lineTo(half, spec.height);
shape.lineTo(-half, spec.height);
shape.closePath();
}
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.18,
bevelEnabled: false,
curveSegments: 1
});
const baseColor = new THREE.Color(spec.accent);
const frontColor = baseColor.clone().lerp(
new THREE.Color(0xffffff),
0.58
);
const sideColor = baseColor.clone().lerp(
new THREE.Color(0x193747),
0.18
);
const fill = new THREE.Mesh(
geometry,
[
material(frontColor),
material(sideColor)
]
);
fill.position.set(spec.center, graphBase, -2.06);
fill.castShadow = true;
fill.receiveShadow = true;
scene.add(fill);
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
new THREE.LineBasicMaterial({
color: spec.accent,
transparent: true,
opacity: 0.6
})
);
outline.position.copy(fill.position);
scene.add(outline);
const ribbonMat = material(spec.accent, { roughness: 0.58 });
const ribbonDepth = 0.64;
const ribbonThickness = 0.16;
const topPoints = spec.shape === "triangle"
? [
[-half, 0],
[0, spec.height],
[half, 0]
]
: [
[-half, spec.height],
[half, spec.height]
];
for (let i = 0; i < topPoints.length - 1; i++) {
const [ax, ay] = topPoints[i];
const [bx, by] = topPoints[i + 1];
const length = Math.hypot(bx - ax, by - ay);
const angle = Math.atan2(by - ay, bx - ax);
const ribbon = new THREE.Mesh(
new THREE.BoxGeometry(
length + 0.03,
ribbonThickness,
ribbonDepth
),
ribbonMat
);
ribbon.position.set(
spec.center + (ax + bx) / 2,
graphBase + (ay + by) / 2,
graphZ
);
ribbon.rotation.z = angle;
ribbon.castShadow = true;
ribbon.receiveShadow = true;
scene.add(ribbon);
}
const platform = addBox(
new THREE.Vector3(spec.width + 0.95, 0.24, 1.65),
new THREE.Vector3(spec.center, 0.12, graphZ),
material(0xd8cdbb)
);
platform.receiveShadow = true;
}
for (const spec of specs) {
addGrid(spec);
addRaisedRibbon(spec);
addAxes(spec);
const titlePanel = createPanel(
[
{
text: spec.title,
size: 47,
bold: true,
color: spec.accentCSS
},
{
text: `Peak ${spec.peak} N · Duration ${spec.duration} s`,
size: 34,
color: "#193747"
},
{
text: spec.equation,
size: 31,
color: "#60717a"
}
],
spec.accentCSS,
4.35
);
const panelY = spec.shape === "triangle" ? 9.55 : 4.35;
titlePanel.position.set(spec.center, panelY, -1.12);
scene.add(titlePanel);
const areaBadge = createPanel(
[
{
text: "J = 12 N·s",
size: 46,
bold: true,
color: spec.accentCSS
}
],
spec.accentCSS,
2.18
);
areaBadge.position.set(
spec.center,
spec.shape === "triangle" ? 2.15 : 1.58,
-1.22
);
scene.add(areaBadge);
const marker = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.13, 1),
material(COLORS.cream, {
emissive: 0x5b4a22,
emissiveIntensity: 0.12
})
);
marker.castShadow = true;
scene.add(marker);
movingMarkers.push({ spec, marker });
chartObjects.push(spec);
}
// Low-poly cart and rail.
const cartZ = 2.65;
const railMaterial = material(COLORS.rail, { metalness: 0.12 });
for (const z of [cartZ - 0.78, cartZ + 0.78]) {
addBox(
new THREE.Vector3(7.4, 0.16, 0.13),
new THREE.Vector3(0, 0.17, z),
railMaterial
);
}
for (let i = 0; i < 12; i++) {
addBox(
new THREE.Vector3(0.26, 0.08, 2.15),
new THREE.Vector3(-3.5 + i * 0.64, 0.055, cartZ),
material(0xb0a38f)
);
}
addBox(
new THREE.Vector3(3.10, 0.18, 1.72),
new THREE.Vector3(0, 0.65, cartZ),
material(COLORS.ink),
scene,
true
);
addBox(
new THREE.Vector3(2.78, 0.70, 1.55),
new THREE.Vector3(0, 1.08, cartZ),
material(0x315d70),
scene,
true
);
addBox(
new THREE.Vector3(1.16, 1.10, 1.14),
new THREE.Vector3(0, 1.98, cartZ),
material(COLORS.gold),
scene,
true
);
const wheelMaterial = material(COLORS.wheel);
const hubMaterial = material(0xb8c7c8, { metalness: 0.22 });
for (const x of [-0.96, 0.96]) {
for (const z of [cartZ - 0.88, cartZ + 0.88]) {
const wheel = new THREE.Mesh(
new THREE.CylinderGeometry(0.39, 0.39, 0.22, 12),
wheelMaterial
);
wheel.rotation.x = Math.PI / 2;
wheel.position.set(x, 0.47, z);
wheel.castShadow = true;
scene.add(wheel);
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.14, 0.14, 0.245, 10),
hubMaterial
);
hub.rotation.x = Math.PI / 2;
hub.position.copy(wheel.position);
scene.add(hub);
}
}
// Downward force arrow over the cart payload.
const forceArrowMaterial = material(COLORS.orange);
cylinderBetween(
new THREE.Vector3(0, 3.52, cartZ),
new THREE.Vector3(0, 2.92, cartZ),
0.045,
forceArrowMaterial,
7
);
coneAt(
new THREE.Vector3(0, 2.77, cartZ),
new THREE.Vector3(0, -1, 0),
0.105,
forceArrowMaterial
);
const forceLabel = createText("F", "#ef6a3a", 0.45, 60);
forceLabel.position.set(0.42, 3.17, cartZ + 0.08);
scene.add(forceLabel);
const equalPanel = createPanel(
[
{
text: "EQUAL IMPULSE",
size: 54,
bold: true,
color: "#193747"
},
{
text: "J = ∫ F(t) dt = 12 N·s for both pushes",
size: 35,
color: "#536771"
}
],
"#f2c45e",
4.85
);
equalPanel.position.set(0, 4.40, cartZ + 0.08);
scene.add(equalPanel);
// Subtle dashed paths connect the two equal impulse areas to the cart.
function addFlowCurve(start, control, end, color) {
const curve = new THREE.QuadraticBezierCurve3(
start,
control,
end
);
const geometry = new THREE.BufferGeometry().setFromPoints(
curve.getPoints(36)
);
const line = new THREE.Line(
geometry,
new THREE.LineDashedMaterial({
color,
dashSize: 0.14,
gapSize: 0.11,
transparent: true,
opacity: 0.5
})
);
line.computeLineDistances();
scene.add(line);
const tangent = curve.getTangent(0.98).normalize();
coneAt(
end.clone().addScaledVector(tangent, -0.10),
tangent,
0.07,
material(color)
);
}
addFlowCurve(
new THREE.Vector3(-3.35, 2.15, -1.18),
new THREE.Vector3(-2.35, 2.55, 0.65),
new THREE.Vector3(-0.36, 2.28, cartZ),
COLORS.teal
);
addFlowCurve(
new THREE.Vector3(3.25, 1.58, -1.18),
new THREE.Vector3(2.15, 2.40, 0.65),
new THREE.Vector3(0.36, 2.28, cartZ),
COLORS.orange
);
// Custom orbit controls: no addon imports.
const controls = {
target: new THREE.Vector3(0.15, 2.85, 0.0),
theta: 0.18,
phi: 1.12,
radius: 18.5
};
let dragging = false;
let previousX = 0;
let previousY = 0;
let userAdjustedCamera = false;
function fittedRadius() {
const halfWidth = 6.25;
const horizontalFov =
2 * Math.atan(Math.tan(THREE.MathUtils.degToRad(camera.fov / 2)) * camera.aspect);
return Math.max(
18.5,
halfWidth / Math.tan(horizontalFov / 2)
);
}
function updateCamera() {
const spherical = new THREE.Spherical(
controls.radius,
controls.phi,
controls.theta
);
camera.position.setFromSpherical(spherical).add(controls.target);
camera.lookAt(controls.target);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.classList.add("dragging");
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
controls.theta -= dx * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.005,
0.52,
1.43
);
userAdjustedCamera = true;
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius + event.deltaY * 0.012,
11,
34
);
userAdjustedCamera = true;
updateCamera();
},
{ passive: false }
);
document.querySelector("#reset").addEventListener("click", () => {
controls.theta = 0.18;
controls.phi = 1.12;
controls.radius = fittedRadius();
controls.target.set(0.15, 2.85, 0);
userAdjustedCamera = false;
updateCamera();
});
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
if (!userAdjustedCamera) {
controls.radius = fittedRadius();
updateCamera();
}
}
window.addEventListener("resize", resize);
controls.radius = fittedRadius();
updateCamera();
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
const phase = (elapsed * 0.17) % 1;
for (const { spec, marker } of movingMarkers) {
const left = spec.center - spec.width / 2;
let forceFactor;
if (spec.shape === "triangle") {
forceFactor = phase < 0.5
? phase * 2
: (1 - phase) * 2;
} else {
forceFactor = 1;
}
marker.position.set(
left + phase * spec.width,
graphBase + forceFactor * spec.height,
graphFrontZ + 0.12
);
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```