Prompt specification
Build or customize the Three.js 3D scene "Data-structure operation explorer" 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: Data-structure operation explorer
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/data-structure-operation-explorer-x1912
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing stack and queue data structures. Let users push, pop, enqueue, and dequeue on small bounded structures; report empty states correctly and display current contents after every operation. Low-poly.
Working Three.js Starter (data-structure-operation-explorer-x1912):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Structure Yard — Stack & Queue</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: #0d1a2b;
--panel-2: #101f32;
--border: rgba(148, 177, 210, 0.17);
--text: #edf6ff;
--muted: #8fa6bd;
--stack: #ffad5c;
--stack-soft: rgba(255, 173, 92, 0.13);
--queue: #58e0d4;
--queue-soft: rgba(88, 224, 212, 0.13);
--success: #76e6a2;
--warning: #ffd166;
--error: #ff7387;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
overflow-x: hidden;
background:
radial-gradient(circle at 18% 0%, rgba(42, 105, 151, 0.18), transparent 34rem),
radial-gradient(circle at 100% 20%, rgba(44, 184, 168, 0.08), transparent 30rem),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
width: min(1480px, 100%);
margin: 0 auto;
padding: 20px;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
margin-bottom: 18px;
}
.brand {
display: flex;
align-items: center;
gap: 14px;
}
.brand-mark {
position: relative;
width: 48px;
height: 48px;
flex: 0 0 auto;
}
.brand-mark span {
position: absolute;
left: 7px;
width: 34px;
height: 8px;
border-radius: 3px;
transform: skewX(-16deg);
box-shadow: 0 5px 16px rgba(0, 0, 0, 0.25);
}
.brand-mark span:nth-child(1) {
top: 29px;
background: #d67b3d;
}
.brand-mark span:nth-child(2) {
top: 19px;
left: 10px;
background: #f09950;
}
.brand-mark span:nth-child(3) {
top: 9px;
left: 13px;
background: var(--stack);
}
.eyebrow {
margin: 0 0 3px;
color: var(--queue);
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.45rem, 3vw, 2.25rem);
line-height: 1;
letter-spacing: -0.04em;
}
.subtitle {
margin: 7px 0 0;
color: var(--muted);
font-size: 0.88rem;
}
.header-badges {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
}
.badge {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 8px 11px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(14, 30, 48, 0.72);
color: #c5d5e6;
font-size: 0.72rem;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.badge::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--queue);
content: "";
box-shadow: 0 0 12px currentColor;
}
.badge.stack::before {
background: var(--stack);
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1fr) 370px;
gap: 16px;
align-items: stretch;
}
.stage {
position: relative;
min-width: 0;
min-height: 570px;
height: min(74vh, 790px);
overflow: hidden;
border: 1px solid var(--border);
border-radius: 26px;
background: #081321;
box-shadow: 0 28px 80px rgba(0, 0, 0, 0.28);
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.stage::after {
position: absolute;
inset: 0;
z-index: 1;
pointer-events: none;
background:
linear-gradient(180deg, rgba(7, 17, 31, 0.35), transparent 24%),
linear-gradient(0deg, rgba(7, 17, 31, 0.5), transparent 24%);
content: "";
}
.stage-top,
.stage-bottom {
position: absolute;
z-index: 2;
left: 18px;
right: 18px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.stage-top {
top: 16px;
}
.stage-bottom {
bottom: 16px;
}
.view-label {
padding: 9px 12px;
border: 1px solid rgba(142, 177, 211, 0.16);
border-radius: 12px;
background: rgba(6, 15, 27, 0.7);
color: #a9bed2;
font-size: 0.7rem;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.view-help {
color: #8fa6bd;
font-size: 0.72rem;
text-align: right;
}
.panel {
display: flex;
flex-direction: column;
gap: 13px;
padding: 17px;
border: 1px solid var(--border);
border-radius: 26px;
background:
linear-gradient(145deg, rgba(17, 34, 54, 0.95), rgba(9, 22, 37, 0.97));
box-shadow: 0 28px 80px rgba(0, 0, 0, 0.24);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 1px 2px 3px;
}
.panel-heading h2 {
margin: 0;
font-size: 1rem;
letter-spacing: -0.01em;
}
.capacity-note {
color: var(--muted);
font-size: 0.72rem;
}
.input-block {
padding: 13px;
border: 1px solid var(--border);
border-radius: 17px;
background: rgba(5, 14, 25, 0.38);
}
.input-label {
display: block;
margin-bottom: 8px;
color: #b9cada;
font-size: 0.74rem;
font-weight: 700;
}
.input-wrap {
display: flex;
gap: 8px;
}
input {
min-width: 0;
flex: 1;
border: 1px solid rgba(143, 177, 211, 0.22);
border-radius: 12px;
outline: none;
background: #091625;
color: var(--text);
padding: 11px 12px;
transition: border-color 150ms, box-shadow 150ms;
}
input:focus {
border-color: rgba(88, 224, 212, 0.7);
box-shadow: 0 0 0 3px rgba(88, 224, 212, 0.09);
}
.input-hint {
margin: 8px 0 0;
color: #7189a0;
font-size: 0.67rem;
}
.structure-card {
padding: 14px;
border: 1px solid var(--border);
border-radius: 19px;
background: rgba(5, 14, 25, 0.28);
}
.structure-card.stack {
box-shadow: inset 3px 0 0 rgba(255, 173, 92, 0.7);
}
.structure-card.queue {
box-shadow: inset 3px 0 0 rgba(88, 224, 212, 0.7);
}
.structure-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.structure-name {
display: flex;
align-items: center;
gap: 10px;
}
.structure-icon {
display: grid;
width: 34px;
height: 34px;
place-items: center;
border-radius: 10px;
background: var(--stack-soft);
color: var(--stack);
font-size: 1.2rem;
font-weight: 900;
}
.queue .structure-icon {
background: var(--queue-soft);
color: var(--queue);
}
.structure-name h3 {
margin: 0;
font-size: 0.92rem;
}
.structure-name p {
margin: 2px 0 0;
color: var(--muted);
font-size: 0.66rem;
}
.count {
padding: 6px 9px;
border-radius: 9px;
background: rgba(130, 161, 193, 0.1);
color: #c5d4e3;
font-size: 0.72rem;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.capacity-dots {
display: flex;
gap: 5px;
margin-bottom: 12px;
}
.capacity-dot {
height: 5px;
flex: 1;
overflow: hidden;
border-radius: 999px;
background: rgba(151, 174, 199, 0.12);
}
.capacity-dot::after {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--stack);
content: "";
transition: width 200ms ease;
}
.capacity-dot.filled::after {
width: 100%;
}
.queue .capacity-dot::after {
background: var(--queue);
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.action {
min-height: 42px;
border: 1px solid transparent;
border-radius: 12px;
cursor: pointer;
font-size: 0.78rem;
font-weight: 800;
transition: transform 120ms ease, filter 120ms ease, border-color 120ms ease;
}
.action:hover {
filter: brightness(1.08);
transform: translateY(-1px);
}
.action:active {
transform: translateY(1px);
}
.action:focus-visible,
input:focus-visible {
outline: 2px solid #b8fff6;
outline-offset: 2px;
}
.action.primary {
background: var(--stack);
color: #251307;
}
.queue .action.primary {
background: var(--queue);
color: #052522;
}
.action.secondary {
border-color: rgba(146, 176, 206, 0.2);
background: rgba(121, 153, 185, 0.09);
color: #d3dfeb;
}
.action.secondary:hover {
border-color: rgba(146, 176, 206, 0.4);
}
.contents {
margin-top: 13px;
}
.contents-label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: #7f97ae;
font-size: 0.65rem;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.chips {
display: flex;
min-height: 37px;
flex-wrap: wrap;
gap: 6px;
}
.chip {
display: inline-flex;
max-width: 100%;
align-items: center;
gap: 6px;
overflow: hidden;
padding: 7px 9px;
border: 1px solid rgba(255, 173, 92, 0.26);
border-radius: 9px;
background: var(--stack-soft);
color: #ffd0a0;
font-size: 0.75rem;
font-weight: 750;
text-overflow: ellipsis;
white-space: nowrap;
}
.queue .chip {
border-color: rgba(88, 224, 212, 0.25);
background: var(--queue-soft);
color: #adfff7;
}
.chip small {
color: rgba(255, 255, 255, 0.45);
font-size: 0.52rem;
font-weight: 900;
letter-spacing: 0.06em;
}
.empty {
display: inline-flex;
align-items: center;
padding: 7px 9px;
border: 1px dashed rgba(146, 176, 206, 0.18);
border-radius: 9px;
color: #647d95;
font-size: 0.72rem;
}
.feedback {
min-height: 62px;
margin-top: auto;
padding: 12px 13px;
border: 1px solid rgba(146, 176, 206, 0.16);
border-radius: 15px;
background: rgba(120, 153, 185, 0.07);
color: #aebfd0;
font-size: 0.76rem;
line-height: 1.45;
}
.feedback[data-tone="success"] {
border-color: rgba(118, 230, 162, 0.25);
background: rgba(118, 230, 162, 0.08);
color: #a8f2c5;
}
.feedback[data-tone="warning"] {
border-color: rgba(255, 209, 102, 0.25);
background: rgba(255, 209, 102, 0.08);
color: #ffe29c;
}
.feedback[data-tone="error"] {
border-color: rgba(255, 115, 135, 0.27);
background: rgba(255, 115, 135, 0.08);
color: #ffacb8;
}
.feedback strong {
display: block;
margin-bottom: 2px;
color: inherit;
font-size: 0.68rem;
letter-spacing: 0.08em;
text-transform: uppercase;
}
footer {
display: flex;
justify-content: space-between;
gap: 16px;
padding: 13px 4px 0;
color: #5f7890;
font-size: 0.67rem;
}
@media (max-width: 980px) {
.workspace {
grid-template-columns: 1fr;
}
.stage {
height: 58vh;
min-height: 440px;
}
.panel {
display: grid;
grid-template-columns: 1fr 1fr;
}
.panel-heading,
.input-block,
.feedback {
grid-column: 1 / -1;
}
.feedback {
margin-top: 0;
}
}
@media (max-width: 640px) {
.app {
padding: 12px;
}
header {
align-items: flex-start;
flex-direction: column;
}
.header-badges {
justify-content: flex-start;
}
.stage {
min-height: 390px;
border-radius: 20px;
}
.panel {
display: flex;
padding: 13px;
border-radius: 20px;
}
.view-help {
display: none;
}
footer {
flex-direction: column;
}
}
</style>
</head>
<body>
<div class="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<span></span><span></span><span></span>
</div>
<div>
<p class="eyebrow">Interactive data structures</p>
<h1>Structure Yard</h1>
<p class="subtitle">A low-poly playground for stack and queue operations.</p>
</div>
</div>
<div class="header-badges">
<span class="badge stack">Stack · LIFO</span>
<span class="badge">Queue · FIFO</span>
<span class="badge">Capacity · 5</span>
</div>
</header>
<main class="workspace">
<section class="stage" aria-label="Three-dimensional visualization">
<div id="scene"></div>
<div class="stage-top">
<span class="view-label">Live 3D workspace</span>
<span class="view-label">Faceted / real-time</span>
</div>
<div class="stage-bottom">
<span class="view-label">Push & pop vertically</span>
<span class="view-help">Drag to orbit · Scroll or pinch to zoom</span>
</div>
</section>
<aside class="panel">
<div class="panel-heading">
<h2>Operations</h2>
<span class="capacity-note">Each structure holds up to 5 values</span>
</div>
<div class="input-block">
<label class="input-label" for="valueInput">Value to add</label>
<div class="input-wrap">
<input
id="valueInput"
type="text"
value="A1"
maxlength="14"
autocomplete="off"
spellcheck="false"
aria-describedby="inputHint"
>
</div>
<p id="inputHint" class="input-hint">Press Enter to push the value onto the stack.</p>
</div>
<section class="structure-card stack" aria-labelledby="stackTitle">
<div class="structure-header">
<div class="structure-name">
<div class="structure-icon" aria-hidden="true">↕</div>
<div>
<h3 id="stackTitle">Stack</h3>
<p>Last in, first out</p>
</div>
</div>
<span class="count" id="stackCount">0 / 5</span>
</div>
<div class="capacity-dots" data-capacity="stack" aria-hidden="true">
<span class="capacity-dot"></span>
<span class="capacity-dot"></span>
<span class="capacity-dot"></span>
<span class="capacity-dot"></span>
<span class="capacity-dot"></span>
</div>
<div class="buttons">
<button class="action primary" id="pushButton" type="button">Push</button>
<button class="action secondary" id="popButton" type="button">Pop</button>
</div>
<div class="contents">
<div class="contents-label">
<span>Stack contents</span>
<span>Bottom → top</span>
</div>
<div class="chips" id="stackContents" aria-live="polite"></div>
</div>
</section>
<section class="structure-card queue" aria-labelledby="queueTitle">
<div class="structure-header">
<div class="structure-name">
<div class="structure-icon" aria-hidden="true">→</div>
<div>
<h3 id="queueTitle">Queue</h3>
<p>First in, first out</p>
</div>
</div>
<span class="count" id="queueCount">0 / 5</span>
</div>
<div class="capacity-dots" data-capacity="queue" aria-hidden="true">
<span class="capacity-dot"></span>
<span class="capacity-dot"></span>
<span class="capacity-dot"></span>
<span class="capacity-dot"></span>
<span class="capacity-dot"></span>
</div>
<div class="buttons">
<button class="action primary" id="enqueueButton" type="button">Enqueue</button>
<button class="action secondary" id="dequeueButton" type="button">Dequeue</button>
</div>
<div class="contents">
<div class="contents-label">
<span>Queue contents</span>
<span>Front → rear</span>
</div>
<div class="chips" id="queueContents" aria-live="polite"></div>
</div>
</section>
<div class="feedback" id="feedback" data-tone="info" role="status" aria-live="polite">
<strong>Ready</strong>
Add a value to either structure to begin.
</div>
</aside>
</main>
<footer>
<span>Stacks remove the newest value. Queues remove the oldest value.</span>
<span>Three.js core · No controls addons · Fully client-side</span>
</footer>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const CAPACITY = 5;
const STACK_X = -3.25;
const QUEUE_FRONT_X = 1.05;
const QUEUE_SPACING = 1.15;
const QUEUE_Y = 0.92;
const stack = [];
const queue = [];
const stackVisuals = [];
const queueVisuals = [];
const animations = [];
let nextId = 1;
let fitFactor = 1;
const stackPalette = [
0xff9f43, 0xf58b55, 0xf6bd60, 0xff8f66, 0xe99b48
];
const queuePalette = [
0x45d8cb, 0x38bdf8, 0x2dd4bf, 0x60a5fa, 0x22c7d9
];
const sceneHost = document.getElementById("scene");
const valueInput = document.getElementById("valueInput");
const feedback = document.getElementById("feedback");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x081321);
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 scene showing a stack and a queue"
);
renderer.domElement.tabIndex = 0;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x081321);
scene.fog = new THREE.Fog(0x081321, 15, 31);
const camera = new THREE.PerspectiveCamera(46, 1, 0.1, 80);
const controls = {
yaw: 0.56,
pitch: 0.48,
radius: 11.8,
target: new THREE.Vector3(0.45, 1.15, 0),
dragging: false,
pointerX: 0,
pointerY: 0
};
const hemisphere = new THREE.HemisphereLight(0xa9ddff, 0x182136, 2.25);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(4, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x45c9ff, 1.25);
rimLight.position.set(-7, 5, -6);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa64d, 16, 10, 2);
warmLight.position.set(-5, 3.5, 2);
scene.add(warmLight);
const coolLight = new THREE.PointLight(0x4be4d6, 15, 10, 2);
coolLight.position.set(5, 3, 1);
scene.add(coolLight);
// Ground and grid.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 30),
new THREE.MeshStandardMaterial({
color: 0x091625,
roughness: 0.96,
metalness: 0.02
})
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(30, 30, 0x25465e, 0x122a3d);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.56;
scene.add(grid);
function addEdges(mesh, color = 0x8fb9d8, opacity = 0.3) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry, 20),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
mesh.add(edges);
return edges;
}
function createPlatform(x, width, accent) {
const group = new THREE.Group();
const base = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.3, 2.2),
new THREE.MeshStandardMaterial({
color: 0x15283b,
roughness: 0.78,
metalness: 0.08,
flatShading: true
})
);
base.position.set(x, 0.19, 0);
base.castShadow = true;
base.receiveShadow = true;
addEdges(base, accent, 0.22);
group.add(base);
const top = new THREE.Mesh(
new THREE.BoxGeometry(width - 0.12, 0.065, 2.04),
new THREE.MeshStandardMaterial({
color: 0x1a3044,
roughness: 0.72,
metalness: 0.06
})
);
top.position.set(x, 0.37, 0);
top.receiveShadow = true;
addEdges(top, accent, 0.16);
group.add(top);
const boltGeometry = new THREE.CylinderGeometry(0.055, 0.055, 0.035, 8);
const boltMaterial = new THREE.MeshStandardMaterial({
color: accent,
roughness: 0.45,
metalness: 0.55,
flatShading: true
});
const corners = [
[-width / 2 + 0.22, -0.88],
[width / 2 - 0.22, -0.88],
[-width / 2 + 0.22, 0.88],
[width / 2 - 0.22, 0.88]
];
for (const [offsetX, offsetZ] of corners) {
const bolt = new THREE.Mesh(boltGeometry, boltMaterial);
bolt.position.set(x + offsetX, 0.415, offsetZ);
group.add(bolt);
}
scene.add(group);
return group;
}
createPlatform(STACK_X, 3.55, 0xffad5c);
createPlatform(3.35, 6.25, 0x58e0d4);
const queueTrack = new THREE.Mesh(
new THREE.BoxGeometry(5.55, 0.025, 0.075),
new THREE.MeshStandardMaterial({
color: 0x4bd8ca,
emissive: 0x163e3d,
roughness: 0.55,
metalness: 0.25
})
);
queueTrack.position.set(3.35, 0.415, 0);
scene.add(queueTrack);
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function createTextSprite(text, color = "#ffffff", background = "rgba(5, 15, 27, 0.84)") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 160;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(context, 8, 8, 496, 144, 28);
context.fillStyle = background;
context.fill();
context.strokeStyle = "rgba(190, 220, 245, 0.16)";
context.lineWidth = 4;
context.stroke();
let fontSize = 72;
context.font = `800 ${fontSize}px system-ui, sans-serif`;
while (context.measureText(text).width > 430 && fontSize > 28) {
fontSize -= 2;
context.font = `800 ${fontSize}px system-ui, sans-serif`;
}
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = color;
context.fillText(text, 256, 83);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 10;
return sprite;
}
const stackTitle = createTextSprite("STACK", "#ffd0a0");
stackTitle.position.set(STACK_X, 0.73, 1.42);
stackTitle.scale.set(1.75, 0.55, 1);
scene.add(stackTitle);
const queueTitle = createTextSprite("QUEUE", "#b8fff7");
queueTitle.position.set(3.35, 0.73, 1.42);
queueTitle.scale.set(1.75, 0.55, 1);
scene.add(queueTitle);
const topLabel = createTextSprite("TOP", "#ffcf9c", "rgba(38, 23, 12, 0.82)");
topLabel.position.set(-4.85, 2.34, -0.63);
topLabel.scale.set(0.72, 0.25, 1);
scene.add(topLabel);
const frontLabel = createTextSprite("FRONT", "#b9fff7", "rgba(8, 37, 38, 0.82)");
frontLabel.position.set(0.58, 0.82, -0.63);
frontLabel.scale.set(0.8, 0.25, 1);
scene.add(frontLabel);
const rearLabel = createTextSprite("REAR", "#b9fff7", "rgba(8, 37, 38, 0.82)");
rearLabel.position.set(6.1, 0.82, -0.63);
rearLabel.scale.set(0.8, 0.25, 1);
scene.add(rearLabel);
const stackArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(-4.82, 0.45, -0.62),
1.5,
0xffad5c,
0.28,
0.15
);
scene.add(stackArrow);
const queueArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(0.62, 0.47, -0.62),
5.35,
0x58e0d4,
0.34,
0.19
);
scene.add(queueArrow);
function stackPosition(index) {
return new THREE.Vector3(STACK_X, 0.66 + index * 0.56, 0);
}
function queuePosition(index) {
return new THREE.Vector3(
QUEUE_FRONT_X + index * QUEUE_SPACING,
QUEUE_Y,
0
);
}
function createNode(value, kind, id) {
const group = new THREE.Group();
const color = kind === "stack"
? stackPalette[id % stackPalette.length]
: queuePalette[id % queuePalette.length];
if (kind === "stack") {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(2.3, 0.46, 1.32),
new THREE.MeshStandardMaterial({
color,
roughness: 0.61,
metalness: 0.12,
flatShading: true
})
);
mesh.castShadow = true;
mesh.receiveShadow = true;
addEdges(mesh, 0xffe0b4, 0.45);
group.add(mesh);
const inset = new THREE.Mesh(
new THREE.BoxGeometry(1.65, 0.025, 0.03),
new THREE.MeshStandardMaterial({
color: 0xffe1b5,
emissive: 0x6e3c18,
roughness: 0.4
})
);
inset.position.set(0, 0.245, 0);
group.add(inset);
const label = createTextSprite(value, "#fff8ed", "rgba(43, 24, 10, 0.9)");
label.position.set(0, -0.01, 0.7);
label.scale.set(1.12, 0.35, 1);
group.add(label);
} else {
const mesh = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.45, 0),
new THREE.MeshStandardMaterial({
color,
roughness: 0.42,
metalness: 0.2,
flatShading: true
})
);
mesh.castShadow = true;
mesh.receiveShadow = true;
addEdges(mesh, 0xc5fffb, 0.34);
group.add(mesh);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.08, 0.25, 6),
new THREE.MeshStandardMaterial({
color: 0xb8fff7,
roughness: 0.4,
metalness: 0.45,
flatShading: true
})
);
stem.position.y = -0.52;
group.add(stem);
const label = createTextSprite(value, "#efffff", "rgba(5, 39, 42, 0.88)");
label.position.set(0, 0.66, 0);
label.scale.set(1.05, 0.33, 1);
group.add(label);
}
return group;
}
function disposeNode(group) {
group.traverse((child) => {
if (child.geometry) {
child.geometry.dispose();
}
if (child.material) {
const materials = Array.isArray(child.material)
? child.material
: [child.material];
for (const material of materials) {
if (material.map) material.map.dispose();
material.dispose();
}
}
});
}
function animateNode(group, to, mode = "shift", duration = 620) {
for (let i = animations.length - 1; i >= 0; i--) {
if (animations[i].group === group) {
animations.splice(i, 1);
}
}
animations.push({
group,
from: group.position.clone(),
to: to.clone(),
fromScale: group.scale.x,
toScale: mode === "exit" ? 0.02 : 1,
start: performance.now(),
duration,
mode
});
}
function updateAnimations(now) {
for (let i = animations.length - 1; i >= 0; i--) {
const animation = animations[i];
const raw = Math.min(1, (now - animation.start) / animation.duration);
const eased = 1 - Math.pow(1 - raw, 3);
animation.group.position.lerpVectors(
animation.from,
animation.to,
eased
);
const scale = THREE.MathUtils.lerp(
animation.fromScale,
animation.toScale,
eased
);
animation.group.scale.setScalar(scale);
if (animation.mode === "enter") {
animation.group.rotation.y = Math.sin(eased * Math.PI) * 0.25;
} else if (animation.mode === "exit") {
animation.group.rotation.y += 0.045;
}
if (raw >= 1) {
if (animation.mode === "exit") {
scene.remove(animation.group);
disposeNode(animation.group);
} else {
animation.group.scale.setScalar(1);
animation.group.rotation.y = 0;
}
animations.splice(i, 1);
}
}
}
function escapeHtml(value) {
return value.replace(/[&<>"']/g, (character) => ({
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
}[character]));
}
function updateCapacity(kind, count) {
const dots = document.querySelectorAll(
`[data-capacity="${kind}"] .capacity-dot`
);
dots.forEach((dot, index) => {
dot.classList.toggle("filled", index < count);
});
}
function updateInterface() {
document.getElementById("stackCount").textContent = `${stack.length} / ${CAPACITY}`;
document.getElementById("queueCount").textContent = `${queue.length} / ${CAPACITY}`;
const stackContents = document.getElementById("stackContents");
const queueContents = document.getElementById("queueContents");
if (stack.length === 0) {
stackContents.innerHTML = '<span class="empty">Empty — nothing to pop</span>';
} else {
stackContents.innerHTML = stack.map((item, index) => {
const position = index === stack.length - 1
? "TOP"
: index === 0
? "BASE"
: index + 1;
return `
<span class="chip">
<small>${position}</small>
${escapeHtml(item.value)}
</span>
`;
}).join("");
}
if (queue.length === 0) {
queueContents.innerHTML = '<span class="empty">Empty — nothing to dequeue</span>';
} else {
queueContents.innerHTML = queue.map((item, index) => {
const position = index === 0
? "FRONT"
: index === queue.length - 1
? "REAR"
: index + 1;
return `
<span class="chip">
<small>${position}</small>
${escapeHtml(item.value)}
</span>
`;
}).join("");
}
updateCapacity("stack", stack.length);
updateCapacity("queue", queue.length);
}
function setFeedback(message, tone = "info", heading = "Operation") {
feedback.dataset.tone = tone;
feedback.innerHTML = `
<strong>${heading}</strong>
${escapeHtml(message)}
`;
}
function readValue() {
const value = valueInput.value.trim();
if (!value) {
setFeedback(
"Enter a non-empty value before adding it to a structure.",
"error",
"Missing value"
);
valueInput.focus();
return null;
}
return value;
}
function pushStack() {
if (stack.length >= CAPACITY) {
setFeedback(
`The stack is full (${CAPACITY}/${CAPACITY}). Pop a value before pushing another.`,
"error",
"Stack full"
);
updateInterface();
return;
}
const value = readValue();
if (value === null) return;
const id = nextId++;
const item = { id, value };
stack.push(item);
const visual = createNode(value, "stack", id);
const target = stackPosition(stack.length - 1);
visual.position.copy(target).add(new THREE.Vector3(0, 0.85, 0));
visual.scale.setScalar(0.05);
scene.add(visual);
stackVisuals.push(visual);
animateNode(visual, target, "enter", 600);
setFeedback(`Pushed “${value}” onto the stack.`, "success", "Push complete");
updateInterface();
}
function popStack() {
if (stack.length === 0) {
setFeedback(
"The stack is empty, so there is no top value to pop.",
"warning",
"Nothing to pop"
);
updateInterface();
return;
}
const item = stack.pop();
const visual = stackVisuals.pop();
const target = visual.position.clone().add(new THREE.Vector3(0, 1.25, 0));
animateNode(visual, target, "exit", 520);
setFeedback(`Popped “${item.value}” from the top of the stack.`, "success", "Pop complete");
updateInterface();
}
function enqueue() {
if (queue.length >= CAPACITY) {
setFeedback(
`The queue is full (${CAPACITY}/${CAPACITY}). Dequeue a value before adding another.`,
"error",
"Queue full"
);
updateInterface();
return;
}
const value = readValue();
if (value === null) return;
const id = nextId++;
const item = { id, value };
queue.push(item);
const visual = createNode(value, "queue", id);
const target = queuePosition(queue.length - 1);
visual.position.copy(target).add(new THREE.Vector3(1.45, 0.35, 0));
visual.scale.setScalar(0.05);
scene.add(visual);
queueVisuals.push(visual);
animateNode(visual, target, "enter", 650);
setFeedback(`Enqueued “${value}” at the rear of the queue.`, "success", "Enqueue complete");
updateInterface();
}
function dequeue() {
if (queue.length === 0) {
setFeedback(
"The queue is empty, so there is no front value to dequeue.",
"warning",
"Nothing to dequeue"
);
updateInterface();
return;
}
const item = queue.shift();
const visual = queueVisuals.shift();
const target = visual.position.clone().add(new THREE.Vector3(-1.4, 0, 0));
animateNode(visual, target, "exit", 520);
queueVisuals.forEach((remaining, index) => {
animateNode(remaining, queuePosition(index), "shift", 620);
});
setFeedback(
`Dequeued “${item.value}” from the front of the queue.`,
"success",
"Dequeue complete"
);
updateInterface();
}
document.getElementById("pushButton").addEventListener("click", pushStack);
document.getElementById("popButton").addEventListener("click", popStack);
document.getElementById("enqueueButton").addEventListener("click", enqueue);
document.getElementById("dequeueButton").addEventListener("click", dequeue);
valueInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
event.preventDefault();
pushStack();
}
});
// Camera controls implemented directly without an addons module.
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
controls.dragging = true;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!controls.dragging) return;
const deltaX = event.clientX - controls.pointerX;
const deltaY = event.clientY - controls.pointerY;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
controls.yaw -= deltaX * 0.006;
controls.pitch = THREE.MathUtils.clamp(
controls.pitch + deltaY * 0.005,
0.16,
1.08
);
});
function endDrag(event) {
controls.dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("lostpointercapture", () => {
controls.dragging = false;
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius + event.deltaY * 0.008,
7.5,
18
);
}, { passive: false });
canvas.addEventListener("dblclick", () => {
controls.yaw = 0.56;
controls.pitch = 0.48;
controls.radius = 11.8;
});
function updateCamera() {
const distance = controls.radius * fitFactor;
const horizontal = Math.cos(controls.pitch) * distance;
camera.position.set(
controls.target.x + Math.sin(controls.yaw) * horizontal,
controls.target.y + Math.sin(controls.pitch) * distance,
controls.target.z + Math.cos(controls.yaw) * horizontal
);
camera.lookAt(controls.target);
}
function resize() {
const width = Math.max(1, sceneHost.clientWidth);
const height = Math.max(1, sceneHost.clientHeight);
const aspect = width / height;
fitFactor = Math.max(1, 1.12 / aspect);
camera.aspect = aspect;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
updateCamera();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(sceneHost);
resize();
updateInterface();
function animate(now) {
requestAnimationFrame(animate);
updateAnimations(now);
updateCamera();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```