Prompt specification
Build or customize the Three.js 3D scene "Single-Bend Bracket Transformation" 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: Single-Bend Bracket Transformation
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/single-bend-bracket-transformation-x2074
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Animate a sheet folding around a fixed line into an L-bracket. Keep connected edges coincident and explicitly simplify bend radius and material deformation.
Working Three.js Starter (single-bend-bracket-transformation-x2074):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Low-Poly Sheet Fold</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(10, 16, 23, 0.82);
--border: rgba(255, 255, 255, 0.12);
--text: #eef4f8;
--muted: #9aabb8;
--accent: #f39a3d;
--cyan: #64d8df;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101820;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
z-index: 10;
top: 20px;
left: 20px;
width: min(340px, calc(100vw - 40px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(12px);
user-select: none;
}
.eyebrow {
margin: 0 0 6px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 23px;
line-height: 1.15;
letter-spacing: -0.03em;
}
.subtitle {
margin: 9px 0 15px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.badge {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
border: 1px solid rgba(243, 154, 61, 0.35);
border-radius: 999px;
background: rgba(243, 154, 61, 0.1);
color: #ffc27c;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.badge::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--accent);
content: "";
box-shadow: 0 0 12px var(--accent);
}
.model {
margin-top: 15px;
padding-top: 14px;
border-top: 1px solid var(--border);
}
.model-title {
margin: 0 0 9px;
font-size: 12px;
font-weight: 750;
}
.model p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 13px;
}
.stat {
padding: 9px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
}
.stat dt {
margin: 0 0 3px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.stat dd {
margin: 0;
color: #f6f9fb;
font-size: 12px;
font-weight: 650;
}
.controls {
position: fixed;
z-index: 10;
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 12px;
width: min(620px, calc(100vw - 32px));
padding: 12px;
transform: translateX(-50%);
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(12px);
user-select: none;
}
.angle-readout {
min-width: 62px;
color: var(--accent);
font-size: 22px;
font-weight: 800;
font-variant-numeric: tabular-nums;
text-align: center;
}
.slider-wrap {
flex: 1;
min-width: 90px;
}
input[type="range"] {
width: 100%;
margin: 0;
accent-color: var(--accent);
cursor: pointer;
}
.status {
min-width: 82px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.09em;
text-align: center;
text-transform: uppercase;
}
button {
min-height: 38px;
padding: 0 14px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 9px;
background: rgba(255, 255, 255, 0.065);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: background 140ms ease, border-color 140ms ease,
transform 140ms ease;
}
button:hover {
border-color: rgba(255, 255, 255, 0.25);
background: rgba(255, 255, 255, 0.11);
}
button:active {
transform: translateY(1px);
}
button.primary {
border-color: rgba(243, 154, 61, 0.55);
background: var(--accent);
color: #1c1309;
}
button.primary:hover {
background: #ffab55;
}
.hint {
position: fixed;
z-index: 8;
right: 18px;
bottom: 18px;
padding: 8px 10px;
border-radius: 8px;
background: rgba(10, 16, 23, 0.55);
color: rgba(238, 244, 248, 0.65);
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
}
@media (max-width: 720px) {
.hud {
top: 12px;
left: 12px;
width: min(310px, calc(100vw - 24px));
padding: 14px;
}
.stats,
.model {
display: none;
}
.controls {
bottom: 12px;
gap: 8px;
padding: 9px;
}
.status {
display: none;
}
button {
padding: 0 10px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly sheet folding into an L-bracket"></canvas>
<section class="hud">
<p class="eyebrow">Mechanical Study / 01</p>
<h1>Fixed-Line Sheet Fold</h1>
<p class="subtitle">
A rigid two-panel sheet rotates around one stationary longitudinal crease
until it forms a 90-degree L-bracket.
</p>
<div class="badge">Idealized kinematic model</div>
<div class="model">
<p class="model-title">Explicit simplification</p>
<p>
Bend radius is zero. The sheet is represented by its zero-thickness
midsurface. Both panels remain rigid and inextensible, so the animation
includes no stretching, compression, or local bending deformation.
</p>
<dl class="stats">
<div class="stat">
<dt>Crease</dt>
<dd>Fixed Z axis</dd>
</div>
<div class="stat">
<dt>Bend radius</dt>
<dd>r = 0</dd>
</div>
<div class="stat">
<dt>Material</dt>
<dd>Rigid</dd>
</div>
<div class="stat">
<dt>Stretch</dt>
<dd>None</dd>
</div>
</dl>
</div>
</section>
<section class="controls" aria-label="Fold controls">
<div class="angle-readout" id="angleValue">0°</div>
<div class="slider-wrap">
<input
id="angleSlider"
type="range"
min="0"
max="90"
step="0.1"
value="0"
aria-label="Fold angle"
>
</div>
<div class="status" id="status">Folding</div>
<button id="foldButton" class="primary" type="button">Unfold</button>
<button id="replayButton" type="button">Replay</button>
<button id="cameraButton" type="button">Camera</button>
</section>
<div class="hint">Drag to orbit · Wheel to zoom · Space to reverse</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101820);
scene.fog = new THREE.Fog(0x101820, 9, 19);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(
0xd9efff,
0x20252b,
2.1
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1dc, 3.4);
keyLight.position.set(-3.5, 7.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x72d9ff, 1.25);
rimLight.position.set(5, 3, -5);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x18232d,
roughness: 0.96,
metalness: 0.03
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.045;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(18, 36, 0x557182, 0x304450);
grid.position.y = -0.038;
grid.material.transparent = true;
grid.material.opacity = 0.28;
grid.material.depthWrite = false;
scene.add(grid);
// A few subdued faceted objects provide scale and reinforce the low-poly style.
const rockGeometry = new THREE.IcosahedronGeometry(1, 0);
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x243441,
roughness: 0.9,
metalness: 0.05,
flatShading: true
});
const rockData = [
[-5.2, -0.1, -3.5, 0.65],
[4.5, -0.18, -4.0, 0.9],
[-5.8, -0.25, 3.8, 0.55],
[5.6, -0.22, 3.2, 0.7]
];
for (const [x, y, z, scale] of rockData) {
const rock = new THREE.Mesh(rockGeometry, rockMaterial);
rock.position.set(x, y, z);
rock.scale.set(scale, scale * 0.65, scale * 0.8);
rock.rotation.set(0.2, x * 0.7, 0.1);
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
// -------------------------------------------------------------------------
// Sheet geometry
//
// The sheet is a single indexed surface. The two rows of vertices at x = 0
// are shared by the fixed and moving panels, so their hinge edges are
// topologically connected and remain exactly coincident at every angle.
//
// Simplifications:
// - bend radius = 0
// - zero-thickness midsurface
// - rigid, inextensible panels
// - no local material deformation
// -------------------------------------------------------------------------
const FIXED_LENGTH = 2.6;
const MOVING_LENGTH = 2.25;
const SHEET_DEPTH = 3.15;
const xCoordinates = [
-FIXED_LENGTH,
-FIXED_LENGTH * 0.5,
0,
MOVING_LENGTH * 0.5,
MOVING_LENGTH
];
const zCoordinates = [
-SHEET_DEPTH * 0.5,
0,
SHEET_DEPTH * 0.5
];
const rowCount = zCoordinates.length;
const columnCount = xCoordinates.length;
const vertexCount = columnCount * rowCount;
const sheetPositions = new Float32Array(vertexCount * 3);
const sheetIndices = [];
for (let column = 0; column < columnCount; column++) {
for (let row = 0; row < rowCount; row++) {
const index = column * rowCount + row;
sheetPositions[index * 3] = xCoordinates[column];
sheetPositions[index * 3 + 1] = 0;
sheetPositions[index * 3 + 2] = zCoordinates[row];
}
}
const sheetGeometry = new THREE.BufferGeometry();
sheetGeometry.setAttribute(
"position",
new THREE.BufferAttribute(sheetPositions, 3).setUsage(
THREE.DynamicDrawUsage
)
);
const sheetMaterials = [
new THREE.MeshStandardMaterial({
color: 0xe98a2e,
roughness: 0.48,
metalness: 0.32,
side: THREE.DoubleSide,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xf09a3c,
roughness: 0.46,
metalness: 0.32,
side: THREE.DoubleSide,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xdc7925,
roughness: 0.52,
metalness: 0.3,
side: THREE.DoubleSide,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xed9133,
roughness: 0.49,
metalness: 0.31,
side: THREE.DoubleSide,
flatShading: true
})
];
let groupNumber = 0;
for (let column = 0; column < columnCount - 1; column++) {
for (let row = 0; row < rowCount - 1; row++) {
const a = column * rowCount + row;
const b = (column + 1) * rowCount + row;
const c = (column + 1) * rowCount + row + 1;
const d = column * rowCount + row + 1;
// Counter-clockwise winding when viewed from +Y.
sheetIndices.push(a, d, b, b, d, c);
sheetGeometry.addGroup(
groupNumber * 6,
6,
(column + row * 2) % sheetMaterials.length
);
groupNumber++;
}
}
sheetGeometry.setIndex(sheetIndices);
const sheet = new THREE.Mesh(sheetGeometry, sheetMaterials);
sheet.castShadow = true;
sheet.receiveShadow = true;
scene.add(sheet);
// -------------------------------------------------------------------------
// Panel edge details
// -------------------------------------------------------------------------
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x633716,
transparent: true,
opacity: 0.72
});
function createLineSegments(points) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(points, 3)
);
return new THREE.LineSegments(geometry, edgeMaterial);
}
const lineOffset = 0.007;
const fixedEdges = createLineSegments([
// Left short edge.
-FIXED_LENGTH, lineOffset, -SHEET_DEPTH * 0.5,
-FIXED_LENGTH, lineOffset, SHEET_DEPTH * 0.5,
// Two longitudinal edges.
-FIXED_LENGTH, lineOffset, -SHEET_DEPTH * 0.5,
0, lineOffset, -SHEET_DEPTH * 0.5,
-FIXED_LENGTH, lineOffset, SHEET_DEPTH * 0.5,
0, lineOffset, SHEET_DEPTH * 0.5,
// Panel seam.
-FIXED_LENGTH * 0.5, lineOffset, -SHEET_DEPTH * 0.5,
-FIXED_LENGTH * 0.5, lineOffset, SHEET_DEPTH * 0.5,
// Center seam.
-FIXED_LENGTH, lineOffset, 0,
0, lineOffset, 0
]);
scene.add(fixedEdges);
const movingEdges = new THREE.Group();
const movingEdgeMesh = createLineSegments([
// Outer short edge.
MOVING_LENGTH, lineOffset, -SHEET_DEPTH * 0.5,
MOVING_LENGTH, lineOffset, SHEET_DEPTH * 0.5,
// Two longitudinal edges.
0, lineOffset, -SHEET_DEPTH * 0.5,
MOVING_LENGTH, lineOffset, -SHEET_DEPTH * 0.5,
0, lineOffset, SHEET_DEPTH * 0.5,
MOVING_LENGTH, lineOffset, SHEET_DEPTH * 0.5,
// Panel seam.
MOVING_LENGTH * 0.5, lineOffset, -SHEET_DEPTH * 0.5,
MOVING_LENGTH * 0.5, lineOffset, SHEET_DEPTH * 0.5,
// Center seam.
0, lineOffset, 0,
MOVING_LENGTH, lineOffset, 0
]);
movingEdges.add(movingEdgeMesh);
scene.add(movingEdges);
// -------------------------------------------------------------------------
// Fixed crease marker and low-poly fasteners
// -------------------------------------------------------------------------
const hardwareMaterial = new THREE.MeshStandardMaterial({
color: 0x26333d,
roughness: 0.34,
metalness: 0.75,
flatShading: true
});
// This is only a visual crease marker; the model's actual bend radius is 0.
const creaseMarker = new THREE.Mesh(
new THREE.CylinderGeometry(
0.045,
0.045,
SHEET_DEPTH + 0.52,
8
),
hardwareMaterial
);
creaseMarker.rotation.x = Math.PI / 2;
creaseMarker.castShadow = true;
scene.add(creaseMarker);
const boltGeometry = new THREE.CylinderGeometry(
0.075,
0.085,
0.038,
6
);
function addBolt(parent, x, z) {
const bolt = new THREE.Mesh(boltGeometry, hardwareMaterial);
bolt.position.set(x, 0.027, z);
bolt.rotation.y = Math.PI / 6;
bolt.castShadow = true;
parent.add(bolt);
}
const fixedHardware = new THREE.Group();
for (const x of [-2.12, -0.52]) {
for (const z of [-0.92, 0.92]) {
addBolt(fixedHardware, x, z);
}
}
scene.add(fixedHardware);
const movingHardware = new THREE.Group();
for (const x of [0.58, 1.67]) {
for (const z of [-0.92, 0.92]) {
addBolt(movingHardware, x, z);
}
}
scene.add(movingHardware);
// -------------------------------------------------------------------------
// Dynamic angle guide
// -------------------------------------------------------------------------
const guideRadius = 0.72;
const guideZ = SHEET_DEPTH * 0.5 + 0.4;
const guideSegments = 28;
const guideMaterial = new THREE.LineBasicMaterial({
color: 0x64d8df,
transparent: true,
opacity: 0.9,
depthTest: false
});
const baselineGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, guideZ),
new THREE.Vector3(guideRadius, 0, guideZ)
]);
const baseline = new THREE.Line(baselineGeometry, guideMaterial);
baseline.renderOrder = 5;
scene.add(baseline);
const arcPositions = new Float32Array((guideSegments + 1) * 3);
const arcGeometry = new THREE.BufferGeometry();
arcGeometry.setAttribute(
"position",
new THREE.BufferAttribute(arcPositions, 3).setUsage(
THREE.DynamicDrawUsage
)
);
const arc = new THREE.Line(arcGeometry, guideMaterial);
arc.renderOrder = 5;
scene.add(arc);
const radialPositions = new Float32Array(6);
const radialGeometry = new THREE.BufferGeometry();
radialGeometry.setAttribute(
"position",
new THREE.BufferAttribute(radialPositions, 3).setUsage(
THREE.DynamicDrawUsage
)
);
const radial = new THREE.Line(radialGeometry, guideMaterial);
radial.renderOrder = 5;
scene.add(radial);
// -------------------------------------------------------------------------
// Fold state and geometry update
// -------------------------------------------------------------------------
const MAX_ANGLE = Math.PI / 2;
let currentAngle = 0;
let targetAngle = MAX_ANGLE;
let playing = true;
function updateSheet(angle) {
const cosine = Math.cos(angle);
const sine = Math.sin(angle);
const positions = sheetGeometry.attributes.position.array;
for (let column = 0; column < columnCount; column++) {
for (let row = 0; row < rowCount; row++) {
const index = column * rowCount + row;
const localX = xCoordinates[column];
let worldX = localX;
let worldY = 0;
// Column 2 is the shared crease and is deliberately locked to the
// fixed Z-axis. Only the moving panel is transformed.
if (column > 2) {
worldX = localX * cosine;
worldY = localX * sine;
}
positions[index * 3] = worldX;
positions[index * 3 + 1] = worldY;
positions[index * 3 + 2] = zCoordinates[row];
}
}
sheetGeometry.attributes.position.needsUpdate = true;
sheetGeometry.computeVertexNormals();
sheetGeometry.attributes.normal.needsUpdate = true;
movingEdges.rotation.z = angle;
movingHardware.rotation.z = angle;
for (let i = 0; i <= guideSegments; i++) {
const t = i / guideSegments;
const guideAngle = angle * t;
arcPositions[i * 3] = guideRadius * Math.cos(guideAngle);
arcPositions[i * 3 + 1] = guideRadius * Math.sin(guideAngle);
arcPositions[i * 3 + 2] = guideZ;
}
arcGeometry.attributes.position.needsUpdate = true;
radialPositions[0] = 0;
radialPositions[1] = 0;
radialPositions[2] = guideZ;
radialPositions[3] = guideRadius * Math.cos(angle);
radialPositions[4] = guideRadius * Math.sin(angle);
radialPositions[5] = guideZ;
radialGeometry.attributes.position.needsUpdate = true;
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
const angleValue = document.querySelector("#angleValue");
const angleSlider = document.querySelector("#angleSlider");
const status = document.querySelector("#status");
const foldButton = document.querySelector("#foldButton");
const replayButton = document.querySelector("#replayButton");
const cameraButton = document.querySelector("#cameraButton");
function updateUI() {
const degrees = THREE.MathUtils.radToDeg(currentAngle);
angleValue.textContent = `${degrees.toFixed(0)}°`;
angleSlider.value = degrees.toFixed(1);
if (playing) {
status.textContent =
targetAngle > currentAngle + 0.005 ? "Folding" : "Unfolding";
} else if (Math.abs(currentAngle - MAX_ANGLE) < 0.002) {
status.textContent = "Locked";
} else if (currentAngle < 0.002) {
status.textContent = "Flat";
} else {
status.textContent = "Scrubbed";
}
foldButton.textContent =
targetAngle > MAX_ANGLE * 0.5 ? "Unfold" : "Fold";
}
foldButton.addEventListener("click", () => {
targetAngle =
targetAngle > MAX_ANGLE * 0.5 ? 0 : MAX_ANGLE;
playing = true;
});
replayButton.addEventListener("click", () => {
currentAngle = 0;
targetAngle = MAX_ANGLE;
playing = true;
updateSheet(currentAngle);
});
angleSlider.addEventListener("input", () => {
currentAngle = THREE.MathUtils.degToRad(
Number(angleSlider.value)
);
targetAngle = currentAngle;
playing = false;
updateSheet(currentAngle);
});
// -------------------------------------------------------------------------
// Camera controls implemented directly without addons
// -------------------------------------------------------------------------
const orbit = {
theta: -0.72,
phi: 0.98,
radius: 8.2,
target: new THREE.Vector3(-0.35, 0.78, 0)
};
const defaultOrbit = {
theta: orbit.theta,
phi: orbit.phi,
radius: orbit.radius,
target: orbit.target.clone()
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.theta = defaultOrbit.theta;
orbit.phi = defaultOrbit.phi;
orbit.radius = defaultOrbit.radius;
orbit.target.copy(defaultOrbit.target);
updateCamera();
}
cameraButton.addEventListener("click", resetCamera);
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.theta -= deltaX * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - deltaY * 0.006,
0.22,
Math.PI - 0.22
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (
canvas.hasPointerCapture &&
event.pointerId !== undefined
) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
4.2,
14
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
foldButton.click();
} else if (event.key.toLowerCase() === "r") {
replayButton.click();
} else if (event.key.toLowerCase() === "c") {
resetCamera();
}
});
// -------------------------------------------------------------------------
// Resize and animation loop
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
updateCamera();
updateSheet(currentAngle);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
// Exponential damping produces a smooth, easily reversible fold.
const smoothing = 1 - Math.exp(-3.1 * delta);
currentAngle += (targetAngle - currentAngle) * smoothing;
if (
Math.abs(targetAngle - currentAngle) < 0.00015
) {
currentAngle = targetAngle;
playing = false;
}
}
updateSheet(currentAngle);
updateUI();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```