accesscodes/src/components/donate-glow.tsx
anonymousratwastaken 372d34cc62 Replace donate page wave background with glow
- Add DonateGlow WebGL gradient that shifts colour per donation intent and rank
- Drive donate steps from URL query params so browser back/forward work
- Clear pending checkout session when an answer changes
- Remove the unused wave-pattern canvas background
2026-10-24 10:49:44 +01:00

349 lines
10 KiB
TypeScript

"use client";
import { useEffect, useRef, useState } from "react";
import { Mesh, Program, Renderer, Triangle } from "ogl";
import { cn } from "@/lib/utils";
// Soft gradient glow behind the donate page. It rises from the bottom edge,
// drifts slowly, leans towards the cursor, and fades to a new colour whenever
// `theme` changes. Grain matches the home page's GradientShimmer.
type Color = [number, number, number];
const hex = (value: string): Color => [
parseInt(value.slice(1, 3), 16) / 255,
parseInt(value.slice(3, 5), 16) / 255,
parseInt(value.slice(5, 7), 16) / 255,
];
// Each theme is a two-colour gradient: `a` is the deeper tone, `b` the lighter.
export const GLOW_THEMES = {
green: { a: hex("#2f8a1a"), b: hex("#78c23a") },
lime: { a: hex("#3fd10a"), b: hex("#a6ff3c") },
teal: { a: hex("#0d9488"), b: hex("#5eead4") },
purple: { a: hex("#6d3df0"), b: hex("#4f8bff") },
blue: { a: hex("#2563eb"), b: hex("#38bdf8") },
iron: { a: hex("#64748b"), b: hex("#cbd5e1") },
gold: { a: hex("#f59e0b"), b: hex("#ffe14d") },
diamond: { a: hex("#06b6d4"), b: hex("#67e8f9") },
} as const;
export type GlowTheme = keyof typeof GLOW_THEMES;
const VERTEX_SHADER = `#version 300 es
in vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}
`;
const FRAGMENT_SHADER = `#version 300 es
precision highp float;
uniform vec2 u_size;
uniform float u_time;
uniform vec3 u_a;
uniform vec3 u_b;
uniform vec2 u_mouse; // 0..1, y up
uniform float u_hover;
uniform float u_boost;
out vec4 fragment_color;
float hash(vec2 p) {
return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}
vec3 overlayBlend(vec3 base, vec3 blend) {
vec3 low = 2.0 * base * blend;
vec3 high = 1.0 - 2.0 * (1.0 - base) * (1.0 - blend);
return mix(low, high, step(vec3(0.5), base));
}
float blob(vec2 q, vec2 centre, vec2 radius) {
vec2 d = (q - centre) / radius;
return exp(-dot(d, d));
}
void main() {
vec2 uv = gl_FragCoord.xy / u_size;
float aspect = u_size.x / u_size.y;
vec2 q = vec2(uv.x * aspect, uv.y);
float t = u_time;
// Blobs rise from the bottom edge and the upper corners so the middle,
// where the content sits, stays calm.
float w_a = 0.0;
float w_b = 0.0;
w_a += 0.75 * blob(q, vec2(0.24 * aspect + 0.08 * sin(t * 0.23), -0.04 + 0.07 * sin(t * 0.31 + 1.0)), vec2(0.55 * aspect * 0.6, 0.52));
w_b += 0.75 * blob(q, vec2(0.76 * aspect + 0.09 * sin(t * 0.19 + 2.0), -0.06 + 0.06 * sin(t * 0.27)), vec2(0.55 * aspect * 0.6, 0.5));
w_a += 0.5 * blob(q, vec2(0.5 * aspect + 0.16 * sin(t * 0.15), 0.0 + 0.05 * sin(t * 0.21 + 0.5)), vec2(0.5 * aspect * 0.6, 0.34));
w_b += 0.32 * blob(q, vec2(0.96 * aspect + 0.05 * sin(t * 0.17 + 4.0), 1.0 + 0.05 * sin(t * 0.25)), vec2(0.4 * aspect * 0.6, 0.42));
w_a += 0.26 * blob(q, vec2(0.02 * aspect + 0.04 * sin(t * 0.2 + 3.0), 1.02), vec2(0.35 * aspect * 0.6, 0.38));
// A light that follows the cursor.
vec2 mouse = vec2(u_mouse.x * aspect, u_mouse.y);
w_b += blob(q, mouse, vec2(0.3, 0.3)) * u_hover * 0.3;
float total = (w_a + w_b) * (1.0 + 0.35 * u_boost);
vec3 color = (u_a * w_a + u_b * w_b) / max(w_a + w_b, 0.0001);
float alpha = 1.0 - exp(-total * 0.95);
alpha = min(alpha, 0.9) * 0.9;
// Film grain on CSS pixels, same recipe as the home page.
vec2 cell = floor(gl_FragCoord.xy);
vec3 shift = vec3(hash(cell + 17.0), hash(cell + 29.0), hash(cell + 41.0)) - 0.5;
float luminance = 0.565 + (hash(cell) - 0.5) * 0.376;
vec3 grain = clamp(vec3(luminance) + shift * 0.125, 0.0, 1.0);
color = mix(color, overlayBlend(color, grain), 0.35);
fragment_color = vec4(color * alpha, alpha);
}
`;
const lerp = (from: number, to: number, amount: number) => from + (to - from) * amount;
export function DonateGlow({ theme, className }: { theme: GlowTheme; className?: string }) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const themeRef = useRef<GlowTheme>(theme);
const themeChangedRef = useRef<() => void>(() => {});
const [failed, setFailed] = useState(false);
const [visible, setVisible] = useState(false);
useEffect(() => {
themeRef.current = theme;
themeChangedRef.current();
}, [theme]);
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) {
return;
}
let renderer: Renderer;
try {
renderer = new Renderer({
canvas,
alpha: true,
antialias: false,
depth: false,
premultipliedAlpha: true,
dpr: 1,
webgl: 2,
});
} catch {
// eslint-disable-next-line react-hooks/set-state-in-effect
setFailed(true);
return;
}
const { gl } = renderer;
if (!gl || !renderer.isWebgl2) {
setFailed(true);
return;
}
gl.clearColor(0, 0, 0, 0);
const start = GLOW_THEMES[themeRef.current];
const colorA: Color = [...start.a];
const colorB: Color = [...start.b];
const uniforms = {
u_size: { value: [1, 1] },
u_time: { value: 0 },
u_a: { value: colorA },
u_b: { value: colorB },
u_mouse: { value: [0.5, 0.5] },
u_hover: { value: 0 },
u_boost: { value: 0 },
};
const geometry = new Triangle(gl);
const program = new Program(gl, {
vertex: VERTEX_SHADER,
fragment: FRAGMENT_SHADER,
uniforms,
cullFace: false,
depthTest: false,
depthWrite: false,
});
const mesh = new Mesh(gl, { geometry, program, frustumCulled: false });
const reducedMotionQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
let time = Math.random() * 30;
let lastFrame: number | null = null;
let animationFrame: number | null = null;
// A small swell when the theme changes: the target eases in and decays,
// and the value follows it, so there is never a sudden jump.
let boost = 0;
let boostTarget = 0;
let hoverTarget = 0;
const target = { x: 0.5, y: 0.5 };
const current = { x: 0.5, y: 0.5 };
let seenPointer = false;
const render = () => {
uniforms.u_time.value = time;
uniforms.u_boost.value = boost;
renderer.render({ scene: mesh, clear: true, update: false, sort: false, frustumCull: false });
};
const resize = () => {
const rect = canvas.getBoundingClientRect();
const width = Math.max(1, rect.width);
const height = Math.max(1, rect.height);
renderer.setSize(width, height);
canvas.style.width = "";
canvas.style.height = "";
uniforms.u_size.value = [width, height];
};
const frame = (now: number) => {
animationFrame = null;
const dt = lastFrame === null ? 0 : Math.min((now - lastFrame) / 1000, 0.064);
lastFrame = now;
time += dt;
const goal = GLOW_THEMES[themeRef.current];
const fade = 1 - Math.exp(-dt * 3);
for (let i = 0; i < 3; i++) {
colorA[i] = lerp(colorA[i]!, goal.a[i]!, fade);
colorB[i] = lerp(colorB[i]!, goal.b[i]!, fade);
}
const follow = 1 - Math.exp(-dt * 4);
current.x = lerp(current.x, target.x, follow);
current.y = lerp(current.y, target.y, follow);
uniforms.u_mouse.value = [current.x, current.y];
uniforms.u_hover.value = lerp(uniforms.u_hover.value, hoverTarget, 1 - Math.exp(-dt * 3));
boost = lerp(boost, boostTarget, 1 - Math.exp(-dt * 5));
boostTarget *= Math.exp(-dt * 1.4);
render();
schedule();
};
const schedule = () => {
if (animationFrame !== null || reducedMotionQuery.matches) {
return;
}
animationFrame = requestAnimationFrame(frame);
};
// Reduced motion: no loop, so snap to the colour and draw one still frame.
const drawStill = () => {
const goal = GLOW_THEMES[themeRef.current];
for (let i = 0; i < 3; i++) {
colorA[i] = goal.a[i]!;
colorB[i] = goal.b[i]!;
}
boost = 0;
boostTarget = 0;
uniforms.u_hover.value = 0;
render();
};
themeChangedRef.current = () => {
if (reducedMotionQuery.matches) {
drawStill();
return;
}
boostTarget = 1;
};
const onPointerMove = (event: PointerEvent) => {
if (reducedMotionQuery.matches || event.pointerType === "touch") {
return;
}
target.x = event.clientX / window.innerWidth;
target.y = 1 - event.clientY / window.innerHeight;
if (!seenPointer) {
seenPointer = true;
current.x = target.x;
current.y = target.y;
}
hoverTarget = 1;
};
const onPointerLeave = () => {
hoverTarget = 0;
};
const onMotionChange = () => {
lastFrame = null;
if (reducedMotionQuery.matches) {
if (animationFrame !== null) {
cancelAnimationFrame(animationFrame);
animationFrame = null;
}
drawStill();
return;
}
schedule();
};
resize();
render();
requestAnimationFrame(() => setVisible(true));
schedule();
const resizeObserver = new ResizeObserver(() => {
resize();
render();
});
resizeObserver.observe(canvas);
window.addEventListener("pointermove", onPointerMove, { passive: true });
document.documentElement.addEventListener("pointerleave", onPointerLeave);
reducedMotionQuery.addEventListener("change", onMotionChange);
return () => {
themeChangedRef.current = () => {};
setVisible(false);
if (animationFrame !== null) {
cancelAnimationFrame(animationFrame);
}
resizeObserver.disconnect();
window.removeEventListener("pointermove", onPointerMove);
document.documentElement.removeEventListener("pointerleave", onPointerLeave);
reducedMotionQuery.removeEventListener("change", onMotionChange);
geometry.remove();
program.remove();
};
}, []);
const [a, b] = [GLOW_THEMES[theme].a, GLOW_THEMES[theme].b].map(
(c) => `rgb(${c.map((v) => Math.round(v * 255)).join(" ")})`,
);
// Sits behind the page content: the parent needs its own stacking context
// (relative z-0) so -z-10 paints above its background but below its children.
return (
<div
aria-hidden="true"
className={cn("pointer-events-none fixed inset-0 -z-10 overflow-hidden", className)}
style={failed ? { background: `radial-gradient(ellipse 80% 50% at 50% 110%, ${a}, transparent), radial-gradient(ellipse 50% 40% at 100% 0%, color-mix(in srgb, ${b} 20%, transparent), transparent)` } : undefined}
>
<canvas
ref={canvasRef}
className={cn("block size-full transition-opacity duration-700", visible ? "opacity-100" : "opacity-0", failed && "hidden")}
/>
</div>
);
}