Prompt specification
Build or customize the Three.js 3D scene "Equal-quantity price comparison" 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: Equal-quantity price comparison
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/equal-quantity-price-comparison-x0554
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: equal-quantity price comparison. Transfer equal quantities from both packages into identical trays; update a fictional cost counter so users compare the same amount rather than package appearance. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (equal-quantity-price-comparison-x0554):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Equal Amount — Fairer Price</title>
<style>
:root {
--ink: #263633;
--muted: #6c7b76;
--paper: #fffaf1;
--line: rgba(38, 54, 51, 0.13);
--coral: #ed705f;
--teal: #299d8f;
--gold: #efb94f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #f2ede3;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
-webkit-tap-highlight-color: transparent;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
#scene canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.masthead {
position: absolute;
top: 28px;
left: 30px;
width: min(390px, calc(100vw - 60px));
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 12px;
color: #52635e;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 3px;
border-radius: 999px;
background: var(--coral);
content: "";
}
h1 {
margin: 0;
font-size: clamp(38px, 4.4vw, 65px);
font-weight: 850;
letter-spacing: -0.055em;
line-height: 0.94;
}
h1 span {
color: var(--coral);
}
.intro {
max-width: 340px;
margin: 17px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.fictional {
display: inline-flex;
align-items: center;
gap: 6px;
margin-top: 14px;
padding: 6px 10px;
border: 1px solid rgba(38, 54, 51, 0.13);
border-radius: 999px;
background: rgba(255, 250, 241, 0.65);
color: #65736f;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.fictional::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--gold);
content: "";
}
.price-board {
position: absolute;
top: 28px;
right: 30px;
width: 365px;
overflow: hidden;
border: 1px solid rgba(38, 54, 51, 0.12);
border-radius: 20px;
background: rgba(255, 250, 241, 0.9);
box-shadow: 0 18px 50px rgba(57, 64, 56, 0.1);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.price-card {
display: grid;
grid-template-columns: 1fr auto;
gap: 4px 18px;
padding: 16px 18px 14px;
}
.price-card + .price-card {
border-top: 1px solid var(--line);
}
.package-name {
align-self: center;
font-size: 13px;
font-weight: 850;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.package-name::before {
display: inline-block;
width: 8px;
height: 8px;
margin-right: 8px;
border-radius: 2px;
background: var(--accent);
content: "";
}
.package-total {
color: var(--muted);
font-size: 11px;
font-weight: 700;
}
.cost {
grid-column: 1;
margin-top: 5px;
font-size: 30px;
font-weight: 850;
letter-spacing: -0.05em;
line-height: 1;
}
.cost-label {
grid-column: 2;
align-self: end;
padding-bottom: 2px;
color: var(--muted);
font-size: 11px;
font-weight: 700;
text-align: right;
}
.unit-price {
grid-column: 1 / -1;
margin-top: 5px;
color: var(--muted);
font-size: 10px;
font-weight: 650;
}
.insight {
display: flex;
gap: 10px;
padding: 13px 18px 15px;
border-top: 1px solid var(--line);
background: rgba(38, 54, 51, 0.035);
color: #586862;
font-size: 11px;
line-height: 1.45;
}
.insight-icon {
flex: 0 0 auto;
display: grid;
width: 23px;
height: 23px;
place-items: center;
border-radius: 50%;
background: var(--ink);
color: var(--paper);
font-size: 12px;
font-weight: 900;
}
.transfer-panel {
position: absolute;
bottom: 24px;
left: 50%;
display: grid;
grid-template-columns: minmax(310px, 1fr) auto;
align-items: center;
gap: 26px;
width: min(820px, calc(100vw - 40px));
padding: 16px 18px 16px 20px;
transform: translateX(-50%);
border: 1px solid rgba(38, 54, 51, 0.14);
border-radius: 20px;
background: rgba(255, 250, 241, 0.94);
box-shadow: 0 18px 60px rgba(57, 64, 56, 0.14);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.quantity-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-bottom: 10px;
}
.quantity-title {
font-size: 12px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.quantity-value {
color: var(--coral);
font-size: 18px;
font-weight: 850;
letter-spacing: -0.03em;
}
.range-row {
display: grid;
grid-template-columns: 32px 1fr 32px;
align-items: center;
gap: 10px;
}
.step-button {
width: 32px;
height: 32px;
padding: 0;
border: 1px solid var(--line);
border-radius: 10px;
background: #fffdf8;
color: var(--ink);
cursor: pointer;
font-size: 19px;
font-weight: 700;
transition: transform 150ms ease, background 150ms ease;
}
.step-button:hover {
transform: translateY(-1px);
background: #f1eadf;
}
.step-button:active {
transform: translateY(1px);
}
input[type="range"] {
width: 100%;
height: 7px;
margin: 0;
appearance: none;
border-radius: 999px;
outline: none;
background:
linear-gradient(
to right,
var(--coral) 0%,
var(--coral) var(--pct),
#ded8ce var(--pct),
#ded8ce 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 21px;
height: 21px;
appearance: none;
border: 4px solid var(--paper);
border-radius: 50%;
background: var(--coral);
box-shadow: 0 2px 9px rgba(38, 54, 51, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 4px solid var(--paper);
border-radius: 50%;
background: var(--coral);
box-shadow: 0 2px 9px rgba(38, 54, 51, 0.25);
}
.portion-note {
margin-top: 9px;
color: var(--muted);
font-size: 10px;
font-weight: 600;
}
.transfer-button {
min-width: 215px;
min-height: 58px;
padding: 0 22px;
border: 0;
border-radius: 14px;
background: var(--ink);
color: #fffaf1;
cursor: pointer;
font-size: 13px;
font-weight: 800;
box-shadow: 0 9px 20px rgba(38, 54, 51, 0.18);
transition: transform 150ms ease, background 150ms ease;
}
.transfer-button:hover {
transform: translateY(-2px);
background: #172421;
}
.transfer-button:active {
transform: translateY(1px);
}
.transfer-button span {
display: block;
margin-top: 3px;
color: rgba(255, 250, 241, 0.58);
font-size: 9px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.scene-help {
position: absolute;
bottom: 29px;
left: 30px;
display: flex;
align-items: center;
gap: 8px;
color: rgba(38, 54, 51, 0.55);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.scene-help::before {
width: 17px;
height: 17px;
border: 1px solid rgba(38, 54, 51, 0.28);
border-radius: 50%;
content: "↔";
display: grid;
place-items: center;
font-size: 11px;
}
.world-tag {
position: absolute;
z-index: 2;
padding: 6px 9px;
transform: translate(-50%, -50%);
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 999px;
background: rgba(38, 54, 51, 0.82);
box-shadow: 0 4px 15px rgba(38, 54, 51, 0.12);
color: #fffaf1;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.09em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
}
.world-tag.tag-a {
background: rgba(237, 112, 95, 0.9);
}
.world-tag.tag-b {
background: rgba(41, 157, 143, 0.92);
}
.loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: #f2ede3;
color: var(--ink);
font-size: 12px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 350ms ease, visibility 350ms ease;
}
.loading.hidden {
visibility: hidden;
opacity: 0;
}
@media (max-width: 900px) {
.masthead {
width: 320px;
}
h1 {
font-size: 44px;
}
.intro {
max-width: 285px;
font-size: 12px;
}
.price-board {
width: 320px;
}
.transfer-panel {
gap: 15px;
}
.transfer-button {
min-width: 185px;
}
.scene-help {
display: none;
}
}
@media (max-width: 680px) {
.masthead {
top: 16px;
left: 16px;
width: calc(100vw - 32px);
}
.eyebrow {
margin-bottom: 8px;
font-size: 9px;
}
h1 {
font-size: 34px;
}
.intro,
.fictional {
display: none;
}
.price-board {
top: 91px;
right: 16px;
left: 16px;
display: grid;
grid-template-columns: 1fr 1fr;
width: auto;
border-radius: 15px;
}
.price-card {
display: block;
padding: 11px 12px;
}
.price-card + .price-card {
border-top: 0;
border-left: 1px solid var(--line);
}
.package-name {
font-size: 10px;
}
.package-total {
display: block;
margin-top: 2px;
font-size: 9px;
}
.cost {
margin-top: 6px;
font-size: 24px;
}
.cost-label {
display: block;
font-size: 9px;
text-align: left;
}
.unit-price {
font-size: 8px;
}
.insight {
grid-column: 1 / -1;
padding: 9px 12px;
font-size: 9px;
}
.insight-icon {
width: 19px;
height: 19px;
font-size: 10px;
}
.transfer-panel {
bottom: 10px;
grid-template-columns: 1fr;
gap: 12px;
width: calc(100vw - 20px);
padding: 13px 14px;
border-radius: 16px;
}
.quantity-row {
margin-bottom: 7px;
}
.quantity-title {
font-size: 10px;
}
.quantity-value {
font-size: 15px;
}
.portion-note {
margin-top: 6px;
font-size: 8px;
}
.transfer-button {
min-height: 46px;
}
.transfer-button span {
display: inline;
margin-left: 6px;
}
.world-tag {
font-size: 7px;
padding: 4px 6px;
}
}
@media (max-height: 650px) and (min-width: 681px) {
.intro,
.fictional {
display: none;
}
.masthead,
.price-board {
top: 16px;
}
.transfer-panel {
bottom: 12px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="hud">
<header class="masthead">
<div class="eyebrow">The equal-quantity lab</div>
<h1>Equal amount.<br /><span>Fairer price.</span></h1>
<p class="intro">
Transfer the same imaginary weight from both packages into matching trays.
Package size and shelf appeal stop mattering.
</p>
<div class="fictional">Fictional products and prices</div>
</header>
<aside class="price-board" aria-label="Price comparison">
<div class="price-card" style="--accent: var(--coral)">
<div class="package-name">Sunny Oat Co.</div>
<div class="package-total">$7.20 · 480 g pack</div>
<div class="cost" id="costA">$0.90</div>
<div class="cost-label" id="costLabelA">cost of 60 g</div>
<div class="unit-price">Equivalent rate: $0.45 per 30 g</div>
</div>
<div class="price-card" style="--accent: var(--teal)">
<div class="package-name">Nova Puffs</div>
<div class="package-total">$5.80 · 300 g pack</div>
<div class="cost" id="costB">$1.16</div>
<div class="cost-label" id="costLabelB">cost of 60 g</div>
<div class="unit-price">Equivalent rate: $0.58 per 30 g</div>
</div>
<div class="insight" id="insight" aria-live="polite">
<div class="insight-icon">✓</div>
<div id="insightText">
Sunny Oat is cheaper for 60 g. Both trays contain six identical 10 g portions.
</div>
</div>
</aside>
<section class="transfer-panel" aria-label="Quantity controls">
<div>
<div class="quantity-row">
<div class="quantity-title">Transfer to each tray</div>
<div class="quantity-value" id="quantityValue">60 g</div>
</div>
<div class="range-row">
<button class="step-button" id="minus" aria-label="Decrease amount">−</button>
<input
id="quantityRange"
type="range"
min="30"
max="120"
step="10"
value="60"
aria-label="Quantity transferred to each tray"
/>
<button class="step-button" id="plus" aria-label="Increase amount">+</button>
</div>
<div class="portion-note">
1 food cube = 10 g · identical cube size and count in both trays
</div>
</div>
<button class="transfer-button" id="transferButton">
Replay transfer
<span>Equal quantity, both sides</span>
</button>
</section>
<div class="scene-help">Drag to orbit · Scroll to zoom</div>
<div class="world-tag tag-a" id="tagA">Package A</div>
<div class="world-tag tag-b" id="tagB">Package B</div>
<div class="world-tag" id="tagEqual">Same weight · same count</div>
</div>
<div class="loading" id="loading">Building the comparison lab…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const loading = document.getElementById("loading");
const COLORS = {
background: 0xf2ede3,
table: 0xded2bd,
tableEdge: 0xcabca5,
tray: 0xeee7da,
trayInner: 0xf8f2e8,
ink: 0x263633,
coral: 0xed705f,
coralDark: 0xc85548,
teal: 0x299d8f,
tealDark: 0x197a70,
oat: 0xefb94f,
puff: 0x76b99d
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.background);
scene.fog = new THREE.Fog(COLORS.background, 19, 38);
const camera = new THREE.PerspectiveCamera(
36,
window.innerWidth / window.innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly scene comparing equal quantities from two packages"
);
renderer.domElement.tabIndex = 0;
sceneHost.appendChild(renderer.domElement);
// Lighting
scene.add(new THREE.HemisphereLight(0xfff8e8, 0x9d927f, 2.2));
const keyLight = new THREE.DirectionalLight(0xfff0cf, 3.25);
keyLight.position.set(6, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -11;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xbfe8e0, 1.25);
fillLight.position.set(-8, 5, 2);
scene.add(fillLight);
// Materials
const standard = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.02,
flatShading: true,
...options
});
const matTable = standard(COLORS.table);
const matTableEdge = standard(COLORS.tableEdge);
const matTray = standard(COLORS.tray, { roughness: 0.68 });
const matTrayInner = standard(COLORS.trayInner, { roughness: 0.82 });
const matInk = standard(COLORS.ink);
const matCoral = standard(COLORS.coral);
const matCoralDark = standard(COLORS.coralDark);
const matTeal = standard(COLORS.teal);
const matTealDark = standard(COLORS.tealDark);
function addBox(parent, size, position, material, castShadow = true) {
const geometry = new THREE.BoxGeometry(...size);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Ground and tabletop
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
standard(0xeee7db)
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.4;
ground.receiveShadow = true;
scene.add(ground);
const table = addBox(
scene,
[12.6, 0.36, 8.2],
[0, -0.18, 0],
matTable
);
table.receiveShadow = true;
addBox(
scene,
[12.68, 0.12, 8.28],
[0, -0.38, 0],
matTableEdge
);
const grid = new THREE.GridHelper(12, 24, 0xb7aa95, 0xc9bda8);
grid.position.y = 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
function createPackageTexture({
accent,
dark,
firstLine,
secondLine,
price,
weight,
servings
}) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 640;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#fff9ed";
ctx.fillRect(0, 0, 512, 640);
ctx.fillStyle = accent;
ctx.fillRect(0, 0, 512, 116);
ctx.fillStyle = "#fffaf0";
ctx.font = "800 20px system-ui";
ctx.textAlign = "center";
ctx.fillText("FICTIONAL PACKAGE", 256, 47);
ctx.fillStyle = dark;
ctx.font = "900 65px system-ui";
ctx.fillText(firstLine, 256, 94);
ctx.fillStyle = "#263633";
ctx.font = "850 61px system-ui";
ctx.fillText(secondLine, 256, 220);
// Geometric package illustration
ctx.fillStyle = accent;
ctx.beginPath();
ctx.moveTo(92, 347);
ctx.lineTo(256, 258);
ctx.lineTo(420, 347);
ctx.lineTo(256, 440);
ctx.closePath();
ctx.fill();
ctx.fillStyle = dark;
ctx.beginPath();
ctx.moveTo(132, 352);
ctx.lineTo(256, 285);
ctx.lineTo(380, 352);
ctx.lineTo(256, 421);
ctx.closePath();
ctx.fill();
ctx.fillStyle = "#fff9ed";
ctx.beginPath();
ctx.arc(256, 353, 31, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = dark;
ctx.font = "900 58px system-ui";
ctx.fillText(price, 256, 505);
ctx.fillStyle = "#53635e";
ctx.font = "750 24px system-ui";
ctx.fillText(`NET WT ${weight}`, 256, 553);
ctx.fillText(servings, 256, 590);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
return texture;
}
function createPackage({
x,
accent,
dark,
bodyMaterial,
firstLine,
secondLine,
price,
weight,
servings
}) {
const group = new THREE.Group();
group.position.set(x, 0, -1.75);
scene.add(group);
addBox(group, [2.15, 2.75, 1.42], [0, 1.5, 0], bodyMaterial);
// Side folds make the cartons feel faceted and low-poly.
addBox(group, [0.055, 2.48, 1.45], [-1.06, 1.5, 0], dark);
addBox(group, [0.055, 2.48, 1.45], [1.06, 1.5, 0], dark);
const texture = createPackageTexture({
accent,
dark,
firstLine,
secondLine,
price,
weight,
servings
});
const label = new THREE.Mesh(
new THREE.PlaneGeometry(1.72, 2.13),
new THREE.MeshBasicMaterial({ map: texture })
);
label.position.set(0, 1.52, 0.716);
group.add(label);
// Open carton top and folded flaps.
addBox(
group,
[1.52, 0.075, 0.88],
[0, 2.91, 0],
standard(0x273633)
);
const leftFlap = addBox(
group,
[0.82, 0.08, 1.25],
[-0.79, 3.02, -0.05],
bodyMaterial
);
leftFlap.rotation.z = -0.33;
const rightFlap = addBox(
group,
[0.82, 0.08, 1.25],
[0.79, 3.02, -0.05],
bodyMaterial
);
rightFlap.rotation.z = 0.33;
const rearFlap = addBox(
group,
[1.55, 0.08, 0.67],
[0, 3.02, -0.67],
bodyMaterial
);
rearFlap.rotation.x = -0.55;
// Small feet
addBox(group, [0.24, 0.13, 1.1], [-0.78, 0.08, 0], dark);
addBox(group, [0.24, 0.13, 1.1], [0.78, 0.08, 0], dark);
return {
group,
top: new THREE.Vector3(x, 3.22, -1.75)
};
}
const packageA = createPackage({
x: -3.62,
accent: "#ed705f",
dark: "#bd4f43",
bodyMaterial: matCoral,
firstLine: "SUNNY",
secondLine: "OAT CO.",
price: "$7.20",
weight: "480 g",
servings: "16 × 30 g servings"
});
const packageB = createPackage({
x: 3.62,
accent: "#299d8f",
dark: "#176e65",
bodyMaterial: matTeal,
firstLine: "NOVA",
secondLine: "PUFFS",
price: "$5.80",
weight: "300 g",
servings: "10 × 30 g servings"
});
function createTrayTexture() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return { canvas, texture };
}
function createTray({ x, z }) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
addBox(group, [3.46, 0.17, 2.28], [0, 0.13, 0], matTray);
addBox(group, [3.12, 0.055, 1.94], [0, 0.235, 0], matTrayInner);
// Four matching raised rails
addBox(group, [3.46, 0.23, 0.15], [0, 0.32, 1.075], matTray);
addBox(group, [3.46, 0.23, 0.15], [0, 0.32, -1.075], matTray);
addBox(group, [0.15, 0.23, 2.0], [-1.655, 0.32, 0], matTray);
addBox(group, [0.15, 0.23, 2.0], [1.655, 0.32, 0], matTray);
// Small feet
addBox(group, [0.28, 0.13, 0.24], [-1.25, 0.02, 0.72], matTableEdge);
addBox(group, [0.28, 0.13, 0.24], [1.25, 0.02, 0.72], matTableEdge);
addBox(group, [0.28, 0.13, 0.24], [-1.25, 0.02, -0.72], matTableEdge);
addBox(group, [0.28, 0.13, 0.24], [1.25, 0.02, -0.72], matTableEdge);
const labelData = createTrayTexture();
const label = new THREE.Mesh(
new THREE.PlaneGeometry(1.72, 0.38),
new THREE.MeshBasicMaterial({ map: labelData.texture })
);
label.position.set(0, 0.34, 1.158);
group.add(label);
return {
group,
x,
z,
labelCanvas: labelData.canvas,
labelTexture: labelData.texture
};
}
const trayA = createTray({ x: -2.16, z: 1.48 });
const trayB = createTray({ x: 2.16, z: 1.48 });
function updateTrayLabel(tray, grams) {
const ctx = tray.labelCanvas.getContext("2d");
const count = grams / 10;
ctx.clearRect(0, 0, 512, 128);
ctx.fillStyle = "#2b3b37";
ctx.fillRect(0, 0, 512, 128);
ctx.fillStyle = "#fff9ec";
ctx.textAlign = "center";
ctx.font = "850 39px system-ui";
ctx.fillText(`${count} × 10 g = ${grams} g`, 256, 55);
ctx.fillStyle = "rgba(255,249,236,0.65)";
ctx.font = "700 19px system-ui";
ctx.fillText("IDENTICAL TRAY · IDENTICAL WEIGHT", 256, 92);
tray.labelTexture.needsUpdate = true;
}
// Subtle transfer paths.
function addTransferPath(from, to, color) {
const curve = new THREE.QuadraticBezierCurve3(
from.clone(),
new THREE.Vector3(from.x, 4.35, (from.z + to.z) * 0.5),
to
);
const geometry = new THREE.BufferGeometry().setFromPoints(
curve.getPoints(42)
);
const material = new THREE.LineDashedMaterial({
color,
dashSize: 0.11,
gapSize: 0.09,
transparent: true,
opacity: 0.34
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
scene.add(line);
}
addTransferPath(
packageA.top,
new THREE.Vector3(trayA.x, 0.55, trayA.z),
COLORS.coral
);
addTransferPath(
packageB.top,
new THREE.Vector3(trayB.x, 0.55, trayB.z),
COLORS.teal
);
// Shared low-poly food geometry.
const chunkGeometry = new THREE.DodecahedronGeometry(0.21, 0);
const chunkMaterialA = standard(COLORS.oat, { roughness: 0.88 });
const chunkMaterialB = standard(COLORS.puff, { roughness: 0.88 });
const state = {
grams: 60,
chunksA: [],
chunksB: [],
animations: [],
transferTimer: null,
displayedA: 0.9,
displayedB: 1.16,
counterAnimation: null
};
function slotPosition(tray, slot) {
const col = slot % 4;
const row = Math.floor(slot / 4);
return new THREE.Vector3(
tray.x + (col - 1.5) * 0.66,
0.55,
tray.z + (row - 1) * 0.58
);
}
function cubicBezier(p0, p1, p2, p3, t) {
const u = 1 - t;
return new THREE.Vector3()
.addScaledVector(p0, u * u * u)
.addScaledVector(p1, 3 * u * u * t)
.addScaledVector(p2, 3 * u * t * t)
.addScaledVector(p3, t * t * t);
}
function cancelSideAnimations(side) {
const remaining = [];
for (const animation of state.animations) {
if (animation.side !== side) {
remaining.push(animation);
continue;
}
if (animation.kind === "in") {
animation.chunk.mesh.position.copy(animation.end);
animation.chunk.mesh.scale.setScalar(1);
} else {
animation.chunk.mesh.position.copy(animation.start);
animation.chunk.mesh.scale.setScalar(1);
animation.chunk.removing = false;
}
}
state.animations = remaining;
}
function animateChunkIn(side, chunk, slot, delay) {
const start = side.package.top.clone();
const end = slotPosition(side.tray, slot);
const control1 = new THREE.Vector3(
start.x + (end.x - start.x) * 0.18,
4.15,
start.z + 0.8
);
const control2 = new THREE.Vector3(
end.x,
2.55,
end.z - 0.8
);
chunk.mesh.position.copy(start);
chunk.mesh.scale.setScalar(0.18);
state.animations.push({
side,
chunk,
kind: "in",
start: performance.now() + delay,
duration: 850,
p0: start,
p1: control1,
p2: control2,
p3: end,
end
});
}
function animateChunkOut(side, chunk) {
const start = chunk.mesh.position.clone();
const end = side.package.top.clone();
const control = new THREE.Vector3(
(start.x + end.x) * 0.5,
3.9,
(start.z + end.z) * 0.5
);
state.animations.push({
side,
chunk,
kind: "out",
start: performance.now(),
duration: 700,
p0: start,
p1: control,
p2: control,
p3: end
});
}
function reconcileSide(side, targetCount) {
cancelSideAnimations(side);
const activeChunks = side.chunks.filter((chunk) => !chunk.removing);
const activeSlots = new Set(activeChunks.map((chunk) => chunk.slot));
// Return portions above the new target.
for (const chunk of activeChunks) {
if (chunk.slot >= targetCount) {
chunk.removing = true;
animateChunkOut(side, chunk);
}
}
// Add missing portions in matching tray slots.
for (let slot = 0; slot < targetCount; slot++) {
if (activeSlots.has(slot)) continue;
const mesh = new THREE.Mesh(
chunkGeometry,
side.chunkMaterial
);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
scene.add(mesh);
const chunk = { mesh, slot, removing: false };
side.chunks.push(chunk);
animateChunkIn(side, chunk, slot, slot * 55);
}
}
const sideA = {
package: packageA,
tray: trayA,
chunkMaterial: chunkMaterialA,
chunks: state.chunksA
};
const sideB = {
package: packageB,
tray: trayB,
chunkMaterial: chunkMaterialB,
chunks: state.chunksB
};
function transferCurrentAmount() {
const count = Math.round(state.grams / 10);
reconcileSide(sideA, count);
reconcileSide(sideB, count);
}
function scheduleTransfer() {
clearTimeout(state.transferTimer);
state.transferTimer = setTimeout(transferCurrentAmount, 130);
}
function updateAnimations(now) {
const finished = [];
for (const animation of state.animations) {
if (now < animation.start) continue;
const raw = Math.min(
1,
(now - animation.start) / animation.duration
);
const eased =
animation.kind === "in"
? 1 - Math.pow(1 - raw, 3)
: raw * raw * (3 - 2 * raw);
const position = cubicBezier(
animation.p0,
animation.p1,
animation.p2,
animation.p3,
eased
);
animation.chunk.mesh.position.copy(position);
animation.chunk.mesh.rotation.x += 0.035;
animation.chunk.mesh.rotation.y += 0.045;
if (animation.kind === "in") {
const pop = raw < 0.72
? 0.18 + 0.82 * eased
: 1 + Math.sin((raw - 0.72) / 0.28 * Math.PI) * 0.09;
animation.chunk.mesh.scale.setScalar(pop);
} else {
animation.chunk.mesh.scale.setScalar(1 - eased * 0.65);
}
if (raw >= 1) {
if (animation.kind === "in") {
animation.chunk.mesh.position.copy(animation.end);
animation.chunk.mesh.scale.setScalar(1);
} else {
scene.remove(animation.chunk.mesh);
animation.side.chunks =
animation.side.chunks.filter(
(chunk) => chunk !== animation.chunk
);
}
finished.push(animation);
}
}
if (finished.length) {
const finishedSet = new Set(finished);
state.animations = state.animations.filter(
(animation) => !finishedSet.has(animation)
);
}
}
const costAElement = document.getElementById("costA");
const costBElement = document.getElementById("costB");
const costLabelA = document.getElementById("costLabelA");
const costLabelB = document.getElementById("costLabelB");
const insightText = document.getElementById("insightText");
const quantityValue = document.getElementById("quantityValue");
const range = document.getElementById("quantityRange");
const transferButton = document.getElementById("transferButton");
const currency = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
function calculateCosts(grams) {
return {
a: (7.2 * grams) / 480,
b: (5.8 * grams) / 300
};
}
function updateInsight(grams, costs) {
const count = grams / 10;
const difference = Math.abs(costs.a - costs.b);
const winner =
costs.a < costs.b ? "Sunny Oat" : "Nova Puffs";
insightText.textContent =
`${winner} is ${currency.format(difference)} cheaper for ${grams} g. ` +
`Both trays contain ${count} identical 10 g portions.`;
}
function animateCounter(targetA, targetB) {
state.counterAnimation = {
fromA: state.displayedA,
fromB: state.displayedB,
toA: targetA,
toB: targetB,
start: performance.now(),
duration: 450
};
}
function updateCounter(now) {
const animation = state.counterAnimation;
if (!animation) return;
const t = Math.min(
1,
(now - animation.start) / animation.duration
);
const eased = 1 - Math.pow(1 - t, 3);
state.displayedA =
animation.fromA + (animation.toA - animation.fromA) * eased;
state.displayedB =
animation.fromB + (animation.toB - animation.fromB) * eased;
costAElement.textContent = currency.format(state.displayedA);
costBElement.textContent = currency.format(state.displayedB);
if (t >= 1) state.counterAnimation = null;
}
function setAmount(grams, shouldTransfer = true) {
state.grams = Math.max(30, Math.min(120, Math.round(grams / 10) * 10));
const costs = calculateCosts(state.grams);
quantityValue.textContent = `${state.grams} g`;
costLabelA.textContent = `cost of ${state.grams} g`;
costLabelB.textContent = `cost of ${state.grams} g`;
const percentage =
((state.grams - Number(range.min)) /
(Number(range.max) - Number(range.min))) *
100;
range.style.setProperty("--pct", `${percentage}%`);
updateTrayLabel(trayA, state.grams);
updateTrayLabel(trayB, state.grams);
updateInsight(state.grams, costs);
animateCounter(costs.a, costs.b);
if (shouldTransfer) scheduleTransfer();
}
range.addEventListener("input", () => {
setAmount(Number(range.value));
});
document.getElementById("minus").addEventListener("click", () => {
setAmount(state.grams - 10);
});
document.getElementById("plus").addEventListener("click", () => {
setAmount(state.grams + 10);
});
function replayTransfer() {
clearTimeout(state.transferTimer);
for (const side of [sideA, sideB]) {
cancelSideAnimations(side);
for (const chunk of side.chunks) {
scene.remove(chunk.mesh);
}
side.chunks.length = 0;
}
transferButton.textContent = "Transferring…";
setTimeout(() => {
transferCurrentAmount();
transferButton.innerHTML =
'Replay transfer <span>Equal quantity, both sides</span>';
}, 120);
}
transferButton.addEventListener("click", replayTransfer);
// Camera controls implemented directly, without addons.
const cameraTarget = new THREE.Vector3(0, 1.05, 0);
let cameraYaw = 0.31;
let cameraPitch = 0.48;
let cameraRadius = 16;
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
function updateCamera() {
const horizontalRadius = Math.cos(cameraPitch) * cameraRadius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontalRadius,
cameraTarget.y + Math.sin(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontalRadius
);
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraYaw -= deltaX * 0.005;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + deltaY * 0.004,
0.25,
0.98
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.01,
11,
21
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
cameraYaw -= 0.08;
break;
case "ArrowRight":
cameraYaw += 0.08;
break;
case "ArrowUp":
cameraPitch = Math.min(0.98, cameraPitch + 0.06);
break;
case "ArrowDown":
cameraPitch = Math.max(0.25, cameraPitch - 0.06);
break;
case "+":
case "=":
cameraRadius = Math.max(11, cameraRadius - 1);
break;
case "-":
cameraRadius = Math.min(21, cameraRadius + 1);
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Projected HTML labels.
const tags = [
{
element: document.getElementById("tagA"),
position: new THREE.Vector3(-3.62, 3.65, -1.75)
},
{
element: document.getElementById("tagB"),
position: new THREE.Vector3(3.62, 3.65, -1.75)
},
{
element: document.getElementById("tagEqual"),
position: new THREE.Vector3(0, 0.8, 2.72)
}
];
function updateTags() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const tag of tags) {
const projected = tag.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
tag.element.style.display = visible ? "block" : "none";
tag.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
tag.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
updateCamera();
setAmount(60, false);
setTimeout(() => {
transferCurrentAmount();
loading.classList.add("hidden");
}, 650);
function frame(now) {
requestAnimationFrame(frame);
updateAnimations(now);
updateCounter(now);
updateTags();
renderer.render(scene, camera);
}
requestAnimationFrame(frame);
</script>
</body>
</html>
```