Prompt specification
Build or customize the Three.js 3D scene "Nested similar triangle frames" 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: Nested similar triangle frames
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/nested-similar-triangle-frames-x1733
Scene Description Prompt:
Make a low-poly Three.js scene in a self-contained HTML file using cdnjs: nested similar triangle frames. Build several triangles with identical angles and different uniform scales; link corresponding edges and display the same length ratios across each pair.
Working Three.js Starter (nested-similar-triangle-frames-x1733):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Nested Similar Triangle Frames</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(10, 19, 34, 0.78);
--panel-border: rgba(165, 214, 255, 0.16);
--text: #eaf6ff;
--muted: #91a9bf;
--cyan: #55e2ff;
--pink: #ff65ad;
--amber: #ffcc62;
--green: #79f0a2;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111f;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#stage,
#labels,
#ui {
position: fixed;
inset: 0;
}
#stage {
z-index: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
#labels {
z-index: 2;
pointer-events: none;
overflow: hidden;
}
#ui {
z-index: 3;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
width: min(470px, calc(100vw - 52px));
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.55);
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
color: var(--text);
font-size: clamp(25px, 3vw, 40px);
font-weight: 720;
letter-spacing: -0.04em;
line-height: 1.05;
}
.brand p {
max-width: 440px;
margin: 12px 0 0;
color: #a9bed0;
font-size: 13px;
line-height: 1.6;
}
.angle-note {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 13px;
padding: 7px 10px;
border: 1px solid rgba(85, 226, 255, 0.22);
border-radius: 999px;
background: rgba(12, 29, 47, 0.62);
color: #ccefff;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.04em;
}
.angle-note::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
content: "";
}
.panel {
position: absolute;
top: 22px;
right: 22px;
width: 340px;
max-height: calc(100vh - 44px);
overflow: auto;
padding: 17px;
border: 1px solid var(--panel-border);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(18, 33, 54, 0.9), rgba(7, 16, 29, 0.82));
box-shadow:
0 24px 60px rgba(0, 0, 0, 0.35),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(16px);
pointer-events: auto;
scrollbar-width: thin;
scrollbar-color: rgba(120, 170, 205, 0.35) transparent;
}
.panel-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.panel-header h2 {
margin: 0;
color: var(--text);
font-size: 14px;
letter-spacing: -0.01em;
}
.panel-header p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.reset {
flex: 0 0 auto;
padding: 7px 9px;
border: 1px solid rgba(160, 210, 240, 0.18);
border-radius: 9px;
background: rgba(140, 200, 235, 0.08);
color: #d8efff;
font: inherit;
font-size: 10px;
font-weight: 750;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease;
}
.reset:hover {
border-color: rgba(85, 226, 255, 0.45);
background: rgba(85, 226, 255, 0.13);
}
#pairs {
display: grid;
gap: 10px;
}
.pair-card {
position: relative;
overflow: hidden;
padding: 12px;
border: 1px solid rgba(170, 210, 235, 0.12);
border-radius: 13px;
background: rgba(4, 12, 23, 0.52);
}
.pair-card::before {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 3px;
background: var(--accent);
box-shadow: 0 0 18px var(--accent);
content: "";
}
.pair-heading {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 8px;
margin-bottom: 9px;
}
.pair-number {
color: var(--accent);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
}
.pair-names {
color: #dceeff;
font-size: 11px;
font-weight: 760;
}
.pair-ratio {
color: var(--accent);
font-size: 12px;
font-weight: 850;
}
.edge-row {
display: grid;
grid-template-columns: 73px 1fr auto;
align-items: center;
gap: 8px;
min-height: 24px;
border-top: 1px solid rgba(170, 210, 235, 0.07);
color: #8fa9bf;
font-size: 9.5px;
}
.edge-row:first-of-type {
border-top: 0;
}
.edge-id {
color: #c2d7e8;
font-weight: 750;
}
.edge-values {
font-variant-numeric: tabular-nums;
letter-spacing: 0.015em;
}
.edge-ratio {
color: #dff8ff;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.panel-foot {
margin: 13px 2px 0;
color: #7893aa;
font-size: 10px;
line-height: 1.55;
}
.legend {
position: absolute;
bottom: 24px;
left: 26px;
display: flex;
flex-wrap: wrap;
gap: 7px;
max-width: 520px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid rgba(170, 210, 235, 0.13);
border-radius: 999px;
background: rgba(7, 17, 30, 0.72);
color: #bdd0df;
font-size: 10px;
font-weight: 700;
backdrop-filter: blur(10px);
}
.legend-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--dot);
box-shadow: 0 0 9px var(--dot);
}
.hint {
position: absolute;
right: 24px;
bottom: 22px;
color: rgba(178, 203, 222, 0.62);
font-size: 10px;
letter-spacing: 0.03em;
}
.world-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
white-space: nowrap;
will-change: left, top, opacity;
}
.frame-label {
padding: 4px 7px;
border: 1px solid;
border-radius: 6px;
background: rgba(5, 13, 24, 0.82);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.07em;
text-transform: uppercase;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.24);
}
.edge-label {
padding: 3px 6px;
border: 1px solid rgba(155, 205, 235, 0.14);
border-radius: 5px;
background: rgba(5, 13, 24, 0.7);
color: #9bb3c7;
font-size: 8px;
font-weight: 720;
letter-spacing: 0.04em;
}
.ratio-label {
padding: 4px 7px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 999px;
background: rgba(6, 15, 28, 0.88);
color: #e9f8ff;
font-size: 9px;
font-weight: 850;
font-variant-numeric: tabular-nums;
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
}
@media (max-width: 780px) {
.brand {
top: 16px;
left: 16px;
width: calc(100vw - 32px);
}
.brand p {
max-width: 330px;
font-size: 11px;
}
.angle-note {
margin-top: 9px;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 34vh;
padding: 13px;
border-radius: 15px;
}
#pairs {
display: flex;
overflow-x: auto;
padding-bottom: 3px;
}
.pair-card {
min-width: 270px;
}
.panel-foot,
.legend,
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="labels" aria-hidden="true"></div>
<div id="ui">
<header class="brand">
<div class="eyebrow">Geometry / Uniform Scaling</div>
<h1>Nested Similar Frames</h1>
<p>
Four concentric equilateral frames share identical angles while their
uniform scale changes. Dashed guides connect corresponding edge midpoints.
</p>
<div class="angle-note">∠A = ∠B = ∠C = 60° in every frame</div>
</header>
<aside class="panel">
<div class="panel-header">
<div>
<h2>Corresponding edge ratios</h2>
<p>Lengths are measured in the same arbitrary unit.</p>
</div>
<button class="reset" id="resetView" type="button">Reset view</button>
</div>
<div id="pairs"></div>
<p class="panel-foot">
Every row in a pair has the same ratio because each frame is similar to
the next. The dashed links join matching AB, BC, and CA edges.
</p>
</aside>
<div class="legend" id="legend"></div>
<div class="hint">
Drag to orbit · Scroll to zoom · Double-click to reset
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Configuration
// -------------------------------------------------------------------------
const BASE_RADIUS = 2.6;
const frames = [
{ name: "Outer", scale: 1.00, color: 0x55e2ff, z: 0.00 },
{ name: "Middle", scale: 0.76, color: 0xff65ad, z: 0.07 },
{ name: "Inner", scale: 0.52, color: 0xffcc62, z: 0.14 },
{ name: "Core", scale: 0.28, color: 0x79f0a2, z: 0.21 }
];
const edgeNames = ["AB", "BC", "CA"];
const edgePairs = [[0, 1], [1, 2], [2, 0]];
const baseSideLength = Math.sqrt(3) * BASE_RADIUS;
const vertices = [
new THREE.Vector3(0, BASE_RADIUS, 0),
new THREE.Vector3(
-Math.sqrt(3) * BASE_RADIUS * 0.5,
-BASE_RADIUS * 0.5,
0
),
new THREE.Vector3(
Math.sqrt(3) * BASE_RADIUS * 0.5,
-BASE_RADIUS * 0.5,
0
)
];
const cssColor = (hex) =>
`#${hex.toString(16).padStart(6, "0")}`;
// -------------------------------------------------------------------------
// Renderer, scene, camera, and lighting
// -------------------------------------------------------------------------
const stage = document.getElementById("stage");
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.18;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 8.5, 18);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const target = new THREE.Vector3(0, 0.12, 0);
const hemisphere = new THREE.HemisphereLight(
0xbdeeff,
0x07101c,
2.0
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.4);
keyLight.position.set(4.5, 7, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
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.0005;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x55e2ff, 18, 9, 2);
cyanLight.position.set(-4, 2.4, 3.5);
scene.add(cyanLight);
const pinkLight = new THREE.PointLight(0xff65ad, 13, 8, 2);
pinkLight.position.set(4, -0.2, 2.5);
scene.add(pinkLight);
// -------------------------------------------------------------------------
// Low-poly floor and background details
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x081321,
roughness: 0.95,
metalness: 0.05
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.76;
ground.receiveShadow = true;
scene.add(ground);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(3.72, 3.72, 0.11, 6),
new THREE.MeshStandardMaterial({
color: 0x10263a,
roughness: 0.7,
metalness: 0.25,
flatShading: true
})
);
platform.position.y = -1.7;
platform.receiveShadow = true;
scene.add(platform);
const grid = new THREE.GridHelper(
22,
44,
0x31516b,
0x183148
);
grid.position.y = -1.635;
grid.material.transparent = true;
grid.material.opacity = 0.23;
grid.material.depthWrite = false;
scene.add(grid);
const decor = [];
const decorMaterial = new THREE.MeshStandardMaterial({
color: 0x31506a,
roughness: 0.7,
metalness: 0.2,
flatShading: true
});
const decorPositions = [
[-4.6, 2.1, -2.8],
[ 4.3, 2.8, -3.5],
[-4.1, -0.3, -2.0],
[ 4.8, -0.6, -2.4],
[-3.2, 3.7, -4.0],
[ 3.6, 3.4, -4.8],
[-5.1, 0.9, -4.4],
[ 5.0, 1.5, -1.8]
];
for (let i = 0; i < decorPositions.length; i++) {
const crystal = new THREE.Mesh(
new THREE.TetrahedronGeometry(0.12 + (i % 3) * 0.035, 0),
decorMaterial
);
crystal.position.fromArray(decorPositions[i]);
crystal.rotation.set(i * 0.71, i * 1.13, i * 0.39);
scene.add(crystal);
decor.push(crystal);
}
const particlePositions = new Float32Array(110 * 3);
let seed = 17;
function seededRandom() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
for (let i = 0; i < 110; i++) {
particlePositions[i * 3] = (seededRandom() - 0.5) * 19;
particlePositions[i * 3 + 1] = (seededRandom() - 0.25) * 11;
particlePositions[i * 3 + 2] = -2.5 - seededRandom() * 5;
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3)
);
const particles = new THREE.Points(
particleGeometry,
new THREE.PointsMaterial({
color: 0x9adfff,
size: 0.035,
transparent: true,
opacity: 0.42,
depthWrite: false
})
);
scene.add(particles);
// -------------------------------------------------------------------------
// Triangle frame construction
// -------------------------------------------------------------------------
const frameGroups = [];
const vertexNodes = [];
function createBar(a, b, radius, material, parent) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
6,
1,
false
);
const bar = new THREE.Mesh(geometry, material);
bar.position.copy(a).add(b).multiplyScalar(0.5);
bar.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
bar.castShadow = true;
bar.receiveShadow = true;
parent.add(bar);
return bar;
}
for (const frame of frames) {
const group = new THREE.Group();
group.position.z = frame.z;
group.scale.setScalar(frame.scale);
const material = new THREE.MeshStandardMaterial({
color: frame.color,
emissive: frame.color,
emissiveIntensity: 0.16,
roughness: 0.34,
metalness: 0.48,
flatShading: true
});
for (const [aIndex, bIndex] of edgePairs) {
createBar(
vertices[aIndex],
vertices[bIndex],
0.055,
material,
group
);
}
for (const vertex of vertices) {
const node = new THREE.Mesh(
new THREE.OctahedronGeometry(0.115, 0),
material
);
node.position.copy(vertex);
node.rotation.set(0.35, 0.72, 0.18);
node.castShadow = true;
node.receiveShadow = true;
group.add(node);
vertexNodes.push(node);
}
scene.add(group);
group.updateMatrixWorld(true);
frameGroups.push(group);
}
// -------------------------------------------------------------------------
// Corresponding-edge connectors
// -------------------------------------------------------------------------
const projectedLabels = [];
function edgeMidpointLocal(edgeIndex) {
const [a, b] = edgePairs[edgeIndex];
return vertices[a].clone().add(vertices[b]).multiplyScalar(0.5);
}
function edgeMidpointWorld(group, edgeIndex) {
return group.localToWorld(edgeMidpointLocal(edgeIndex));
}
function addProjectedLabel(element, worldPosition, offsetX = 0, offsetY = 0) {
document.getElementById("labels").appendChild(element);
projectedLabels.push({
element,
position: worldPosition.clone(),
offsetX,
offsetY
});
}
const connectorGroups = [];
for (let pairIndex = 0; pairIndex < frames.length - 1; pairIndex++) {
const outer = frames[pairIndex];
const inner = frames[pairIndex + 1];
const outerGroup = frameGroups[pairIndex];
const innerGroup = frameGroups[pairIndex + 1];
const mixedColor = new THREE.Color(outer.color)
.lerp(new THREE.Color(inner.color), 0.45);
const connectorMaterial = new THREE.LineDashedMaterial({
color: mixedColor,
transparent: true,
opacity: 0.72,
dashSize: 0.085,
gapSize: 0.065,
depthWrite: false
});
const markerGeometry = new THREE.SphereGeometry(0.042, 6, 4);
const markerMaterial = new THREE.MeshStandardMaterial({
color: mixedColor,
emissive: mixedColor,
emissiveIntensity: 0.32,
roughness: 0.4,
metalness: 0.3,
flatShading: true
});
for (let edgeIndex = 0; edgeIndex < edgePairs.length; edgeIndex++) {
const start = edgeMidpointWorld(outerGroup, edgeIndex);
const end = edgeMidpointWorld(innerGroup, edgeIndex);
const geometry = new THREE.BufferGeometry().setFromPoints([
start,
end
]);
const line = new THREE.Line(geometry, connectorMaterial);
line.computeLineDistances();
line.renderOrder = 2;
scene.add(line);
for (const point of [start, end]) {
const marker = new THREE.Mesh(markerGeometry, markerMaterial);
marker.position.copy(point);
marker.castShadow = true;
scene.add(marker);
}
}
connectorGroups.push({
pairIndex,
outer,
inner,
ratio: outer.scale / inner.scale
});
}
// -------------------------------------------------------------------------
// DOM information panel
// -------------------------------------------------------------------------
const pairContainer = document.getElementById("pairs");
connectorGroups.forEach((pair, index) => {
const outerLength = baseSideLength * pair.outer.scale;
const innerLength = baseSideLength * pair.inner.scale;
const ratio = pair.ratio;
const accent = cssColor(pair.outer.color);
const card = document.createElement("section");
card.className = "pair-card";
card.style.setProperty("--accent", accent);
let rows = "";
for (const edgeName of edgeNames) {
rows += `
<div class="edge-row">
<span class="edge-id">${edgeName} ↔ ${edgeName}′</span>
<span class="edge-values">
${outerLength.toFixed(3)} / ${innerLength.toFixed(3)}
</span>
<span class="edge-ratio">${ratio.toFixed(3)}×</span>
</div>
`;
}
card.innerHTML = `
<div class="pair-heading">
<span class="pair-number">
${String(index + 1).padStart(2, "0")}
</span>
<span class="pair-names">
${pair.outer.name} ↔ ${pair.inner.name}
</span>
<span class="pair-ratio">${ratio.toFixed(3)}×</span>
</div>
${rows}
`;
pairContainer.appendChild(card);
});
const legend = document.getElementById("legend");
for (const frame of frames) {
const item = document.createElement("div");
item.className = "legend-item";
item.style.setProperty("--dot", cssColor(frame.color));
item.innerHTML = `
<span class="legend-dot"></span>
${frame.name} · s=${frame.scale.toFixed(3)}
`;
legend.appendChild(item);
}
// -------------------------------------------------------------------------
// World-space labels
// -------------------------------------------------------------------------
frames.forEach((frame, index) => {
const group = frameGroups[index];
const frameLabel = document.createElement("div");
frameLabel.className = "world-label frame-label";
frameLabel.textContent = `${frame.name} · s ${frame.scale.toFixed(3)}`;
frameLabel.style.color = cssColor(frame.color);
frameLabel.style.borderColor = `${cssColor(frame.color)}66`;
const topPoint = group.localToWorld(vertices[0].clone());
addProjectedLabel(
frameLabel,
topPoint,
0,
index === 0 ? -23 : -14
);
});
// Label the three edges on the outer frame.
frames.forEach((frame, frameIndex) => {
const group = frameGroups[frameIndex];
for (let edgeIndex = 0; edgeIndex < edgePairs.length; edgeIndex++) {
if (frameIndex !== 0) continue;
const localMidpoint = edgeMidpointLocal(edgeIndex);
const outward = localMidpoint.clone().setZ(0).normalize();
const labelPosition = group.localToWorld(
localMidpoint.clone().addScaledVector(outward, 0.2)
);
const label = document.createElement("div");
label.className = "world-label edge-label";
label.textContent =
`${edgeNames[edgeIndex]} · ${baseSideLength.toFixed(2)}`;
addProjectedLabel(label, labelPosition);
}
});
// One floating ratio badge per adjacent frame pair.
connectorGroups.forEach((pair) => {
const outerGroup = frameGroups[pair.pairIndex];
const innerGroup = frameGroups[pair.pairIndex + 1];
// The BC edge points downward, leaving a clear area for the badge.
const start = edgeMidpointWorld(outerGroup, 1);
const end = edgeMidpointWorld(innerGroup, 1);
const anchor = start.clone().lerp(end, 0.5);
const badge = document.createElement("div");
badge.className = "world-label ratio-label";
badge.textContent = `r ${pair.ratio.toFixed(3)}×`;
badge.style.borderColor = `${cssColor(pair.outer.color)}88`;
addProjectedLabel(badge, anchor, 0, 4);
});
// -------------------------------------------------------------------------
// Custom orbit, zoom, keyboard, and reset controls
// -------------------------------------------------------------------------
const orbit = {
theta: 0.47,
phi: 1.04,
radius: 8.2,
desiredTheta: 0.47,
desiredPhi: 1.04,
desiredRadius: 8.2
};
const initialOrbit = {
theta: orbit.theta,
phi: orbit.phi,
radius: orbit.radius
};
function resetCamera() {
orbit.desiredTheta = initialOrbit.theta;
orbit.desiredPhi = initialOrbit.phi;
orbit.desiredRadius = initialOrbit.radius;
}
const canvas = renderer.domElement;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.pointerType === "mouse" && event.button !== 0) return;
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.classList.add("dragging");
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.desiredTheta -= deltaX * 0.006;
orbit.desiredPhi = THREE.MathUtils.clamp(
orbit.desiredPhi - deltaY * 0.005,
0.35,
Math.PI - 0.35
);
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (
event.pointerType !== "mouse" &&
canvas.hasPointerCapture(event.pointerId)
) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.desiredRadius = THREE.MathUtils.clamp(
orbit.desiredRadius * Math.exp(event.deltaY * 0.001),
5.2,
14
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("resetView").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "arrowleft") {
orbit.desiredTheta += 0.1;
} else if (key === "arrowright") {
orbit.desiredTheta -= 0.1;
} else if (key === "arrowup") {
orbit.desiredPhi = THREE.MathUtils.clamp(
orbit.desiredPhi - 0.08,
0.35,
Math.PI - 0.35
);
} else if (key === "arrowdown") {
orbit.desiredPhi = THREE.MathUtils.clamp(
orbit.desiredPhi + 0.08,
0.35,
Math.PI - 0.35
);
} else if (key === "+" || key === "=") {
orbit.desiredRadius = Math.max(5.2, orbit.desiredRadius - 0.4);
} else if (key === "-") {
orbit.desiredRadius = Math.min(14, orbit.desiredRadius + 0.4);
} else if (key === "r") {
resetCamera();
}
});
// -------------------------------------------------------------------------
// Project DOM labels into the Three.js scene
// -------------------------------------------------------------------------
const projectedPoint = new THREE.Vector3();
function updateProjectedLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of projectedLabels) {
projectedPoint.copy(label.position).project(camera);
const visible =
projectedPoint.z >= -1 &&
projectedPoint.z <= 1 &&
Math.abs(projectedPoint.x) < 1.15 &&
Math.abs(projectedPoint.y) < 1.15;
if (!visible) {
label.element.style.opacity = "0";
continue;
}
const x = (projectedPoint.x * 0.5 + 0.5) * width;
const y = (-projectedPoint.y * 0.5 + 0.5) * height;
label.element.style.left = `${x + label.offsetX}px`;
label.element.style.top = `${y + label.offsetY}px`;
label.element.style.opacity = "1";
}
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
const cameraOffset = new THREE.Vector3();
const spherical = new THREE.Spherical();
let previousTime = performance.now();
function animate(time) {
requestAnimationFrame(animate);
const delta = Math.min((time - previousTime) / 1000, 0.05);
previousTime = time;
const damping = 1 - Math.pow(0.001, delta);
orbit.theta +=
(orbit.desiredTheta - orbit.theta) * damping;
orbit.phi +=
(orbit.desiredPhi - orbit.phi) * damping;
orbit.radius +=
(orbit.desiredRadius - orbit.radius) * damping;
spherical.set(
orbit.radius,
orbit.phi,
orbit.theta
);
cameraOffset.setFromSpherical(spherical);
camera.position.copy(target).add(cameraOffset);
camera.lookAt(target);
camera.updateMatrixWorld();
for (let i = 0; i < vertexNodes.length; i++) {
const node = vertexNodes[i];
node.rotation.x += delta * (0.16 + (i % 3) * 0.025);
node.rotation.y += delta * (0.23 + (i % 2) * 0.035);
}
for (let i = 0; i < decor.length; i++) {
decor[i].rotation.x += delta * (0.08 + i * 0.002);
decor[i].rotation.y += delta * (0.11 + i * 0.003);
}
particles.rotation.y += delta * 0.008;
renderer.render(scene, camera);
updateProjectedLabels();
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```