Prompt specification
Build or customize the Three.js 3D scene "One Counter Two Queue Rules" 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: One Counter Two Queue Rules
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/one-counter-two-queue-rules-x3250
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows queue order and explicit priority rules. Animate a fictional help desk under first-arrival and short-task-first service. Use identical arrivals and task durations, then display individual waiting times instead of only averages. Voxel style.
Working Three.js Starter (one-counter-two-queue-rules-x3250):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Queue Priority Lab</title>
<style>
:root {
--paper: #f5f0e7;
--paper-dark: #e9e1d4;
--ink: #172a3a;
--muted: #65727c;
--line: #172a3a;
--blue: #4f7cac;
--teal: #2a9d8f;
--orange: #e76f51;
--yellow: #e9c46a;
--purple: #8e6cbb;
--white: #fffdf8;
--shadow: 5px 5px 0 rgba(23, 42, 58, 0.15);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
}
body {
color: var(--ink);
background:
linear-gradient(rgba(23, 42, 58, 0.035) 1px, transparent 1px),
linear-gradient(90deg, rgba(23, 42, 58, 0.035) 1px, transparent 1px),
var(--paper);
background-size: 24px 24px;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
select,
input {
font: inherit;
}
button,
select {
color: var(--ink);
}
.app {
min-height: 100dvh;
display: grid;
grid-template-rows: auto 1fr;
}
.topbar {
position: relative;
z-index: 5;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 16px 20px;
border-bottom: 3px solid var(--line);
background: rgba(255, 253, 248, 0.94);
backdrop-filter: blur(10px);
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 0;
}
.brand-mark {
width: 44px;
height: 44px;
display: grid;
place-items: center;
flex: 0 0 auto;
border: 3px solid var(--line);
background: var(--yellow);
box-shadow: 4px 4px 0 var(--ink);
font: 900 20px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.brand h1 {
margin: 0;
font-size: clamp(18px, 2vw, 25px);
line-height: 1;
letter-spacing: -0.04em;
text-transform: uppercase;
}
.brand p {
margin: 5px 0 0;
color: var(--muted);
font-size: 12px;
font-weight: 700;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.policy-switch {
display: flex;
align-items: stretch;
padding: 4px;
border: 2px solid var(--line);
background: var(--paper-dark);
box-shadow: 3px 3px 0 rgba(23, 42, 58, 0.16);
}
.policy-switch button {
min-width: 145px;
padding: 10px 13px;
border: 0;
cursor: pointer;
background: transparent;
font-size: 12px;
font-weight: 900;
letter-spacing: 0.035em;
text-transform: uppercase;
transition: background 120ms ease, color 120ms ease;
}
.policy-switch button:hover {
background: rgba(255, 255, 255, 0.45);
}
.policy-switch button.active {
color: white;
background: var(--ink);
box-shadow: 2px 2px 0 rgba(23, 42, 58, 0.25);
}
.policy-switch button[data-policy="stf"].active {
background: var(--orange);
}
.workspace {
min-height: 0;
display: grid;
grid-template-columns: minmax(0, 1.65fr) minmax(365px, 0.8fr);
gap: 14px;
padding: 14px;
}
.scene-card,
.panel {
border: 3px solid var(--line);
background: var(--white);
box-shadow: var(--shadow);
}
.scene-card {
position: relative;
min-height: 560px;
overflow: hidden;
background: #e8e1d5;
}
#scene {
position: absolute;
inset: 0;
cursor: grab;
touch-action: none;
}
#scene:active {
cursor: grabbing;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
}
.scene-heading {
position: absolute;
top: 16px;
left: 16px;
z-index: 2;
pointer-events: none;
}
.eyebrow {
margin: 0 0 5px;
color: var(--muted);
font: 900 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.scene-heading h2 {
margin: 0;
font-size: clamp(24px, 3.2vw, 42px);
line-height: 0.95;
letter-spacing: -0.055em;
text-transform: uppercase;
}
.clock-chip {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 10px;
padding: 7px 10px;
border: 2px solid var(--line);
background: rgba(255, 253, 248, 0.9);
font: 900 13px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.clock-chip::before {
width: 8px;
height: 8px;
content: "";
background: var(--orange);
box-shadow: 0 0 0 3px rgba(231, 111, 81, 0.2);
}
.rule-banner {
position: absolute;
top: 16px;
right: 16px;
z-index: 2;
width: min(355px, calc(100% - 32px));
padding: 12px 14px;
border: 2px solid var(--line);
background: rgba(255, 253, 248, 0.94);
box-shadow: 4px 4px 0 rgba(23, 42, 58, 0.16);
pointer-events: none;
}
.rule-banner strong {
display: block;
margin-bottom: 4px;
font-size: 13px;
text-transform: uppercase;
}
.rule-banner p {
margin: 0;
color: #46545e;
font-size: 12px;
line-height: 1.45;
}
.scene-legend {
position: absolute;
left: 16px;
bottom: 105px;
z-index: 2;
display: flex;
flex-wrap: wrap;
gap: 7px;
pointer-events: none;
}
.legend-chip {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 8px;
border: 1.5px solid var(--line);
background: rgba(255, 253, 248, 0.88);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.legend-dot {
width: 9px;
height: 9px;
border: 1px solid var(--line);
background: var(--teal);
}
.legend-dot.waiting {
background: var(--yellow);
}
.legend-dot.done {
background: #aab1b3;
}
.camera-tip {
position: absolute;
right: 16px;
bottom: 105px;
z-index: 2;
color: rgba(23, 42, 58, 0.62);
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
pointer-events: none;
}
.control-dock {
position: absolute;
right: 14px;
bottom: 14px;
left: 14px;
z-index: 3;
display: grid;
grid-template-columns: auto auto minmax(120px, 1fr) auto;
align-items: center;
gap: 10px;
padding: 11px;
border: 2px solid var(--line);
background: rgba(255, 253, 248, 0.96);
box-shadow: 4px 4px 0 rgba(23, 42, 58, 0.18);
}
.control-button {
min-height: 39px;
padding: 9px 13px;
border: 2px solid var(--line);
cursor: pointer;
background: var(--white);
font-size: 12px;
font-weight: 900;
text-transform: uppercase;
box-shadow: 2px 2px 0 rgba(23, 42, 58, 0.18);
}
.control-button:hover {
transform: translateY(-1px);
box-shadow: 2px 3px 0 rgba(23, 42, 58, 0.22);
}
.control-button.primary {
min-width: 92px;
color: white;
background: var(--ink);
}
.timeline-wrap {
min-width: 0;
}
.timeline-label {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
color: var(--muted);
font: 800 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
text-transform: uppercase;
}
input[type="range"] {
--pct: 0%;
width: 100%;
height: 8px;
margin: 0;
border: 1px solid var(--line);
border-radius: 0;
cursor: pointer;
appearance: none;
background: linear-gradient(
to right,
var(--orange) 0%,
var(--orange) var(--pct),
#d9d3c9 var(--pct),
#d9d3c9 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 18px;
border: 2px solid var(--line);
border-radius: 0;
appearance: none;
background: var(--yellow);
box-shadow: 1px 1px 0 rgba(23, 42, 58, 0.2);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 16px;
border: 2px solid var(--line);
border-radius: 0;
background: var(--yellow);
}
.speed-label {
display: grid;
gap: 4px;
color: var(--muted);
font: 800 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
text-transform: uppercase;
}
select {
height: 34px;
padding: 5px 25px 5px 8px;
border: 2px solid var(--line);
border-radius: 0;
background: var(--white);
font-weight: 900;
}
.sidebar {
min-height: 0;
overflow: auto;
display: grid;
align-content: start;
gap: 12px;
padding-right: 3px;
}
.panel {
padding: 14px;
}
.panel-header {
display: flex;
align-items: start;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
}
.panel h3 {
margin: 0;
font-size: 16px;
letter-spacing: -0.02em;
text-transform: uppercase;
}
.panel-subtitle {
margin: 4px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.count-badge {
flex: 0 0 auto;
padding: 5px 7px;
border: 2px solid var(--line);
background: var(--yellow);
font: 900 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.now-serving {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 10px;
padding: 10px;
border: 2px solid var(--line);
background: #e5f1ee;
}
.now-icon {
width: 36px;
height: 36px;
display: grid;
place-items: center;
border: 2px solid var(--line);
background: var(--teal);
color: white;
font-weight: 950;
}
.now-copy strong {
display: block;
font-size: 13px;
}
.now-copy small {
color: var(--muted);
font-size: 10px;
}
.progress-track {
width: 74px;
height: 9px;
border: 1.5px solid var(--line);
background: white;
overflow: hidden;
}
.progress-fill {
width: 0;
height: 100%;
background: var(--teal);
transition: width 100ms linear;
}
.queue-list {
display: grid;
gap: 6px;
margin: 10px 0 0;
padding: 0;
list-style: none;
}
.queue-item {
display: grid;
grid-template-columns: 27px 12px 1fr auto;
align-items: center;
gap: 8px;
padding: 8px;
border: 1.5px solid var(--line);
background: white;
}
.queue-item.next {
background: #fff6d7;
box-shadow: 3px 3px 0 rgba(23, 42, 58, 0.13);
}
.rank {
width: 24px;
height: 24px;
display: grid;
place-items: center;
border: 1.5px solid var(--line);
background: var(--paper-dark);
font: 900 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.next .rank {
background: var(--yellow);
}
.person-swatch {
width: 11px;
height: 28px;
border: 1px solid var(--line);
}
.queue-copy strong {
display: block;
font-size: 12px;
}
.queue-copy small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.reason-tag {
padding: 4px 5px;
border: 1px solid var(--line);
background: var(--paper-dark);
font: 800 8px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.empty-state {
padding: 18px 10px;
border: 1.5px dashed #a9a49c;
color: var(--muted);
font-size: 11px;
text-align: center;
}
.table-wrap {
overflow-x: auto;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 10px;
}
th {
padding: 7px 5px;
border-bottom: 2px solid var(--line);
color: var(--muted);
font: 900 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.05em;
text-align: left;
text-transform: uppercase;
}
td {
padding: 8px 5px;
border-bottom: 1px solid #d8d3ca;
vertical-align: middle;
}
tbody tr:last-child td {
border-bottom: 0;
}
tbody tr.is-serving {
background: #e5f1ee;
}
tbody tr.is-done {
color: var(--muted);
}
tbody tr.is-future {
opacity: 0.48;
}
.person-cell strong {
display: block;
font-size: 11px;
}
.status {
display: inline-block;
margin-top: 3px;
padding: 2px 4px;
border: 1px solid #9ba2a5;
background: #e8e8e5;
color: #596268;
font: 800 7px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.status.serving {
border-color: var(--teal);
background: var(--teal);
color: white;
}
.status.done {
border-color: #8e999e;
}
.wait-number {
display: flex;
align-items: center;
justify-content: space-between;
gap: 5px;
min-width: 62px;
font-weight: 900;
}
.wait-bar {
width: 34px;
height: 6px;
border: 1px solid var(--line);
background: #e4dfd6;
}
.wait-bar i {
display: block;
height: 100%;
background: var(--orange);
}
.policy-note {
margin-top: 10px;
padding: 9px 10px;
border-left: 4px solid var(--orange);
background: #f2eee7;
color: #47545c;
font-size: 11px;
line-height: 1.45;
}
.compare-row {
display: grid;
grid-template-columns: 92px 1fr;
gap: 8px;
align-items: center;
padding: 8px 0;
border-bottom: 1px solid #ddd7ce;
}
.compare-row:last-child {
border-bottom: 0;
}
.compare-person strong {
display: block;
font-size: 11px;
}
.compare-person small {
color: var(--muted);
font-size: 8px;
}
.compare-values {
display: grid;
gap: 4px;
}
.compare-item {
display: grid;
grid-template-columns: 30px 1fr 31px;
align-items: center;
gap: 5px;
font: 800 8px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.compare-item span {
color: var(--muted);
}
.compare-bar {
height: 7px;
border: 1px solid var(--line);
background: #e8e3db;
}
.compare-bar i {
display: block;
height: 100%;
background: var(--blue);
}
.compare-item.stf .compare-bar i {
background: var(--orange);
}
.compare-item b {
text-align: right;
}
.footnote {
margin: 10px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 980px) {
.app {
height: auto;
}
.topbar {
align-items: flex-start;
flex-wrap: wrap;
}
.policy-switch {
width: 100%;
}
.policy-switch button {
flex: 1;
min-width: 0;
}
.workspace {
grid-template-columns: 1fr;
}
.scene-card {
min-height: 66vh;
}
.sidebar {
overflow: visible;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
@media (max-width: 650px) {
.workspace {
padding: 8px;
}
.scene-card {
min-height: 620px;
}
.rule-banner {
top: 112px;
right: auto;
left: 16px;
width: calc(100% - 32px);
}
.control-dock {
grid-template-columns: 1fr 1fr;
}
.timeline-wrap {
grid-column: 1 / -1;
grid-row: 1;
}
.speed-label {
justify-self: end;
}
.sidebar {
grid-template-columns: 1fr;
}
.camera-tip {
display: none;
}
.scene-legend {
bottom: 150px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">Q</div>
<div>
<h1>Queue Priority Lab</h1>
<p>1 desk · 6 arrivals · identical workload</p>
</div>
</div>
<div class="policy-switch" role="group" aria-label="Service policy">
<button type="button" class="active" data-policy="fcfs" aria-pressed="true">
First-arrival
</button>
<button type="button" data-policy="stf" aria-pressed="false">
Short-task-first
</button>
</div>
</header>
<main class="workspace">
<section class="scene-card" aria-label="Animated help desk">
<div id="scene"></div>
<div class="scene-heading">
<p class="eyebrow">Live queue / single server</p>
<h2>Help Desk</h2>
<div class="clock-chip">
<span id="clock">00.0 min</span>
</div>
</div>
<div class="rule-banner">
<strong id="rule-title">First-arrival rule</strong>
<p id="rule-text">
The customer with the earliest arrival time is served next.
Arrival ties use the original arrival order.
</p>
</div>
<div class="scene-legend">
<span class="legend-chip"><i class="legend-dot"></i>Serving</span>
<span class="legend-chip"><i class="legend-dot waiting"></i>Waiting</span>
<span class="legend-chip"><i class="legend-dot done"></i>Completed</span>
</div>
<div class="camera-tip">DRAG TO ORBIT · SCROLL TO ZOOM · DOUBLE-CLICK TO RESET</div>
<div class="control-dock">
<button type="button" id="play" class="control-button primary">Play</button>
<button type="button" id="reset" class="control-button">Reset</button>
<div class="timeline-wrap">
<div class="timeline-label">
<span>Simulation timeline</span>
<span id="timeline-end">20 min</span>
</div>
<input id="timeline" type="range" min="0" max="20" value="0" step="0.1"
aria-label="Simulation timeline">
</div>
<label class="speed-label">
Speed
<select id="speed" aria-label="Simulation speed">
<option value="0.5">0.5×</option>
<option value="1" selected>1×</option>
<option value="2">2×</option>
<option value="4">4×</option>
</select>
</label>
</div>
</section>
<aside class="sidebar">
<section class="panel">
<div class="panel-header">
<div>
<p class="eyebrow">Dispatch order</p>
<h3>Queue Order</h3>
<p class="panel-subtitle" id="queue-subtitle">
Ordered by earliest arrival.
</p>
</div>
<span class="count-badge" id="waiting-count">0 waiting</span>
</div>
<div class="now-serving">
<div class="now-icon" id="now-icon">—</div>
<div class="now-copy">
<strong id="now-name">Waiting for first arrival</strong>
<small id="now-detail">Desk is idle</small>
</div>
<div class="progress-track" aria-hidden="true">
<div class="progress-fill" id="service-progress"></div>
</div>
</div>
<ol class="queue-list" id="queue-list"></ol>
</section>
<section class="panel">
<div class="panel-header">
<div>
<p class="eyebrow">Not just an average</p>
<h3>Individual Waiting Times</h3>
<p class="panel-subtitle">
Every customer's actual wait before service begins.
</p>
</div>
</div>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>Customer</th>
<th>Arrive</th>
<th>Task</th>
<th>Start</th>
<th>Wait</th>
</tr>
</thead>
<tbody id="wait-table"></tbody>
</table>
</div>
<div class="policy-note" id="policy-note"></div>
</section>
<section class="panel">
<div class="panel-header">
<div>
<p class="eyebrow">Same people, same tasks</p>
<h3>Policy A/B Comparison</h3>
<p class="panel-subtitle">
Both columns use the same six arrivals and task durations.
</p>
</div>
</div>
<div id="comparison"></div>
<p class="footnote">
Short-task-first is non-preemptive: once a task starts, it finishes
before the desk selects another customer.
</p>
</section>
</aside>
</main>
</div>
<div id="live-status" class="sr-only" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Workload: exactly the same arrivals and task durations are
// used by both policies. Only the dispatch rule changes.
// ------------------------------------------------------------
const workload = [
{ id: "A1", name: "Ari", arrive: 0, task: 3, color: 0xe76f51 },
{ id: "A2", name: "Bo", arrive: 2, task: 5, color: 0x2a9d8f },
{ id: "A3", name: "Cleo", arrive: 4, task: 2, color: 0xe9c46a },
{ id: "A4", name: "Drew", arrive: 5, task: 6, color: 0x457b9d },
{ id: "A5", name: "Eli", arrive: 7, task: 3, color: 0x8e6cbb },
{ id: "A6", name: "Fay", arrive: 9, task: 4, color: 0xf4a261 }
];
const policyInfo = {
fcfs: {
title: "First-arrival rule",
queueText: "Ordered by earliest arrival.",
rule:
"The customer with the earliest arrival time is served next. " +
"Arrival ties use the original arrival order.",
note:
"Arrival order wins: Drew's 6-minute task starts before Eli's and Fay's " +
"shorter tasks simply because Drew arrived earlier."
},
stf: {
title: "Short-task-first rule",
queueText: "Ordered by shortest waiting task.",
rule:
"Among everyone currently waiting, the shortest task is served next. " +
"Ties use earliest arrival. A running task is never interrupted.",
note:
"Cleo, Eli and Fay move ahead of Drew. Drew's individual wait rises " +
"to 12 minutes while Fay's falls from 7 minutes to 1 minute."
}
};
function pickNext(waiting, policy) {
if (policy === "stf") {
return [...waiting].sort(
(a, b) => a.task - b.task || a.arrive - b.arrive
)[0];
}
return [...waiting].sort(
(a, b) => a.arrive - b.arrive
)[0];
}
function buildSchedule(policy) {
const schedule = new Map();
let time = 0;
let active = null;
while (true) {
if (!active) {
const waiting = workload.filter(
customer => customer.arrive <= time && !schedule.has(customer.id)
);
if (!waiting.length) break;
const next = pickNext(waiting, policy);
const entry = {
start: time,
finish: time + next.task,
wait: time - next.arrive
};
schedule.set(next.id, entry);
active = { customer: next, entry };
continue;
}
const nextArrival = workload
.filter(customer => customer.arrive > time && !schedule.has(customer.id))
.map(customer => customer.arrive)
.sort((a, b) => a - b)[0];
if (nextArrival !== undefined && nextArrival < active.entry.finish) {
time = nextArrival;
} else {
time = active.entry.finish;
active = null;
}
}
return schedule;
}
const schedules = {
fcfs: buildSchedule("fcfs"),
stf: buildSchedule("stf")
};
const scheduleDuration = policy =>
Math.max(...schedules[policy].values().map(entry => entry.finish));
const state = {
policy: "fcfs",
time: 0,
speed: 1,
playing: false,
duration: scheduleDuration("fcfs")
};
function snapshot(time = state.time, policy = state.policy) {
const schedule = schedules[policy];
const waiting = [];
const serving = [];
const done = [];
const future = [];
for (const customer of workload) {
const entry = schedule.get(customer.id);
if (time < customer.arrive) {
future.push(customer);
} else if (time >= entry.finish) {
done.push(customer);
} else if (time >= entry.start) {
serving.push(customer);
} else {
waiting.push(customer);
}
}
waiting.sort((a, b) => {
if (policy === "stf") {
return a.task - b.task || a.arrive - b.arrive;
}
return a.arrive - b.arrive;
});
done.sort(
(a, b) => schedule.get(a.id).finish - schedule.get(b.id).finish
);
return { waiting, serving, done, future };
}
// ------------------------------------------------------------
// DOM references
// ------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const playButton = document.getElementById("play");
const resetButton = document.getElementById("reset");
const speedSelect = document.getElementById("speed");
const timeline = document.getElementById("timeline");
const timelineEnd = document.getElementById("timeline-end");
const clock = document.getElementById("clock");
const queueList = document.getElementById("queue-list");
const waitingCount = document.getElementById("waiting-count");
const queueSubtitle = document.getElementById("queue-subtitle");
const nowIcon = document.getElementById("now-icon");
const nowName = document.getElementById("now-name");
const nowDetail = document.getElementById("now-detail");
const serviceProgress = document.getElementById("service-progress");
const waitTable = document.getElementById("wait-table");
const comparison = document.getElementById("comparison");
const policyNote = document.getElementById("policy-note");
const ruleTitle = document.getElementById("rule-title");
const ruleText = document.getElementById("rule-text");
const liveStatus = document.getElementById("live-status");
function colorHex(value) {
return `#${value.toString(16).padStart(6, "0")}`;
}
function statusFor(customer, time = state.time, policy = state.policy) {
const entry = schedules[policy].get(customer.id);
if (time < customer.arrive) return "future";
if (time >= entry.finish) return "done";
if (time >= entry.start) return "serving";
return "waiting";
}
function updateUI() {
const info = snapshot();
const policy = policyInfo[state.policy];
clock.textContent = `${state.time.toFixed(1).padStart(4, "0")} min`;
timeline.value = state.time;
timeline.style.setProperty(
"--pct",
`${(state.time / state.duration) * 100}%`
);
ruleTitle.textContent = policy.title;
ruleText.textContent = policy.rule;
queueSubtitle.textContent = policy.queueText;
policyNote.textContent = policy.note;
waitingCount.textContent = `${info.waiting.length} waiting`;
if (info.serving.length) {
const customer = info.serving[0];
const entry = schedules[state.policy].get(customer.id);
const progress = THREE.MathUtils.clamp(
(state.time - entry.start) / customer.task,
0,
1
);
nowIcon.textContent = customer.id;
nowIcon.style.background = colorHex(customer.color);
nowName.textContent = `${customer.id} · ${customer.name}`;
nowDetail.textContent = `${customer.task}-minute task in progress`;
serviceProgress.style.width = `${progress * 100}%`;
} else {
nowIcon.textContent = "—";
nowIcon.style.background = "#aab1b3";
nowName.textContent =
state.time >= state.duration ? "All tasks complete" : "Desk is idle";
nowDetail.textContent =
state.time >= state.duration
? "Simulation finished"
: "Waiting for the next arrival";
serviceProgress.style.width = "0%";
}
if (!info.waiting.length) {
queueList.innerHTML = `
<li class="empty-state">
${
state.time >= state.duration
? "The queue is empty."
: "Nobody is waiting right now."
}
</li>
`;
} else {
queueList.innerHTML = info.waiting.map((customer, index) => {
const waited = Math.max(0, state.time - customer.arrive);
const reason = state.policy === "stf" ? "shortest" : "earliest";
return `
<li class="queue-item ${index === 0 ? "next" : ""}">
<span class="rank">${index + 1}</span>
<span class="person-swatch"
style="background:${colorHex(customer.color)}"></span>
<span class="queue-copy">
<strong>${customer.id} · ${customer.name}</strong>
<small>${customer.task}m task · waited ${waited.toFixed(1)}m</small>
</span>
<span class="reason-tag">${reason}</span>
</li>
`;
}).join("");
}
const currentSchedule = schedules[state.policy];
const maxWait = Math.max(
...[...currentSchedule.values()].map(entry => entry.wait)
);
waitTable.innerHTML = workload.map(customer => {
const entry = currentSchedule.get(customer.id);
const status = statusFor(customer);
const arrived = state.time >= customer.arrive;
const width = entry.wait ? (entry.wait / maxWait) * 100 : 0;
const statusText = {
future: "not arrived",
waiting: "waiting",
serving: "serving",
done: "done"
}[status];
return `
<tr class="is-${status}">
<td class="person-cell">
<strong>${customer.id} · ${customer.name}</strong>
<span class="status ${status}">${statusText}</span>
</td>
<td>${customer.arrive}m</td>
<td>${customer.task}m</td>
<td>${arrived ? `${entry.start}m` : "—"}</td>
<td>
${
arrived
? `
<div class="wait-number">
<span>${entry.wait}m</span>
<span class="wait-bar"><i style="width:${width}%"></i></span>
</div>
`
: "—"
}
</td>
</tr>
`;
}).join("");
}
function renderComparison() {
const fcfs = schedules.fcfs;
const stf = schedules.stf;
const maxWait = Math.max(
...[...fcfs.values()].map(entry => entry.wait),
...[...stf.values()].map(entry => entry.wait)
);
comparison.innerHTML = workload.map(customer => {
const first = fcfs.get(customer.id).wait;
const short = stf.get(customer.id).wait;
return `
<div class="compare-row">
<div class="compare-person">
<strong>${customer.id} · ${customer.name}</strong>
<small>Arrive ${customer.arrive}m · task ${customer.task}m</small>
</div>
<div class="compare-values">
<div class="compare-item">
<span>FA</span>
<div class="compare-bar">
<i style="width:${(first / maxWait) * 100}%"></i>
</div>
<b>${first}m</b>
</div>
<div class="compare-item stf">
<span>STF</span>
<div class="compare-bar">
<i style="width:${(short / maxWait) * 100}%"></i>
</div>
<b>${short}m</b>
</div>
</div>
</div>
`;
}).join("");
}
// ------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e1d5);
scene.fog = new THREE.Fog(0xe8e1d5, 30, 55);
const camera = new THREE.OrthographicCamera(-10, 10, 8, -8, 0.1, 100);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.domElement.setAttribute(
"aria-label",
"Voxel help desk with an animated priority queue"
);
sceneHost.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfff7e7, 0x87929a, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d1, 3.2);
sun.position.set(-8, 15, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -16;
sun.shadow.camera.right = 16;
sun.shadow.camera.top = 16;
sun.shadow.camera.bottom = -16;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.0008;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9ec9ff, 0.65);
fill.position.set(10, 8, -8);
scene.add(fill);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
...options
});
}
function addBox(parent, width, height, depth, color, x, y, z, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Ground and checkerboard voxel tiles.
addBox(scene, 25, 0.3, 25, 0xc7c0b4, 0, -0.28, 2.5);
const tileColors = [0xe8e1d5, 0xded6c8];
for (let x = -6; x <= 6; x++) {
for (let z = -6; z <= 12; z++) {
addBox(
scene,
0.96,
0.08,
0.96,
tileColors[(x + z + 20) % 2],
x,
-0.08,
z
);
}
}
// Queue pads.
const queuePads = [];
for (let i = 0; i < 6; i++) {
const pad = addBox(
scene,
1.45,
0.07,
1.35,
0xd5cec1,
0,
0.015,
1.45 + i * 1.65
);
queuePads.push(pad);
}
const servicePad = addBox(
scene,
1.55,
0.08,
1.35,
0x7ec7b9,
0,
0.02,
-0.15
);
// Dashed queue center line.
for (let i = 0; i < 6; i++) {
addBox(
scene,
0.12,
0.035,
0.72,
0x9f9b91,
0,
0.055,
1.45 + i * 1.65
);
}
// Help desk.
addBox(scene, 6.4, 1.45, 1.65, 0x31546a, 0, 0.75, -2.45);
addBox(scene, 6.9, 0.28, 2.35, 0xe9c46a, 0, 1.57, -2.35);
addBox(scene, 6.25, 0.16, 0.18, 0x172a3a, 0, 1.13, -1.57);
// Desk drawers and legs.
for (const x of [-2.2, 2.2]) {
addBox(scene, 0.35, 1.35, 1.35, 0x244052, x, 0.68, -2.45);
}
addBox(scene, 1.4, 0.9, 0.2, 0x172a3a, 0, 2.45, -2.78);
addBox(scene, 0.18, 0.5, 0.18, 0x172a3a, 0, 1.95, -2.78);
// Keyboard and monitor.
addBox(scene, 1.65, 0.12, 0.65, 0x1a2d3b, 0, 1.79, -1.82);
addBox(scene, 2.35, 1.38, 0.22, 0x172a3a, 0, 2.38, -2.65);
// Agent behind desk.
function makeAvatar(color, withBadge = false) {
const root = new THREE.Group();
addBox(root, 0.29, 0.42, 0.32, 0x253b4b, -0.2, 0.21, 0);
addBox(root, 0.29, 0.42, 0.32, 0x253b4b, 0.2, 0.21, 0);
addBox(root, 0.72, 0.76, 0.43, color, 0, 0.76, 0);
addBox(root, 0.22, 0.64, 0.3, color, -0.49, 0.75, 0);
addBox(root, 0.22, 0.64, 0.3, color, 0.49, 0.75, 0);
addBox(root, 0.62, 0.57, 0.56, 0xe8b58f, 0, 1.37, 0);
addBox(root, 0.67, 0.18, 0.59, 0x47382f, 0, 1.67, -0.015);
addBox(root, 0.08, 0.09, 0.035, 0x172a3a, -0.15, 1.42, 0.295);
addBox(root, 0.08, 0.09, 0.035, 0x172a3a, 0.15, 1.42, 0.295);
let badge = null;
if (withBadge) {
badge = makeTextSprite("", 1.72, 0.56, "#fffdf8", "#172a3a");
badge.position.set(0, 2.22, 0);
badge.userData.isBadge = true;
root.add(badge);
}
return { root, badge };
}
const agent = makeAvatar(0x4f7cac, false);
agent.root.position.set(0, 0, -3.75);
agent.root.rotation.y = 0;
scene.add(agent.root);
// Text sprites.
function roundedPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeTextSprite(
text,
worldWidth,
worldHeight,
background = "#172a3a",
foreground = "#fffdf8"
) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 200;
const context = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(worldWidth, worldHeight, 1);
sprite.renderOrder = 10;
sprite.userData.canvas = canvas;
sprite.userData.context = context;
sprite.userData.texture = texture;
sprite.userData.background = background;
sprite.userData.foreground = foreground;
paintSprite(sprite, text);
return sprite;
}
function paintSprite(sprite, text, background, foreground) {
const { canvas, context, texture } = sprite.userData;
const bg = background || sprite.userData.background;
const fg = foreground || sprite.userData.foreground;
context.clearRect(0, 0, canvas.width, canvas.height);
roundedPath(context, 8, 8, canvas.width - 16, canvas.height - 16, 28);
context.fillStyle = bg;
context.fill();
context.strokeStyle = "#172a3a";
context.lineWidth = 12;
context.stroke();
const lines = String(text).split("\n");
const startY = canvas.height / 2 - ((lines.length - 1) * 34);
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = fg;
context.font = `900 ${lines.length > 1 ? 54 : 62}px ui-monospace, SFMono-Regular, Menlo, monospace`;
lines.forEach((line, index) => {
context.fillText(line, canvas.width / 2, startY + index * 68, canvas.width - 60);
});
texture.needsUpdate = true;
}
const deskSign = makeTextSprite("HELP DESK", 3.3, 0.95, "#172a3a", "#fffdf8");
deskSign.position.set(0, 3.55, -2.7);
scene.add(deskSign);
const monitorSprite = makeTextSprite("READY", 2.02, 0.78, "#0d2637", "#8ce0d0");
monitorSprite.position.set(0, 2.42, -2.51);
scene.add(monitorSprite);
const queueSign = makeTextSprite(
"WAITING QUEUE\nORDER: ARRIVAL",
3.25,
1.05,
"#fffdf8",
"#172a3a"
);
queueSign.position.set(-3.15, 0.75, 4.7);
scene.add(queueSign);
const doneSign = makeTextSprite(
"COMPLETED",
2.8,
0.78,
"#fffdf8",
"#172a3a"
);
doneSign.position.set(5.0, 0.62, 4.45);
scene.add(doneSign);
// Customer avatars.
const avatars = new Map();
for (const customer of workload) {
const avatar = makeAvatar(customer.color, true);
avatar.root.position.set(0, 0, 1.45);
avatar.root.rotation.y = Math.PI;
avatar.root.visible = false;
scene.add(avatar.root);
avatars.set(customer.id, {
...avatar,
customer,
lastLabel: "",
target: new THREE.Vector3(0, 0, 1.45),
currentRotation: Math.PI
});
}
// ------------------------------------------------------------
// Custom camera controls: no Three.js addons.
// ------------------------------------------------------------
const orbit = {
theta: 0.67,
phi: 1.01,
zoom: 1,
target: new THREE.Vector3(0.45, 0.25, 1.05)
};
function updateCamera() {
const radius = 29;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + radius * Math.cos(orbit.phi),
orbit.target.z + radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
camera.zoom = orbit.zoom;
camera.updateProjectionMatrix();
}
function resizeRenderer() {
const width = sceneHost.clientWidth;
const height = sceneHost.clientHeight;
const aspect = width / Math.max(height, 1);
const viewHeight = Math.max(14.5, 18.5 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
updateCamera();
}
new ResizeObserver(resizeRenderer).observe(sceneHost);
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.005, 0.55, 1.28);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
orbit.zoom = THREE.MathUtils.clamp(
orbit.zoom * Math.exp(-event.deltaY * 0.001),
0.7,
1.85
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
orbit.theta = 0.67;
orbit.phi = 1.01;
orbit.zoom = 1;
updateCamera();
});
// ------------------------------------------------------------
// Animation and visual state
// ------------------------------------------------------------
const waitingSlots = workload.map((_, index) =>
new THREE.Vector3(0, 0, 1.45 + index * 1.65)
);
const servicePosition = new THREE.Vector3(0, 0, -0.15);
const donePositions = [
new THREE.Vector3(4.15, 0, -0.15),
new THREE.Vector3(5.75, 0, -0.15),
new THREE.Vector3(4.15, 0, 1.45),
new THREE.Vector3(5.75, 0, 1.45),
new THREE.Vector3(4.15, 0, 3.05),
new THREE.Vector3(5.75, 0, 3.05)
];
function lerpAngle(a, b, amount) {
const difference = ((b - a + Math.PI) % (Math.PI * 2)) - Math.PI;
return a + difference * amount;
}
function updateScene(delta) {
const info = snapshot();
const servingCustomer = info.serving[0] || null;
const rankById = new Map(
info.waiting.map((customer, index) => [customer.id, index])
);
const doneIndexById = new Map(
info.done.map((customer, index) => [customer.id, index])
);
for (const customer of workload) {
const avatar = avatars.get(customer.id);
const status = statusFor(customer);
const age = state.time - customer.arrive;
if (status === "future") {
avatar.root.visible = false;
continue;
}
avatar.root.visible = true;
let targetRotation = Math.PI;
let bob = 0;
if (status === "serving") {
avatar.target.copy(servicePosition);
targetRotation = Math.PI;
} else if (status === "waiting") {
const rank = rankById.get(customer.id);
avatar.target.copy(waitingSlots[rank]);
targetRotation = Math.PI;
bob = Math.sin(state.time * 3 + customer.arrive) * 0.025;
} else if (status === "done") {
const doneIndex = doneIndexById.get(customer.id);
avatar.target.copy(donePositions[doneIndex]);
targetRotation = 0;
}
const smoothing = 1 - Math.exp(-delta * 7);
avatar.root.position.x = THREE.MathUtils.lerp(
avatar.root.position.x,
avatar.target.x,
smoothing
);
avatar.root.position.z = THREE.MathUtils.lerp(
avatar.root.position.z,
avatar.target.z,
smoothing
);
avatar.root.position.y = bob;
avatar.currentRotation = lerpAngle(
avatar.currentRotation,
targetRotation,
smoothing
);
avatar.root.rotation.y = avatar.currentRotation;
const grow = THREE.MathUtils.clamp(age / 0.35, 0, 1);
const easedGrow = 1 - Math.pow(1 - grow, 3);
avatar.root.scale.setScalar(Math.max(0.02, easedGrow));
let label = "";
if (status === "serving") {
label = `NOW ${customer.id} · ${customer.task}m`;
} else if (status === "waiting") {
label = `${rankById.get(customer.id) + 1} ${customer.id} · ${customer.task}m`;
} else if (status === "done") {
label = `${customer.id} DONE`;
}
if (label !== avatar.lastLabel) {
paintSprite(
avatar.badge,
label,
status === "serving" ? "#2a9d8f" : "#fffdf8",
status === "serving" ? "#ffffff" : "#172a3a"
);
avatar.lastLabel = label;
}
}
// Highlight the front of the waiting queue.
const priorityColor = state.policy === "stf" ? 0xe76f51 : 0x4f7cac;
queuePads.forEach((pad, index) => {
const isFront = index === 0 && info.waiting.length > 0;
pad.material.color.setHex(
isFront ? priorityColor : index < info.waiting.length ? 0xeee3c8 : 0xd5cec1
);
pad.scale.y = isFront ? 1.8 : 1;
});
servicePad.material.color.setHex(servingCustomer ? 0x2a9d8f : 0x7ec7b9);
paintSprite(
monitorSprite,
servingCustomer
? `NOW ${servingCustomer.id}\n${servingCustomer.task}m TASK`
: "READY",
"#0d2637",
"#8ce0d0"
);
paintSprite(
queueSign,
state.policy === "stf"
? "WAITING QUEUE\nORDER: SHORTEST TASK"
: "WAITING QUEUE\nORDER: ARRIVAL",
"#fffdf8",
"#172a3a"
);
}
// ------------------------------------------------------------
// Controls
// ------------------------------------------------------------
function setPolicy(policy) {
state.policy = policy;
state.time = 0;
state.duration = scheduleDuration(policy);
state.playing = true;
document.querySelectorAll("[data-policy]").forEach(button => {
const active = button.dataset.policy === policy;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
timeline.max = state.duration;
timelineEnd.textContent = `${state.duration} min`;
playButton.textContent = "Pause";
liveStatus.textContent =
`${policyInfo[policy].title} selected. Simulation restarted.`;
updateUI();
}
document.querySelectorAll("[data-policy]").forEach(button => {
button.addEventListener("click", () => setPolicy(button.dataset.policy));
});
playButton.addEventListener("click", () => {
if (state.time >= state.duration) {
state.time = 0;
state.playing = true;
} else {
state.playing = !state.playing;
}
playButton.textContent = state.playing ? "Pause" : "Play";
});
resetButton.addEventListener("click", () => {
state.time = 0;
state.playing = false;
playButton.textContent = "Play";
updateUI();
});
speedSelect.addEventListener("change", () => {
state.speed = Number(speedSelect.value);
});
timeline.addEventListener("input", () => {
state.time = Number(timeline.value);
state.playing = false;
playButton.textContent = "Play";
updateUI();
});
// ------------------------------------------------------------
// Main loop
// ------------------------------------------------------------
let previousTime = performance.now();
let previousUiTick = -1;
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - previousTime) / 1000, 0.1);
previousTime = now;
if (state.playing) {
state.time = Math.min(
state.duration,
state.time + delta * state.speed
);
if (state.time >= state.duration) {
state.playing = false;
playButton.textContent = "Replay";
liveStatus.textContent = "Simulation complete.";
}
}
updateScene(delta);
const uiTick = Math.floor(state.time * 5);
if (uiTick !== previousUiTick) {
updateUI();
previousUiTick = uiTick;
}
renderer.render(scene, camera);
}
renderComparison();
timeline.max = state.duration;
timelineEnd.textContent = `${state.duration} min`;
resizeRenderer();
updateUI();
requestAnimationFrame(animate);
</script>
</body>
</html>
```