Prompt specification
Build or customize the Three.js 3D scene "Grooved lid gasket section" 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: Grooved lid gasket section
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/grooved-lid-gasket-section-x0261
Scene Description Prompt:
Create a standalone stylized scene in one HTML file using Three.js from cdnjs. Model a circular lid groove, flexible sealing ring, and matching rim; separate them vertically so the ring's continuous contact path is visible around the opening.
Working Three.js Starter (grooved-lid-gasket-section-x0261):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Continuous Seal — Exploded Mechanical Study</title>
<style>
:root {
color-scheme: dark;
--ink: #eaf7f6;
--muted: #8ca7ae;
--accent: #70ded1;
--seal: #ff7658;
--panel: rgba(8, 20, 28, 0.72);
--line: rgba(150, 218, 213, 0.17);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #071019;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
#scene.dragging {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(
circle at 52% 44%,
transparent 32%,
rgba(2, 7, 12, 0.16) 66%,
rgba(2, 7, 12, 0.72) 100%
);
}
header {
position: fixed;
top: 28px;
left: 32px;
z-index: 5;
max-width: 430px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
color: var(--accent);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 30px;
height: 1px;
background: var(--accent);
box-shadow: 0 0 12px rgba(112, 222, 209, 0.8);
}
h1 {
margin: 0;
font-size: clamp(30px, 4.2vw, 58px);
font-weight: 540;
letter-spacing: -0.055em;
line-height: 0.98;
}
header p {
max-width: 390px;
margin: 14px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.65;
}
.status {
position: fixed;
top: 30px;
right: 32px;
z-index: 5;
display: flex;
align-items: center;
gap: 9px;
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(7, 17, 25, 0.52);
color: #bdd4d5;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
backdrop-filter: blur(12px);
pointer-events: none;
}
.status-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--seal);
box-shadow: 0 0 12px rgba(255, 118, 88, 0.9);
}
.controls {
position: fixed;
left: 32px;
bottom: 30px;
z-index: 6;
width: min(360px, calc(100vw - 64px));
padding: 18px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(18px);
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
color: #c9dcdd;
font-size: 10px;
font-weight: 720;
letter-spacing: 0.15em;
text-transform: uppercase;
}
output {
color: var(--accent);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 18px;
margin: 0;
appearance: none;
background: transparent;
cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
height: 3px;
border-radius: 4px;
background: linear-gradient(
90deg,
rgba(112, 222, 209, 0.85),
rgba(112, 222, 209, 0.14)
);
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
margin-top: -6px;
appearance: none;
border: 2px solid #d9fffa;
border-radius: 50%;
background: #19363d;
box-shadow: 0 0 0 5px rgba(112, 222, 209, 0.1);
}
input[type="range"]::-moz-range-track {
height: 3px;
border-radius: 4px;
background: rgba(112, 222, 209, 0.3);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 2px solid #d9fffa;
border-radius: 50%;
background: #19363d;
}
.buttons {
display: flex;
gap: 8px;
margin-top: 14px;
}
button {
flex: 1;
padding: 10px 12px;
border: 1px solid rgba(157, 218, 214, 0.17);
border-radius: 10px;
background: rgba(129, 191, 191, 0.07);
color: #d9e9e9;
font: inherit;
font-size: 11px;
font-weight: 650;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(112, 222, 209, 0.52);
background: rgba(112, 222, 209, 0.13);
transform: translateY(-1px);
}
button.primary {
border-color: rgba(112, 222, 209, 0.34);
background: rgba(112, 222, 209, 0.15);
color: #cffff9;
}
.hint {
position: fixed;
right: 32px;
bottom: 34px;
z-index: 5;
color: rgba(177, 204, 207, 0.67);
font-size: 10px;
letter-spacing: 0.05em;
pointer-events: none;
}
.hint span {
color: var(--accent);
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
}
.tag {
--dx: 24px;
position: absolute;
display: flex;
align-items: center;
gap: 9px;
min-width: 142px;
padding: 8px 10px;
border-left: 1px solid rgba(112, 222, 209, 0.6);
background: linear-gradient(
90deg,
rgba(5, 15, 22, 0.72),
rgba(5, 15, 22, 0.08)
);
transform: translate(var(--dx), -50%);
transition: opacity 120ms ease;
will-change: left, top;
}
.tag::before {
content: "";
position: absolute;
top: 50%;
left: -25px;
width: 25px;
height: 1px;
background: rgba(112, 222, 209, 0.4);
}
.tag.left {
border-right: 1px solid rgba(112, 222, 209, 0.6);
border-left: 0;
transform: translate(calc(-100% - var(--dx)), -50%);
text-align: right;
}
.tag.left::before {
right: -25px;
left: auto;
}
.tag .number {
color: var(--accent);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
}
.tag.seal {
border-color: rgba(255, 118, 88, 0.72);
}
.tag.seal::before {
background: rgba(255, 118, 88, 0.5);
}
.tag.seal .number {
color: var(--seal);
}
.tag b {
display: block;
color: #d8e8e8;
font-size: 9px;
letter-spacing: 0.11em;
white-space: nowrap;
}
.tag small {
display: block;
margin-top: 3px;
color: rgba(162, 190, 194, 0.72);
font-size: 8px;
letter-spacing: 0.04em;
white-space: nowrap;
}
@media (max-width: 700px) {
header {
top: 20px;
left: 20px;
right: 20px;
}
header p {
display: none;
}
.status {
top: 20px;
right: 20px;
padding: 8px 10px;
}
.status span:last-child {
display: none;
}
.controls {
left: 20px;
bottom: 20px;
width: calc(100vw - 40px);
}
.hint {
display: none;
}
#labels {
display: none;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive exploded view of a lid groove, flexible sealing ring, and matching rim"
></canvas>
<div class="vignette"></div>
<header>
<div class="eyebrow">Mechanical study / 01</div>
<h1>Continuous seal</h1>
<p>
An exploded circular closure study: the flexible ring remains fully visible
around the opening, aligned between the lid recess and matching rim groove.
</p>
</header>
<div class="status">
<span class="status-dot"></span>
<span>360° contact path</span>
</div>
<div id="labels" aria-hidden="true">
<div class="tag" id="tag-lid">
<span class="number">01</span>
<div>
<b>LID GROOVE</b>
<small>annular recess</small>
</div>
</div>
<div class="tag seal" id="tag-seal">
<span class="number">02</span>
<div>
<b>FLEX SEAL</b>
<small>360° sealing path</small>
</div>
</div>
<div class="tag" id="tag-rim">
<span class="number">03</span>
<div>
<b>MATCHING RIM</b>
<small>compliant seat</small>
</div>
</div>
</div>
<section class="controls" aria-label="Scene controls">
<div class="control-heading">
<span>Exploded separation</span>
<output id="explosion-output" aria-live="polite">100%</output>
</div>
<input
id="explosion"
type="range"
min="0"
max="100"
value="100"
aria-label="Exploded separation"
>
<div class="buttons">
<button id="explode" class="primary" type="button">Explode</button>
<button id="assemble" type="button">Assemble</button>
<button id="reset" type="button">Reset view</button>
</div>
</section>
<div class="hint">
<span>Drag</span> to orbit ·
<span>Scroll</span> to zoom ·
<span>Space</span> to toggle
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 9, 20);
const camera = new THREE.PerspectiveCamera(
34,
window.innerWidth / window.innerHeight,
0.1,
40
);
const target = new THREE.Vector3(0, 0.55, 0);
let azimuth = 0.67;
let elevation = 0.105;
let distance = 8.35;
// A generated equirectangular environment provides soft metallic reflections
// without importing any examples or addons.
function createEnvironment() {
const envCanvas = document.createElement("canvas");
envCanvas.width = 1024;
envCanvas.height = 512;
const ctx = envCanvas.getContext("2d");
const base = ctx.createLinearGradient(0, 0, 0, 512);
base.addColorStop(0, "#172b39");
base.addColorStop(0.48, "#0a1822");
base.addColorStop(1, "#02070c");
ctx.fillStyle = base;
ctx.fillRect(0, 0, 1024, 512);
function glow(x, y, radius, color) {
const gradient = ctx.createRadialGradient(x, y, 0, x, y, radius);
gradient.addColorStop(0, color);
gradient.addColorStop(0.35, color.replace(/0\.9\)$/, "0.42)"));
gradient.addColorStop(1, "rgba(0,0,0,0)");
ctx.fillStyle = gradient;
ctx.fillRect(x - radius, y - radius, radius * 2, radius * 2);
}
glow(210, 135, 230, "rgba(194,235,255,0.95)");
glow(755, 225, 260, "rgba(255,132,102,0.72)");
glow(525, 42, 180, "rgba(112,222,209,0.68)");
const texture = new THREE.CanvasTexture(envCanvas);
texture.mapping = THREE.EquirectangularReflectionMapping;
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
scene.environment = createEnvironment();
scene.environmentIntensity = 0.72;
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xcbe9ff,
0x071018,
1.7
);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xffffff, 3.2);
key.position.set(4.5, 7, 5.5);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -5;
key.shadow.camera.right = 5;
key.shadow.camera.top = 5;
key.shadow.camera.bottom = -5;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 18;
key.shadow.bias = -0.00015;
scene.add(key);
const fill = new THREE.DirectionalLight(0x64dfd2, 1.7);
fill.position.set(-5, 1.5, 3);
scene.add(fill);
const rimLight = new THREE.DirectionalLight(0xff8b70, 1.8);
rimLight.position.set(3, 1.5, -5);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Ground and soft grounding shadow
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x07131d,
roughness: 0.96,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.12;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(
20,
40,
0x28505a,
0x17313b
);
grid.position.y = -1.108;
grid.material.transparent = true;
grid.material.opacity = 0.14;
grid.material.depthWrite = false;
scene.add(grid);
function createShadowTexture() {
const shadowCanvas = document.createElement("canvas");
shadowCanvas.width = 256;
shadowCanvas.height = 256;
const ctx = shadowCanvas.getContext("2d");
const gradient = ctx.createRadialGradient(
128, 128, 4,
128, 128, 126
);
gradient.addColorStop(0, "rgba(0,0,0,0.58)");
gradient.addColorStop(0.4, "rgba(0,0,0,0.28)");
gradient.addColorStop(1, "rgba(0,0,0,0)");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, 256, 256);
return new THREE.CanvasTexture(shadowCanvas);
}
const softShadow = new THREE.Mesh(
new THREE.CircleGeometry(3.8, 96),
new THREE.MeshBasicMaterial({
map: createShadowTexture(),
transparent: true,
depthWrite: false
})
);
softShadow.rotation.x = -Math.PI / 2;
softShadow.position.y = -1.104;
scene.add(softShadow);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const lidMaterial = new THREE.MeshPhysicalMaterial({
color: 0xa9c1ca,
metalness: 0.46,
roughness: 0.27,
clearcoat: 0.38,
clearcoatRoughness: 0.3,
side: THREE.DoubleSide
});
const rimMaterial = new THREE.MeshPhysicalMaterial({
color: 0x244b5d,
metalness: 0.58,
roughness: 0.26,
clearcoat: 0.32,
clearcoatRoughness: 0.35,
side: THREE.DoubleSide
});
const sealMaterial = new THREE.MeshPhysicalMaterial({
color: 0xff7658,
emissive: 0x361008,
emissiveIntensity: 0.24,
metalness: 0.03,
roughness: 0.3,
clearcoat: 0.85,
clearcoatRoughness: 0.2,
sheen: 0.7,
sheenColor: new THREE.Color(0xffb39f),
sheenRoughness: 0.55
});
const accentMaterial = new THREE.MeshStandardMaterial({
color: 0x9af6e9,
emissive: 0x2a7c72,
emissiveIntensity: 0.7,
metalness: 0.3,
roughness: 0.3
});
const edgeMaterial = new THREE.MeshStandardMaterial({
color: 0x5e8794,
emissive: 0x102d34,
emissiveIntensity: 0.3,
metalness: 0.65,
roughness: 0.32
});
// -------------------------------------------------------------------------
// Procedural mechanical components
// -------------------------------------------------------------------------
const assembly = new THREE.Group();
scene.add(assembly);
const lidGroup = new THREE.Group();
const sealGroup = new THREE.Group();
const rimGroup = new THREE.Group();
assembly.add(lidGroup, sealGroup, rimGroup);
function makeLathe(profile, material) {
const points = profile.map(
([radius, height]) => new THREE.Vector2(radius, height)
);
const geometry = new THREE.LatheGeometry(points, 192);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function addHorizontalRing(
parent,
radius,
tube,
height,
material,
radialSegments = 12
) {
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(
radius,
tube,
radialSegments,
192
),
material
);
mesh.rotation.x = Math.PI / 2;
mesh.position.y = height;
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
// Annular lid flange with a recessed underside groove.
const lidProfile = [
[1.30, 0.06],
[1.30, 0.40],
[2.04, 0.40],
[2.22, 0.34],
[2.36, 0.22],
[2.36, 0.00],
[2.16, 0.00],
[1.98, 0.00],
[1.86, 0.00],
[1.82, 0.09],
[1.78, 0.15],
[1.66, 0.15],
[1.61, 0.10],
[1.57, 0.00],
[1.34, 0.00],
[1.30, 0.06]
];
lidGroup.add(makeLathe(lidProfile, lidMaterial));
// Fine rings clarify the edges of the lid's annular groove.
addHorizontalRing(lidGroup, 1.575, 0.018, -0.009, accentMaterial, 8);
addHorizontalRing(lidGroup, 1.845, 0.018, -0.009, accentMaterial, 8);
addHorizontalRing(lidGroup, 1.30, 0.016, 0.08, edgeMaterial, 8);
addHorizontalRing(lidGroup, 2.355, 0.022, 0.11, edgeMaterial, 8);
// Matching lower rim with a compliant annular seat around the opening.
const rimProfile = [
[1.30, -0.32],
[1.30, 0.22],
[1.48, 0.22],
[1.58, 0.22],
[1.62, 0.14],
[1.66, 0.11],
[1.76, 0.11],
[1.81, 0.14],
[1.85, 0.22],
[2.05, 0.22],
[2.18, 0.18],
[2.25, 0.08],
[2.25, -0.42],
[2.13, -0.52],
[1.52, -0.52],
[1.40, -0.43],
[1.30, -0.32]
];
rimGroup.add(makeLathe(rimProfile, rimMaterial));
addHorizontalRing(rimGroup, 1.575, 0.018, 0.228, accentMaterial, 8);
addHorizontalRing(rimGroup, 1.845, 0.018, 0.228, accentMaterial, 8);
addHorizontalRing(rimGroup, 1.30, 0.018, -0.03, edgeMaterial, 8);
addHorizontalRing(rimGroup, 2.252, 0.024, -0.16, edgeMaterial, 8);
// Flexible 360-degree sealing ring.
const seal = new THREE.Mesh(
new THREE.TorusGeometry(1.72, 0.115, 24, 256),
sealMaterial
);
seal.rotation.x = Math.PI / 2;
seal.castShadow = true;
seal.receiveShadow = true;
sealGroup.add(seal);
// Subtle alignment guides, hidden when fully assembled.
const guideMaterial = new THREE.LineDashedMaterial({
color: 0x7cddd4,
transparent: true,
opacity: 0.18,
dashSize: 0.075,
gapSize: 0.12,
depthWrite: false
});
const guides = [];
const guideAngles = [0.42, 2.53, 4.65];
for (const angle of guideAngles) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]);
const line = new THREE.Line(geometry, guideMaterial);
line.computeLineDistances();
scene.add(line);
guides.push({ line, angle });
}
// -------------------------------------------------------------------------
// Exploded-view animation
// -------------------------------------------------------------------------
const explosionInput = document.querySelector("#explosion");
const explosionOutput = document.querySelector("#explosion-output");
function applyExplosion() {
const amount = Number(explosionInput.value) / 100;
rimGroup.position.y = -0.38 * amount;
sealGroup.position.y = 0.30 + 0.72 * amount;
lidGroup.position.y = 0.22 + 1.48 * amount;
// The ring appears slightly flattened in the assembled state and
// relaxes toward a rounder section as the parts separate.
seal.scale.z = THREE.MathUtils.lerp(0.74, 0.88, amount);
guideMaterial.opacity = 0.08 + amount * 0.1;
const lowerY = rimGroup.position.y + 0.17;
const upperY = lidGroup.position.y + 0.015;
for (const guide of guides) {
const x = Math.cos(guide.angle) * 1.72;
const z = Math.sin(guide.angle) * 1.72;
guide.line.geometry.setFromPoints([
new THREE.Vector3(x, lowerY, z),
new THREE.Vector3(x, upperY, z)
]);
guide.line.computeLineDistances();
guide.line.visible = amount > 0.025;
}
explosionOutput.value = `${Math.round(amount * 100)}%`;
explosionOutput.textContent = `${Math.round(amount * 100)}%`;
}
explosionInput.addEventListener("input", applyExplosion);
document.querySelector("#explode").addEventListener("click", () => {
explosionInput.value = "100";
applyExplosion();
});
document.querySelector("#assemble").addEventListener("click", () => {
explosionInput.value = "0";
applyExplosion();
});
applyExplosion();
// -------------------------------------------------------------------------
// Direct camera controls: orbit, wheel zoom, touch pinch, keyboard
// -------------------------------------------------------------------------
function updateCamera() {
const horizontalDistance = Math.cos(elevation) * distance;
camera.position.set(
target.x + Math.cos(azimuth) * horizontalDistance,
target.y + Math.sin(elevation) * distance,
target.z + Math.sin(azimuth) * horizontalDistance
);
camera.lookAt(target);
}
function resetView() {
azimuth = 0.67;
elevation = 0.105;
distance = 8.35;
updateCamera();
}
document.querySelector("#reset").addEventListener("click", resetView);
canvas.addEventListener("dblclick", resetView);
const pointers = new Map();
let pinchDistance = 0;
function getPinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
if (pointers.size === 2) {
pinchDistance = getPinchDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const pointer = pointers.get(event.pointerId);
if (!pointer) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
if (pointers.size === 1) {
azimuth -= dx * 0.006;
elevation = THREE.MathUtils.clamp(
elevation - dy * 0.004,
-0.07,
1.25
);
} else if (pointers.size === 2) {
const nextPinchDistance = getPinchDistance();
if (pinchDistance > 0 && nextPinchDistance > 0) {
distance = THREE.MathUtils.clamp(
distance * (pinchDistance / nextPinchDistance),
5.2,
14
);
}
pinchDistance = nextPinchDistance;
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
pinchDistance = 0;
}
if (pointers.size === 0) {
canvas.classList.remove("dragging");
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
distance = THREE.MathUtils.clamp(
distance * Math.exp(event.deltaY * 0.001),
5.2,
14
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
let handled = true;
switch (event.key) {
case "ArrowLeft":
azimuth += 0.08;
break;
case "ArrowRight":
azimuth -= 0.08;
break;
case "ArrowUp":
elevation = THREE.MathUtils.clamp(
elevation + 0.05,
-0.07,
1.25
);
break;
case "ArrowDown":
elevation = THREE.MathUtils.clamp(
elevation - 0.05,
-0.07,
1.25
);
break;
case "+":
case "=":
distance = Math.max(5.2, distance - 0.35);
break;
case "-":
distance = Math.min(14, distance + 0.35);
break;
case " ":
explosionInput.value =
explosionInput.value === "100" ? "0" : "100";
applyExplosion();
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
// -------------------------------------------------------------------------
// Screen-space component labels
// -------------------------------------------------------------------------
const tagElements = {
lid: document.querySelector("#tag-lid"),
seal: document.querySelector("#tag-seal"),
rim: document.querySelector("#tag-rim")
};
const projected = new THREE.Vector3();
const horizontalCameraDirection = new THREE.Vector3();
function placeTag(element, radius, height, offset) {
projected.copy(horizontalCameraDirection);
projected.multiplyScalar(radius);
projected.y = height;
projected.project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.setProperty("--dx", `${offset}px`);
element.classList.toggle("left", x > window.innerWidth * 0.7);
element.style.opacity = projected.z < 1 ? "1" : "0";
}
function updateLabels() {
horizontalCameraDirection
.set(
camera.position.x - target.x,
0,
camera.position.z - target.z
)
.normalize();
placeTag(tagElements.lid, 2.34, lidGroup.position.y + 0.08, 24);
placeTag(tagElements.seal, 1.72, sealGroup.position.y + 0.08, 48);
placeTag(tagElements.rim, 2.24, rimGroup.position.y + 0.12, 24);
}
// -------------------------------------------------------------------------
// Resize and render
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.fov = camera.aspect < 0.85 ? 55 : 34;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
updateCamera();
renderer.setAnimationLoop(() => {
updateCamera();
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```