Prompt specification
Build or customize the Three.js 3D scene "Hamper folding sequence" 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: Hamper folding sequence
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/hamper-folding-sequence-x0030
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a folding laundry hamper. Animate the crossed supports rotating inward as the flexible bag narrows between them; keep paired pivots aligned and stop before the frame pieces intersect. Low-poly.
Working Three.js Starter (hamper-folding-sequence-x0030):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Folding Laundry Hamper</title>
<style>
:root {
color-scheme: light;
--ink: #203f42;
--muted: #607476;
--glass: rgba(250, 249, 244, 0.82);
--line: rgba(32, 63, 66, 0.16);
--accent: #c66f4a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e6dc;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
touch-action: none;
}
.title {
position: fixed;
top: 26px;
left: 30px;
z-index: 2;
pointer-events: none;
}
.eyebrow {
margin: 0 0 5px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 38px);
font-weight: 650;
letter-spacing: -0.045em;
}
.subtitle {
margin: 6px 0 0;
max-width: 340px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.controls {
position: fixed;
left: 50%;
bottom: 25px;
z-index: 3;
display: flex;
align-items: center;
gap: 14px;
width: min(680px, calc(100vw - 32px));
padding: 12px 14px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--glass);
box-shadow: 0 14px 42px rgba(43, 59, 56, 0.14);
backdrop-filter: blur(14px);
transform: translateX(-50%);
}
button {
appearance: none;
min-width: 92px;
padding: 10px 13px;
border: 1px solid rgba(32, 63, 66, 0.15);
border-radius: 10px;
background: rgba(255, 255, 255, 0.76);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition: transform 150ms ease, background 150ms ease;
}
button:hover {
background: white;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
.slider-wrap {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 9px 12px;
min-width: 220px;
flex: 1;
}
.slider-label {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
output {
min-width: 40px;
color: var(--ink);
font-variant-numeric: tabular-nums;
text-align: right;
}
input[type="range"] {
grid-column: 1 / -1;
width: 100%;
height: 5px;
margin: 0;
border-radius: 999px;
appearance: none;
background: linear-gradient(
90deg,
var(--accent) 0%,
var(--accent) var(--value, 0%),
rgba(32, 63, 66, 0.14) var(--value, 0%),
rgba(32, 63, 66, 0.14) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #fff;
border-radius: 50%;
appearance: none;
background: var(--accent);
box-shadow: 0 2px 8px rgba(32, 63, 66, 0.24);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #fff;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 2px 8px rgba(32, 63, 66, 0.24);
}
.hint {
position: fixed;
right: 24px;
bottom: 28px;
z-index: 2;
color: rgba(32, 63, 66, 0.58);
font-size: 11px;
pointer-events: none;
}
@media (max-width: 760px) {
.title {
top: 18px;
left: 20px;
}
.subtitle {
max-width: 270px;
}
.controls {
bottom: 16px;
gap: 9px;
padding: 10px;
}
#reset-view {
display: none;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="title">
<p class="eyebrow">Collapsible study</p>
<h1>Folding laundry hamper</h1>
<p class="subtitle">
Paired crossed supports rotate inward while the faceted fabric body pinches
between them. The motion stops at a protected 86% fold limit.
</p>
</header>
<div class="controls" aria-label="Animation controls">
<button id="play" type="button">Pause</button>
<div class="slider-wrap">
<div class="slider-label">
<span>Fold amount</span>
<output id="fold-value" for="fold">0%</output>
</div>
<input
id="fold"
type="range"
min="0"
max="86"
step="0.1"
value="0"
aria-label="Fold amount"
/>
</div>
<button id="reset-view" type="button">Reset view</button>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.querySelector("#scene");
const foldInput = document.querySelector("#fold");
const foldOutput = document.querySelector("#fold-value");
const playButton = document.querySelector("#play");
const resetViewButton = document.querySelector("#reset-view");
const MAX_FOLD = 0.86;
const reducedMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#e9e6dc");
scene.fog = new THREE.Fog("#e9e6dc", 9, 18);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
37,
innerWidth / innerHeight,
0.1,
50
);
const cameraState = {
azimuth: 0.69,
polar: 1.13,
radius: 5.3,
target: new THREE.Vector3(0, 1.02, 0)
};
function updateCamera() {
const { azimuth, polar, radius, target } = cameraState;
const sinPolar = Math.sin(polar);
camera.position.set(
target.x + radius * sinPolar * Math.sin(azimuth),
target.y + radius * Math.cos(polar),
target.z + radius * sinPolar * Math.cos(azimuth)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(
"#fffdf4",
"#81796a",
2.05
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight("#fff4db", 3.15);
keyLight.position.set(4.5, 7, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -4;
keyLight.shadow.camera.right = 4;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 16;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight("#b8d9dc", 1.0);
fillLight.position.set(-4, 3, -3);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: "#ded9cc",
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.012;
ground.receiveShadow = true;
scene.add(ground);
const materials = {
support: new THREE.MeshStandardMaterial({
color: "#b96342",
roughness: 0.7,
metalness: 0.04
}),
frame: new THREE.MeshStandardMaterial({
color: "#244b50",
roughness: 0.64,
metalness: 0.1
}),
pivot: new THREE.MeshStandardMaterial({
color: "#d6aa62",
roughness: 0.43,
metalness: 0.58
}),
bolt: new THREE.MeshStandardMaterial({
color: "#24373a",
roughness: 0.4,
metalness: 0.68
}),
clip: new THREE.MeshStandardMaterial({
color: "#e5d6b9",
roughness: 0.72
})
};
function makeBeam(material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
const unitX = new THREE.Vector3(1, 0, 0);
const beamDirection = new THREE.Vector3();
function placeBeam(mesh, a, b, thickness = 0.09, depth = 0.09) {
mesh.position.copy(a).add(b).multiplyScalar(0.5);
beamDirection.subVectors(b, a);
const length = beamDirection.length();
mesh.scale.set(length, thickness, depth);
mesh.quaternion.setFromUnitVectors(unitX, beamDirection.normalize());
}
// Two crossed supports on the front and two matching ones on the back.
const frontBars = [makeBeam(materials.support), makeBeam(materials.support)];
const backBars = [makeBeam(materials.support), makeBeam(materials.support)];
const topFrame = [
makeBeam(materials.frame),
makeBeam(materials.frame),
makeBeam(materials.frame),
makeBeam(materials.frame)
];
const bottomFrame = [
makeBeam(materials.frame),
makeBeam(materials.frame),
makeBeam(materials.frame),
makeBeam(materials.frame)
];
const clips = [];
for (let i = 0; i < 4; i++) {
const clip = new THREE.Mesh(
new THREE.BoxGeometry(0.14, 0.14, 0.1),
materials.clip
);
clip.castShadow = true;
scene.add(clip);
clips.push(clip);
}
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.035, 1, 10),
materials.bolt
);
axle.rotation.x = Math.PI / 2;
axle.castShadow = true;
scene.add(axle);
const pivotDiscs = [];
const pivotBoltHeads = [];
for (let i = 0; i < 2; i++) {
const disc = new THREE.Mesh(
new THREE.CylinderGeometry(0.145, 0.145, 0.11, 12),
materials.pivot
);
disc.rotation.x = Math.PI / 2;
disc.castShadow = true;
scene.add(disc);
pivotDiscs.push(disc);
const bolt = new THREE.Mesh(
new THREE.CylinderGeometry(0.047, 0.047, 0.125, 8),
materials.bolt
);
bolt.rotation.x = Math.PI / 2;
bolt.castShadow = true;
scene.add(bolt);
pivotBoltHeads.push(bolt);
}
/*
The bag is deliberately non-indexed so every polygon can keep a crisp,
low-poly face normal. Five octagonal rings form a soft, pinched body.
*/
const RING_COUNT = 5;
const SIDES = 8;
const TRIANGLES_PER_QUAD = 2;
const BODY_VERTEX_COUNT =
(RING_COUNT - 1) * SIDES * TRIANGLES_PER_QUAD * 3;
const bagPositions = new Float32Array(BODY_VERTEX_COUNT * 3);
const bagColors = new Float32Array(BODY_VERTEX_COUNT * 3);
const bagGeometry = new THREE.BufferGeometry();
bagGeometry.setAttribute(
"position",
new THREE.BufferAttribute(bagPositions, 3)
);
bagGeometry.setAttribute(
"color",
new THREE.BufferAttribute(bagColors, 3)
);
const fabricPalette = [
new THREE.Color("#91b8ae"),
new THREE.Color("#a8c8b9"),
new THREE.Color("#bdd2bf"),
new THREE.Color("#82aaa4")
];
let bodyVertex = 0;
for (let ring = 0; ring < RING_COUNT - 1; ring++) {
for (let side = 0; side < SIDES; side++) {
const color =
fabricPalette[(ring * 3 + side) % fabricPalette.length];
for (let triangle = 0; triangle < 2; triangle++) {
for (let component = 0; component < 3; component++) {
bagColors[bodyVertex * 3] = color.r;
bagColors[bodyVertex * 3 + 1] = color.g;
bagColors[bodyVertex * 3 + 2] = color.b;
bodyVertex++;
}
}
}
}
const bagMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.94,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide
});
const bag = new THREE.Mesh(bagGeometry, bagMaterial);
bag.castShadow = true;
bag.receiveShadow = true;
scene.add(bag);
// Recessed, dark polygon used as the visible mouth of the bag.
const openingPositions = new Float32Array((SIDES + 1) * 3);
const openingGeometry = new THREE.BufferGeometry();
openingGeometry.setAttribute(
"position",
new THREE.BufferAttribute(openingPositions, 3)
);
const openingIndices = [];
for (let i = 0; i < SIDES; i++) {
openingIndices.push(0, i + 1, (i + 1) % SIDES + 1);
}
openingGeometry.setIndex(openingIndices);
const opening = new THREE.Mesh(
openingGeometry,
new THREE.MeshBasicMaterial({
color: "#263f40",
side: THREE.DoubleSide
})
);
scene.add(opening);
const seamPositions = new Float32Array(SIDES * 3);
const seamGeometry = new THREE.BufferGeometry();
seamGeometry.setAttribute(
"position",
new THREE.BufferAttribute(seamPositions, 3)
);
const seam = new THREE.LineLoop(
seamGeometry,
new THREE.LineBasicMaterial({
color: "#456965",
transparent: true,
opacity: 0.55
})
);
scene.add(seam);
// Faceted bundles suggest that this is a laundry hamper.
const laundry = [];
const laundryColors = ["#df9b72", "#e8d69f", "#719aa6"];
for (let i = 0; i < laundryColors.length; i++) {
const bundle = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.235, 0),
new THREE.MeshStandardMaterial({
color: laundryColors[i],
roughness: 0.96,
flatShading: true
})
);
bundle.castShadow = true;
bundle.receiveShadow = true;
bundle.rotation.set(0.2 + i * 0.3, i * 1.7, 0.18 * i);
scene.add(bundle);
laundry.push(bundle);
}
function smoothstep(t) {
return t * t * (3 - 2 * t);
}
function lerp(a, b, t) {
return a + (b - a) * t;
}
function perimeterPoint(index, width, depth, y, target) {
const c = Math.min(width, depth) * 0.18;
const points = [
[-width + c, -depth],
[width - c, -depth],
[width, -depth + c],
[width, depth - c],
[width - c, depth],
[-width + c, depth],
[-width, depth - c],
[-width, -depth + c]
];
const point = points[index];
target.set(point[0], y, point[1]);
return target;
}
const pointA = new THREE.Vector3();
const pointB = new THREE.Vector3();
const pointC = new THREE.Vector3();
function writeBodyVertex(vertexIndex, point) {
bagPositions[vertexIndex * 3] = point.x;
bagPositions[vertexIndex * 3 + 1] = point.y;
bagPositions[vertexIndex * 3 + 2] = point.z;
}
function updateMechanism(foldAmount) {
const normalized = THREE.MathUtils.clamp(
foldAmount / MAX_FOLD,
0,
1
);
const eased = smoothstep(normalized);
const bottomY = 0.28;
const bottomHalfWidth = 0.62;
const bottomHalfDepth = 0.62;
const topHalfWidth = lerp(0.98, 0.22, eased);
const topY = lerp(2.2, 1.82, eased);
const supportZ = lerp(0.76, 0.55, eased);
/*
The two diagonals of each X share the same mathematical intersection.
Front and back use identical X/Y dimensions, so their paired pivots
remain perfectly aligned across the width of the hamper.
*/
const pivotY =
bottomY +
(bottomHalfWidth * (topY - bottomY)) /
(bottomHalfWidth + topHalfWidth);
const frontZ = supportZ;
const backZ = -supportZ;
placeBeam(
frontBars[0],
new THREE.Vector3(-bottomHalfWidth, bottomY, frontZ),
new THREE.Vector3(topHalfWidth, topY, frontZ),
0.105,
0.095
);
placeBeam(
frontBars[1],
new THREE.Vector3(bottomHalfWidth, bottomY, frontZ),
new THREE.Vector3(-topHalfWidth, topY, frontZ),
0.105,
0.095
);
placeBeam(
backBars[0],
new THREE.Vector3(-bottomHalfWidth, bottomY, backZ),
new THREE.Vector3(topHalfWidth, topY, backZ),
0.105,
0.095
);
placeBeam(
backBars[1],
new THREE.Vector3(bottomHalfWidth, bottomY, backZ),
new THREE.Vector3(-topHalfWidth, topY, backZ),
0.105,
0.095
);
const topDepth = supportZ - 0.055;
// Top rectangular rim.
placeBeam(
topFrame[0],
new THREE.Vector3(-topHalfWidth, topY, topDepth),
new THREE.Vector3(topHalfWidth, topY, topDepth),
0.075,
0.075
);
placeBeam(
topFrame[1],
new THREE.Vector3(-topHalfWidth, topY, -topDepth),
new THREE.Vector3(topHalfWidth, topY, -topDepth),
0.075,
0.075
);
placeBeam(
topFrame[2],
new THREE.Vector3(-topHalfWidth, topY, -topDepth),
new THREE.Vector3(-topHalfWidth, topY, topDepth),
0.075,
0.075
);
placeBeam(
topFrame[3],
new THREE.Vector3(topHalfWidth, topY, -topDepth),
new THREE.Vector3(topHalfWidth, topY, topDepth),
0.075,
0.075
);
// Bottom rectangular base.
const bottomDepth = supportZ - 0.055;
placeBeam(
bottomFrame[0],
new THREE.Vector3(-bottomHalfWidth, bottomY, bottomDepth),
new THREE.Vector3(bottomHalfWidth, bottomY, bottomDepth),
0.085,
0.085
);
placeBeam(
bottomFrame[1],
new THREE.Vector3(-bottomHalfWidth, bottomY, -bottomDepth),
new THREE.Vector3(bottomHalfWidth, bottomY, -bottomDepth),
0.085,
0.085
);
placeBeam(
bottomFrame[2],
new THREE.Vector3(-bottomHalfWidth, bottomY, -bottomDepth),
new THREE.Vector3(-bottomHalfWidth, bottomY, bottomDepth),
0.085,
0.085
);
placeBeam(
bottomFrame[3],
new THREE.Vector3(bottomHalfWidth, bottomY, -bottomDepth),
new THREE.Vector3(bottomHalfWidth, bottomY, bottomDepth),
0.085,
0.085
);
// Four fabric attachment clips.
let clipIndex = 0;
for (const xSign of [-1, 1]) {
for (const zSign of [-1, 1]) {
clips[clipIndex].position.set(
xSign * topHalfWidth,
topY,
zSign * topDepth
);
clips[clipIndex].scale.set(
1,
1,
1
);
clipIndex++;
}
}
// Shared axle and the two aligned pivot caps.
axle.position.set(0, pivotY, 0);
axle.scale.y = (supportZ + 0.15) * 2;
pivotDiscs[0].position.set(0, pivotY, supportZ + 0.065);
pivotDiscs[1].position.set(0, pivotY, -supportZ - 0.065);
pivotBoltHeads[0].position.set(0, pivotY, supportZ + 0.075);
pivotBoltHeads[1].position.set(0, pivotY, -supportZ - 0.075);
/*
Flexible bag profile. The middle ring follows the shared pivot height
and becomes the narrowest part of the body while folding.
*/
const lowerY = lerp(bottomY, topY, 0.23);
const upperY = Math.max(
pivotY + 0.085,
lerp(bottomY, topY, 0.76)
);
const rings = [
{
y: bottomY + 0.025,
width: bottomHalfWidth,
depth: supportZ - 0.07
},
{
y: lowerY,
width: lerp(0.7, 0.52, eased),
depth: lerp(0.67, 0.43, eased)
},
{
y: pivotY,
width: lerp(0.7, 0.22, eased),
depth: lerp(0.6, 0.305, eased)
},
{
y: upperY,
width: lerp(0.88, 0.25, eased),
depth: lerp(0.67, 0.39, eased)
},
{
y: topY,
width: topHalfWidth,
depth: topDepth
}
];
bodyVertex = 0;
for (let ring = 0; ring < RING_COUNT - 1; ring++) {
const lower = rings[ring];
const upper = rings[ring + 1];
for (let side = 0; side < SIDES; side++) {
const next = (side + 1) % SIDES;
perimeterPoint(side, lower.width, lower.depth, lower.y, pointA);
perimeterPoint(next, lower.width, lower.depth, lower.y, pointB);
perimeterPoint(side, upper.width, upper.depth, upper.y, pointC);
writeBodyVertex(bodyVertex++, pointA);
writeBodyVertex(bodyVertex++, pointC);
writeBodyVertex(bodyVertex++, pointB);
perimeterPoint(next, lower.width, lower.depth, lower.y, pointA);
perimeterPoint(side, upper.width, upper.depth, upper.y, pointB);
perimeterPoint(next, upper.width, upper.depth, upper.y, pointC);
writeBodyVertex(bodyVertex++, pointA);
writeBodyVertex(bodyVertex++, pointB);
writeBodyVertex(bodyVertex++, pointC);
}
}
bagGeometry.attributes.position.needsUpdate = true;
bagGeometry.computeVertexNormals();
bagGeometry.computeBoundingSphere();
// Update the recessed opening.
openingPositions[0] = 0;
openingPositions[1] = topY - 0.045;
openingPositions[2] = 0;
for (let i = 0; i < SIDES; i++) {
perimeterPoint(
i,
topHalfWidth * 0.91,
topDepth * 0.91,
topY - 0.045,
pointA
);
openingPositions[(i + 1) * 3] = pointA.x;
openingPositions[(i + 1) * 3 + 1] = pointA.y;
openingPositions[(i + 1) * 3 + 2] = pointA.z;
}
openingGeometry.attributes.position.needsUpdate = true;
openingGeometry.computeBoundingSphere();
// Waist seam.
const waist = rings[2];
for (let i = 0; i < SIDES; i++) {
perimeterPoint(i, waist.width, waist.depth, waist.y + 0.003, pointA);
seamPositions[i * 3] = pointA.x;
seamPositions[i * 3 + 1] = pointA.y;
seamPositions[i * 3 + 2] = pointA.z;
}
seamGeometry.attributes.position.needsUpdate = true;
seamGeometry.computeBoundingSphere();
// Laundry bundles compress and move inward with the mouth.
const bundleSpread = topHalfWidth / 0.98;
laundry[0].position.set(-0.31 * bundleSpread, topY + 0.035, 0.06);
laundry[1].position.set(0.02 * bundleSpread, topY + 0.055, -0.08);
laundry[2].position.set(0.32 * bundleSpread, topY + 0.025, 0.05);
laundry[0].scale.set(0.72 + 0.28 * bundleSpread, 0.8, 1);
laundry[1].scale.set(0.65 + 0.35 * bundleSpread, 0.72, 0.94);
laundry[2].scale.set(0.7 + 0.3 * bundleSpread, 0.84, 0.98);
}
// Direct camera controls; no examples/addons are used.
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.azimuth -= deltaX * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar - deltaY * 0.005,
0.42,
1.45
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.003,
3.5,
8
);
updateCamera();
},
{ passive: false }
);
function resetView() {
cameraState.azimuth = 0.69;
cameraState.polar = 1.13;
cameraState.radius = 5.3;
cameraState.target.set(0, 1.02, 0);
updateCamera();
}
renderer.domElement.addEventListener("dblclick", resetView);
resetViewButton.addEventListener("click", resetView);
let foldAmount = 0;
let autoPlay = !reducedMotion;
let direction = 1;
let holdTime = 0;
function updateControlDisplay() {
const percent = foldAmount * 100;
foldInput.value = foldAmount * 100;
foldOutput.value = `${percent.toFixed(0)}%`;
foldInput.style.setProperty(
"--value",
`${(foldAmount / MAX_FOLD) * 100}%`
);
}
function setAutoPlay(enabled) {
autoPlay = enabled;
playButton.textContent = autoPlay ? "Pause" : "Play";
playButton.setAttribute("aria-pressed", String(!autoPlay));
}
playButton.addEventListener("click", () => {
setAutoPlay(!autoPlay);
if (autoPlay) {
direction = foldAmount >= MAX_FOLD - 0.001 ? -1 : 1;
holdTime = 0;
}
});
foldInput.addEventListener("input", () => {
setAutoPlay(false);
foldAmount = Number(foldInput.value) / 100;
updateControlDisplay();
});
setAutoPlay(autoPlay);
updateMechanism(foldAmount);
updateControlDisplay();
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (autoPlay) {
if (holdTime > 0) {
holdTime -= delta;
} else {
foldAmount += direction * delta * 0.27;
if (foldAmount >= MAX_FOLD) {
foldAmount = MAX_FOLD;
direction = -1;
holdTime = 0.85;
} else if (foldAmount <= 0) {
foldAmount = 0;
direction = 1;
holdTime = 0.85;
}
}
}
updateMechanism(foldAmount);
updateControlDisplay();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>
```