Lab 02Rebuild
The CD player, a year later
Tap carries the spin’s 72°/s into a no-bounce spring that lands on the next full turn, instead of unwinding to 0°.
The 2024 disc spun at 72°/s, and tapping it sprang the disc back to 0°. Wherever the spin was, the cover unwound up to 359° backwards from a standstill before it opened, so the disc you tapped seemed to stop and reverse.
Now the tap keeps the motion it already had: a spring with no bounce starts at 72°/s and lands on the next full turn, opening in about 590ms and closing in about 430ms. If that turn is closer than speed / spring rate (72 / 8 = 9°), the spring would overshoot it, so it rolls on to the one after. Every tap starts from where the disc is and how fast it’s moving, so a tap mid-flight turns it around without a jump, a release carries on from the 4% press, and closing eases back to 72°/s over 600ms.
The cover’s frame stays upright while the art turns inside it at a fixed 1.2×. While the art is more than about 12° off square, the frame stays just round enough for the tilted art to fill it (corner radius ≥ height × (spread − 1.2) / (spread − 1), where spread = cos(tilt) + sin(tilt)), then squares off into the card’s corners as the turn lands, so the corners never show and the art never changes size.
"use client"
import { useCallback, useEffect, useRef, useState } from "react"
import { discAt, INITIAL_PHASE, NO_PRESS, type Phase, type Press, pressAt, toggle, TRACKS, type Variant } from "../../_lib/disc"
import { useClock } from "../../_lib/use-clock"
import { useReducedMotion } from "../../_lib/use-reduced-motion"
import { CdPlayerCard } from "./nested/card"
import { CdPlayerVariantSwitch } from "./nested/variant-switch"
/** The rebuilt CD player, with a switch between the 2024 tap and the new one. */
function CdPlayer() {
const reduced = useReducedMotion()
const [variant, setVariant] = useState<Variant>("now")
const [phase, setPhase] = useState<Phase>(INITIAL_PHASE)
const [press, setPress] = useState<Press>(NO_PRESS)
const root = useRef<HTMLDivElement>(null)
const { now, nowRef } = useClock(root, reduced)
const track = TRACKS[variant]
const expanded = phase.kind === "cover"
/* Warm both covers so switching versions doesn't flash an empty disc. */
useEffect(() => {
for (const each of Object.values(TRACKS)) new Image().src = each.cover
}, [])
/* Only the disc lifts on press; the open cover stays put. */
const pressLift = (at: number) => (phase.kind === "disc" ? pressAt(press, at) : 0)
/* A tap hands its press lift to the motion, so the release carries on from the raised size. */
const flip = useCallback(
(pressed = 0) => {
setPhase((current) => toggle(current, nowRef.current, variant, pressed))
setPress(NO_PRESS)
},
[nowRef, variant],
)
const pressTo = (down: boolean) => {
const at = nowRef.current
setPress({ down, at, from: pressAt(press, at) })
}
useEffect(() => {
if (!expanded) return
const onKey = (event: KeyboardEvent) => {
if (event.key === "Escape") flip()
}
window.addEventListener("keydown", onKey)
return () => window.removeEventListener("keydown", onKey)
}, [expanded, flip])
const disc = reduced ? { angle: 0, tilt: 0, lift: 0, open: expanded ? 1 : 0 } : discAt(phase, now)
const lift = reduced ? 0 : Math.max(disc.lift, pressLift(now))
return (
<div ref={root} className="flex flex-col items-center gap-8">
<button
type="button"
aria-pressed={expanded}
aria-label={`Show the cover: ${track.title} by ${track.artist}`}
onClick={() => flip(pressLift(nowRef.current))}
onPointerDown={(event) => event.button === 0 && pressTo(true)}
onPointerUp={() => pressTo(false)}
onPointerLeave={() => press.down && pressTo(false)}
onPointerCancel={() => pressTo(false)}
className="rounded-[64px] [touch-action:manipulation] [-webkit-tap-highlight-color:transparent] focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-teal max-sm:[zoom:0.9]"
>
<CdPlayerCard track={track} look={variant} angle={disc.angle} tilt={disc.tilt} lift={lift} open={disc.open} now={now} still={reduced} />
</button>
<CdPlayerVariantSwitch value={variant} onChange={setVariant} />
</div>
)
}
CdPlayer.Card = CdPlayerCard
CdPlayer.VariantSwitch = CdPlayerVariantSwitch
export { CdPlayer }