mirror of
https://github.com/narrrl/pl8list-rewind.git
synced 2026-11-19 01:25:03 +01:00
Fix the flat drop on Linux and morph its play/pause in one shape
The kick wave in the vertex shader squared a negative number with pow(), which GLSL leaves undefined. Mesa returns NaN there, so on Linux the drop's position pinned to the clamp: it stayed a plain round sphere without wobble, and its normals split it into a dark yin-yang. The squares are written out now, and the normal comes from the slope of the displacement instead of two whole positions. The play/pause icon is one shape whose corners move: each pause bar turns into half of the play triangle, with a small swing in between, instead of two icons faded over each other. It sits on a disc of frosted artwork with a lit rim, and a ring round it shows the track's progress, or runs while the track loads, which replaces the flat spinner. The surface calms down under the icon gradually instead of jumping flat, a double-tap no longer flickers the icon, and a mouse resting on the playing drop shows the pause bars faintly. Also: - Low-density desktop screens render the drop at twice the resolution. - Quality steps follow the display's own frame rate and step back up again. - A lost WebGL context brings the drop back once it is restored. - A paused drop keeps breathing at a low frame rate on desktop. - Without Web Audio (iOS) jumps in the waveform count as kicks, so the drop lands on drops there too. - Artists without an image get their own colour from their name, so their drop no longer sinks into the page. - z toggles zen mode: just the drop, bigger, and the music.
This commit is contained in:
1 parent
bcdca34244
commit
2dcca0bc94
9 files changed
+346
-110
No files matched your search
+162
-42
@@ -30,30 +30,53 @@ const SNARE_RELEASE = 0.15; // s
|
||||
// playing turns the triangle back into the bars as they fade.
|
||||
const ICON_MELT = 0.35; // s the bars show before turning into the triangle
|
||||
const ICON_FADE = 0.3; // s the bars show after playing starts
|
||||
const ICON_MORPH = 0.22; // s from one shape to the other
|
||||
const ICON_IN = 0.05; // s; fades in fast …
|
||||
const ICON_OUT = 0.12; // … and out a little slower
|
||||
const CALM = 0.25; // s for the surface to settle down under the icon, and back
|
||||
const HOVER = 0.45; // how much the bars show while a mouse rests on the playing drop
|
||||
const TEXTURE_SIZE = 512;
|
||||
const SLOW_FRAME = 0.02; // s; frames slower than this for SLOW_FOR …
|
||||
const SLOW_FOR = 2; // … seconds step the quality down
|
||||
// Quality follows how fast frames come against what this display normally does, so
|
||||
// a 30 Hz screen is not mistaken for a slow GPU.
|
||||
const SLOW_RATIO = 1.4; // a frame this much slower than usual is slow …
|
||||
const SLOW_FOR = 2; // … and this many seconds of them step the quality down
|
||||
const FAST_FOR = 10; // s of frames on time before it tries one step up again
|
||||
/// Quality steps: icosphere subdivisions and resolution scale.
|
||||
const QUALITY: [number, number][] = [[5, 1], [4, 1], [4, 0.6], [3, 0.5]];
|
||||
|
||||
const UNIFORMS = [
|
||||
"uProj", "uDist", "uTime", "uMorph", "uLevel", "uWobble", "uKick", "uKickAge", "uSnare", "uDrop", "uBass", "uGrowl",
|
||||
"uGain", "uSpike", "uStep", "uTwist", "uRipple", "uShock", "uScale", "uLift", "uJelly", "uPointer", "uRot", "uFront", "uIcon", "uIconA",
|
||||
"uGain", "uSpike", "uStep", "uTwist", "uRipple", "uShock", "uScale", "uLift", "uJelly", "uPointer", "uRot", "uFront",
|
||||
"uIcon", "uIconA", "uCalm", "uBusy", "uProgress", "uClock",
|
||||
"uArt", "uAccent", "uHue", "uIri", "uMetal", "uHeat",
|
||||
] as const;
|
||||
|
||||
const ease = (dt: number, tau: number) => 1 - Math.exp(-dt / tau);
|
||||
|
||||
/// What the page knows about playback, besides the music itself.
|
||||
export interface DropState {
|
||||
playing: boolean;
|
||||
/// 0..1 of the track played.
|
||||
progress: number;
|
||||
/// Waiting for audio.
|
||||
buffering: boolean;
|
||||
/// A mouse rests on the drop.
|
||||
hover: boolean;
|
||||
}
|
||||
|
||||
export class LiquidBlob {
|
||||
readonly canvas = document.createElement("canvas");
|
||||
/// Called once if the GPU drops the context; the page then falls back to plain art.
|
||||
/// Called if the GPU drops the context; the page shows plain art until `onRestored`.
|
||||
onLost: (() => void) | null = null;
|
||||
onRestored: (() => void) | null = null;
|
||||
/// Turning it by hand.
|
||||
readonly spin = new Spin();
|
||||
private gl: WebGL2RenderingContext;
|
||||
private u = {} as Record<(typeof UNIFORMS)[number], WebGLUniformLocation | null>;
|
||||
private vao: WebGLVertexArrayObject;
|
||||
private vao!: WebGLVertexArrayObject;
|
||||
private buffers: WebGLBuffer[] = [];
|
||||
private count = 0;
|
||||
private tex: WebGLTexture;
|
||||
private tex!: WebGLTexture;
|
||||
private quality: number;
|
||||
private look: Look | null = null;
|
||||
private accent: [number, number, number] = [1, 0.3, 0.43];
|
||||
@@ -64,15 +87,26 @@ export class LiquidBlob {
|
||||
private pointer = [0, 0, 1, 0];
|
||||
private pointerGoal = [0, 0, 1, 0];
|
||||
private slow = 0;
|
||||
private fast = 0;
|
||||
/// Usual frame time of this display, s.
|
||||
private usual = 1 / 60;
|
||||
/// Seconds on time needed before stepping up; doubles each time a step up fails.
|
||||
private fastFor = FAST_FOR;
|
||||
private steppedUp = false;
|
||||
/// Springs, position and speed: the jump (upwards) and the pump (size).
|
||||
private jump = [0, 0];
|
||||
private pump = [0, 0];
|
||||
private snare = 0;
|
||||
/// The icon: its shape (0 pause, 1 play), how much it shows, and since when the
|
||||
/// playing state is what it is. Null until the first frame of a track.
|
||||
private icon = [1, 1];
|
||||
/// playing state is what it is. `iconFor` is null until the first frame of a track.
|
||||
private iconShape = 1;
|
||||
private iconAlpha = 0;
|
||||
private iconFor: boolean | null = null;
|
||||
private iconAge = 0;
|
||||
/// Take the next playing state as it is, without showing a change.
|
||||
private settle = false;
|
||||
private busy = 0;
|
||||
private calm = 0;
|
||||
|
||||
/// Null where WebGL2 is not available.
|
||||
static create(touch: boolean): LiquidBlob | null {
|
||||
@@ -89,10 +123,31 @@ export class LiquidBlob {
|
||||
if (!gl) throw new Error("no WebGL2");
|
||||
this.gl = gl;
|
||||
this.canvas.className = "liquid-canvas";
|
||||
this.canvas.addEventListener("webglcontextlost", () => {
|
||||
this.quality = touch ? 1 : 0;
|
||||
this.canvas.addEventListener("webglcontextlost", (e) => {
|
||||
e.preventDefault(); // or it never comes back
|
||||
this.ready = false;
|
||||
this.onLost?.();
|
||||
});
|
||||
this.canvas.addEventListener("webglcontextrestored", () => {
|
||||
try {
|
||||
this.init();
|
||||
} catch (err) {
|
||||
console.warn("liquid visuals not restored:", (err as Error).message);
|
||||
return;
|
||||
}
|
||||
this.onRestored?.();
|
||||
});
|
||||
this.init();
|
||||
}
|
||||
|
||||
get lost(): boolean {
|
||||
return this.gl.isContextLost();
|
||||
}
|
||||
|
||||
/// Everything on the GPU; again after a lost context comes back.
|
||||
private init(): void {
|
||||
const gl = this.gl;
|
||||
const prog = gl.createProgram();
|
||||
gl.attachShader(prog, this.shader(gl.VERTEX_SHADER, VERTEX));
|
||||
gl.attachShader(prog, this.shader(gl.FRAGMENT_SHADER, FRAGMENT));
|
||||
@@ -102,7 +157,7 @@ export class LiquidBlob {
|
||||
gl.useProgram(prog);
|
||||
for (const name of UNIFORMS) this.u[name] = gl.getUniformLocation(prog, name);
|
||||
this.vao = gl.createVertexArray();
|
||||
this.quality = touch ? 1 : 0;
|
||||
this.buffers = [];
|
||||
this.buildMesh();
|
||||
this.tex = gl.createTexture();
|
||||
gl.bindTexture(gl.TEXTURE_2D, this.tex);
|
||||
@@ -147,16 +202,17 @@ export class LiquidBlob {
|
||||
|
||||
/// Show a new track in `slot`. Resolves once its artwork is on the GPU, which is
|
||||
/// when the slot's own image can be hidden.
|
||||
async mount(slot: HTMLElement, look: Look, art: string | null, initial: string): Promise<boolean> {
|
||||
/// `tint` colours the stand-in when there is no artwork.
|
||||
async mount(slot: HTMLElement, look: Look, art: string | null, initial: string, tint: string): Promise<boolean> {
|
||||
if (this.canvas.parentElement !== slot) slot.append(this.canvas);
|
||||
const gen = ++this.gen;
|
||||
this.look = look;
|
||||
this.ready = false;
|
||||
this.morph = 0;
|
||||
this.slow = 0;
|
||||
this.slow = this.fast = 0;
|
||||
this.spin.reset();
|
||||
this.iconFor = null;
|
||||
const source = await artwork(art, initial).catch(() => artwork(null, initial));
|
||||
const source = await artwork(art, initial, tint).catch(() => artwork(null, initial, tint));
|
||||
if (gen !== this.gen || this.gl.isContextLost()) return false;
|
||||
const gl = this.gl;
|
||||
gl.bindTexture(gl.TEXTURE_2D, this.tex);
|
||||
@@ -179,14 +235,21 @@ export class LiquidBlob {
|
||||
this.pointerGoal = [dx, dy, dz, on && r < 1.4 ? 1 : 0];
|
||||
}
|
||||
|
||||
/// The playing state just flipped and flipped back (a double-tap): let the icon
|
||||
/// go without playing out the change.
|
||||
settleIcon(): void {
|
||||
this.settle = true;
|
||||
}
|
||||
|
||||
/// Draw one frame. Returns true while it still moves without music (morphing in,
|
||||
/// settling, spinning, following the pointer).
|
||||
frame(f: Features, dt: number, playing: boolean): boolean {
|
||||
/// settling, spinning, following the pointer, showing the icon).
|
||||
frame(f: Features, dt: number, s: DropState): boolean {
|
||||
const look = this.look;
|
||||
if (!look || !this.ready) return false;
|
||||
const gl = this.gl;
|
||||
this.watch(dt);
|
||||
const scale = Math.min(devicePixelRatio || 1, this.touch ? 1 : 1.5) * QUALITY[this.quality][1];
|
||||
const { playing } = s;
|
||||
if (playing) this.watch(dt);
|
||||
const scale = this.density() * QUALITY[this.quality][1];
|
||||
const w = Math.round(this.canvas.clientWidth * scale), h = Math.round(this.canvas.clientHeight * scale);
|
||||
if (!w || !h) return false;
|
||||
if (this.canvas.width !== w || this.canvas.height !== h) {
|
||||
@@ -202,11 +265,12 @@ export class LiquidBlob {
|
||||
const k = 1 - Math.exp(-dt / 0.15);
|
||||
for (let i = 0; i < 4; i++) this.pointer[i] += (this.pointerGoal[i] - this.pointer[i]) * k;
|
||||
const spinning = this.spin.step(dt);
|
||||
const showing = this.showIcon(playing, dt);
|
||||
const showing = this.showIcon(s, dt);
|
||||
const moving = this.bounce(f, dt, look);
|
||||
|
||||
const u = this.u;
|
||||
gl.uniform1f(u.uTime, this.time);
|
||||
gl.uniform1f(u.uClock, (performance.now() / 1000) % 1000);
|
||||
gl.uniform1f(u.uMorph, morph);
|
||||
gl.uniform1f(u.uLevel, (f.sub + f.low) / 2);
|
||||
gl.uniform1f(u.uWobble, f.wobble);
|
||||
@@ -215,7 +279,7 @@ export class LiquidBlob {
|
||||
gl.uniform1f(u.uDrop, f.drop);
|
||||
gl.uniform1f(u.uBass, f.bass * look.throb);
|
||||
gl.uniform1f(u.uGrowl, f.growl);
|
||||
this.snare += (f.snare - this.snare) * (1 - Math.exp(-dt / (f.snare > this.snare ? SNARE_ATTACK : SNARE_RELEASE)));
|
||||
this.snare += (f.snare - this.snare) * ease(dt, f.snare > this.snare ? SNARE_ATTACK : SNARE_RELEASE);
|
||||
gl.uniform1f(u.uSnare, this.snare);
|
||||
gl.uniform1f(u.uGain, look.gain);
|
||||
gl.uniform1f(u.uSpike, look.spike);
|
||||
@@ -240,6 +304,15 @@ export class LiquidBlob {
|
||||
return this.morph < 1 || moving || spinning || showing || this.pointer[3] > 0.01 || this.pointerGoal[3] > 0;
|
||||
}
|
||||
|
||||
/// Canvas pixels per CSS pixel at full quality. Phones stay at 1 for the battery.
|
||||
/// Low-density desktop screens render at 2 and scale down, which smooths the
|
||||
/// edges and the icon that would otherwise fall on too few pixels.
|
||||
private density(): number {
|
||||
const dpr = devicePixelRatio || 1;
|
||||
if (this.touch) return Math.min(dpr, 1);
|
||||
return dpr < 1.5 ? 2 : 1.5;
|
||||
}
|
||||
|
||||
/// Step the springs and set the jump uniforms. True while they still move.
|
||||
private bounce(f: Features, dt: number, look: Look): boolean {
|
||||
const [jump, pump] = [this.jump, this.pump];
|
||||
@@ -270,41 +343,84 @@ export class LiquidBlob {
|
||||
return Math.abs(jump[0]) + Math.abs(jump[1]) + Math.abs(pump[1]) > 0.002;
|
||||
}
|
||||
|
||||
/// Step the play/pause icon and set its uniforms. True while it still moves.
|
||||
private showIcon(playing: boolean, dt: number): boolean {
|
||||
const icon = this.icon;
|
||||
/// Step the play/pause icon, the loading ring and the calm under them, and set
|
||||
/// their uniforms. True while any of them still moves.
|
||||
private showIcon(s: DropState, dt: number): boolean {
|
||||
const { playing } = s;
|
||||
if (this.iconFor === null) {
|
||||
// A new track: no pop, just what holds now.
|
||||
icon[0] = icon[1] = playing ? 0 : 1;
|
||||
this.iconShape = playing ? 0 : 1;
|
||||
this.iconAlpha = this.calm = playing ? 0 : 1;
|
||||
this.iconAge = 1;
|
||||
} else if (this.iconFor !== playing) {
|
||||
this.iconAge = 0;
|
||||
if (!playing) icon[0] = 0;
|
||||
icon[1] = 1;
|
||||
// Pausing starts from the bars; jump to them only while nothing shows.
|
||||
if (!playing && this.iconAlpha < 0.2) this.iconShape = 0;
|
||||
}
|
||||
if (this.settle) {
|
||||
this.iconAge = ICON_MELT + ICON_FADE;
|
||||
this.settle = false;
|
||||
}
|
||||
this.iconFor = playing;
|
||||
this.iconAge += dt;
|
||||
const goal = playing ? [0, this.iconAge < ICON_FADE ? 1 : 0] : [this.iconAge < ICON_MELT ? 0 : 1, 1];
|
||||
const k = 1 - Math.exp(-dt / 0.1);
|
||||
for (let i = 0; i < 2; i++) icon[i] += (goal[i] - icon[i]) * k;
|
||||
this.gl.uniform1f(this.u.uIcon, icon[0]);
|
||||
this.gl.uniform1f(this.u.uIconA, icon[1]);
|
||||
return Math.abs(goal[0] - icon[0]) + Math.abs(goal[1] - icon[1]) > 0.005 || this.iconAge < ICON_MELT;
|
||||
const [shape, alpha] = !playing
|
||||
? [this.iconAge < ICON_MELT ? 0 : 1, 1]
|
||||
: [0, this.iconAge < ICON_FADE ? 1 : s.hover ? HOVER : 0];
|
||||
// The shape moves at a steady pace; the shader eases it.
|
||||
const step = dt / ICON_MORPH;
|
||||
this.iconShape += Math.max(-step, Math.min(step, shape - this.iconShape));
|
||||
this.iconAlpha += (alpha - this.iconAlpha) * ease(dt, alpha > this.iconAlpha ? ICON_IN : ICON_OUT);
|
||||
const busy = s.buffering ? 1 : 0;
|
||||
this.busy += (busy - this.busy) * ease(dt, 0.15);
|
||||
const calm = Math.max(alpha, busy);
|
||||
this.calm += (calm - this.calm) * ease(dt, CALM);
|
||||
|
||||
const u = this.u;
|
||||
this.gl.uniform1f(u.uIcon, this.iconShape);
|
||||
this.gl.uniform1f(u.uIconA, this.iconAlpha);
|
||||
this.gl.uniform1f(u.uBusy, this.busy);
|
||||
this.gl.uniform1f(u.uCalm, this.calm);
|
||||
this.gl.uniform1f(u.uProgress, s.progress);
|
||||
return (
|
||||
s.buffering ||
|
||||
this.iconAge < ICON_MELT ||
|
||||
Math.abs(shape - this.iconShape) > 0.001 ||
|
||||
Math.abs(alpha - this.iconAlpha) > 0.005 ||
|
||||
Math.abs(calm - this.calm) > 0.005 ||
|
||||
this.busy > 0.005
|
||||
);
|
||||
}
|
||||
|
||||
/// Step the quality down when frames keep coming in slow.
|
||||
/// Step the quality down when frames keep coming in slow, and back up once they
|
||||
/// have been on time for a while.
|
||||
private watch(dt: number): void {
|
||||
this.slow = dt > SLOW_FRAME ? this.slow + dt : Math.max(0, this.slow - dt);
|
||||
if (this.slow < SLOW_FOR || this.quality >= QUALITY.length - 1) return;
|
||||
this.slow = 0;
|
||||
// The usual frame time drops quickly to what the display does and creeps up only slowly.
|
||||
this.usual = Math.min(1 / 24, this.usual + (dt - this.usual) * (dt < this.usual ? 0.2 : 0.004));
|
||||
const late = dt > this.usual * SLOW_RATIO + 0.002;
|
||||
this.slow = late ? this.slow + dt : Math.max(0, this.slow - dt);
|
||||
this.fast = late ? 0 : this.fast + dt;
|
||||
const best = this.touch ? 1 : 0;
|
||||
if (this.slow >= SLOW_FOR && this.quality < QUALITY.length - 1) {
|
||||
// Stepping up did not hold: wait longer before the next try.
|
||||
if (this.steppedUp) this.fastFor *= 2;
|
||||
this.steppedUp = false;
|
||||
this.setQuality(this.quality + 1);
|
||||
} else if (this.fast >= this.fastFor && this.quality > best) {
|
||||
this.steppedUp = true;
|
||||
this.setQuality(this.quality - 1);
|
||||
}
|
||||
}
|
||||
|
||||
private setQuality(q: number): void {
|
||||
this.slow = this.fast = 0;
|
||||
const before = QUALITY[this.quality][0];
|
||||
this.quality++;
|
||||
if (QUALITY[this.quality][0] !== before) this.buildMesh();
|
||||
this.quality = q;
|
||||
if (QUALITY[q][0] !== before) this.buildMesh();
|
||||
}
|
||||
}
|
||||
|
||||
/// The artwork cropped square, or a gradient with the artist's initial.
|
||||
async function artwork(src: string | null, initial: string): Promise<HTMLCanvasElement> {
|
||||
/// The artwork cropped square, or a gradient in `tint` with the artist's initial.
|
||||
async function artwork(src: string | null, initial: string, tint: string): Promise<HTMLCanvasElement> {
|
||||
const c = document.createElement("canvas");
|
||||
c.width = c.height = TEXTURE_SIZE;
|
||||
const ctx = c.getContext("2d")!;
|
||||
@@ -315,12 +431,16 @@ async function artwork(src: string | null, initial: string): Promise<HTMLCanvasE
|
||||
const side = Math.min(img.naturalWidth, img.naturalHeight);
|
||||
ctx.drawImage(img, (img.naturalWidth - side) / 2, (img.naturalHeight - side) / 2, side, side, 0, 0, TEXTURE_SIZE, TEXTURE_SIZE);
|
||||
} else {
|
||||
// Dark enough that the initial and the rim light read, light enough that the
|
||||
// drop does not sink into the page.
|
||||
// `tint` is `hsl(h s% l%)` from color.ts; only its lightness changes.
|
||||
const shade = (l: number) => tint.replace(/[\d.]+%\s*\)$/, `${l}%)`);
|
||||
const g = ctx.createLinearGradient(0, 0, TEXTURE_SIZE, TEXTURE_SIZE);
|
||||
g.addColorStop(0, "#2a2a2e");
|
||||
g.addColorStop(1, "#111");
|
||||
g.addColorStop(0, shade(42));
|
||||
g.addColorStop(1, shade(14));
|
||||
ctx.fillStyle = g;
|
||||
ctx.fillRect(0, 0, TEXTURE_SIZE, TEXTURE_SIZE);
|
||||
ctx.fillStyle = "#ffffff66";
|
||||
ctx.fillStyle = "#ffffffcc";
|
||||
ctx.font = `700 ${TEXTURE_SIZE * 0.4}px system-ui, sans-serif`;
|
||||
ctx.textAlign = "center";
|
||||
ctx.textBaseline = "middle";
|
||||
|
||||
Reference in new issue
Block a user