Prompt specification
Build or customize the Three.js 3D scene "Three-Step Paper Badge Kit" 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: Three-Step Paper Badge Kit
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/three-step-paper-badge-kit-x3145
Scene Description Prompt:
A low-poly badge-making station: blank cards, hole punch, and ribbon arranged across three sequential trays. Three.js from cdnjs, one HTML file.
Working Three.js Starter (three-step-paper-badge-kit-x3145):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Little Badge Workshop</title>
<style>
:root {
--ink: #273442;
--cream: #fffaf0;
--paper: rgba(255, 250, 240, 0.88);
--coral: #db716d;
--mustard: #e7b95d;
--mint: #7fb8a5;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9dfcf;
color: var(--ink);
font-family: ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
}
#app,
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
canvas {
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.header {
position: absolute;
top: 26px;
left: 30px;
max-width: 430px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 7px;
color: #756b61;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 22px;
height: 3px;
border-radius: 3px;
background: var(--coral);
}
h1 {
margin: 0;
font-size: clamp(27px, 4vw, 48px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
margin: 12px 0 0;
color: #6e685f;
font-size: 13px;
font-weight: 600;
}
.panel {
position: absolute;
top: 26px;
right: 28px;
width: 205px;
padding: 13px;
border: 1px solid rgba(80, 66, 55, 0.12);
border-radius: 20px;
background: var(--paper);
box-shadow: 0 14px 40px rgba(80, 59, 42, 0.12);
backdrop-filter: blur(12px);
pointer-events: auto;
}
.counter {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
padding-bottom: 11px;
border-bottom: 1px solid rgba(80, 66, 55, 0.12);
}
.counter-label {
color: #736b63;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.counter-value {
min-width: 44px;
text-align: right;
font-size: 27px;
font-weight: 900;
line-height: 1;
letter-spacing: -0.05em;
}
button {
width: 100%;
border: 0;
border-radius: 13px;
padding: 12px 14px;
background: var(--ink);
color: #fffdf7;
font: inherit;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.025em;
cursor: pointer;
transition:
transform 160ms ease,
background 160ms ease,
opacity 160ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-2px);
background: #3c4c5b;
}
button:active:not(:disabled) {
transform: translateY(0);
}
button:disabled {
opacity: 0.55;
cursor: default;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 7px;
}
.button-row button {
padding: 9px 7px;
background: #e7ded0;
color: var(--ink);
font-size: 10px;
}
.button-row button:hover {
background: #ddd1bf;
}
.views {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
margin-top: 8px;
}
.views button {
padding: 8px 2px;
font-size: 9px;
}
.status {
position: absolute;
left: 30px;
bottom: 28px;
display: flex;
align-items: center;
gap: 9px;
padding: 10px 14px 10px 11px;
border: 1px solid rgba(80, 66, 55, 0.12);
border-radius: 999px;
background: var(--paper);
box-shadow: 0 8px 26px rgba(80, 59, 42, 0.09);
color: #5f5a54;
font-size: 11px;
font-weight: 750;
backdrop-filter: blur(10px);
}
.status-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 0 4px rgba(127, 184, 165, 0.17);
}
.status.busy .status-dot {
background: var(--mustard);
box-shadow: 0 0 0 4px rgba(231, 185, 93, 0.18);
animation: pulse 850ms ease-in-out infinite alternate;
}
@keyframes pulse {
to {
transform: scale(1.35);
}
}
.hint {
position: absolute;
right: 30px;
bottom: 31px;
color: rgba(52, 48, 44, 0.64);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.04em;
}
.loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: #e9dfcf;
color: var(--ink);
font-size: 12px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
.header {
top: 18px;
left: 18px;
}
.subtitle {
display: none;
}
.panel {
top: 18px;
right: 16px;
width: 166px;
padding: 10px;
border-radius: 17px;
}
.counter {
margin-bottom: 8px;
padding-bottom: 8px;
}
.counter-value {
font-size: 22px;
}
.status {
left: 16px;
bottom: 16px;
}
.hint {
display: none;
}
}
@media (max-width: 460px) {
h1 {
max-width: 175px;
font-size: 27px;
}
.eyebrow {
font-size: 9px;
}
.panel {
width: 150px;
}
.views {
grid-template-columns: 1fr 1fr;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div class="hud">
<header class="header">
<div class="eyebrow">Little Badge Workshop</div>
<h1>Make something<br />worth wearing.</h1>
<p class="subtitle">Cards, punches, and ribbon — three tidy little steps.</p>
</header>
<aside class="panel">
<div class="counter">
<span class="counter-label">Badges</span>
<span class="counter-value" id="badgeCount">00</span>
</div>
<button id="makeButton">Make a badge</button>
<div class="button-row">
<button id="resetButton">Reset</button>
<button id="surpriseButton">Surprise me</button>
</div>
<div class="views">
<button data-view="angle">Angle</button>
<button data-view="front">Front</button>
<button data-view="top">Top</button>
</div>
</aside>
<div class="status" id="status">
<span class="status-dot"></span>
<span id="statusText">Ready for a fresh badge</span>
</div>
<div class="hint">Drag to orbit · Scroll to zoom</div>
</div>
<div class="loading" id="loading">Setting up the workshop</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.querySelector("#app");
const loading = document.querySelector("#loading");
const makeButton = document.querySelector("#makeButton");
const resetButton = document.querySelector("#resetButton");
const surpriseButton = document.querySelector("#surpriseButton");
const badgeCount = document.querySelector("#badgeCount");
const status = document.querySelector("#status");
const statusText = document.querySelector("#statusText");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9dfcf);
scene.fog = new THREE.Fog(0xe9dfcf, 18, 34);
const camera = new THREE.PerspectiveCamera(
36,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
app.appendChild(renderer.domElement);
const COLORS = {
ink: 0x273442,
navy: 0x40566b,
blue: 0x66899a,
coral: 0xdb716d,
coralDark: 0xb95055,
mustard: 0xe7b95d,
mint: 0x7fb8a5,
mintDark: 0x4f8b79,
cream: 0xfff9ec,
paper: 0xf7efdf,
tan: 0xc99a72,
tanDark: 0x9e7656,
wood: 0xba835f,
woodLight: 0xd4aa7e,
floor: 0xe9dfcf,
lilac: 0xc6b9db,
blush: 0xf0bbb4
};
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
flatShading: true,
...options
});
}
function addMesh(parent, geometry, mat, position = [0, 0, 0]) {
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addBox(parent, size, position, mat) {
return addMesh(
parent,
new THREE.BoxGeometry(...size),
mat,
position
);
}
function roundedShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const r = Math.min(radius, width / 2, height / 2);
const shape = new THREE.Shape();
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + height - r);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
shape.lineTo(x + r, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
return shape;
}
function roundedMesh(width, height, depth, radius, mat) {
const geometry = new THREE.ExtrudeGeometry(
roundedShape(width, height, radius),
{
depth,
steps: 1,
curveSegments: 3,
bevelEnabled: true,
bevelSegments: 1,
bevelSize: Math.min(0.035, depth * 0.18),
bevelThickness: Math.min(0.025, depth * 0.18)
}
);
geometry.translate(0, 0, -depth / 2);
return new THREE.Mesh(geometry, mat);
}
function horizontalRounded(width, height, depth, radius, mat) {
const mesh = roundedMesh(width, height, depth, radius, mat);
mesh.rotation.x = -Math.PI / 2;
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function cylinder(parent, radiusTop, radiusBottom, height, segments, mat, position) {
return addMesh(
parent,
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
mat,
position
);
}
// Lighting
scene.add(new THREE.HemisphereLight(0xfff6df, 0xa89883, 2.25));
const keyLight = new THREE.DirectionalLight(0xfff4dc, 3.2);
keyLight.position.set(-5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb8d8e2, 1.2);
fillLight.position.set(7, 5, -6);
scene.add(fillLight);
const warmLight = new THREE.PointLight(0xffc978, 1.1, 14);
warmLight.position.set(-1, 5, 4);
scene.add(warmLight);
// Floor and work table
const floor = addMesh(
scene,
new THREE.PlaneGeometry(60, 60),
material(COLORS.floor, { roughness: 1 }),
[0, -0.21, 0]
);
floor.rotation.x = -Math.PI / 2;
floor.castShadow = false;
const tableTop = addBox(
scene,
[15.2, 0.34, 6.6],
[0, 0, 0],
material(COLORS.woodLight)
);
const tableEdge = addBox(
scene,
[15.45, 0.23, 6.82],
[0, -0.16, 0],
material(COLORS.tanDark)
);
// Small decorative table edge pegs
for (let i = -3; i <= 3; i++) {
const peg = cylinder(
scene,
0.08,
0.08,
0.08,
10,
material(COLORS.cream),
[i * 2, -0.15, 3.44]
);
peg.rotation.x = Math.PI / 2;
}
const trayPositions = [-4, 0, 4];
function makeTray(x, color, innerColor) {
const group = new THREE.Group();
group.position.x = x;
scene.add(group);
const base = horizontalRounded(
3.25,
2.45,
0.24,
0.24,
material(color)
);
base.position.y = 0.39;
group.add(base);
const inset = horizontalRounded(
3.02,
2.22,
0.08,
0.18,
material(innerColor)
);
inset.position.y = 0.54;
group.add(inset);
return group;
}
const trayOne = makeTray(-4, COLORS.tan, COLORS.cream);
const trayTwo = makeTray(0, COLORS.navy, 0xe8e2d5);
const trayThree = makeTray(4, COLORS.tan, COLORS.cream);
// Tray labels
function makeLabel(number, text, x, color) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#fffdf6";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(102, 128, 68, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#ffffff";
ctx.font = "900 72px Arial, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(number, 102, 130);
ctx.fillStyle = "#34404b";
ctx.font = "800 58px Arial, sans-serif";
ctx.textAlign = "left";
ctx.fillText(text, 205, 134);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const backing = addBox(
scene,
[2.72, 0.64, 0.09],
[x, 0.48, 1.48],
material(COLORS.ink)
);
backing.rotation.x = -0.13;
const plane = new THREE.Mesh(
new THREE.PlaneGeometry(2.57, 0.51),
new THREE.MeshBasicMaterial({ map: texture })
);
plane.position.set(x, 0.49, 1.535);
plane.rotation.x = -0.13;
scene.add(plane);
}
makeLabel("1", "BLANK CARDS", -4, "#db716d");
makeLabel("2", "HOLE PUNCH", 0, "#66899a");
makeLabel("3", "RIBBON + BOW", 4, "#e7b95d");
// Direction arrows
function makeArrow(x) {
const group = new THREE.Group();
group.position.set(x, 0.59, 0.93);
scene.add(group);
addBox(
group,
[0.42, 0.055, 0.105],
[-0.12, 0, 0],
material(COLORS.mustard)
);
const tip = addMesh(
group,
new THREE.ConeGeometry(0.15, 0.29, 3),
material(COLORS.mustard),
[0.22, 0, 0]
);
tip.rotation.z = -Math.PI / 2;
}
makeArrow(-2);
makeArrow(2);
// Cards and bow construction
const cardColors = [
COLORS.cream,
COLORS.blush,
COLORS.mint,
COLORS.lilac,
COLORS.paper,
0xf3cf77
];
function makeBow() {
const group = new THREE.Group();
const ribbonMat = material(COLORS.coral, {
roughness: 0.48,
flatShading: false
});
const left = addMesh(
group,
new THREE.TorusGeometry(0.20, 0.055, 6, 12),
ribbonMat,
[-0.13, 0, 0]
);
left.rotation.x = -Math.PI / 2;
left.scale.z = 0.76;
const right = addMesh(
group,
new THREE.TorusGeometry(0.20, 0.055, 6, 12),
ribbonMat,
[0.13, 0, 0]
);
right.rotation.x = -Math.PI / 2;
right.scale.z = 0.76;
const knot = addMesh(
group,
new THREE.IcosahedronGeometry(0.10, 0),
material(COLORS.coralDark),
[0, 0.035, 0]
);
knot.scale.set(1, 0.65, 0.8);
const leftTail = addBox(
group,
[0.075, 0.035, 0.42],
[-0.10, -0.012, 0.31],
ribbonMat
);
leftTail.rotation.y = -0.24;
const rightTail = addBox(
group,
[0.075, 0.035, 0.42],
[0.10, -0.012, 0.31],
ribbonMat
);
rightTail.rotation.y = 0.24;
group.scale.setScalar(0.001);
return group;
}
function makeCard(color) {
const group = new THREE.Group();
const slab = horizontalRounded(
1.06,
1.38,
0.065,
0.12,
material(color)
);
group.add(slab);
const hole = addMesh(
group,
new THREE.CircleGeometry(0.073, 12),
new THREE.MeshBasicMaterial({
color: 0x3b3430,
side: THREE.DoubleSide
}),
[0, 0.061, -0.35]
);
hole.rotation.x = -Math.PI / 2;
hole.visible = false;
const holeRim = addMesh(
group,
new THREE.TorusGeometry(0.087, 0.015, 4, 12),
material(0xb7a891),
[0, 0.068, -0.35]
);
holeRim.rotation.x = Math.PI / 2;
holeRim.visible = false;
const bow = makeBow();
bow.position.set(0, 0.13, 0.23);
group.add(bow);
group.userData.hole = hole;
group.userData.holeRim = holeRim;
group.userData.bow = bow;
return group;
}
const cards = [];
for (let i = 0; i < cardColors.length; i++) {
const card = makeCard(cardColors[i]);
const row = Math.floor(i / 3);
const col = i % 3;
card.position.set(
-4 + (col - 1) * 0.18,
0.635 + i * 0.013,
-0.08 + row * 0.19 + (i % 2) * 0.035
);
card.rotation.y = (i % 3 - 1) * 0.055;
card.userData.homePosition = card.position.clone();
card.userData.homeRotation = card.rotation.clone();
trayOne.add(card);
cards.push(card);
}
// A sample finished badge already resting on the ribbon tray
const sampleBadge = makeCard(COLORS.lilac);
sampleBadge.position.set(2.92, 0.68, 0.60);
sampleBadge.rotation.y = -0.12;
sampleBadge.scale.setScalar(0.76);
sampleBadge.userData.hole.visible = true;
sampleBadge.userData.holeRim.visible = true;
sampleBadge.userData.bow.visible = true;
sampleBadge.userData.bow.scale.setScalar(1);
trayThree.add(sampleBadge);
// Hole punch machine
const punch = new THREE.Group();
punch.position.set(0, 0, -0.02);
trayTwo.add(punch);
const punchBase = horizontalRounded(
1.56,
1.40,
0.30,
0.20,
material(COLORS.navy)
);
punchBase.position.y = 0.68;
punch.add(punchBase);
const punchInset = horizontalRounded(
1.24,
1.05,
0.07,
0.15,
material(0x7893a0)
);
punchInset.position.set(0, 0.855, 0.02);
punch.add(punchInset);
addBox(
punch,
[0.17, 0.73, 0.22],
[-0.49, 1.21, -0.12],
material(COLORS.blue)
);
addBox(
punch,
[0.17, 0.73, 0.22],
[0.49, 1.21, -0.12],
material(COLORS.blue)
);
addBox(
punch,
[1.25, 0.40, 0.62],
[0, 1.64, -0.06],
material(COLORS.navy)
);
cylinder(
punch,
0.13,
0.10,
0.26,
12,
material(0xc4d0d3, { metalness: 0.45, roughness: 0.34 }),
[0, 1.32, 0.02]
);
const lever = new THREE.Group();
lever.position.set(0, 1.85, 0.02);
lever.rotation.x = 0.55;
punch.add(lever);
addBox(
lever,
[0.13, 0.13, 1.02],
[0, 0.12, 0.42],
material(COLORS.blue)
);
const handle = addBox(
lever,
[0.78, 0.17, 0.20],
[0, 0.15, 0.96],
material(COLORS.coral)
);
handle.rotation.x = 0.04;
const statusLight = addMesh(
punch,
new THREE.IcosahedronGeometry(0.075, 0),
material(COLORS.mint),
[0.48, 1.48, 0.27]
);
// Ribbon spool
const spool = new THREE.Group();
spool.position.set(3.94, 1.02, -0.43);
trayThree.add(spool);
const spoolRoll = new THREE.Group();
spool.add(spoolRoll);
const ribbonRoll = cylinder(
spoolRoll,
0.51,
0.51,
0.68,
16,
material(COLORS.coral, { roughness: 0.52 }),
[0, 0, 0]
);
ribbonRoll.rotation.z = Math.PI / 2;
const flangeLeft = cylinder(
spoolRoll,
0.69,
0.69,
0.10,
16,
material(COLORS.navy),
[-0.39, 0, 0]
);
flangeLeft.rotation.z = Math.PI / 2;
const flangeRight = cylinder(
spoolRoll,
0.69,
0.69,
0.10,
16,
material(COLORS.navy),
[0.39, 0, 0]
);
flangeRight.rotation.z = Math.PI / 2;
const axle = cylinder(
spool,
0.12,
0.12,
1.02,
12,
material(COLORS.mustard),
[0, 0, 0]
);
axle.rotation.z = Math.PI / 2;
// Flat ribbon trailing from the spool
const ribbonCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(4.02, 0.78, -0.12),
new THREE.Vector3(4.08, 0.70, 0.10),
new THREE.Vector3(4.25, 0.635, 0.39),
new THREE.Vector3(4.12, 0.615, 0.68)
]);
function makeRibbonStrip() {
const segments = 36;
const positions = [];
const indices = [];
const up = new THREE.Vector3(0, 1, 0);
for (let i = 0; i <= segments; i++) {
const t = i / segments;
const point = ribbonCurve.getPoint(t);
const tangent = ribbonCurve.getTangent(t).normalize();
const side = new THREE.Vector3().crossVectors(tangent, up).normalize();
positions.push(
point.x + side.x * 0.10,
point.y,
point.z + side.z * 0.10
);
positions.push(
point.x - side.x * 0.10,
point.y,
point.z - side.z * 0.10
);
if (i < segments) {
const a = i * 2;
indices.push(a, a + 1, a + 2, a + 1, a + 3, a + 2);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(
geometry,
material(COLORS.coral, {
roughness: 0.5,
side: THREE.DoubleSide,
flatShading: false
})
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
const trailingRibbon = makeRibbonStrip();
// Camera controls implemented without external controls modules
const cameraState = {
theta: 0.54,
phi: 0.92,
radius: innerWidth < 720 ? 14.2 : 12.4,
desiredTheta: 0.54,
desiredPhi: 0.92,
desiredRadius: innerWidth < 720 ? 14.2 : 12.4,
target: new THREE.Vector3(0, 0.58, 0)
};
const pointer = {
down: false,
id: null,
x: 0,
y: 0
};
renderer.domElement.addEventListener("pointerdown", (event) => {
pointer.down = true;
pointer.id = event.pointerId;
pointer.x = event.clientX;
pointer.y = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer.down || event.pointerId !== pointer.id) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
cameraState.desiredTheta -= dx * 0.006;
cameraState.desiredPhi = THREE.MathUtils.clamp(
cameraState.desiredPhi - dy * 0.005,
0.38,
1.24
);
pointer.x = event.clientX;
pointer.y = event.clientY;
});
function stopPointer(event) {
if (event.pointerId !== pointer.id) return;
pointer.down = false;
pointer.id = null;
renderer.domElement.classList.remove("dragging");
}
renderer.domElement.addEventListener("pointerup", stopPointer);
renderer.domElement.addEventListener("pointercancel", stopPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.desiredRadius = THREE.MathUtils.clamp(
cameraState.desiredRadius + event.deltaY * 0.008,
8.5,
18
);
},
{ passive: false }
);
function setView(name) {
if (name === "front") {
cameraState.desiredTheta = 0.01;
cameraState.desiredPhi = 0.91;
cameraState.desiredRadius = innerWidth < 720 ? 14.5 : 12.8;
} else if (name === "top") {
cameraState.desiredTheta = 0.02;
cameraState.desiredPhi = 0.39;
cameraState.desiredRadius = innerWidth < 720 ? 15.2 : 13.1;
} else {
cameraState.desiredTheta = 0.54;
cameraState.desiredPhi = 0.92;
cameraState.desiredRadius = innerWidth < 720 ? 14.2 : 12.4;
}
}
document.querySelectorAll("[data-view]").forEach((button) => {
button.addEventListener("click", () => {
setView(button.dataset.view);
});
});
// Badge-making animation
let outputCount = 0;
let animation = null;
const maxBadges = 3;
const punchPosition = new THREE.Vector3(0, 0.925, 0.02);
const ribbonPosition = new THREE.Vector3(4.15, 1.20, 0.36);
function smoothStep(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function easeOutBack(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
const c1 = 1.70158;
const c3 = c1 + 1;
return 1 + c3 * Math.pow(t - 1, 3) + c1 * Math.pow(t - 1, 2);
}
function setStatus(text, busy = false) {
statusText.textContent = text;
status.classList.toggle("busy", busy);
}
function updateCounter() {
badgeCount.textContent = String(outputCount).padStart(2, "0");
makeButton.disabled = outputCount >= maxBadges || !!animation;
makeButton.textContent =
outputCount >= maxBadges ? "Tray full — reset" : "Make a badge";
}
function startBadge(surprise = false) {
if (animation || outputCount >= maxBadges) return;
const card = cards[cards.length - 1 - outputCount];
const finalX = outputCount === 0 ? 4.30 : outputCount === 1 ? 3.68 : 4.32;
const finalZ = outputCount === 1 ? 0.48 : 0.66;
const finalPosition = new THREE.Vector3(finalX, 0.675, finalZ);
animation = {
card,
startTime: performance.now(),
duration: 4700,
startPosition: card.position.clone(),
finalPosition,
surprise,
spoolStart: spoolRoll.rotation.x
};
card.userData.hole.visible = false;
card.userData.holeRim.visible = false;
card.userData.bow.visible = false;
card.userData.bow.scale.setScalar(0.001);
makeButton.disabled = true;
setStatus(surprise ? "Picking a colorful card…" : "Choosing a blank card…", true);
}
function animateBadge(now) {
if (!animation) return;
const elapsed = (now - animation.startTime) / 1000;
const card = animation.card;
const bow = card.userData.bow;
const hole = card.userData.hole;
const holeRim = card.userData.holeRim;
// Move from the card tray to the punch
if (elapsed < 0.95) {
const u = smoothStep(elapsed / 0.95);
card.position.lerpVectors(
animation.startPosition,
punchPosition,
u
);
card.position.y += Math.sin(u * Math.PI) * 0.72;
card.rotation.y = u * 0.25;
card.rotation.z = Math.sin(u * Math.PI) * 0.08;
setStatus("Bringing the card to the punch…", true);
}
// Punch
else if (elapsed < 1.72) {
const u = (elapsed - 0.95) / 0.77;
card.position.copy(punchPosition);
card.rotation.set(0, 0, 0);
lever.rotation.x = 0.55 - Math.sin(u * Math.PI) * 0.49;
statusLight.material.color.setHex(
u > 0.36 && u < 0.67 ? COLORS.coral : COLORS.mint
);
if (elapsed > 1.05) {
hole.visible = true;
holeRim.visible = true;
}
setStatus("Punching a tidy little hole…", true);
}
// Move to the ribbon station
else if (elapsed < 2.90) {
const u = smoothStep((elapsed - 1.72) / 1.18);
card.position.lerpVectors(punchPosition, ribbonPosition, u);
card.position.y += Math.sin(u * Math.PI) * 0.85;
card.rotation.y = u * Math.PI * 0.75;
card.rotation.z = Math.sin(u * Math.PI) * 0.06;
setStatus("Threading the ribbon through…", true);
}
// Tie the bow
else if (elapsed < 3.78) {
const u = (elapsed - 2.90) / 0.88;
card.position.copy(ribbonPosition);
card.rotation.y = Math.PI * 0.75;
card.rotation.z = 0;
bow.visible = true;
const scale = Math.max(0.001, easeOutBack(u));
bow.scale.setScalar(scale);
spoolRoll.rotation.x =
animation.spoolStart - smoothStep(u) * Math.PI * 1.35;
setStatus("Tying the perfect bow…", true);
}
// Place the finished badge
else if (elapsed < animation.duration / 1000) {
const u = smoothStep(
(elapsed - 3.78) / (animation.duration / 1000 - 3.78)
);
card.position.lerpVectors(
ribbonPosition,
animation.finalPosition,
u
);
card.position.y += Math.sin(u * Math.PI) * 0.20;
card.rotation.y = Math.PI * 0.75 * (1 - u);
card.rotation.z = 0;
setStatus("Finishing up your new badge…", true);
}
// Complete
else {
card.position.copy(animation.finalPosition);
card.rotation.set(0, 0, 0);
bow.visible = true;
bow.scale.setScalar(1);
hole.visible = true;
holeRim.visible = true;
outputCount++;
animation = null;
lever.rotation.x = 0.55;
statusLight.material.color.setHex(COLORS.mint);
updateCounter();
setStatus(
outputCount === 1
? "One lovely badge, ready to wear!"
: `${outputCount} cheerful badges and counting.`
);
}
}
function resetStation() {
animation = null;
outputCount = 0;
cards.forEach((card) => {
card.position.copy(card.userData.homePosition);
card.rotation.copy(card.userData.homeRotation);
card.scale.setScalar(1);
card.userData.hole.visible = false;
card.userData.holeRim.visible = false;
card.userData.bow.visible = false;
card.userData.bow.scale.setScalar(0.001);
});
lever.rotation.x = 0.55;
spoolRoll.rotation.x = 0;
statusLight.material.color.setHex(COLORS.mint);
updateCounter();
setStatus("Ready for a fresh badge");
}
makeButton.addEventListener("click", () => startBadge(false));
resetButton.addEventListener("click", resetStation);
surpriseButton.addEventListener("click", () => {
if (animation || outputCount >= maxBadges) return;
const colorIndex = Math.floor(Math.random() * cardColors.length);
const chosen = cards[cards.length - 1 - outputCount];
chosen.userData.homePosition.copy(chosen.userData.homePosition);
chosen.children[0].material.color.setHex(cardColors[colorIndex]);
startBadge(true);
});
updateCounter();
// Resize
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
// Main loop
const clock = new THREE.Clock();
function render(now) {
const delta = clock.getDelta();
const damping = 1 - Math.exp(-delta * 8);
cameraState.theta = THREE.MathUtils.lerp(
cameraState.theta,
cameraState.desiredTheta,
damping
);
cameraState.phi = THREE.MathUtils.lerp(
cameraState.phi,
cameraState.desiredPhi,
damping
);
cameraState.radius = THREE.MathUtils.lerp(
cameraState.radius,
cameraState.desiredRadius,
damping
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
animateBadge(now);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(render);
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
});
</script>
</body>
</html>
```