Build a 3D cassette tape in three.js (0.160, ES modules, no model files) for an off-white page. - Shell: a rounded rectangle, 10 × 6.3 × 0.8 units with a small bevel and the reel windows cut out, with a fine silver line on the edges where the moulding catches the light. - Colour: a smoked clear grey shell you can see the reels and screw posts through, and a matte black label written in white pen. - Label: one per side. The title is hand-written in pen (Reenie Beanie), with small print under a hairline. Draw it to a canvas texture once the fonts have loaded. - Reels: toothed hubs with wound tape packs. Centre the packs on the teeth so the teeth stand proud by the same amount on both faces, and add faint streaks so you can see them turn. - Build the face details (rims, window, screws, head marks) once and mirror them onto side B, so both faces look finished. - Pose it tilted, like a tape lying on a table. One soft directional light, a RoomEnvironment for reflections, and a faint shadow on the page behind it. The shadow catcher must not write depth, or a turned case loses its shell behind it. - Drag to turn it in your hand; let go and it springs back. Tap to flip it to side B with a quick spring. - A clear shell: draw it two-sided, make it more solid at grazing angles (Fresnel), and give the see-through parts a fixed draw order, inside out, so nothing blinks out as it turns. - Render only when something moves, cap the pixel ratio at 1.75, and respect prefers-reduced-motion. Finally, add a small credit at the bottom of the page: "3D cassette by Naveen Meena", linking to https://arteffects.inaveenmeena.com/cassette/
// Cassette (Arteffects No. 001) by Naveen Meena — https://arteffects.inaveenmeena.com/
// © 2026 Naveen Meena. Free for personal, non-commercial use (commercial use: ask first, inaveenmeena@outlook.com). If you share it, credit "3D cassette by Naveen Meena" linked to https://arteffects.inaveenmeena.com/cassette/ (or "Inspired by Naveen Meena" if it only inspired you).
// The 3D cassette. site.js drives it: position and reels.
import * as THREE from "three";
import { RoomEnvironment } from "three/addons/environments/RoomEnvironment.js";
import { makePencil } from "./pencil3d.js?v=6";
const reduceMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
// The cassette is built in steps, with a breath between them, so a phone can answer taps and draw the
// page while it builds; built in one go, it held the main thread for about 1.7s on a mid-range phone
const breathe = () => new Promise((done) => setTimeout(done, 0));
const W = 10, H = 6.3, D = 0.8, BEVEL = 0.06;
const FRONT = D / 2 + BEVEL;
const HUB_X = 2.1, HUB_Y = 0.55, HUB_R = 0.58;
const LABEL = { l: -4.55, r: 4.55, b: -1.7, t: 2.75 };
// ---------- Renderer ----------
const canvas = document.createElement("canvas");
canvas.id = "cassette3d";
canvas.setAttribute("aria-hidden", "true");
document.body.appendChild(canvas);
const renderer = new THREE.WebGLRenderer({ canvas, alpha: true, antialias: true, preserveDrawingBuffer: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.VSMShadowMap;
const scene = new THREE.Scene();
await breathe();
const pmrem = new THREE.PMREMGenerator(renderer);
scene.environment = pmrem.fromScene(new RoomEnvironment(), 0.04).texture;
scene.environmentIntensity = 0.95;
await breathe();
const camera = new THREE.PerspectiveCamera(20, 1, 1, 400);
camera.position.z = 100;
const worldHeight = 2 * camera.position.z * Math.tan(THREE.MathUtils.degToRad(camera.fov / 2));
const sun = new THREE.DirectionalLight(0xffffff, 1.05);
sun.position.set(-5, 9, 40);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.radius = 22;
sun.shadow.blurSamples = 12;
Object.assign(sun.shadow.camera, { left: -40, right: 40, top: 40, bottom: -40, near: 1, far: 120 });
scene.add(sun, sun.target);
// soft shadow on the page behind the cassette
const floor = new THREE.Mesh(new THREE.PlaneGeometry(300, 300), new THREE.ShadowMaterial({ opacity: 0.12 }));
floor.position.z = -6;
floor.receiveShadow = true;
// the shadow catcher must not hide anything: a case turned past it would lose its clear shell behind it
floor.material.depthWrite = false;
floor.renderOrder = -1;
scene.add(floor);
await breathe();
// ---------- Shapes ----------
function rounded(l, b, r, t, rad, Cls = THREE.Shape) {
const s = new Cls();
s.moveTo(l + rad, b);
s.lineTo(r - rad, b); s.quadraticCurveTo(r, b, r, b + rad);
s.lineTo(r, t - rad); s.quadraticCurveTo(r, t, r - rad, t);
s.lineTo(l + rad, t); s.quadraticCurveTo(l, t, l, t - rad);
s.lineTo(l, b + rad); s.quadraticCurveTo(l, b, l + rad, b);
return s;
}
function cutHoles(shape) {
[-HUB_X, HUB_X].forEach((x) => {
const c = new THREE.Path();
c.absarc(x, HUB_Y, HUB_R, 0, Math.PI * 2, true);
shape.holes.push(c);
});
shape.holes.push(rounded(-1.15, 0.12, 1.15, 0.98, 0.1, THREE.Path));
return shape;
}
const std = (color, roughness = 0.55, metalness = 0) => new THREE.MeshStandardMaterial({ color, roughness, metalness });
function add(parent, geo, mat, x = 0, y = 0, z = 0) {
const m = new THREE.Mesh(geo, mat);
m.position.set(x, y, z);
m.castShadow = true;
parent.add(m);
return m;
}
// ---------- Cassette ----------
const cassette = new THREE.Group();
scene.add(cassette);
// fine moulded-plastic grain, shared by the body parts
function grain() {
const t = document.createElement("canvas");
t.width = t.height = 256;
const g = t.getContext("2d"), d = g.createImageData(256, 256);
for (let i = 0; i < d.data.length; i += 4) {
const v = 118 + Math.random() * 20;
d.data[i] = d.data[i + 1] = d.data[i + 2] = v;
d.data[i + 3] = 255;
}
g.putImageData(d, 0, 0);
const tex = new THREE.CanvasTexture(t);
tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
tex.repeat.set(1.6, 1.6);
return tex;
}
const grainTex = grain();
// the speckle you can see, multiplied into the colour like the player's noise layer
function speckle() {
const t = document.createElement("canvas");
t.width = t.height = 256;
const g = t.getContext("2d"), d = g.createImageData(256, 256);
for (let i = 0; i < d.data.length; i += 4) {
const v = 236 + Math.random() * 19;
d.data[i] = d.data[i + 1] = d.data[i + 2] = v;
d.data[i + 3] = 255;
}
g.putImageData(d, 0, 0);
const tex = new THREE.CanvasTexture(t);
tex.colorSpace = THREE.SRGBColorSpace;
tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
tex.repeat.set(0.9, 0.9);
return tex;
}
const speckleTex = speckle();
await breathe();
// Colourways. `clear` shells show the reels, posts and rollers through the plastic; the rest are solid.
// Smoked is this site's own; indigo and sunset is the live build's.
const SMOKED_LABEL = { bg: "#121211", ink: "#f3f2ee", print: "#a9acaf", line: "rgba(255, 255, 255, .18)" };
const PALETTES = {
smoked: { name: "Smoked clear", shell: 0x5c6269, clear: true, frost: 0xc4c8cc, rim: 0xbfc3c7, rib: 0x9ea3a8, post: 0xc3c7cb, label: SMOKED_LABEL },
indigo: { name: "Indigo and sunset", shell: 0x35326f, clear: false, frost: 0xdfd3b8, rim: 0xbfb396, rib: 0x2a2860, post: 0x35326f, label: "sunset" },
red: { name: "Clear red", shell: 0x9c0d14, clear: true, alpha: 0.66, frost: 0xdccfcc, rim: 0xcbc2bf, rib: 0x8e1d22, post: 0xc79a9a, label: SMOKED_LABEL },
olive: { name: "Olive green", shell: 0x5c6135, clear: false, frost: 0xe7e0ca, rim: 0xc9c1a3, rib: 0x4a4e2a, post: 0x5c6135, label: { bg: "#efe8d6", ink: "#2f3119", print: "#6d6f52", line: "rgba(47, 49, 25, .2)" } },
mustard: { name: "Mustard", shell: 0xd39b1f, clear: false, frost: 0x2a2a28, rim: 0x3a3a37, rib: 0xb98516, post: 0xd39b1f, label: { bg: "#f4efe3", ink: "#1b1a18", print: "#6b675e", line: "rgba(27, 26, 24, .2)" } },
};
let palette = PALETTES.smoked;
// smoked clear plastic: a cool grey tint you can see the reels, posts and rollers through
const shellMat = new THREE.MeshPhysicalMaterial({
color: 0x5c6269, roughness: 0.16, bumpMap: grainTex, bumpScale: 0.015,
transparent: true, opacity: 0.5, depthWrite: false,
clearcoat: 1, clearcoatRoughness: 0.28, specularIntensity: 1,
});
// Real clear plastic gets more solid the flatter you look across it (the Fresnel effect). Without it the
// walls and rounded corners turn as see-through as the face and fade away as the case turns.
shellMat.onBeforeCompile = (shader) => {
shader.fragmentShader = shader.fragmentShader.replace("#include <opaque_fragment>", `
float grazing = 1.0 - abs(dot(normal, normalize(vViewPosition)));
diffuseColor.a = mix(diffuseColor.a, 0.92, pow(grazing, 2.2));
#include <opaque_fragment>`);
};
// frosted sections: the window plate and the head block, satin rather than clear
const frostMat = new THREE.MeshPhysicalMaterial({
color: 0xc4c8cc, roughness: 0.7, map: speckleTex, bumpMap: grainTex, bumpScale: 0.05,
transparent: true, opacity: 0.74, clearcoat: 0.2, clearcoatRoughness: 0.7,
});
const shell = new THREE.ExtrudeGeometry(cutHoles(rounded(-W / 2, -H / 2, W / 2, H / 2, 0.3)), {
depth: D, bevelEnabled: true, bevelThickness: BEVEL, bevelSize: BEVEL, bevelSegments: 4, curveSegments: 40,
});
shell.translate(0, 0, -D / 2);
add(cassette, shell, shellMat);
// a fine silver line where the moulding catches the light, front and back
cassette.add(new THREE.LineSegments(new THREE.EdgesGeometry(shell, 40), new THREE.LineBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0.5 })));
await breathe();
// tape packs and toothed hubs
function packTexture() {
const t = document.createElement("canvas");
t.width = t.height = 512;
const g = t.getContext("2d");
g.fillStyle = "#1c1b1a";
g.fillRect(0, 0, 512, 512);
// wound layers, plus faint streaks so rotation reads
for (let r = 40; r < 256; r += 5) {
g.strokeStyle = `rgba(255, 255, 255, ${0.03 + (r % 7) * 0.01})`;
g.lineWidth = 2;
g.beginPath(); g.arc(256, 256, r, 0, 7); g.stroke();
}
for (let i = 0; i < 9; i++) {
const a = i / 9 * Math.PI * 2;
g.strokeStyle = "rgba(255, 255, 255, .07)";
g.lineWidth = 9 + (i % 3) * 5;
g.beginPath(); g.arc(256, 256, 120 + (i % 4) * 34, a, a + 0.5); g.stroke();
}
const tex = new THREE.CanvasTexture(t);
tex.colorSpace = THREE.SRGBColorSpace;
return tex;
}
const packTop = new THREE.MeshStandardMaterial({ map: packTexture(), roughness: 0.72 });
const packMat = [std(0x151514, 0.45), packTop, packTop];
// centred on the hub teeth (z -0.16 to 0.24), so the teeth stand 0.06 proud of the tape on both faces
const packGeo = new THREE.CylinderGeometry(1, 1, 0.28, 72).rotateX(Math.PI / 2);
const packL = add(cassette, packGeo, packMat, -HUB_X, HUB_Y, 0.04);
const packR = add(cassette, packGeo, packMat, HUB_X, HUB_Y, 0.04);
packL.scale.set(1.52, 1.52, 1);
packR.scale.set(1.52, 1.52, 1);
function hub(x) {
const g = new THREE.Group();
g.position.set(x, HUB_Y, 0);
const white = std(0xf2f2f0, 0.4);
const ring = new THREE.Shape();
ring.absarc(0, 0, 0.53, 0, Math.PI * 2, false);
const open = new THREE.Path();
open.absarc(0, 0, 0.32, 0, Math.PI * 2, true);
ring.holes.push(open);
const ringGeo = new THREE.ExtrudeGeometry(ring, { depth: 0.5, bevelEnabled: false, curveSegments: 48 });
ringGeo.translate(0, 0, -0.24);
add(g, ringGeo, white);
for (let i = 0; i < 6; i++) {
const a = (i / 6) * Math.PI * 2;
const tooth = add(g, new THREE.BoxGeometry(0.17, 0.1, 0.4), white, Math.cos(a) * 0.26, Math.sin(a) * 0.26, 0.04);
tooth.rotation.z = a;
}
cassette.add(g);
return g;
}
const hubL = hub(-HUB_X), hubR = hub(HUB_X);
await breathe();
// printed label: one per side
function makeLabel() {
const c = document.createElement("canvas");
c.width = 1638; c.height = 800;
const tex = new THREE.CanvasTexture(c);
tex.colorSpace = THREE.SRGBColorSpace;
tex.anisotropy = renderer.capabilities.getMaxAnisotropy();
const geo = new THREE.ShapeGeometry(cutHoles(rounded(LABEL.l, LABEL.b, LABEL.r, LABEL.t, 0.14)), 40);
const pos = geo.attributes.position, uv = geo.attributes.uv;
for (let i = 0; i < pos.count; i++) {
uv.setXY(i, (pos.getX(i) - LABEL.l) / (LABEL.r - LABEL.l), (pos.getY(i) - LABEL.b) / (LABEL.t - LABEL.b));
}
const mesh = new THREE.Mesh(geo, new THREE.MeshLambertMaterial({ map: tex, emissive: 0xffffff, emissiveMap: tex, emissiveIntensity: 0.78 }));
mesh.position.z = FRONT + 0.005;
mesh.receiveShadow = true;
return { c, tex, mesh };
}
const labelA = makeLabel(), labelB = makeLabel();
const sideA = new THREE.Group(), sideB = new THREE.Group();
sideA.add(labelA.mesh);
sideB.add(labelB.mesh);
sideB.rotation.y = Math.PI;
cassette.add(sideA, sideB);
function paint({ c, tex }, side, title, track) {
const g = c.getContext("2d"), w = c.width, h = c.height;
if (palette.label === "sunset") return paintSunset(g, w, h, tex, side, title, track);
const L = palette.label;
// a matte label: the title in pen, small print under a hairline (black and silver on the smoked case)
g.fillStyle = L.bg;
g.fillRect(0, 0, w, h);
g.fillStyle = L.ink;
g.font = '168px "Reenie Beanie", cursive';
g.textAlign = "center";
g.textBaseline = "alphabetic";
g.fillText(title, w / 2, 168);
g.fillStyle = L.line;
g.fillRect(52, 610, w - 104, 3);
g.fillStyle = L.print;
g.font = '500 54px Geist, sans-serif';
g.textAlign = "left";
g.fillText(side, 52, 700);
g.textAlign = "center";
g.font = '400 46px Geist, sans-serif';
g.fillText(track, w / 2, 696);
g.textAlign = "right";
g.font = '500 50px Geist, sans-serif';
g.fillText("C60", w - 52, 698);
// paper tooth, the same speckle as the plastic
const n = g.getImageData(0, 0, w, h), px = n.data;
for (let i = 0; i < px.length; i += 4) {
const v = Math.random() * 7;
px[i] += v; px[i + 1] += v; px[i + 2] += v;
}
g.putImageData(n, 0, 0);
tex.needsUpdate = true;
}
// the indigo-and-sunset label from the live build: cream paper, four sunset bands, an indigo foot
function paintSunset(g, w, h, tex, side, title, track) {
g.fillStyle = "#fbf4e4";
g.fillRect(0, 0, w, h);
["#ffc145", "#ff8a3d", "#f4476b", "#b5338a"].forEach((col, i) => { g.fillStyle = col; g.fillRect(0, 340 + i * 64, w, 64); });
g.fillStyle = "#35326f";
g.fillRect(0, 596, w, h - 596);
g.font = '168px "Reenie Beanie", cursive';
g.textAlign = "center";
g.textBaseline = "alphabetic";
g.fillText(title, w / 2, 168);
g.fillStyle = "#ffc145";
g.font = '800 70px Geist, sans-serif';
g.textAlign = "left";
g.fillText(side, 52, 700);
g.textAlign = "center";
g.fillStyle = "#fbf4e4";
g.font = '600 50px Geist, sans-serif';
g.fillText(track, w / 2, 690);
g.textAlign = "right";
g.fillStyle = "#ffc145";
g.font = '800 60px Geist, sans-serif';
g.fillText("C60", w - 52, 696);
const n = g.getImageData(0, 0, w, h), px = n.data;
for (let i = 0; i < px.length; i += 4) {
const v = 1 - Math.random() * 0.07;
px[i] *= v; px[i + 1] *= v; px[i + 2] *= v;
}
g.putImageData(n, 0, 0);
tex.needsUpdate = true;
}
await breathe();
// raised plate around the window
const plate = new THREE.ExtrudeGeometry(cutHoles(rounded(-3.3, -0.42, 3.3, 1.52, 0.32)), {
depth: 0.06, bevelEnabled: true, bevelThickness: 0.03, bevelSize: 0.03, bevelSegments: 3, curveSegments: 40,
});
const plateMat = frostMat;
add(cassette, plate, plateMat, 0, 0, FRONT + 0.01);
const plateBack = add(cassette, plate, plateMat, 0, 0, 0);
plateBack.rotation.y = Math.PI;
plateBack.position.z = -FRONT - 0.01;
// Details on the face: label frame, rims, window, head-block marks and screws. Built once for side A,
// then the same group is turned onto side B, so both faces catch the light the same way.
const face = new THREE.Group();
// raised frame around the label, and rims around the reels and window
const frameShape = rounded(LABEL.l - 0.1, LABEL.b - 0.1, LABEL.r + 0.1, LABEL.t + 0.1, 0.2);
frameShape.holes.push(rounded(LABEL.l, LABEL.b, LABEL.r, LABEL.t, 0.14, THREE.Path));
const frameGeo = new THREE.ExtrudeGeometry(frameShape, { depth: 0.03, bevelEnabled: true, bevelThickness: 0.012, bevelSize: 0.012, bevelSegments: 2, curveSegments: 16 });
add(face, frameGeo, shellMat, 0, 0, FRONT);
const rimMat = std(0xbfc3c7, 0.35, 0.5);
[-HUB_X, HUB_X].forEach((x) => {
const rim = new THREE.Mesh(new THREE.TorusGeometry(HUB_R + 0.02, 0.04, 12, 64), rimMat);
rim.position.set(x, HUB_Y, FRONT + 0.1);
face.add(rim);
});
const winFrame = rounded(-1.22, 0.05, 1.22, 1.05, 0.13);
winFrame.holes.push(rounded(-1.15, 0.12, 1.15, 0.98, 0.1, THREE.Path));
add(face, new THREE.ExtrudeGeometry(winFrame, { depth: 0.03, bevelEnabled: false, curveSegments: 12 }), rimMat, 0, 0, FRONT + 0.08);
// window plastic
const glass = new THREE.MeshPhysicalMaterial({ color: 0xffffff, transparent: true, opacity: 0.12, roughness: 0.1, clearcoat: 1, clearcoatRoughness: 0.2 });
add(face, new THREE.PlaneGeometry(2.3, 0.86), glass, 0, 0.55, FRONT + 0.03).castShadow = false;
// lower head block, both sides
const trap = new THREE.Shape();
trap.moveTo(-3.7, -H / 2); trap.lineTo(3.7, -H / 2); trap.lineTo(3.15, -1.86); trap.lineTo(-3.15, -1.86); trap.closePath();
const trapGeo = new THREE.ExtrudeGeometry(trap, { depth: 0.1, bevelEnabled: true, bevelThickness: 0.03, bevelSize: 0.03, bevelSegments: 2 });
add(cassette, trapGeo, plateMat, 0, 0, FRONT);
const trapBack = add(cassette, trapGeo, plateMat, 0, 0, -FRONT);
trapBack.rotation.y = Math.PI;
const dark = std(0x2a2a28, 0.6);
[-2.4, 2.4].forEach((x) => add(face, new THREE.CylinderGeometry(0.17, 0.17, 0.04, 24).rotateX(Math.PI / 2), dark, x, -2.5, FRONT + 0.14));
[-1.05, 1.05].forEach((x) => add(face, new THREE.BoxGeometry(0.3, 0.3, 0.04), dark, x, -2.5, FRONT + 0.14));
// ribbed lower body, both faces
const ribGeo = new THREE.BoxGeometry(W - 0.5, 0.045, 0.03);
const ribs = new THREE.InstancedMesh(ribGeo, new THREE.MeshStandardMaterial({ color: 0x9ea3a8, roughness: 0.3, transparent: true, opacity: 0.5 }), 28);
const ribM = new THREE.Matrix4();
for (let i = 0; i < 14; i++) {
const y = -H / 2 + 0.16 + i * 0.1;
ribM.setPosition(0, y, FRONT + 0.01); ribs.setMatrixAt(i, ribM);
ribM.setPosition(0, y, -FRONT - 0.01); ribs.setMatrixAt(i + 14, ribM);
}
cassette.add(ribs);
// the opening on the bottom edge: slot, rollers and the exposed run of tape
add(cassette, new THREE.BoxGeometry(5.2, 0.12, D * 0.72), std(0x1d1d1c, 0.7), 0, -H / 2 - 0.02, 0);
[-2.3, 2.3].forEach((x) => add(cassette, new THREE.CylinderGeometry(0.15, 0.15, D * 0.6, 28).rotateX(Math.PI / 2), std(0xd6d9dc, 0.25, 0.8), x, -H / 2 - 0.02, 0));
add(cassette, new THREE.BoxGeometry(4.6, 0.05, D * 0.34), std(0x1c1b1a, 0.4), 0, -H / 2 - 0.1, 0);
// screws
const screwMat = std(0xc9ccd0, 0.3, 0.8);
// the moulded posts the screws run into, seen through the clear case
const postMat = new THREE.MeshStandardMaterial({ color: 0xc3c7cb, roughness: 0.35, transparent: true, opacity: 0.55 });
[[-4.72, 2.87], [4.72, 2.87], [-4.72, -2.87], [4.72, -2.87], [0, -2.5]].forEach(([x, y]) => add(cassette, new THREE.CylinderGeometry(0.3, 0.3, D * 0.92, 28).rotateX(Math.PI / 2), postMat, x, y, 0));
[[-4.72, 2.87], [4.72, 2.87], [-4.72, -2.87], [4.72, -2.87], [0, -2.5]].forEach(([x, y]) => {
const s = add(face, new THREE.CylinderGeometry(0.13, 0.13, 0.05, 24).rotateX(Math.PI / 2), screwMat, x, y, FRONT + (y === -2.5 ? 0.14 : 0.01));
add(s, new THREE.BoxGeometry(0.17, 0.03, 0.02), dark, 0, 0, 0.03);
});
const faceB = face.clone(); // shares geometry and materials
faceB.rotation.y = Math.PI;
cassette.add(face, faceB);
// The see-through parts draw in a fixed order, inside out: the posts, then the shell, then what sits on
// its surface, then the silver edge line. Left to sort by distance they swap as the case turns, and parts
// blink out. The shell draws its inner walls too (two-sided), so turned on edge it still reads as a body.
shellMat.side = THREE.DoubleSide;
cassette.traverse((o) => {
if (o.isLineSegments) o.renderOrder = 4;
else if (o.material === postMat) o.renderOrder = 1;
else if (o.material === shellMat && o.geometry === shell) o.renderOrder = 2;
else if (o.isMesh) o.renderOrder = 3;
});
await breathe();
// ---------- Pencil ----------
const pencil = new THREE.Group();
const lead = makePencil("apsara"); // tip at the origin, body along +Y
pencil.add(lead);
const pencilDir = new THREE.Vector3(-0.8, 0, 0.6).normalize();
const UP = new THREE.Vector3(0, 1, 0);
const heroQ = new THREE.Quaternion().setFromEuler(new THREE.Euler(-0.3, 0.14, -0.36));
const pencilWorld = pencilDir.clone().applyQuaternion(heroQ);
// The seat in the hub that faces you: the left reel on side A, and on side B the other reel, from the back.
// Both faces point the same way once flipped and the hole is round, so the pencil keeps one angle.
const seatA = new THREE.Vector3(-HUB_X + 0.24, HUB_Y - 0.03, 0.06), seatB = new THREE.Vector3(HUB_X - 0.24, HUB_Y - 0.03, -0.06);
const hubPoint = new THREE.Vector3(), hubPointB = new THREE.Vector3();
const spinQ = new THREE.Quaternion();
lead.scale.set(1.35, 1.43, 1.35);
const PENCIL_LEN = 7.9;
const restDirWide = new THREE.Vector3(0.26, 0.96, 0.1).normalize(), restDirNarrow = new THREE.Vector3(0.97, 0.2, 0.1).normalize();
const restPos = new THREE.Vector3(), restQ = new THREE.Quaternion(), holdPos = new THREE.Vector3(), wiggleQ = new THREE.Quaternion(), FACING = new THREE.Vector3(0, 0, 1);
const pencilEnd = new THREE.Vector3(), pencilTip = new THREE.Vector3(), pencilDirWorld = new THREE.Vector3();
const pencilBtn = document.getElementById("pencilBtn"), pencilNote = document.getElementById("pencilNote");
scene.add(pencil); // not a child of the cassette: when the cassette rocks, the pencil holds its line
// ---------- Driven by site.js ----------
const state = {
cx: 0, cy: 0, width: 300, angle: 0, p: 0, wind: 0, locked: false,
};
let flip = 0, flipVel = 0, flipTarget = 0, bump = 0, bumpVel = 0, inserted = true;
let seat = 1, seatTarget = 1, wiggle = 0, wiggleVel = 0, onSeated = null; // seat: 0 resting beside the cassette, 1 in the reel
let last = performance.now(), dirty = true, userFlip = false;
const slot = new THREE.Vector3(), mouth = { x: 0, y: 0 };
const easeInOut = (t) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2);
const part = (t, a, b) => Math.min(1, Math.max(0, (t - a) / (b - a)));
// drag to spin it in your hand, tap to flip it. It settles back on a spring.
const held = { on: false, x: 0, y: 0, moved: 0, rx: 0, ry: 0, vx: 0, vy: 0 };
const inBox = (e) => Math.abs(e.clientX - state.cx) < state.width * 0.52
&& Math.abs(e.clientY - state.cy) < state.width * 0.34 && !e.target.closest?.("button");
// While the pencil is in the reel (or on its way there) the case stays put: turning or flipping it
// would tear the pencil out. A tap just knocks it, so you can tell it's held.
const pencilIn = () => pencil.visible && seatTarget === 1;
addEventListener("pointerdown", (e) => {
if (reduceMotion || state.locked || !inBox(e)) return;
if (pencilIn()) { bumpVel = -1.6; return; }
Object.assign(held, { on: true, x: e.clientX, y: e.clientY, moved: 0 });
document.body.style.cursor = "grabbing";
});
addEventListener("pointermove", (e) => {
if (held.on) {
const dx = e.clientX - held.x, dy = e.clientY - held.y;
held.x = e.clientX; held.y = e.clientY;
held.moved += Math.abs(dx) + Math.abs(dy);
held.vy = dx * 0.012; held.vx = dy * 0.012;
held.ry += held.vy; held.rx += held.vx;
dirty = true;
} else if (e.pointerType === "mouse" && !reduceMotion && !state.locked && !pencilIn()) {
document.body.style.cursor = inBox(e) ? "grab" : document.body.style.cursor === "grab" ? "" : document.body.style.cursor;
}
}, { passive: true });
const letGo = (e) => {
if (!held.on) return;
held.on = false;
document.body.style.cursor = "";
if (e.type === "pointerup" && held.moved < 6) { userFlip = !userFlip; flipTarget = userFlip ? 1 : 0; }
};
addEventListener("pointerup", letGo);
addEventListener("pointercancel", letGo); // a touch that turns into a page scroll never sends pointerup
function resize() {
renderer.setSize(innerWidth, innerHeight, false);
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
dirty = true;
}
addEventListener("resize", resize);
resize();
function frame(now) {
const dt = Math.min(0.05, (now - last) / 1000);
last = now;
const px = worldHeight / innerHeight;
// pencil choreography: picked up from beside the cassette, carried over the reel, plunged in
const p = state.p, wind = state.wind;
seat += Math.sign(seatTarget - seat) * Math.min(Math.abs(seatTarget - seat), dt / 0.8);
const carry = easeInOut(part(seat, 0, 0.72));
const dip = easeInOut(part(seat, 0.72, 1)); // the plunge is quick
if (dip >= 1 && !inserted) { inserted = true; bumpVel = -3; onSeated?.(); }
if (dip < 1) inserted = false;
wiggleVel += (-wiggle * 240 - wiggleVel * 9) * dt;
wiggle += wiggleVel * dt;
// the press of the pencil knocks the cassette, which settles on a spring
bumpVel += (-bump * 90 - bumpVel * 9) * dt;
bump += bumpVel * dt;
// Side B: a quick spring flip
flipVel += ((flipTarget - flip) * 42 - flipVel * 9) * dt;
flip += flipVel * dt;
// let go and it glides back
if (!held.on) {
held.vx += (-held.rx * 26 - held.vx * 7) * dt;
held.vy += (-held.ry * 26 - held.vy * 7) * dt;
held.rx += held.vx * dt;
held.ry += held.vy * dt;
}
const k = state.width / W * px;
cassette.scale.setScalar(k);
floor.position.z = -k * 1.1;
cassette.position.set(
(state.cx - innerWidth / 2) * px,
(innerHeight / 2 - state.cy) * px,
0,
);
// tilted like a tape on a table
const rock = reduceMotion ? 0 : 0.016 * easeInOut(part(p, 0, 0.05));
cassette.rotation.set(
-0.3 + bump * 0.5 + rock * Math.cos(wind) + held.rx,
0.14 + flip * Math.PI + bump * 0.2 + rock * Math.sin(wind) + held.ry,
-0.36,
);
// reels: site.js owns the scroll spin, the pencil adds to the reel it sits in: the left one as you
// see it, which is hubR on side B. Seen from the back a reel turns the other way, hence the sign.
const spin = -THREE.MathUtils.degToRad(state.angle);
const onB = flip > 0.5;
hubL.rotation.z = onB ? spin + wind * 0.6 : spin - wind;
hubR.rotation.z = onB ? spin + wind : spin - wind * 0.6;
packL.rotation.z = hubL.rotation.z;
packR.rotation.z = hubR.rotation.z;
// tip stays in the hub; once seated the pencil turns with the reel.
// While the case flips, the seat slides to the other face's hub and the pencil lifts clear of the turning case.
const f = Math.min(1, Math.max(0, flip));
const h = (1 - dip) * 1.3 + Math.sin(f * Math.PI) * 5.5;
// it spins on its own length as it drives the reel, and nothing else moves it
lead.quaternion.setFromUnitVectors(UP, pencilDir).multiply(spinQ.setFromAxisAngle(UP, -wind));
cassette.updateMatrixWorld();
cassette.localToWorld(hubPoint.copy(seatA));
cassette.localToWorld(hubPointB.copy(seatB));
hubPoint.lerp(hubPointB, easeInOut(f));
pencil.scale.setScalar(k);
holdPos.copy(hubPoint).addScaledVector(pencilWorld, h * k);
if (carry < 1) {
const wide = innerWidth > 760, dir = wide ? restDirWide : restDirNarrow;
const bob = reduceMotion ? 0 : Math.sin(now * 0.0016) * k * 0.09;
restPos.set(
(state.cx + state.width * (wide ? 0.74 : 0) - innerWidth / 2) * px,
(innerHeight / 2 - state.cy - state.width * (wide ? 0 : innerHeight < 700 ? 0.72 : 0.62)) * px + bob,
k * 0.3,
).addScaledVector(dir, -PENCIL_LEN * 0.5 * k);
restQ.setFromUnitVectors(pencilDir, dir);
wiggleQ.setFromAxisAngle(FACING, wiggle * 0.5);
restQ.premultiply(wiggleQ);
pencil.quaternion.copy(restQ).slerp(heroQ, carry);
pencil.position.copy(restPos).lerp(holdPos, carry);
pencil.position.z += Math.sin(carry * Math.PI) * k * 1.6; // lifted over the cassette, not through it
} else {
pencil.quaternion.copy(heroQ);
pencil.position.copy(holdPos);
}
if (seat === 0 && !pencilBtn.hidden) {
// the button over the pencil follows what's drawn
pencilDirWorld.copy(pencilDir).applyQuaternion(pencil.quaternion);
pencilTip.copy(pencil.position).project(camera);
pencilEnd.copy(pencil.position).addScaledVector(pencilDirWorld, PENCIL_LEN * k).project(camera);
const x1 = (pencilTip.x + 1) / 2 * innerWidth, y1 = (1 - pencilTip.y) / 2 * innerHeight;
const x2 = (pencilEnd.x + 1) / 2 * innerWidth, y2 = (1 - pencilEnd.y) / 2 * innerHeight;
pencilBtn.style.width = `${Math.hypot(x2 - x1, y2 - y1).toFixed(1)}px`;
pencilBtn.style.transform = `translate(${((x1 + x2) / 2).toFixed(1)}px, ${((y1 + y2) / 2).toFixed(1)}px) translate(-50%, -50%) rotate(${Math.atan2(y2 - y1, x2 - x1).toFixed(3)}rad)`;
const wide = innerWidth > 760;
// on phones the hero text sits right under the pencil, so the note goes above its tip
const noteX = wide ? (x1 + x2) / 2 - 4 : x1 - 6, noteY = wide ? (y1 + y2) / 2 - 36 : y1 - 92;
pencilNote.style.transform = `translate(${noteX.toFixed(1)}px, ${noteY.toFixed(1)}px) rotate(-4deg)`;
}
slot.set(0, -H / 2 - 0.1, 0);
cassette.localToWorld(slot).project(camera);
mouth.x = (slot.x + 1) / 2 * innerWidth;
mouth.y = (1 - slot.y) / 2 * innerHeight;
const moving = seat !== seatTarget || (seat === 0 && !reduceMotion) || Math.abs(wiggle) + Math.abs(wiggleVel) > 0.001 || held.on || Math.abs(held.rx) + Math.abs(held.ry) > 0.001 || Math.abs(flipTarget - flip) > 0.001 || Math.abs(flipVel) > 0.001 || Math.abs(bump) > 0.0005 || Math.abs(bumpVel) > 0.002;
if (moving || dirty) {
renderer.render(scene, camera);
dirty = false;
}
requestAnimationFrame(frame);
}
function repaint() {
paint(labelA, "A", "Naveen's Story", "Side A");
paint(labelB, "B", "Naveen's Story", "Side B");
dirty = true;
}
window.cassette3d = {
mouth,
update(next) {
Object.assign(state, next);
if (state.locked && held.on) { held.on = false; document.body.style.cursor = ""; } // let go: it springs back
dirty = true;
},
knock() { bumpVel = -3; },
// put the pencil in the reel, or back beside the cassette; `now` skips the move
seat(on, now = false) {
seatTarget = on ? 1 : 0;
if (on) {
// the case goes back to how it first lay, side A up, before the pencil goes in
userFlip = false;
flipTarget = 0;
held.on = false;
}
if (now) { seat = seatTarget; inserted = on; }
pencilBtn.hidden = on;
if (on && document.body.style.cursor === "grab") document.body.style.cursor = ""; // it can't be turned now
if (!on) pencilNote.hidden = false; // site.js takes the note away once the tape is moving
dirty = true;
},
nudge() { wiggleVel = 3.4; },
palettes: Object.fromEntries(Object.entries(PALETTES).map(([key, p]) => [key, { name: p.name, swatch: `#${p.shell.toString(16).padStart(6, "0")}` }])),
palette(name) {
const p = PALETTES[name];
if (!p || p === palette) return;
palette = p;
const clear = (mat, on, opacity) => { mat.transparent = on; mat.opacity = on ? opacity : 1; mat.needsUpdate = true; };
shellMat.color.setHex(p.shell);
clear(shellMat, p.clear, p.alpha ?? 0.5);
shellMat.depthWrite = !p.clear; // a clear shell lets the parts inside draw through it
shellMat.roughness = p.clear ? 0.16 : 0.5;
shellMat.clearcoat = p.clear ? 1 : 0.3;
frostMat.color.setHex(p.frost);
clear(frostMat, p.clear, 0.74);
rimMat.color.setHex(p.rim);
ribs.material.color.setHex(p.rib);
clear(ribs.material, p.clear, 0.5);
postMat.color.setHex(p.post);
clear(postMat, p.clear, 0.55);
repaint();
},
// Arteffects shows the cassette on its own
showPencil(on) { pencil.visible = on; pencilBtn.hidden = true; dirty = true; },
set onSeated(fn) { onSeated = fn; },
};
Promise.all([document.fonts.load('168px "Reenie Beanie"'), document.fonts.load('500 54px Geist')]).catch(() => {}).then(async () => {
repaint();
await renderer.compileAsync(scene, camera).catch(() => {}); // shaders ready before the first frame, so it doesn't stall
document.body.classList.add("has3d");
window.dispatchEvent(new Event("cassette3d-ready"));
requestAnimationFrame(frame);
});
How I made it
The centrepiece of my portfolio. Every part is drawn in code: the shell, the label, the reels and the screws. There are no 3D model files.
Cassette No. 001
Code
New pieces, now and then
One email when something new lands in Arteffects. No spam, unsubscribe any time.