Prompt specification
Build or customize the Three.js 3D scene "Pedal force demonstration" 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: Pedal force demonstration
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pedal-force-demonstration-x0054
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a pedal-operated waste bin. Show a foot pressing the pedal as the lid rises smoothly; mark the pivot locations and return the lid only when the pedal is released. Low-poly.
Working Three.js Starter (pedal-force-demonstration-x0054):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pedal-Operated Waste Bin</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(13, 25, 31, 0.88);
--border: rgba(255, 255, 255, 0.14);
--text: #f4f7f5;
--muted: #afc0c2;
--accent: #f3bb45;
--green: #60d0ad;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9e8;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.hud {
position: fixed;
z-index: 5;
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 18px 20px;
color: var(--text);
background: var(--panel);
border: 1px solid var(--border);
border-radius: 16px;
backdrop-filter: blur(12px);
box-shadow: 0 16px 50px rgba(20, 35, 38, 0.16);
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 4vw, 31px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.description {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.status-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 16px;
color: #dce7e5;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.status-dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: #819092;
box-shadow: 0 0 0 4px rgba(129, 144, 146, 0.12);
transition: 180ms ease;
}
.status-dot.active {
background: var(--green);
box-shadow: 0 0 0 4px rgba(96, 208, 173, 0.16);
}
.meter {
height: 4px;
margin-top: 12px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.09);
}
#meterFill {
width: 100%;
height: 100%;
transform: scaleX(0);
transform-origin: left center;
border-radius: inherit;
background: linear-gradient(90deg, var(--accent), var(--green));
}
.controls {
position: fixed;
z-index: 10;
left: 50%;
bottom: 24px;
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
width: min(430px, calc(100vw - 32px));
transform: translateX(-50%);
}
#pedalButton {
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
width: 100%;
min-height: 62px;
padding: 0 24px;
color: #172124;
background: var(--accent);
border: 0;
border-radius: 16px;
box-shadow:
0 10px 0 #a97618,
0 18px 38px rgba(45, 38, 20, 0.24);
font: inherit;
font-size: 14px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
user-select: none;
touch-action: none;
transition:
transform 100ms ease,
box-shadow 100ms ease,
background 160ms ease;
}
#pedalButton.active {
transform: translateY(7px);
background: #ffd166;
box-shadow:
0 3px 0 #a97618,
0 10px 24px rgba(45, 38, 20, 0.22);
}
.button-icon {
display: grid;
place-items: center;
width: 29px;
height: 29px;
border: 2px solid currentColor;
border-radius: 50%;
font-size: 18px;
line-height: 1;
}
.hint {
padding: 7px 12px;
color: #294044;
background: rgba(244, 249, 247, 0.76);
border-radius: 99px;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.03em;
text-align: center;
backdrop-filter: blur(8px);
pointer-events: none;
}
#resetCamera {
position: fixed;
z-index: 8;
right: 20px;
bottom: 22px;
padding: 9px 13px;
color: #284145;
background: rgba(245, 250, 248, 0.82);
border: 1px solid rgba(30, 60, 65, 0.12);
border-radius: 10px;
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
box-shadow: 0 8px 24px rgba(20, 35, 38, 0.1);
}
@media (max-width: 700px) {
.hud {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.description {
max-width: 320px;
}
.controls {
bottom: 14px;
}
#resetCamera {
top: 180px;
right: 12px;
bottom: auto;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section class="hud">
<div class="eyebrow">Low-poly mechanism study</div>
<h1>Pedal-operated<br>waste bin</h1>
<p class="description">
Hold the pedal to drive the linkage and raise the hinged lid.
The lid returns only after pressure is released.
</p>
<div class="status-row">
<span class="status-dot" id="statusDot"></span>
<span id="statusText">Ready</span>
</div>
<div class="meter"><div id="meterFill"></div></div>
</section>
<div class="controls">
<button id="pedalButton" aria-label="Hold to press the pedal">
<span class="button-icon">↓</span>
<span>Hold to pedal</span>
</button>
<div class="hint">Hold button or Space · Drag to orbit · Scroll to zoom</div>
</div>
<button id="resetCamera">Reset view</button>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.querySelector("#scene");
const pedalButton = document.querySelector("#pedalButton");
const statusDot = document.querySelector("#statusDot");
const statusText = document.querySelector("#statusText");
const meterFill = document.querySelector("#meterFill");
const resetCameraButton = document.querySelector("#resetCamera");
// ---------------------------------------------------------------------
// Renderer, camera, and lighting
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e8);
scene.fog = new THREE.Fog(0xdce9e8, 13, 29);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 1.12, 0.15);
const orbit = {
radius: 7.7,
theta: 0.69,
phi: 1.14
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + orbit.radius * Math.cos(orbit.phi),
cameraTarget.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
orbit.radius = 7.7;
orbit.theta = 0.69;
orbit.phi = 1.14;
updateCamera();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xc9efff, 0x33423f, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0d5, 3.1);
keyLight.position.set(4.5, 8, 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 = 7;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x70b7d4, 1.05);
fillLight.position.set(-5, 3.5, -4);
scene.add(fillLight);
// ---------------------------------------------------------------------
// Materials
// ---------------------------------------------------------------------
const teal = new THREE.MeshStandardMaterial({
color: 0x28766f,
roughness: 0.76,
metalness: 0.08,
flatShading: true
});
const tealLight = new THREE.MeshStandardMaterial({
color: 0x3b9488,
roughness: 0.72,
metalness: 0.05,
flatShading: true
});
const charcoal = new THREE.MeshStandardMaterial({
color: 0x273236,
roughness: 0.68,
metalness: 0.18,
flatShading: true
});
const dark = new THREE.MeshStandardMaterial({
color: 0x172126,
roughness: 0.8,
metalness: 0.12,
flatShading: true
});
const amber = new THREE.MeshStandardMaterial({
color: 0xf2b544,
roughness: 0.55,
metalness: 0.14,
flatShading: true
});
const steel = new THREE.MeshStandardMaterial({
color: 0xb9c7c9,
roughness: 0.38,
metalness: 0.72,
flatShading: true
});
const shoeMaterial = new THREE.MeshStandardMaterial({
color: 0x233e59,
roughness: 0.9,
flatShading: true
});
const soleMaterial = new THREE.MeshStandardMaterial({
color: 0xe8e3d7,
roughness: 0.95,
flatShading: true
});
const trouserMaterial = new THREE.MeshStandardMaterial({
color: 0x334a56,
roughness: 0.95,
flatShading: true
});
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0x8b5a00,
emissiveIntensity: 0.32,
roughness: 0.45,
metalness: 0.25,
flatShading: true
});
// ---------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------
const UP = new THREE.Vector3(0, 1, 0);
const X_AXIS = new THREE.Vector3(1, 0, 0);
function orientBar(mesh, start, end) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(UP, direction.normalize());
mesh.scale.set(1, length, 1);
}
function makeBar(start, end, radius, material, segments = 6) {
const geometry = new THREE.CylinderGeometry(
radius,
radius,
1,
segments
);
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
orientBar(mesh, start, end);
return mesh;
}
function addShadow(mesh) {
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function makeLabel(text, accent = "#ffd166") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
const radius = 24;
context.beginPath();
context.moveTo(radius, 4);
context.lineTo(canvas.width - radius, 4);
context.quadraticCurveTo(
canvas.width - 4, 4,
canvas.width - 4, radius
);
context.lineTo(
canvas.width - 4,
canvas.height - radius
);
context.quadraticCurveTo(
canvas.width - 4, canvas.height - 4,
canvas.width - radius, canvas.height - 4
);
context.lineTo(radius, canvas.height - 4);
context.quadraticCurveTo(
4, canvas.height - 4,
4, canvas.height - radius
);
context.lineTo(4, radius);
context.quadraticCurveTo(4, 4, radius, 4);
context.closePath();
context.fillStyle = "rgba(13, 25, 31, 0.91)";
context.fill();
context.strokeStyle = accent;
context.lineWidth = 5;
context.stroke();
context.fillStyle = accent;
context.font = "800 37px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 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
})
);
sprite.scale.set(1.45, 0.36, 1);
sprite.renderOrder = 20;
return sprite;
}
function makePivotMarker(position) {
const group = new THREE.Group();
group.position.copy(position);
const core = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.075, 1),
markerMaterial
);
core.castShadow = true;
group.add(core);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.13, 0.018, 6, 18),
markerMaterial
);
ring.rotation.y = 0.48;
group.add(ring);
const crossMaterial = new THREE.MeshBasicMaterial({
color: 0x26353a
});
const crossA = new THREE.Mesh(
new THREE.BoxGeometry(0.28, 0.014, 0.014),
crossMaterial
);
const crossB = new THREE.Mesh(
new THREE.BoxGeometry(0.014, 0.28, 0.014),
crossMaterial
);
group.add(crossA, crossB);
scene.add(group);
return group;
}
// ---------------------------------------------------------------------
// Ground and display platform
// ---------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0xc9d9d7,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.13;
ground.receiveShadow = true;
scene.add(ground);
const platform = addShadow(
new THREE.Mesh(
new THREE.CylinderGeometry(2.55, 2.7, 0.12, 32),
new THREE.MeshStandardMaterial({
color: 0xe7eeeb,
roughness: 0.92,
flatShading: true
})
)
);
platform.position.y = -0.06;
scene.add(platform);
// ---------------------------------------------------------------------
// Bin body
// ---------------------------------------------------------------------
const bodyGeometry = new THREE.CylinderGeometry(
0.78,
0.66,
1.82,
8,
1,
false
);
const body = addShadow(new THREE.Mesh(bodyGeometry, teal));
body.position.y = 1.08;
scene.add(body);
const bodyEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(bodyGeometry, 22),
new THREE.LineBasicMaterial({
color: 0x1d5c57,
transparent: true,
opacity: 0.42
})
);
bodyEdges.position.copy(body.position);
scene.add(bodyEdges);
// Faceted vertical ribs emphasize the low-poly construction.
for (let index = 0; index < 8; index++) {
const angle = index * Math.PI / 4;
const radius = 0.725;
const rib = addShadow(
new THREE.Mesh(
new THREE.BoxGeometry(0.045, 1.48, 0.055),
tealLight
)
);
rib.position.set(
Math.sin(angle) * radius,
1.07,
Math.cos(angle) * radius
);
rib.rotation.y = angle;
scene.add(rib);
}
const upperRim = addShadow(
new THREE.Mesh(
new THREE.TorusGeometry(0.755, 0.052, 6, 8),
charcoal
)
);
upperRim.rotation.x = Math.PI / 2;
upperRim.position.y = 1.985;
scene.add(upperRim);
const lowerRim = addShadow(
new THREE.Mesh(
new THREE.TorusGeometry(0.655, 0.055, 6, 8),
charcoal
)
);
lowerRim.rotation.x = Math.PI / 2;
lowerRim.position.y = 0.19;
scene.add(lowerRim);
// Small front badge.
const badge = addShadow(
new THREE.Mesh(
new THREE.BoxGeometry(0.46, 0.29, 0.035),
dark
)
);
badge.position.set(0, 1.25, 0.757);
scene.add(badge);
for (let index = 0; index < 3; index++) {
const stripe = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 0.14 - index * 0.02, 0.012),
amber
);
stripe.position.set(
-0.105 + index * 0.105,
1.25,
0.781
);
stripe.rotation.z = index === 1 ? 0 : index === 0 ? -0.28 : 0.28;
scene.add(stripe);
}
// ---------------------------------------------------------------------
// Hinged lid
// ---------------------------------------------------------------------
const lidPivot = new THREE.Group();
lidPivot.position.set(0, 2.03, -0.70);
scene.add(lidPivot);
const lid = addShadow(
new THREE.Mesh(
new THREE.CylinderGeometry(0.82, 0.82, 0.16, 8),
charcoal
)
);
lid.position.set(0, 0.08, 0.72);
lidPivot.add(lid);
const lidTop = addShadow(
new THREE.Mesh(
new THREE.CylinderGeometry(0.73, 0.78, 0.055, 8),
tealLight
)
);
lidTop.position.set(0, 0.185, 0.72);
lidPivot.add(lidTop);
const handle = addShadow(
new THREE.Mesh(
new THREE.BoxGeometry(0.38, 0.10, 0.13),
amber
)
);
handle.position.set(0, 0.245, 0.74);
lidPivot.add(handle);
const hingeAxle = addShadow(
new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 1.74, 8),
steel
)
);
hingeAxle.rotation.z = Math.PI / 2;
lidPivot.add(hingeAxle);
// ---------------------------------------------------------------------
// Pedal and linkage
// ---------------------------------------------------------------------
const pedalPivot = new THREE.Group();
pedalPivot.position.set(0, 0.22, 0.72);
scene.add(pedalPivot);
const pedalPlate = addShadow(
new THREE.Mesh(
new THREE.BoxGeometry(0.78, 0.10, 0.58),
amber
)
);
pedalPlate.position.set(0, 0, 0.32);
pedalPivot.add(pedalPlate);
// Raised tread bars.
for (let index = 0; index < 4; index++) {
const tread = new THREE.Mesh(
new THREE.BoxGeometry(0.62, 0.018, 0.035),
dark
);
tread.position.set(0, 0.059, 0.15 + index * 0.11);
pedalPivot.add(tread);
}
const pedalAxle = addShadow(
new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 1.74, 8),
steel
)
);
pedalAxle.rotation.z = Math.PI / 2;
pedalPivot.add(pedalAxle);
const LINK_X = 0.84;
// Side crank arms.
pedalPivot.add(
makeBar(
new THREE.Vector3(LINK_X, 0, 0),
new THREE.Vector3(LINK_X, -0.10, 0.27),
0.043,
steel
)
);
lidPivot.add(
makeBar(
new THREE.Vector3(LINK_X, 0, 0),
new THREE.Vector3(LINK_X, -0.30, 0.08),
0.043,
steel
)
);
// Telescoping vertical linkage.
const linkageSleeve = addShadow(
new THREE.Mesh(
new THREE.CylinderGeometry(0.052, 0.052, 1, 7),
dark
)
);
const linkageRod = addShadow(
new THREE.Mesh(
new THREE.CylinderGeometry(0.022, 0.022, 1, 7),
steel
)
);
// ---------------------------------------------------------------------
// Stylized low-poly foot
// ---------------------------------------------------------------------
const foot = new THREE.Group();
foot.position.set(0, 0.20, 0.31);
pedalPivot.add(foot);
const sole = addShadow(
new THREE.Mesh(
new THREE.BoxGeometry(0.45, 0.075, 0.68),
soleMaterial
)
);
sole.position.set(0, 0.13, -0.04);
foot.add(sole);
const shoeUpper = addShadow(
new THREE.Mesh(
new THREE.DodecahedronGeometry(0.5, 0),
shoeMaterial
)
);
shoeUpper.scale.set(0.74, 0.49, 1.05);
shoeUpper.position.set(0, 0.235, -0.09);
shoeUpper.rotation.y = 0.12;
foot.add(shoeUpper);
const heel = addShadow(
new THREE.Mesh(
new THREE.BoxGeometry(0.38, 0.24, 0.29),
shoeMaterial
)
);
heel.position.set(0, 0.245, 0.22);
heel.rotation.x = -0.10;
foot.add(heel);
const ankle = addShadow(
new THREE.Mesh(
new THREE.CylinderGeometry(0.14, 0.17, 0.33, 7),
shoeMaterial
)
);
ankle.position.set(0, 0.38, 0.28);
ankle.rotation.x = 0.28;
foot.add(ankle);
const trouserLeg = addShadow(
new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.22, 1.12, 7),
trouserMaterial
)
);
trouserLeg.position.set(0, 0.88, 0.47);
trouserLeg.rotation.x = 0.32;
foot.add(trouserLeg);
// ---------------------------------------------------------------------
// Visible pivot markers and labels
// ---------------------------------------------------------------------
makePivotMarker(
new THREE.Vector3(LINK_X, lidPivot.position.y, lidPivot.position.z)
);
makePivotMarker(
new THREE.Vector3(LINK_X, pedalPivot.position.y, pedalPivot.position.z)
);
const lidLabel = makeLabel("LID HINGE");
lidLabel.position.set(1.03, 2.31, -0.73);
scene.add(lidLabel);
const pedalLabel = makeLabel("PEDAL PIVOT");
pedalLabel.position.set(1.03, 0.49, 0.78);
scene.add(pedalLabel);
const mechanismLabel = makeLabel("LINKAGE", "#60d0ad");
mechanismLabel.position.set(1.05, 1.28, -0.23);
mechanismLabel.scale.set(1.18, 0.29, 1);
scene.add(mechanismLabel);
// ---------------------------------------------------------------------
// Interaction state
// ---------------------------------------------------------------------
let manualPressed = false;
let userHasInteracted = false;
// One automatic demonstration begins shortly after load.
const demoStart = performance.now() + 350;
const demoEnd = demoStart + 3000;
let pedalProgress = 0;
let lidProgress = 0;
function pedalIsPressed(now) {
if (userHasInteracted) {
return manualPressed;
}
return now >= demoStart && now < demoEnd;
}
function setManualPressed(value) {
userHasInteracted = true;
manualPressed = value;
pedalButton.classList.toggle("active", value);
}
pedalButton.addEventListener("pointerdown", (event) => {
event.preventDefault();
pedalButton.setPointerCapture(event.pointerId);
setManualPressed(true);
});
pedalButton.addEventListener("pointerup", (event) => {
event.preventDefault();
setManualPressed(false);
});
pedalButton.addEventListener("pointercancel", () => {
setManualPressed(false);
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && !event.repeat) {
event.preventDefault();
setManualPressed(true);
}
});
window.addEventListener("keyup", (event) => {
if (event.code === "Space") {
event.preventDefault();
setManualPressed(false);
}
});
window.addEventListener("blur", () => {
if (userHasInteracted) {
manualPressed = false;
pedalButton.classList.remove("active");
}
});
// ---------------------------------------------------------------------
// Custom camera controls, implemented without Three.js addons
// ---------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pedalHitMeshes = [pedalPlate];
let orbiting = false;
let pressingScenePedal = false;
let pointerDownX = 0;
let pointerDownY = 0;
let previousX = 0;
let previousY = 0;
function updateRay(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
updateRay(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pedalHitMeshes, false);
pointerDownX = event.clientX;
pointerDownY = event.clientY;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
if (hits.length > 0) {
pressingScenePedal = true;
setManualPressed(true);
} else {
orbiting = true;
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!orbiting) {
return;
}
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
orbit.theta -= deltaX * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - deltaY * 0.006,
0.42,
1.43
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
function finishPointer(event) {
if (pressingScenePedal) {
setManualPressed(false);
pressingScenePedal = false;
}
orbiting = false;
if (
Math.abs(event.clientX - pointerDownX) +
Math.abs(event.clientY - pointerDownY) < 4
) {
// A simple click without movement intentionally does nothing.
}
}
renderer.domElement.addEventListener("pointerup", finishPointer);
renderer.domElement.addEventListener("pointercancel", finishPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.006,
5.1,
12
);
updateCamera();
},
{ passive: false }
);
resetCameraButton.addEventListener("click", resetCamera);
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
const pedalAttachment = new THREE.Vector3();
const lidAttachment = new THREE.Vector3();
const sleeveEnd = new THREE.Vector3();
function damp(current, target, smoothing, deltaTime) {
return THREE.MathUtils.lerp(
current,
target,
1 - Math.exp(-smoothing * deltaTime)
);
}
function updateMechanism() {
const pedalAngle = pedalProgress * 0.34;
const lidAngle = -lidProgress * 1.05;
pedalPivot.rotation.x = pedalAngle;
lidPivot.rotation.x = lidAngle;
// The foot rests lightly above the pedal, then lowers to press it.
foot.position.y = THREE.MathUtils.lerp(
0.20,
-0.025,
pedalProgress
);
trouserLeg.rotation.x = 0.32 + pedalProgress * 0.08;
// Moving attachment points on the two crank arms.
pedalAttachment
.set(LINK_X, -0.10, 0.27)
.applyAxisAngle(X_AXIS, pedalAngle)
.add(pedalPivot.position);
lidAttachment
.set(LINK_X, -0.30, 0.08)
.applyAxisAngle(X_AXIS, lidAngle)
.add(lidPivot.position);
sleeveEnd.copy(pedalAttachment).lerp(lidAttachment, 0.62);
orientBar(linkageSleeve, pedalAttachment, sleeveEnd);
orientBar(linkageRod, pedalAttachment, lidAttachment);
}
function animate() {
requestAnimationFrame(animate);
const deltaTime = Math.min(clock.getDelta(), 0.05);
const now = performance.now();
const pressed = pedalIsPressed(now);
// The pedal reacts quickly; the lid follows with a slight mechanical lag.
pedalProgress = damp(
pedalProgress,
pressed ? 1 : 0,
10,
deltaTime
);
// The lid only moves toward closed while the pedal is not pressed.
lidProgress = damp(
lidProgress,
pressed ? 1 : 0,
5.0,
deltaTime
);
updateMechanism();
if (pressed) {
statusDot.classList.add("active");
statusText.textContent =
lidProgress > 0.88 ? "Pedal down · lid raised" : "Pedal down · lid rising";
} else {
statusDot.classList.remove("active");
statusText.textContent =
lidProgress > 0.025
? "Pedal released · lid returning"
: "Ready";
}
meterFill.style.transform = `scaleX(${lidProgress.toFixed(4)})`;
if (!userHasInteracted) {
pedalButton.classList.toggle("active", pressed);
}
renderer.render(scene, camera);
}
animate();
// ---------------------------------------------------------------------
// Resize handling
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```