Prompt specification
Build or customize the Three.js 3D scene "Foot-operated household bin" 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: Foot-operated household bin
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/foot-operated-household-bin-x0053
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a pedal-operated waste bin. Model a cylindrical bin with hinged lid, pedal, and rear linkage; use an open side panel to reveal the rod connecting pedal movement to lid rotation. Stylized solid forms.
Working Three.js Starter (foot-operated-household-bin-x0053):
```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>
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8d1c5;
color: #183d39;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
.hud {
position: fixed;
top: 28px;
left: 30px;
z-index: 10;
width: min(330px, calc(100vw - 60px));
pointer-events: none;
}
.eyebrow {
margin-bottom: 9px;
color: #b66d32;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 48px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 0.95;
}
.subtitle {
max-width: 290px;
margin: 13px 0 0;
color: rgba(24, 61, 57, 0.72);
font-size: 13px;
line-height: 1.55;
}
.readout {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 17px;
padding: 8px 11px;
border: 1px solid rgba(24, 61, 57, 0.17);
border-radius: 999px;
background: rgba(247, 243, 235, 0.58);
backdrop-filter: blur(8px);
color: rgba(24, 61, 57, 0.78);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.readout .dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #d5813c;
box-shadow: 0 0 0 4px rgba(213, 129, 60, 0.15);
}
.controls {
position: fixed;
right: 28px;
top: 28px;
z-index: 20;
display: flex;
gap: 8px;
}
button {
border: 1px solid rgba(24, 61, 57, 0.18);
border-radius: 999px;
padding: 10px 14px;
background: rgba(247, 243, 235, 0.76);
color: #183d39;
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
backdrop-filter: blur(8px);
}
button:hover {
background: #f8f4ec;
transform: translateY(-1px);
}
.hint {
position: fixed;
left: 30px;
bottom: 26px;
z-index: 10;
max-width: calc(100vw - 60px);
padding: 9px 12px;
border-left: 3px solid #b66d32;
background: rgba(247, 243, 235, 0.62);
color: rgba(24, 61, 57, 0.7);
font-size: 11px;
font-weight: 650;
letter-spacing: 0.025em;
pointer-events: none;
backdrop-filter: blur(8px);
}
.caption {
position: fixed;
right: 28px;
bottom: 26px;
z-index: 10;
max-width: 220px;
color: rgba(24, 61, 57, 0.56);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.12em;
line-height: 1.55;
text-align: right;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 680px) {
.hud {
top: 20px;
left: 20px;
}
.subtitle,
.caption {
display: none;
}
.controls {
top: 20px;
right: 20px;
}
.hint {
left: 20px;
bottom: 20px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="hud">
<div class="eyebrow">Mechanical Study · 01</div>
<h1>Pedal Bin</h1>
<p class="subtitle">
A stylized pedal-operated waste bin with an open service panel,
exposed push rod, rear linkage, and hinged lifting lid.
</p>
<div class="readout">
<span class="dot"></span>
<span id="status">Lid 28% · Cutaway open</span>
</div>
</header>
<div class="controls">
<button id="reset" type="button">Reset view</button>
</div>
<div class="hint">
Drag to orbit · Scroll to zoom · Shift-drag to pan · Click pedal
</div>
<div class="caption">
Copper rod transfers pedal force<br />
through the rear linkage to the lid hinge.
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, scene, and camera
// ------------------------------------------------------------
const stage = document.querySelector("#stage");
const status = document.querySelector("#status");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Interactive 3D pedal-operated waste bin");
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8d1c5);
scene.fog = new THREE.Fog(0xd8d1c5, 11, 22);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
50
);
const orbit = {
theta: 0.70,
phi: 1.14,
radius: 7.25,
target: new THREE.Vector3(0, 1.48, 0)
};
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);
}
updateCamera();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xfffbef, 0x777d74, 2.0));
const keyLight = new THREE.DirectionalLight(0xfff5df, 4.0);
keyLight.position.set(4.5, 7.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xa8c9d0, 1.3);
fillLight.position.set(-5, 3, -4);
scene.add(fillLight);
const rimLight = new THREE.PointLight(0xffb96f, 16, 9, 2);
rimLight.position.set(2.5, 3.6, -3.2);
scene.add(rimLight);
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0x376b64,
roughness: 0.64,
metalness: 0.08,
side: THREE.DoubleSide
});
const panelMaterial = new THREE.MeshStandardMaterial({
color: 0x467b71,
roughness: 0.58,
metalness: 0.1,
side: THREE.DoubleSide
});
const lidMaterial = new THREE.MeshStandardMaterial({
color: 0x28534e,
roughness: 0.6,
metalness: 0.1
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x183b37,
roughness: 0.72,
metalness: 0.12
});
const linerMaterial = new THREE.MeshStandardMaterial({
color: 0x173531,
roughness: 0.9,
metalness: 0.02,
side: THREE.BackSide
});
const rubberMaterial = new THREE.MeshStandardMaterial({
color: 0x222b29,
roughness: 0.92,
metalness: 0.02
});
const steelMaterial = new THREE.MeshStandardMaterial({
color: 0xaab6b1,
roughness: 0.32,
metalness: 0.72
});
const copperMaterial = new THREE.MeshStandardMaterial({
color: 0xe09a43,
roughness: 0.28,
metalness: 0.66
});
const brassMaterial = new THREE.MeshStandardMaterial({
color: 0xc9893e,
roughness: 0.35,
metalness: 0.6
});
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xc8c0b3,
roughness: 0.95,
metalness: 0
});
const plinthMaterial = new THREE.MeshStandardMaterial({
color: 0xbeb6a8,
roughness: 0.88,
metalness: 0
});
// ------------------------------------------------------------
// Utility functions
// ------------------------------------------------------------
const upAxis = new THREE.Vector3(0, 1, 0);
function addMesh(parent, geometry, material, position = null) {
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
if (position) {
mesh.position.copy(position);
}
parent.add(mesh);
return mesh;
}
function addBox(parent, size, material, position) {
return addMesh(
parent,
new THREE.BoxGeometry(size.x, size.y, size.z),
material,
position
);
}
function createRod(radius, material) {
const geometry = new THREE.CylinderGeometry(radius, radius, 1, 14);
return addMesh(THREE.Object3D.prototype, geometry, material);
}
function makeRod(parent, radius, material) {
const rod = addMesh(
parent,
new THREE.CylinderGeometry(radius, radius, 1, 14),
material
);
return rod;
}
function setRod(rod, pointA, pointB) {
const direction = new THREE.Vector3().subVectors(pointB, pointA);
const length = Math.max(direction.length(), 0.0001);
rod.position.copy(pointA).add(pointB).multiplyScalar(0.5);
rod.quaternion.setFromUnitVectors(upAxis, direction.normalize());
rod.scale.set(1, length, 1);
}
function makeJoint(parent, radius, material) {
const joint = addMesh(
parent,
new THREE.SphereGeometry(radius, 18, 12),
material
);
return joint;
}
function makeVerticalTrim(parent, radius, height, material, x, y, z) {
return addMesh(
parent,
new THREE.CylinderGeometry(radius, radius, height, 14),
material,
new THREE.Vector3(x, y, z)
);
}
// ------------------------------------------------------------
// Ground and display plinth
// ------------------------------------------------------------
const ground = addMesh(
scene,
new THREE.PlaneGeometry(30, 30),
floorMaterial,
new THREE.Vector3(0, -0.07, 0)
);
ground.rotation.x = -Math.PI / 2;
ground.castShadow = false;
const plinth = addMesh(
scene,
new THREE.CylinderGeometry(1.78, 1.9, 0.09, 72),
plinthMaterial,
new THREE.Vector3(0, -0.025, 0)
);
plinth.castShadow = false;
// ------------------------------------------------------------
// Cylindrical bin body with a cutaway opening
// ------------------------------------------------------------
const bodyRadius = 1.08;
const bodyHeight = 2.2;
const bodyCenterY = 1.22;
const gapStart = -0.05;
const gapEnd = 1.30;
const gapWidth = gapEnd - gapStart;
const shell = addMesh(
scene,
new THREE.CylinderGeometry(
bodyRadius,
bodyRadius,
bodyHeight,
72,
1,
true,
gapEnd,
Math.PI * 2 - gapWidth
),
bodyMaterial,
new THREE.Vector3(0, bodyCenterY, 0)
);
// A dark inner liner gives the open panel and linkage a deep backdrop.
const innerLiner = addMesh(
scene,
new THREE.CylinderGeometry(1.005, 1.005, 2.08, 72, 1, true),
linerMaterial,
new THREE.Vector3(0, 1.21, 0)
);
innerLiner.castShadow = false;
addMesh(
scene,
new THREE.CylinderGeometry(1.045, 1.045, 0.11, 72),
darkMaterial,
new THREE.Vector3(0, 0.17, 0)
);
addMesh(
scene,
new THREE.CylinderGeometry(1.16, 1.19, 0.13, 72),
darkMaterial,
new THREE.Vector3(0, 0.07, 0)
);
// Full structural rims.
for (const y of [0.15, 2.33]) {
const rim = addMesh(
scene,
new THREE.TorusGeometry(1.065, 0.047, 10, 72),
darkMaterial,
new THREE.Vector3(0, y, 0)
);
rim.rotation.x = Math.PI / 2;
}
// Raised vertical ribs around the body.
const ribAngles = [
1.48, 1.95, 2.45, 2.95, 3.45,
3.95, 4.45, 4.95, 5.45, 5.95
];
for (const angle of ribAngles) {
makeVerticalTrim(
scene,
0.018,
2.08,
darkMaterial,
Math.sin(angle) * 1.095,
bodyCenterY,
Math.cos(angle) * 1.095
);
}
// Trim around both edges of the service opening.
for (const angle of [gapStart, gapEnd]) {
makeVerticalTrim(
scene,
0.034,
bodyHeight,
darkMaterial,
Math.sin(angle) * 1.09,
bodyCenterY,
Math.cos(angle) * 1.09
);
}
// Small raised front badge.
const badge = addMesh(
scene,
new THREE.CylinderGeometry(0.17, 0.17, 0.045, 32),
brassMaterial,
new THREE.Vector3(0, 1.35, 1.075)
);
badge.rotation.x = Math.PI / 2;
for (let i = 0; i < 3; i++) {
const angle = (i / 3) * Math.PI * 2;
const mark = addBox(
scene,
new THREE.Vector3(0.105, 0.027, 0.025),
darkMaterial,
new THREE.Vector3(
Math.sin(angle) * 0.065,
1.35 + Math.cos(angle) * 0.065,
1.108
)
);
mark.rotation.z = -angle;
}
// ------------------------------------------------------------
// Open, curved service panel
// ------------------------------------------------------------
const panelPivotPoint = new THREE.Vector3(
Math.sin(gapEnd) * 1.095,
0,
Math.cos(gapEnd) * 1.095
);
const panelGroup = new THREE.Group();
panelGroup.position.set(
panelPivotPoint.x,
bodyCenterY,
panelPivotPoint.z
);
panelGroup.rotation.y = 1.05;
scene.add(panelGroup);
const panelGeometry = new THREE.CylinderGeometry(
1.095,
1.095,
2.12,
40,
1,
true,
gapStart,
gapWidth
);
panelGeometry.translate(-panelPivotPoint.x, 0, -panelPivotPoint.z);
addMesh(panelGroup, panelGeometry, panelMaterial);
function panelLocalPoint(angle, y) {
return new THREE.Vector3(
Math.sin(angle) * 1.095 - panelPivotPoint.x,
y,
Math.cos(angle) * 1.095 - panelPivotPoint.z
);
}
// Curved top and bottom panel borders.
for (const y of [-1.06, 1.06]) {
const points = [];
for (let i = 0; i <= 24; i++) {
const angle = THREE.MathUtils.lerp(gapStart, gapEnd, i / 24);
points.push(panelLocalPoint(angle, y));
}
const curve = new THREE.CatmullRomCurve3(points);
addMesh(
panelGroup,
new THREE.TubeGeometry(curve, 40, 0.026, 7, false),
darkMaterial
);
}
// Vertical panel edges.
for (const angle of [gapStart, gapEnd]) {
const point = panelLocalPoint(angle, 0);
makeVerticalTrim(
panelGroup,
0.032,
2.12,
darkMaterial,
point.x,
0,
point.z
);
}
// Handle on the free edge of the open panel.
const freeEdge = panelLocalPoint(gapStart, 0);
const handle = addBox(
panelGroup,
new THREE.Vector3(0.3, 0.075, 0.08),
steelMaterial,
freeEdge.clone().add(new THREE.Vector3(0.1, 0, 0))
);
handle.rotation.y = -0.05;
// Small latch on the open panel.
const latch = addMesh(
panelGroup,
new THREE.CylinderGeometry(0.065, 0.065, 0.035, 20),
brassMaterial,
freeEdge.clone().add(new THREE.Vector3(0.09, -0.42, 0))
);
latch.rotation.z = Math.PI / 2;
// ------------------------------------------------------------
// Hinged lid
// ------------------------------------------------------------
const lidPivot = new THREE.Group();
lidPivot.position.set(0, 2.39, -1.08);
scene.add(lidPivot);
addMesh(
lidPivot,
new THREE.CylinderGeometry(1.145, 1.145, 0.145, 72),
lidMaterial,
new THREE.Vector3(0, 0.065, 1.08)
);
addMesh(
lidPivot,
new THREE.CylinderGeometry(0.94, 0.94, 0.027, 56),
darkMaterial,
new THREE.Vector3(0, 0.15, 1.08)
);
const lidRim = addMesh(
lidPivot,
new THREE.TorusGeometry(1.09, 0.038, 10, 72),
darkMaterial,
new THREE.Vector3(0, 0.12, 1.08)
);
lidRim.rotation.x = Math.PI / 2;
// Raised grip on the lid.
addBox(
lidPivot,
new THREE.Vector3(0.55, 0.09, 0.12),
steelMaterial,
new THREE.Vector3(0, 0.19, 1.88)
);
addBox(
lidPivot,
new THREE.Vector3(0.07, 0.13, 0.12),
steelMaterial,
new THREE.Vector3(-0.22, 0.105, 1.88)
);
addBox(
lidPivot,
new THREE.Vector3(0.07, 0.13, 0.12),
steelMaterial,
new THREE.Vector3(0.22, 0.105, 1.88)
);
// Hinge axle and brackets remain attached to the body.
const hingeAxle = addMesh(
scene,
new THREE.CylinderGeometry(0.095, 0.095, 1.52, 22),
steelMaterial,
new THREE.Vector3(0, 2.39, -1.08)
);
hingeAxle.rotation.z = Math.PI / 2;
for (const x of [-0.78, 0.78]) {
addBox(
scene,
new THREE.Vector3(0.17, 0.31, 0.28),
darkMaterial,
new THREE.Vector3(x, 2.32, -1.07)
);
}
// ------------------------------------------------------------
// Pedal and lower frame
// ------------------------------------------------------------
const pedalPivot = new THREE.Group();
pedalPivot.position.set(0, 0.39, 1.02);
scene.add(pedalPivot);
const pedalPad = addBox(
pedalPivot,
new THREE.Vector3(0.76, 0.13, 0.48),
steelMaterial,
new THREE.Vector3(0, 0, 0.34)
);
const pedalTread = addBox(
pedalPivot,
new THREE.Vector3(0.62, 0.035, 0.34),
rubberMaterial,
new THREE.Vector3(0, 0.082, 0.34)
);
for (let i = -2; i <= 2; i++) {
addBox(
pedalPivot,
new THREE.Vector3(0.025, 0.012, 0.31),
steelMaterial,
new THREE.Vector3(i * 0.105, 0.106, 0.34)
);
}
// Pedal axle.
const pedalAxle = addMesh(
scene,
new THREE.CylinderGeometry(0.07, 0.07, 1.45, 20),
steelMaterial,
new THREE.Vector3(0, 0.39, 1.02)
);
pedalAxle.rotation.z = Math.PI / 2;
// Pedal support brackets.
for (const x of [-0.59, 0.59]) {
addBox(
scene,
new THREE.Vector3(0.1, 0.3, 0.28),
darkMaterial,
new THREE.Vector3(x, 0.25, 0.93)
);
}
// ------------------------------------------------------------
// Rear linkage
// ------------------------------------------------------------
const rearBottomY = 0.61;
const rearBottomZ = -0.79;
for (const x of [-0.59, 0.59]) {
const lowerLink = makeRod(scene, 0.034, steelMaterial);
setRod(
lowerLink,
new THREE.Vector3(x, 0.38, 0.94),
new THREE.Vector3(x, rearBottomY, rearBottomZ)
);
const rearUpright = makeRod(scene, 0.031, steelMaterial);
setRod(
rearUpright,
new THREE.Vector3(x, rearBottomY, rearBottomZ),
new THREE.Vector3(x, 2.27, -0.93)
);
makeJoint(scene, 0.075, brassMaterial).position.set(
x,
rearBottomY,
rearBottomZ
);
}
const rearCrossbar = makeRod(scene, 0.032, brassMaterial);
setRod(
rearCrossbar,
new THREE.Vector3(-0.59, 2.27, -0.93),
new THREE.Vector3(0.59, 2.27, -0.93)
);
// ------------------------------------------------------------
// Exposed copper actuator linkage
// ------------------------------------------------------------
const sliderTop = new THREE.Vector3(0.68, 2.04, 0.48);
const sliderBottom = new THREE.Vector3(0.68, 0.49, 0.48);
const verticalRod = makeRod(scene, 0.039, copperMaterial);
const pedalCrankRod = makeRod(scene, 0.038, copperMaterial);
const upperLinkRod = makeRod(scene, 0.038, copperMaterial);
const sliderBottomJoint = makeJoint(scene, 0.083, brassMaterial);
const sliderTopJoint = makeJoint(scene, 0.078, brassMaterial);
const pedalJoint = makeJoint(scene, 0.074, brassMaterial);
const lidJoint = makeJoint(scene, 0.08, brassMaterial);
// Twin guide rails flank the moving vertical rod.
for (const x of [0.56, 0.80]) {
makeVerticalTrim(
scene,
0.018,
1.23,
steelMaterial,
x,
1.28,
0.51
);
}
// A short guide sleeve surrounds the lower section.
addMesh(
scene,
new THREE.CylinderGeometry(0.075, 0.075, 0.38, 18),
darkMaterial,
new THREE.Vector3(0.68, 0.88, 0.48)
);
// Crank attached to the rotating lid.
const lidCrankLocalA = new THREE.Vector3(0, 0, 0);
const lidCrankLocalB = new THREE.Vector3(0.38, -0.12, 0.36);
const lidCrank = makeRod(lidPivot, 0.04, copperMaterial);
setRod(lidCrank, lidCrankLocalA, lidCrankLocalB);
const lidCrankHub = makeJoint(lidPivot, 0.095, brassMaterial);
lidCrankHub.position.set(0, 0, 0);
// ------------------------------------------------------------
// Pedal interaction
// ------------------------------------------------------------
let pedalPressed = false;
let progress = 0.28;
let targetProgress = 0.28;
pedalPad.userData.clickable = true;
pedalTread.userData.clickable = true;
function togglePedal() {
pedalPressed = !pedalPressed;
targetProgress = pedalPressed ? 1 : 0.28;
status.textContent = pedalPressed
? "Pedal down · Lid opening"
: "Pedal released · Lid holding";
}
// ------------------------------------------------------------
// Mechanism animation
// ------------------------------------------------------------
function updateMechanism() {
pedalPivot.rotation.x = 0.025 + progress * 0.2;
lidPivot.rotation.x = -0.18 - progress * 0.54;
pedalPivot.updateMatrixWorld(true);
lidPivot.updateMatrixWorld(true);
sliderBottom.y = 0.49 + progress * 0.1;
const pedalCrankPoint = pedalPivot.localToWorld(
new THREE.Vector3(0.58, 0, -0.05)
);
const lidAttachment = lidPivot.localToWorld(lidCrankLocalB.clone());
setRod(pedalCrankRod, pedalCrankPoint, sliderBottom);
setRod(verticalRod, sliderBottom, sliderTop);
setRod(upperLinkRod, sliderTop, lidAttachment);
pedalJoint.position.copy(pedalCrankPoint);
sliderBottomJoint.position.copy(sliderBottom);
sliderTopJoint.position.copy(sliderTop);
lidJoint.position.copy(lidAttachment);
}
// ------------------------------------------------------------
// Direct camera controls
// ------------------------------------------------------------
const pointerState = {
down: false,
button: 0,
x: 0,
y: 0,
startX: 0,
startY: 0,
moved: false
};
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function setPointerFromEvent(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;
}
function pointerHitsPedal(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObject(pedalPivot, true);
return hits.some((hit) => hit.object.userData.clickable);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerState.down = true;
pointerState.button = event.button;
pointerState.x = event.clientX;
pointerState.y = event.clientY;
pointerState.startX = event.clientX;
pointerState.startY = event.clientY;
pointerState.moved = false;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerState.down) {
renderer.domElement.style.cursor = pointerHitsPedal(event)
? "pointer"
: "grab";
return;
}
const dx = event.clientX - pointerState.x;
const dy = event.clientY - pointerState.y;
pointerState.x = event.clientX;
pointerState.y = event.clientY;
if (
Math.abs(event.clientX - pointerState.startX) +
Math.abs(event.clientY - pointerState.startY) > 5
) {
pointerState.moved = true;
}
if (event.shiftKey || pointerState.button === 2) {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const cameraUp = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
1
);
const panScale = orbit.radius * 0.0012;
orbit.target.addScaledVector(right, -dx * panScale);
orbit.target.addScaledVector(cameraUp, dy * panScale);
orbit.target.y = THREE.MathUtils.clamp(orbit.target.y, 0.35, 3.1);
} else {
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.005,
0.48,
1.48
);
}
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
const wasClick =
!pointerState.moved &&
Math.abs(event.clientX - pointerState.startX) +
Math.abs(event.clientY - pointerState.startY) < 7;
pointerState.down = false;
if (wasClick && pointerHitsPedal(event)) {
togglePedal();
}
renderer.domElement.style.cursor = pointerHitsPedal(event)
? "pointer"
: "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerState.down = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
4.25,
11.5
);
updateCamera();
},
{ passive: false }
);
document.querySelector("#reset").addEventListener("click", () => {
orbit.theta = 0.70;
orbit.phi = 1.14;
orbit.radius = 7.25;
orbit.target.set(0, 1.48, 0);
updateCamera();
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePedal();
}
});
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const easing = 1 - Math.exp(-7.5 * delta);
progress += (targetProgress - progress) * easing;
updateMechanism();
if (!pointerState.down) {
status.textContent =
`Lid ${Math.round(progress * 100)}% · Cutaway open`;
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateMechanism();
animate();
</script>
</body>
</html>
```