diff --git a/bun.lock b/bun.lock index 6aae881..e0add36 100644 --- a/bun.lock +++ b/bun.lock @@ -22,6 +22,7 @@ "motion": "^12.43.0", "next": "16.0.10", "next-themes": "^0.4.6", + "ogl": "^1.0.11", "qrcode": "^1.5.4", "radix-ui": "^1.6.7", "react": "19.2.1", @@ -1229,6 +1230,8 @@ "object.values": ["object.values@1.2.1", "", { "dependencies": { "call-bind": "^1.0.8", "call-bound": "^1.0.3", "define-properties": "^1.2.1", "es-object-atoms": "^1.0.0" } }, "sha512-gXah6aZrcUxjWg2zR2MwouP2eHlCBzdV4pygudehaKXSGW4v2AsRQUK+lwwXhii6KFZcunEnmSUoYp5CXibxtA=="], + "ogl": ["ogl@1.0.11", "", {}, "sha512-kUpC154AFfxi16pmZUK4jk3J+8zxwTWGPo03EoYA8QPbzikHoaC82n6pNTbd+oEaJonaE8aPWBlX7ad9zrqLsA=="], + "on-finished": ["on-finished@2.4.1", "", { "dependencies": { "ee-first": "1.1.1" } }, "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg=="], "once": ["once@1.4.0", "", { "dependencies": { "wrappy": "1" } }, "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w=="], diff --git a/package.json b/package.json index 21bf000..fdad4cd 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "motion": "^12.43.0", "next": "16.0.10", "next-themes": "^0.4.6", + "ogl": "^1.0.11", "qrcode": "^1.5.4", "radix-ui": "^1.6.7", "react": "19.2.1", diff --git a/src/app/globals.css b/src/app/globals.css index 7ec6c58..590980d 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -149,3 +149,48 @@ @apply bg-background text-foreground; } } + +/* GradientShimmer (src/components/gradient-shimmer.tsx). Colors must be hex: + the renderer parses them through a 2D canvas context. */ +.gradient-shimmer { + --shimmer-alpha: 0.35; + --shimmer-grain-alpha: 0.4; + --shimmer-grain-luminance: 144; + --shimmer-grain-contrast: 96; + --shimmer-grain-saturation: 32; + --shimmer-intro-alpha: 0.6; + --shimmer-speed-up-shine-boost: 0.15; + --shimmer-start: #131316; + --shimmer-highlight: #5d8a2e; + + display: block; + z-index: 0; + width: 100%; + height: 100%; + opacity: 0; + pointer-events: none; + image-rendering: pixelated; + transition: opacity 0.3s; +} + +.gradient-shimmer.visible { + opacity: 1; +} + +.gradient-shimmer.fallback { + background: var(--shimmer-start); +} + +.gradient-shimmer.background { + position: fixed; + top: 0; + left: 0; + width: 100vw; + height: 100vh; +} + +@media (prefers-reduced-motion: reduce) { + .gradient-shimmer { + transition-duration: 1ms; + } +} diff --git a/src/components/gradient-shimmer.tsx b/src/components/gradient-shimmer.tsx new file mode 100644 index 0000000..78a3107 --- /dev/null +++ b/src/components/gradient-shimmer.tsx @@ -0,0 +1,276 @@ +"use client"; + +// React port of GradientShimmer.svelte from helium-prism (https://github.com/imputnet/helium-prism), +// licensed under GPL-3.0. This file is part of the FRG web app and is licensed under GPL-3.0 (see LICENSE). + +import { useEffect, useImperativeHandle, useRef, useState, type Ref } from "react"; +import * as Shimmer from "@/lib/gradient-shimmer"; +import { cn } from "@/lib/utils"; + +export type GradientShimmerHandle = { + intro: () => void; + emphasize: () => void; +}; + +type GradientShimmerProps = { + intro?: boolean; + background?: boolean; + className?: string; + ref?: Ref; +}; + +export function GradientShimmer({ + intro: shouldIntro, + background, + className, + ref, +}: GradientShimmerProps) { + const canvasRef = useRef(null); + const controllerRef = useRef(null); + const [hasStaticFallback, setHasStaticFallback] = useState(false); + const [visible, setVisible] = useState(false); + + useImperativeHandle(ref, () => ({ + intro: () => controllerRef.current?.intro(), + emphasize: () => controllerRef.current?.emphasize(), + }), []); + + useEffect(() => { + const canvas = canvasRef.current; + + if (!canvas) { + return; + } + + const renderer = Shimmer.createGradientShimmerRenderer(canvas); + + if (!renderer) { + // One-off sync from a failed WebGL probe; there is no later render to wait for. + // eslint-disable-next-line react-hooks/set-state-in-effect + setHasStaticFallback(true); + setVisible(true); + return; + } + + const colorSchemeQuery = window.matchMedia( + "(prefers-color-scheme: dark)", + ); + const reducedMotionQuery = window.matchMedia( + "(prefers-reduced-motion: reduce)", + ); + + let animationFrame: number | null = null; + let resizeFrame: number | null = null; + let introAnimation: Shimmer.IntroAnimation | null = null; + let speedUpAnimation: Shimmer.WaveSpeedUpAnimation | null = null; + let wavePhase = Shimmer.getRandomWavePhase(); + let secondaryWavePhase = -wavePhase * 0.7; + let lastFrameTime: number | null = null; + let onScreen = true; + + const cancelResizeFrame = () => { + if (resizeFrame === null) { + return; + } + + cancelAnimationFrame(resizeFrame); + resizeFrame = null; + }; + + const cancelAnimation = () => { + if (animationFrame === null) { + return; + } + + cancelAnimationFrame(animationFrame); + animationFrame = null; + }; + + const drawFrame = (time: number) => { + const reducedMotion = reducedMotionQuery.matches; + const deltaSeconds = reducedMotion || lastFrameTime === null + ? 0 + : Math.min((time - lastFrameTime) / 1000, 0.064); + const speedUpState = Shimmer.updateSpeedUpAnimation( + time, + speedUpAnimation, + ); + const activeIntroAnimation = reducedMotion ? null : introAnimation; + + lastFrameTime = reducedMotion ? null : time; + speedUpAnimation = reducedMotion ? null : speedUpState.animation; + + if (!reducedMotion) { + wavePhase += Shimmer.IDLE_WAVE.speed + * speedUpState.multiplier + * deltaSeconds; + secondaryWavePhase += Shimmer.IDLE_WAVE.secondarySpeed + * speedUpState.multiplier + * deltaSeconds; + } + + renderer.render({ + time, + wavePhase, + secondaryWavePhase, + introStartedAt: activeIntroAnimation?.startedAt ?? null, + shineProgress: reducedMotion ? 0 : speedUpState.shineProgress, + }); + + if ( + Shimmer.isIntroComplete( + time, + introAnimation, + renderer.getStripeCount(), + ) + ) { + introAnimation = null; + } + }; + + const startIntro = () => { + if (reducedMotionQuery.matches) { + return; + } + + introAnimation = { startedAt: performance.now() }; + }; + + const startWaveSpeedUp = () => { + if (reducedMotionQuery.matches) { + return; + } + + const time = performance.now(); + + speedUpAnimation = Shimmer.triggerSpeedUpAnimation( + time, + speedUpAnimation, + ); + }; + + const controller: Shimmer.GradientShimmerControls = { + intro: startIntro, + emphasize: startWaveSpeedUp, + }; + + const applyResize = () => { + renderer.resize(); + renderer.readColors(); + }; + + const scheduleResize = () => { + cancelResizeFrame(); + resizeFrame = requestAnimationFrame((time) => { + applyResize(); + drawFrame(time); + }); + }; + + const draw = (time: number) => { + animationFrame = null; + drawFrame(time); + requestAnimation(); + }; + + const requestAnimation = () => { + if (animationFrame !== null || reducedMotionQuery.matches || + !onScreen) { + return; + } + + animationFrame = requestAnimationFrame(draw); + }; + + const resumeAnimation = () => { + lastFrameTime = null; + requestAnimation(); + }; + + applyResize(); + + if (shouldIntro !== false) { + startIntro(); + } + + // Delay the reveal until after the first WebGL frame has been painted. + drawFrame(performance.now()); + requestAnimationFrame(() => { + setVisible(true); + }); + controllerRef.current = controller; + + const resizeObserver = new ResizeObserver(scheduleResize); + resizeObserver.observe(canvas); + + const scheduleColorRead = () => { + cancelResizeFrame(); + resizeFrame = requestAnimationFrame((time) => { + renderer.readColors(); + drawFrame(time); + }); + }; + + const intersectionObserver = new IntersectionObserver(([entry]) => { + onScreen = entry?.isIntersecting ?? true; + + if (!onScreen) { + cancelAnimation(); + return; + } + + resumeAnimation(); + }); + intersectionObserver.observe(canvas); + + const themeObserver = new MutationObserver(scheduleColorRead); + themeObserver.observe(document.documentElement, { attributes: true }); + + const updateMotionPreference = () => { + if (reducedMotionQuery.matches) { + introAnimation = null; + speedUpAnimation = null; + cancelAnimation(); + drawFrame(performance.now()); + return; + } + + resumeAnimation(); + }; + + colorSchemeQuery.addEventListener("change", scheduleColorRead); + reducedMotionQuery.addEventListener("change", updateMotionPreference); + + updateMotionPreference(); + + return () => { + if (controllerRef.current === controller) { + controllerRef.current = null; + } + + setVisible(false); + cancelAnimation(); + cancelResizeFrame(); + resizeObserver.disconnect(); + intersectionObserver.disconnect(); + themeObserver.disconnect(); + colorSchemeQuery.removeEventListener("change", scheduleColorRead); + reducedMotionQuery.removeEventListener("change", updateMotionPreference); + renderer.dispose(); + }; + }, [shouldIntro]); + + return ( +