About
Experience fluid optical physics in motion—an interactive 3D liquid glass progress loader with real-time RGB refraction, organic wave dynamics, and elastic spring controls.
Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Loading Screen Glass to your project and fits it to your theme.
Code
How Loading Screen Glass is used: the demo's code. The component's own source comes with a free account.
Usage.tsx
import React, { useEffect, useRef } from 'react';
export interface LiquidGlassLoaderProps {
/** Durasi animasi loading dalam milidetik (default: 7000) */
duration?: number;
/** Tampilkan background penuh satu layar (default: true) */
fullScreen?: boolean;
/** Callback saat progress mencapai 100% */
onComplete?: () => void;
/** Callback saat teks 100% menghilang (1 detik setelah selesai) dan ikon selesai menyala */
onFinishFade?: () => void;
/** Class tambahan untuk wrapper luar */
className?: string;
}
export const LiquidGlassLoader: React.FC<LiquidGlassLoaderProps> = ({
duration = 7000,
fullScreen = true,
onComplete,
onFinishFade,
className = '',
}) => {
const trackRowRef = useRef<HTMLDivElement>(null);
const trackWrapperRef = useRef<HTMLDivElement>(null);
const barCanvasRef = useRef<HTMLCanvasElement>(null);
const sandCanvasRef = useRef<HTMLCanvasElement>(null);
const refractCanvasRef = useRef<HTMLCanvasElement>(null);
const glass4kCanvasRef = useRef<HTMLCanvasElement>(null);
const capsuleRef = useRef<HTMLDivElement>(null);
const underglowRef = useRef<HTMLDivElement>(null);
const percentTextRef = useRef<HTMLSpanElement>(null);
const reflectLeftRef = useRef<HTMLSpanElement>(null);
const reflectRightRef = useRef<HTMLSpanElement>(null);
const reflectIconLeftRef = useRef<HTMLSpanElement>(null);
const reflectIconRightRef = useRef<HTMLSpanElement>(null);
const iconLeftRef = useRef<HTMLDivElement>(null);
const iconRightRef = useRef<HTMLDivElement>(null);
const btnRef = useRef<HTMLButtonElement>(null);
const rippleRef = useRef<HTMLSpanElement>(null);
const iconReplayRef = useRef<SVGSVGElement>(null);
const callbacksRef = useRef({ onComplete, onFinishFade });
callbacksRef.current = { onComplete, onFinishFade };
useEffect(() => {
let rafId: number | null = null;
let startTime: number | null = null;
let lastT: number | null = null;
let done = false;
let lastPct = -1;
let completeGlowActive = false;
let hide100Timer: number | null = null;
let trackW = 420;
let trackH = 12;
let dpr = Math.min(3, Math.max(1, window.devicePixelRatio || 1));
const ICON_W = 24;
const ICON_GAP = 8;
const startCapX = -(ICON_GAP + ICON_W / 2); // -20px (tepat di tengah ikon download kiri)
const getEndCapX = () => trackW + ICON_GAP + ICON_W / 2; // trackW + 20px (tepat di tengah ikon complete kanan)
const syncBar = () => {
if (trackWrapperRef.current) {
const r = trackWrapperRef.current.getBoundingClientRect();
trackW = Math.max(1, Math.round(r.width || 420));
trackH = Math.max(1, Math.round(r.height || 12));
}
dpr = Math.min(3, Math.max(1, window.devicePixelRatio || 1));
const bc = barCanvasRef.current;
if (bc) {
const w = Math.round(trackW * dpr);
const h = Math.round(trackH * dpr);
if (bc.width !== w || bc.height !== h) {
bc.width = w;
bc.height = h;
}
}
};
const septic = (u: number) => {
if (u <= 0) return 0;
if (u >= 1) return 1;
const u2 = u * u;
const u4 = u2 * u2;
return u4 * (35 - 84 * u + 70 * u2 - 20 * u2 * u);
};
const quintic = (t: number) => {
if (t <= 0) return 0;
if (t >= 1) return 1;
return t * t * t * (t * (t * 6 - 15) + 10);
};
// --- 4x Supersampled Internal Sand Suction ---
const SAND_W = 336;
const SAND_H = 136;
const GRAINS = 125;
interface Grain {
progress: number;
speed: number;
lane: number;
radius: number;
phase: number;
wob: number;
alpha: number;
}
let grains: Grain[] = [];
const SS = Math.min(12, Math.max(6, Math.ceil((window.devicePixelRatio || 1) * 4)));
const setupSand = () => {
const sc = sandCanvasRef.current;
if (sc) {
sc.width = 84 * SS;
sc.height = 34 * SS;
}
};
const mkGrain = (p?: number): Grain => ({
progress: p !== undefined ? p : Math.random(),
speed: 0.34 + Math.random() * 0.42,
lane: (Math.random() - 0.5) * 2,
radius: 1.4 + Math.random() * 3.1,
phase: Math.random() * Math.PI * 2,
wob: 1.5 + Math.random() * 3.2,
alpha: 0.55 + Math.random() * 0.45,
});
const resetGrains = () => {
grains = [];
for (let i = 0; i < GRAINS; i++) grains.push(mkGrain(i / GRAINS));
};
const renderSand = (dt: number, time: number, ratio: number, pct: number) => {
const scv = sandCanvasRef.current;
if (!scv) return;
const sctx = scv.getContext('2d');
if (!sctx) return;
sctx.setTransform(1, 0, 0, 1, 0, 0);
sctx.clearRect(0, 0, scv.width, scv.height);
sctx.setTransform(scv.width / SAND_W, 0, 0, scv.height / SAND_H, 0, 0);
if (pct >= 100 || ratio >= 1) {
scv.style.opacity = '0';
return;
}
const endFade = quintic(pct >= 95 ? Math.max(0, 1 - (pct - 95) / 5) : 1);
scv.style.opacity = endFade.toFixed(3);
if (endFade <= 0.001) return;
const energy = endFade * (pct < 2 ? 0.45 + 0.55 * (pct / 2) : 1);
const rim = SAND_W - 14;
const sink = SAND_W * 0.46;
const midY = SAND_H * 0.5;
for (let i = 0; i < grains.length; i++) {
const g = grains[i];
g.progress += g.speed * dt * (0.35 + 0.65 * energy);
if (g.progress >= 1) {
grains[i] = mkGrain(g.progress - 1);
}
const curG = grains[i];
for (let s = 2; s >= 0; s--) {
const t = Math.max(0, curG.progress - s * 0.028);
const e = quintic(t);
const x = rim - (rim - sink) * e;
const y =
midY +
curG.lane * (1 - e * 0.66) * (SAND_H * 0.34) +
Math.sin(t * 7.5 + curG.phase + time * 2.4) * curG.wob * (1 - e * 0.55);
const cm = quintic(Math.min(1, t * 1.18));
let r: number, gr: number, b: number;
if (cm < 0.52) {
const k = cm / 0.52;
r = 130 + (37 - 130) * k;
gr = 145 + (130 - 145) * k;
b = 165 + (246 - 165) * k;
} else {
const k2 = (cm - 0.52) / 0.48;
r = 37 + (20 - 37) * k2;
gr = 130 + (212 - 130) * k2;
b = 246 + (196 - 246) * k2;
}
const alpha =
Math.min(1, t * 5.5) *
(t > 0.72 ? Math.max(0, 1 - (t - 0.72) / 0.28) : 1) *
(1 - (s / 3) * 0.72) *
curG.alpha *
energy *
0.9;
if (alpha <= 0.01) continue;
const rad = curG.radius * (1 - s * 0.14) * (0.85 + 0.25 * cm);
const rg = rad * 1.85;
const grad = sctx.createRadialGradient(x, y, 0, x, y, rg);
const cStr = `rgba(${Math.round(r)},${Math.round(gr)},${Math.round(b)},`;
grad.addColorStop(0, `${cStr}${alpha.toFixed(3)})`);
grad.addColorStop(0.55, `${cStr}${(alpha * 0.55).toFixed(3)})`);
grad.addColorStop(1, `${cStr}0)`);
sctx.fillStyle = grad;
sctx.beginPath();
sctx.arc(x, y, rg, 0, Math.PI * 2);
sctx.fill();
}
}
};
// --- Real-Time RGB Chromatic Optical Refraction ---
const RK = Math.min(4, Math.max(2, Math.ceil((window.devicePixelRatio || 1) * 2)));
const OW = 84 * RK;
const OH = 34 * RK;
const PAD = 24;
const SW = (84 + PAD * 2) * RK;
const SH = 34 * RK;
const offSc = document.createElement('canvas');
offSc.width = SW;
offSc.height = SH;
const sctx = offSc.getContext('2d', { willReadFrequently: true });
const mx = new Float32Array(OW * OH);
const my = new Float32Array(OW * OH);
const ma = new Uint8ClampedArray(OW * OH);
const R_val = OH / 2;
const half = OW / 2 - R_val;
const maxM = 9 * RK;
for (let y = 0; y < OH; y++) {
for (let x = 0; x < OW; x++) {
const cx = x + 0.5 - OW / 2;
const cy = y + 0.5 - OH / 2;
const dx = Math.max(0, Math.abs(cx) - half);
const dist = Math.sqrt(dx * dx + cy * cy);
const d = R_val - dist;
const i = y * OW + x;
if (d <= 0) continue;
ma[i] = Math.min(1, d / 1.5) * 255;
const s = 1 - Math.min(1, d / R_val);
const ex = dist > 0 ? (cx < 0 ? -dx : dx) / dist : 0;
const ey = dist > 0 ? cy / dist : 0;
const m = maxM * Math.pow(s, 2.4);
mx[i] = -ex * m;
my[i] = -ey * m;
}
}
let refractCtx: CanvasRenderingContext2D | null = null;
let refractOut: ImageData | null = null;
const setupRefract = () => {
const refCv = refractCanvasRef.current;
if (!refCv) return;
refCv.width = OW;
refCv.height = OH;
refractCtx = refCv.getContext('2d');
if (refractCtx) {
refractOut = refractCtx.createImageData(OW, OH);
}
};
const drawRefractSideIcons = (
c: CanvasRenderingContext2D,
pct: number,
glowComplete: boolean
) => {
const isComplete = pct >= 100;
// Left Download Bar Icon (x = -32, y = 0, w = 24, h = 12)
c.save();
c.translate(-32, 0);
c.fillStyle = isComplete
? 'rgba(255,255,255,0.16)'
: 'rgba(255,255,255,0.28)';
c.beginPath();
if (c.roundRect) c.roundRect(0.6, 0.6, 22.8, 10.8, 5.4);
else c.rect(0.6, 0.6, 22.8, 10.8);
c.fill();
c.strokeStyle = isComplete
? 'rgba(255,255,255,0.38)'
: 'rgba(255,255,255,0.72)';
c.lineWidth = 1.2;
c.stroke();
c.strokeStyle = isComplete ? 'rgba(255,255,255,0.72)' : '#ffffff';
c.lineWidth = 1.85;
c.lineCap = 'round';
c.lineJoin = 'round';
c.beginPath();
c.moveTo(12, 2.3);
c.lineTo(12, 7.6);
c.moveTo(9.4, 5.3);
c.lineTo(12, 7.9);
c.lineTo(14.6, 5.3);
c.moveTo(8, 9.7);
c.lineTo(16, 9.7);
c.stroke();
c.restore();
// Right Complete Bar Icon (x = trackW + 8, y = 0, w = 24, h = 12)
c.save();
c.translate(trackW + 8, 0);
if (glowComplete) {
c.shadowColor = 'rgba(255,255,255,0.95)';
c.shadowBlur = 6;
}
c.fillStyle = glowComplete
? 'rgba(255,255,255,0.34)'
: 'rgba(148,163,184,0.18)';
c.beginPath();
if (c.roundRect) c.roundRect(0.6, 0.6, 22.8, 10.8, 5.4);
else c.rect(0.6, 0.6, 22.8, 10.8);
c.fill();
c.strokeStyle = glowComplete
? 'rgba(255,255,255,0.95)'
: 'rgba(203,213,225,0.45)';
c.lineWidth = 1.2;
c.stroke();
c.strokeStyle = glowComplete ? '#ffffff' : 'rgba(226,232,240,0.62)';
c.lineWidth = 1.85;
c.lineCap = 'round';
c.lineJoin = 'round';
c.beginPath();
c.moveTo(8.3, 6.2);
c.lineTo(10.8, 8.7);
c.lineTo(15.8, 3.6);
c.stroke();
c.restore();
};
const renderRefract = (capX: number, pct: number) => {
if (!sctx || !refractCtx || !refractOut) return;
sctx.setTransform(1, 0, 0, 1, 0, 0);
sctx.fillStyle = '#0d0d0e';
sctx.fillRect(0, 0, SW, SH);
sctx.setTransform(RK, 0, 0, RK, (PAD + 42 - capX) * RK, 11 * RK);
drawRefractSideIcons(sctx, pct, completeGlowActive);
sctx.save();
sctx.beginPath();
if (sctx.roundRect) sctx.roundRect(0, 0, trackW, 12, 6);
else sctx.rect(0, 0, trackW, 12);
sctx.clip();
const g = sctx.createLinearGradient(0, 0, trackW, 0);
g.addColorStop(0, '#e2e8f0');
g.addColorStop(0.52, '#94a3b8');
g.addColorStop(1, '#475569');
sctx.fillStyle = g;
sctx.fillRect(0, 0, trackW, 12);
const barCv = barCanvasRef.current;
sctx.globalAlpha = parseFloat(barCv?.style.opacity || '0');
if (barCv) sctx.drawImage(barCv, 0, 0, trackW, trackH);
sctx.globalAlpha = 1;
sctx.restore();
const sd = sctx.getImageData(0, 0, SW, SH).data;
const od = refractOut.data;
const ks = [1.1, 1, 0.9];
const v = [0, 0, 0];
for (let y = 0; y < OH; y++) {
for (let x = 0; x < OW; x++) {
const i = y * OW + x;
const o = i * 4;
const a = ma[i];
if (!a) {
od[o + 3] = 0;
continue;
}
const bx = SW / 2 + (x + 0.5 - OW / 2) * 0.92;
const by = SH / 2 + (y + 0.5 - OH / 2) * 0.92;
const mX = mx[i];
const mY = my[i];
for (let ch = 0; ch < 3; ch++) {
let sx = bx + mX * ks[ch] - 0.5;
let sy = by + mY * ks[ch] - 0.5;
sx = sx < 0 ? 0 : sx > SW - 1.001 ? SW - 1.001 : sx;
sy = sy < 0 ? 0 : sy > SH - 1.001 ? SH - 1.001 : sy;
const x0 = Math.floor(sx);
const y0 = Math.floor(sy);
const fx = sx - x0;
const fy = sy - y0;
const p = (y0 * SW + x0) * 4 + ch;
const q = p + SW * 4;
v[ch] =
(sd[p] * (1 - fx) + sd[p + 4] * fx) * (1 - fy) +
(sd[q] * (1 - fx) + sd[q + 4] * fx) * fy;
}
od[o] = (v[0] - 128) * 1.04 + 128;
od[o + 1] = (v[1] - 128) * 1.04 + 128;
od[o + 2] = (v[2] - 128) * 1.04 + 128;
od[o + 3] = a;
}
}
refractCtx.putImageData(refractOut, 0, 0);
};
// --- 4K Physically-Shaded Convex Glass Specular Shell ---
const paintGlass = () => {
const gcv = glass4kCanvasRef.current;
if (!gcv) return;
const K = Math.min(16, Math.max(8, Math.ceil((window.devicePixelRatio || 1) * 6)));
const W = 84 * K;
const H = 34 * K;
gcv.width = W;
gcv.height = H;
const gctx = gcv.getContext('2d');
if (!gctx) return;
const img = gctx.createImageData(W, H);
const D = img.data;
const nrm = (a: number, b: number, c: number) => {
const l = Math.sqrt(a * a + b * b + c * c);
return [a / l, b / l, c / l];
};
const dot = (a: number[], b: number[]) =>
a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
const L1 = nrm(-0.45, -0.8, 0.55);
const L2 = nrm(0.5, 0.85, 0.45);
const H1 = nrm(L1[0], L1[1], L1[2] + 1);
const H2 = nrm(L2[0], L2[1], L2[2] + 1);
const R_cap = H / 2;
const half_cap = W / 2 - R_cap;
const hairW = 0.8 * K;
for (let y = 0; y < H; y++) {
for (let x = 0; x < W; x++) {
const cx = x + 0.5 - W / 2;
const cy = y + 0.5 - H / 2;
const dx = Math.max(0, Math.abs(cx) - half_cap);
const dist = Math.sqrt(dx * dx + cy * cy);
const d = R_cap - dist;
if (d <= 0) continue;
const i = (y * W + x) * 4;
const h = Math.min(1, d / R_cap);
const s = 1 - h;
const ex = dist > 0 ? (cx < 0 ? -dx : dx) / dist : 0;
const ey = dist > 0 ? cy / dist : 0;
const n = nrm(
ex * s,
ey * s,
Math.sqrt(Math.max(0.0001, 1 - s * s))
);
const s1 =
Math.pow(Math.max(0, dot(n, H1)), 110) * 0.95 +
Math.pow(Math.max(0, dot(n, H1)), 16) * 0.16;
const s2 = Math.pow(Math.max(0, dot(n, H2)), 60) * 0.4;
const fres = Math.pow(1 - n[2], 2.4);
const hr =
d < hairW
? (1 - d / hairW) * 0.6 * (0.45 + 0.55 * Math.max(0, -ey))
: 0;
const nz = (Math.sin(x * 12.9898 + y * 78.233) * 43758.5453) % 1;
const nval = nz < 0 ? nz + 1 : nz;
const a1 = Math.min(1, fres * 0.3);
const a2 = Math.min(1, s1);
const a3 = Math.min(1, hr);
const a4 = Math.min(1, s2);
const a5 = 0.05 * nval * (0.4 + fres);
const a6 = 0.06 * s * Math.max(0, ey);
const A =
1 -
(1 - a1) *
(1 - a2) *
(1 - a3) *
(1 - a4) *
(1 - a5) *
(1 - a6);
if (A <= 0.002) continue;
const w = a2 + a3 + a5;
const sum = a1 + w + a4 + a6;
D[i] = (165 * a1 + 255 * w + 205 * a4 + 14 * a6) / sum;
D[i + 1] = (243 * a1 + 255 * w + 250 * a4 + 116 * a6) / sum;
D[i + 2] = (252 * a1 + 255 * w + 255 * a4 + 144 * a6) / sum;
D[i + 3] = A * 255 * Math.min(1, d / 1.2);
}
}
gctx.putImageData(img, 0, 0);
};
// --- C3 Septic Hermite Liquid Wave Fill ---
const renderBar = (time: number, ratio: number, pct: number) => {
const bc = barCanvasRef.current;
if (!bc) return;
const bctx = bc.getContext('2d');
if (!bctx) return;
bctx.setTransform(1, 0, 0, 1, 0, 0);
bctx.clearRect(0, 0, bc.width, bc.height);
if (pct < 2) {
bc.style.opacity = '0';
if (underglowRef.current) {
underglowRef.current.style.opacity = '0';
underglowRef.current.style.width = '0px';
}
return;
}
const em = Math.min(1, (pct - 2) / 1.8);
bc.style.opacity = em.toFixed(3);
const tip = ratio * trackW;
if (underglowRef.current) {
underglowRef.current.style.opacity = (em * 0.85).toFixed(3);
underglowRef.current.style.width = Math.max(0, tip).toFixed(1) + 'px';
}
if (tip <= 0.5) return;
bctx.scale(dpr, dpr);
const q = trackW * 0.25;
const ws = Math.max(0, tip - q);
const span = Math.max(1, tip - ws);
const settle = pct >= 95 ? Math.max(0, 1 - (pct - 95) / 5) : 1;
const amp = 1.65 * settle;
const dip = amp * 1.24;
const fg = bctx.createLinearGradient(0, 0, Math.max(1, tip), 0);
fg.addColorStop(0, '#2563eb');
fg.addColorStop(0.35, '#0ea5e9');
fg.addColorStop(0.68, '#06b6d4');
fg.addColorStop(0.86, '#14b8a6');
fg.addColorStop(1, '#2dd4bf');
const n = Math.max(24, Math.ceil(span * 1.5));
const pts: { x: number; y: number; b: number }[] = [];
for (let i = 0; i <= n; i++) {
const t = i / n;
const x = ws + t * span;
const bl = septic(
Math.min(1, Math.max(0, (x - (tip - q)) / Math.max(1, q)))
);
const w =
(Math.sin(x * 0.135 - time * 5.2) +
Math.sin(x * 0.24 + time * 3.7) * 0.42 +
Math.cos(t * Math.PI * 2.2 - time * 4.4) * 0.28) /
1.7;
pts.push({
x,
y: Math.max(0, Math.min(trackH - 1.5, bl * (dip + amp * w))),
b: bl,
});
}
bctx.beginPath();
bctx.moveTo(0, 0);
if (ws > 0) bctx.lineTo(ws, 0);
for (let j = 0; j < pts.length; j++) bctx.lineTo(pts[j].x, pts[j].y);
bctx.lineTo(tip, trackH);
bctx.lineTo(0, trackH);
bctx.closePath();
bctx.fillStyle = fg;
bctx.fill();
if (settle > 0.02) {
for (let k = 1; k < pts.length; k++) {
const sa = Math.min(pts[k - 1].b, pts[k].b) * 0.72 * settle;
if (sa <= 0.01) continue;
bctx.beginPath();
bctx.moveTo(pts[k - 1].x, pts[k - 1].y + 0.65);
bctx.lineTo(pts[k].x, pts[k].y + 0.65);
bctx.strokeStyle = `rgba(224,252,255,${sa.toFixed(3)})`;
bctx.lineWidth = 1.25;
bctx.lineCap = 'round';
bctx.stroke();
}
}
};
// --- Icon Reflection & Completion State Coordination ---
const updateIconReflections = (capX: number) => {
const distLeft = Math.abs(capX - startCapX);
const distRight = Math.abs(capX - getEndCapX());
const leftOp = Math.max(0, 1 - distLeft / 76) * 0.88;
const rightOp = completeGlowActive
? Math.max(0, 1 - distRight / 76) * 0.96
: 0;
if (reflectIconLeftRef.current) {
reflectIconLeftRef.current.style.opacity = leftOp.toFixed(3);
}
if (reflectIconRightRef.current) {
reflectIconRightRef.current.style.opacity = rightOp.toFixed(3);
}
};
const activateCompleteIconGlow = () => {
completeGlowActive = true;
const ir = iconRightRef.current;
if (ir) {
ir.classList.remove('is-idle');
ir.classList.add('is-complete');
ir.classList.remove('pop');
void ir.offsetWidth;
ir.classList.add('pop');
}
const endX = Math.round(getEndCapX() * dpr) / dpr;
renderRefract(endX, 100);
updateIconReflections(endX);
callbacksRef.current.onFinishFade?.();
};
// --- Elastic Spring Physics for Replay Pill ---
const P = {
vis: false,
pressed: false,
recoil: false,
timer: null as number | null,
x: 0,
y: 12,
vy: 0,
vx: 0,
s: 0.92,
vs: 0,
tx: 0,
ty: 12,
ts: 0.92,
spin: 0,
vspin: 0,
};
const showBtn = () => {
P.vis = true;
P.recoil = false;
P.tx = 0;
P.ty = 0;
P.ts = 1;
btnRef.current?.classList.add('visible');
};
const hideBtn = () => {
P.vis = false;
P.pressed = false;
P.recoil = false;
P.tx = 0;
P.ty = 12;
P.ts = 0.92;
btnRef.current?.classList.remove(
'visible',
'sheen-active',
'ripple-active',
'ring-active'
);
};
const fx = (cx: number, cy: number) => {
const btn = btnRef.current;
const ripple = rippleRef.current;
if (!btn) return;
const r = btn.getBoundingClientRect();
if (ripple && cx > 0 && cy > 0) {
ripple.style.left =
Math.max(0, Math.min(r.width, cx - r.left)) + 'px';
ripple.style.top =
Math.max(0, Math.min(r.height, cy - r.top)) + 'px';
}
btn.classList.remove('ripple-active', 'sheen-active', 'ring-active');
void btn.offsetWidth;
btn.classList.add('ripple-active', 'sheen-active', 'ring-active');
};
const stepBtn = (dt: number) => {
const k = P.pressed ? 420 : 290;
const d = P.pressed ? 24 : 18;
P.vx += ((P.tx - P.x) * k - P.vx * d) * dt;
P.x += P.vx * dt;
P.vy += ((P.ty - P.y) * k - P.vy * d) * dt;
P.y += P.vy * dt;
P.vs += ((P.ts - P.s) * k * 1.1 - P.vs * d * 0.92) * dt;
P.s += P.vs * dt;
P.vspin += ((0 - P.spin) * 190 - P.vspin * 14) * dt;
P.spin += P.vspin * dt;
if (btnRef.current) {
btnRef.current.style.transform = `translate3d(${P.x.toFixed(
2
)}px,${P.y.toFixed(2)}px,0) scale(${P.s.toFixed(4)})`;
}
if (iconReplayRef.current) {
iconReplayRef.current.style.transform = `rotate(${P.spin.toFixed(
1
)}deg)`;
}
};
const btn = btnRef.current;
const onPointerEnter = () => {
if (P.vis && !P.recoil && !P.pressed) {
P.ty = -1.5;
P.ts = 1.03;
}
};
const onPointerMove = (e: PointerEvent) => {
if (!P.vis || P.recoil || !btn) return;
const r = btn.getBoundingClientRect();
const nx = Math.max(
-1,
Math.min(1, (e.clientX - r.left - r.width / 2) / (r.width / 2))
);
const ny = Math.max(
-1,
Math.min(1, (e.clientY - r.top - r.height / 2) / (r.height / 2))
);
P.tx = nx * 4.2;
P.ty = (P.pressed ? 2.5 : -1.2) + ny * 2.7;
};
const onPointerDown = (e: PointerEvent) => {
if (!P.vis || P.recoil) return;
P.pressed = true;
P.ts = 0.91;
P.ty = 3;
P.vspin -= 240;
fx(e.clientX, e.clientY);
};
const onPointerLeave = () => {
if (P.vis && !P.recoil && !P.pressed) {
P.tx = 0;
P.ty = 0;
P.ts = 1;
}
};
const onPointerCancel = () => {
P.pressed = false;
if (P.vis && !P.recoil) {
P.tx = 0;
P.ty = 0;
P.ts = 1;
}
};
const onClick = (e: MouseEvent) => {
if (!P.vis || P.recoil) return;
P.pressed = false;
P.recoil = true;
P.tx = 0;
P.ty = -3.5;
P.ts = 1.09;
P.vs += 2.6;
P.vspin -= 760;
fx(e.clientX, e.clientY);
if (P.timer) window.clearTimeout(P.timer);
P.timer = window.setTimeout(start, 290);
};
if (btn) {
btn.addEventListener('pointerenter', onPointerEnter);
btn.addEventListener('pointermove', onPointerMove);
btn.addEventListener('pointerdown', onPointerDown);
btn.addEventListener('pointerleave', onPointerLeave);
btn.addEventListener('pointercancel', onPointerCancel);
btn.addEventListener('click', onClick);
}
// --- Main Animation Frame Loop ---
const frame = (now: number) => {
if (!startTime) startTime = now;
if (!lastT) lastT = now;
const dt = Math.min(0.05, Math.max(0.001, (now - lastT) / 1000));
lastT = now;
syncBar();
const raw = Math.min(1, Math.max(0, (now - startTime) / duration));
let ratio =
raw < 0.5
? 2 * raw * raw * (1.15 - 0.3 * raw)
: 1 - Math.pow(-2 * raw + 2, 2) / 2;
ratio = Math.min(1, Math.max(0, ratio));
let pct = Math.min(100, Math.floor(ratio * 100));
if (raw >= 1) {
ratio = 1;
pct = 100;
}
const endCapX = getEndCapX();
const capPos = startCapX + ratio * (endCapX - startCapX);
const snappedCapX = Math.round(capPos * dpr) / dpr;
if (capsuleRef.current) {
capsuleRef.current.style.left = snappedCapX.toFixed(3) + 'px';
}
if (pct !== lastPct) {
lastPct = pct;
const label = pct + '%';
if (percentTextRef.current) percentTextRef.current.textContent = label;
if (reflectLeftRef.current) reflectLeftRef.current.textContent = label;
if (reflectRightRef.current) reflectRightRef.current.textContent = label;
}
renderBar(now / 1000, ratio, pct);
renderRefract(snappedCapX, pct);
updateIconReflections(snappedCapX);
renderSand(dt, now / 1000, ratio, pct);
stepBtn(dt);
if (raw >= 1 && !done) {
done = true;
if (iconLeftRef.current) {
iconLeftRef.current.classList.remove('is-active');
iconLeftRef.current.classList.add('is-done');
}
showBtn();
callbacksRef.current.onComplete?.();
if (hide100Timer) window.clearTimeout(hide100Timer);
hide100Timer = window.setTimeout(() => {
percentTextRef.current?.classList.add('pct-hidden');
reflectLeftRef.current?.classList.add('pct-hidden');
reflectRightRef.current?.classList.add('pct-hidden');
activateCompleteIconGlow();
}, 1000);
}
rafId = requestAnimationFrame(frame);
};
const start = () => {
if (rafId) cancelAnimationFrame(rafId);
if (P.timer) window.clearTimeout(P.timer);
if (hide100Timer) window.clearTimeout(hide100Timer);
startTime = null;
lastT = null;
lastPct = -1;
done = false;
completeGlowActive = false;
if (iconLeftRef.current) {
iconLeftRef.current.classList.remove('is-done');
iconLeftRef.current.classList.add('is-active');
}
if (iconRightRef.current) {
iconRightRef.current.classList.remove('is-complete', 'pop');
iconRightRef.current.classList.add('is-idle');
}
percentTextRef.current?.classList.remove('pct-hidden');
reflectLeftRef.current?.classList.remove('pct-hidden');
reflectRightRef.current?.classList.remove('pct-hidden');
setupSand();
paintGlass();
setupRefract();
resetGrains();
hideBtn();
syncBar();
rafId = requestAnimationFrame(frame);
};
window.addEventListener('resize', syncBar);
start();
return () => {
if (rafId) cancelAnimationFrame(rafId);
if (P.timer) window.clearTimeout(P.timer);
if (hide100Timer) window.clearTimeout(hide100Timer);
window.removeEventListener('resize', syncBar);
if (btn) {
btn.removeEventListener('pointerenter', onPointerEnter);
btn.removeEventListener('pointermove', onPointerMove);
btn.removeEventListener('pointerdown', onPointerDown);
btn.removeEventListener('pointerleave', onPointerLeave);
btn.removeEventListener('pointercancel', onPointerCancel);
btn.removeEventListener('click', onClick);
}
};
}, [duration]);
return (
<div
className={`lgl-root ${fullScreen ? 'lgl-fullscreen' : ''} ${className}`}
>
<style>{`
.lgl-root{
--lgl-bg:#0d0d0e;--lgl-ink:#e5e7eb;
width:100%;height:100%;min-height:100vh;background:var(--lgl-bg);color:var(--lgl-ink);
font-family:system-ui,-apple-system,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;
-webkit-font-smoothing:antialiased;display:flex;align-items:center;justify-content:center;
user-select:none;overflow:hidden;box-sizing:border-box;
padding-top:env(safe-area-inset-top,0px);padding-bottom:env(safe-area-inset-bottom,0px);
}
.lgl-root.lgl-fullscreen{
position:fixed;
inset:0;
width:100vw;
height:100vh;
height:100dvh;
margin:0;
z-index:9999;
}
.lgl-stage{
width:min(420px,calc(100vw - 176px));
padding:0 88px;
box-sizing:content-box;
display:flex;flex-direction:column;align-items:center;
}
.lgl-track-row{
position:relative;
width:100%;
height:12px;
display:flex;
align-items:center;
gap:8px;
}
.lgl-track-wrapper{
position:relative;
flex:1 1 auto;
height:12px;
display:flex;
align-items:center;
overflow:visible;
}
.lgl-progress-track{
position:relative;width:100%;height:12px;border-radius:9999px;
background:linear-gradient(90deg,#e2e8f0 0%,#94a3b8 52%,#475569 100%);
box-shadow:inset 0 1px 2px rgba(0,0,0,.6),0 1px 0 rgba(255,255,255,.07);
overflow:hidden;clip-path:inset(0 round 9999px);
}
.lgl-bar-wave-canvas{
position:absolute;inset:0;width:100%;height:100%;display:block;pointer-events:none;opacity:0;
}
.lgl-bar-underglow{
position:absolute;top:3px;left:0;height:10px;width:0;border-radius:9999px;
background:linear-gradient(90deg,rgba(37,99,235,.28),rgba(6,182,212,.34) 55%,rgba(45,212,191,.38));
filter:blur(6px);opacity:0;pointer-events:none;z-index:1;
}
/* Side bar icons matched to 12px track height (Pure White) */
.lgl-track-bar-icon{
position:relative;
flex:0 0 24px;
width:24px;
height:12px;
border-radius:9999px;
display:flex;
align-items:center;
justify-content:center;
z-index:5;
transition:transform .38s cubic-bezier(.22,1,.36,1),filter .38s ease,opacity .35s ease;
}
.lgl-track-bar-icon svg{
width:24px;
height:12px;
display:block;
overflow:visible;
}
.lgl-icon-download.is-active{
filter:drop-shadow(0 0 7px rgba(255,255,255,.65)) drop-shadow(0 0 2px rgba(255,255,255,.85));
}
.lgl-icon-download.is-done{
opacity:.72;
filter:drop-shadow(0 0 3px rgba(255,255,255,.28));
}
.lgl-icon-complete.is-idle{
opacity:.55;
transform:scale(.96);
filter:none;
}
.lgl-icon-complete.is-complete{
opacity:1;
transform:scale(1);
filter:drop-shadow(0 0 8px rgba(255,255,255,.85)) drop-shadow(0 0 3px rgba(255,255,255,.95));
}
.lgl-icon-complete.pop{
animation:lglIconPop .52s cubic-bezier(.16,1,.3,1) forwards;
}
@keyframes lglIconPop{
0%{transform:scale(.88)}
52%{transform:scale(1.18)}
100%{transform:scale(1)}
}
/* Glass capsule 84x34 */
.lgl-glass-capsule{
position:absolute;top:50%;left:-20px;width:84px;height:34px;transform:translate(-50%,-50%);
border-radius:9999px;overflow:hidden;clip-path:inset(0 round 9999px);z-index:20;pointer-events:none;
display:flex;align-items:center;justify-content:center;border:none;
background:linear-gradient(180deg,rgba(255,255,255,.14),rgba(255,255,255,0) 45%,rgba(255,255,255,.05));
-webkit-backdrop-filter:none;backdrop-filter:none;
box-shadow:0 10px 22px -10px rgba(15,23,42,.22),0 2px 6px -2px rgba(15,23,42,.10),inset 0 1px 0 rgba(255,255,255,.55);
}
.lgl-internal-sand-canvas{
position:absolute;inset:0;width:100%;height:100%;border-radius:9999px;display:block;pointer-events:none;z-index:4;
}
.lgl-glass-4k{
position:absolute;inset:0;width:100%;height:100%;border-radius:9999px;z-index:8;pointer-events:none;
}
.lgl-glass-refract{
position:absolute;inset:0;width:100%;height:100%;border-radius:9999px;z-index:2;pointer-events:none;
}
/* Curved Fresnel specular reflections of the side bar icons inside glass */
.lgl-icon-reflection-left,.lgl-icon-reflection-right{
position:absolute;top:50%;width:14px;height:8px;pointer-events:none;z-index:7;opacity:0;
filter:blur(.55px) drop-shadow(0 0 3px rgba(255,255,255,.65));
transition:opacity .32s ease;
}
.lgl-icon-reflection-left{
left:5px;transform:translateY(-50%) scaleX(-.46) scaleY(1.08);transform-origin:left center;
-webkit-mask-image:linear-gradient(90deg,transparent,rgba(0,0,0,.82) 34%,transparent);
mask-image:linear-gradient(90deg,transparent,rgba(0,0,0,.82) 34%,transparent);
}
.lgl-icon-reflection-right{
right:5px;transform:translateY(-50%) scaleX(-.46) scaleY(1.08);transform-origin:right center;
-webkit-mask-image:linear-gradient(270deg,transparent,rgba(0,0,0,.82) 34%,transparent);
mask-image:linear-gradient(270deg,transparent,rgba(0,0,0,.82) 34%,transparent);
}
.lgl-percent-text{
position:relative;z-index:10;font-size:13.5px;font-weight:700;letter-spacing:-.02em;font-variant-numeric:tabular-nums;color:#fff;line-height:1;
text-shadow:0 1px 2px rgba(15,23,42,.52),0 0 8px rgba(14,116,144,.45),-.5px 0 1.2px rgba(34,211,238,.42),.5px 0 1.2px rgba(45,212,191,.42);
transition:opacity .42s cubic-bezier(.22,1,.36,1),transform .42s cubic-bezier(.22,1,.36,1),filter .42s ease;
}
.lgl-percent-text.pct-hidden{
opacity:0;
transform:scale(.82);
filter:blur(3px);
}
.lgl-percent-reflection-left,.lgl-percent-reflection-right{
position:absolute;top:50%;font-size:12.5px;font-weight:700;font-variant-numeric:tabular-nums;
color:rgba(224,242,254,.42);filter:blur(.65px);pointer-events:none;z-index:9;white-space:nowrap;text-shadow:0 0 4px rgba(6,182,212,.34);
transition:opacity .42s cubic-bezier(.22,1,.36,1),filter .42s ease;
}
.lgl-percent-reflection-left.pct-hidden,.lgl-percent-reflection-right.pct-hidden{
opacity:0;
filter:blur(3px);
}
.lgl-percent-reflection-left{
left:6px;transform:translateY(-50%) scaleX(-.38) scaleY(1.05);transform-origin:left center;
-webkit-mask-image:linear-gradient(90deg,transparent,rgba(0,0,0,.72) 28%,transparent);
mask-image:linear-gradient(90deg,transparent,rgba(0,0,0,.72) 28%,transparent);
}
.lgl-percent-reflection-right{
right:6px;transform:translateY(-50%) scaleX(-.38) scaleY(1.05);transform-origin:right center;
-webkit-mask-image:linear-gradient(270deg,transparent,rgba(0,0,0,.72) 28%,transparent);
mask-image:linear-gradient(270deg,transparent,rgba(0,0,0,.72) 28%,transparent);
}
/* Replay pill */
.lgl-replay-anchor{
margin-top:32px;height:38px;display:flex;align-items:center;justify-content:center;
}
.lgl-replay-btn{
position:relative;display:inline-flex;align-items:center;justify-content:center;padding:8px 22px;border-radius:9999px;
border:1px solid rgba(148,163,184,.36);background:linear-gradient(180deg,rgba(255,255,255,.12),rgba(255,255,255,.05));
color:#e5e7eb;font:600 12.5px/1 inherit;font-family:inherit;letter-spacing:.015em;cursor:pointer;outline:none;appearance:none;-webkit-appearance:none;
-webkit-tap-highlight-color:transparent;touch-action:manipulation;opacity:0;pointer-events:none;will-change:transform,opacity;
transform:translate3d(0,12px,0) scale(.92);
box-shadow:0 4px 12px -2px rgba(15,23,42,.07),0 1px 3px rgba(15,23,42,.05),inset 0 1px 0 rgba(255,255,255,.16);
transition:opacity .28s ease,border-color .2s,color .2s,box-shadow .22s;
}
.lgl-replay-btn.visible{
opacity:1;pointer-events:auto;
}
.lgl-replay-btn:hover{
border-color:rgba(6,182,212,.55);box-shadow:0 8px 20px -4px rgba(6,182,212,.22),0 2px 6px rgba(15,23,42,.06),inset 0 1px 0 #fff;
}
.lgl-replay-btn:focus-visible{
border-color:rgba(6,182,212,.72);box-shadow:0 0 0 3px rgba(6,182,212,.22),0 8px 20px -4px rgba(6,182,212,.24);
}
.lgl-replay-pill-ring{
position:absolute;inset:-1px;border-radius:9999px;border:1.5px solid rgba(6,182,212,.65);box-shadow:0 0 12px rgba(45,212,191,.35);pointer-events:none;opacity:0;
}
.lgl-replay-btn.ring-active .lgl-replay-pill-ring{
animation:lglRing .48s cubic-bezier(.16,1,.3,1) forwards;
}
@keyframes lglRing{
0%{opacity:.9;transform:scale(1)}
100%{opacity:0;transform:scale(1.22,1.48)}
}
.lgl-replay-surface-clip{
position:absolute;inset:0;border-radius:9999px;overflow:hidden;clip-path:inset(0 round 9999px);pointer-events:none;
}
.lgl-replay-radial-ripple{
position:absolute;width:140px;height:140px;margin:-70px 0 0 -70px;left:50%;top:50%;border-radius:9999px;
background:radial-gradient(circle,rgba(37,99,235,.16),rgba(6,182,212,.22) 42%,rgba(45,212,191,.08) 70%,transparent);transform:scale(0);opacity:0;
}
.lgl-replay-btn.ripple-active .lgl-replay-radial-ripple{
animation:lglRipple .52s cubic-bezier(.22,1,.36,1) forwards;
}
@keyframes lglRipple{
0%{opacity:.95;transform:scale(.08)}
100%{opacity:0;transform:scale(1.35)}
}
.lgl-replay-sheen{
position:absolute;inset:0;background:linear-gradient(112deg,transparent 18%,rgba(165,243,252,.35) 42%,rgba(255,255,255,.92) 50%,rgba(45,212,191,.32) 58%,transparent 82%);transform:translateX(-140%);opacity:0;
}
.lgl-replay-btn.sheen-active .lgl-replay-sheen{
animation:lglSheen .44s cubic-bezier(.22,1,.36,1) forwards;
}
@keyframes lglSheen{
0%{opacity:.95;transform:translateX(-140%)}
100%{opacity:0;transform:translateX(140%)}
}
.lgl-replay-inner{
position:relative;z-index:2;display:inline-flex;align-items:center;gap:6px;pointer-events:none;
}
.lgl-replay-icon{
width:13px;height:13px;stroke:currentColor;stroke-width:2.2;fill:none;stroke-linecap:round;stroke-linejoin:round;
}
`}</style>
<div className="lgl-stage" aria-label="Loading progress">
<div className="lgl-track-row" ref={trackRowRef}>
{/* Left Download Bar Icon (24x12px matched to 12px bar height, Pure White) */}
<div
className="lgl-track-bar-icon lgl-icon-download is-active"
ref={iconLeftRef}
aria-hidden="true"
>
<svg viewBox="0 0 24 12" fill="none">
<defs>
<linearGradient id="lgl-dl-bar-grad" x1="0" y1="0" x2="24" y2="0" gradientUnits="userSpaceOnUse">
<stop offset="0%" stopColor="#ffffff" stopOpacity="0.34" />
<stop offset="100%" stopColor="#ffffff" stopOpacity="0.16" />
</linearGradient>
</defs>
<rect
x="0.6"
y="0.6"
width="22.8"
height="10.8"
rx="5.4"
fill="url(#lgl-dl-bar-grad)"
stroke="#ffffff"
strokeOpacity="0.68"
strokeWidth="1.2"
/>
<path
d="M12 2.3V7.6M9.4 5.3L12 7.9L14.6 5.3"
stroke="#ffffff"
strokeWidth="1.85"
strokeLinecap="round"
strokeLinejoin="round"
/>
<path
d="M8 9.7H16"
stroke="#ffffff"
strokeOpacity="0.92"
strokeWidth="1.75"
strokeLinecap="round"
/>
</svg>
</div>
<div className="lgl-track-wrapper" ref={trackWrapperRef}>
<div className="lgl-bar-underglow" ref={underglowRef} />
<div className="lgl-progress-track">
<canvas className="lgl-bar-wave-canvas" ref={barCanvasRef} />
</div>
<div className="lgl-glass-capsule" ref={capsuleRef}>
<canvas className="lgl-glass-refract" ref={refractCanvasRef} aria-hidden="true" />
<canvas className="lgl-internal-sand-canvas" ref={sandCanvasRef} />
<canvas className="lgl-glass-4k" ref={glass4kCanvasRef} aria-hidden="true" />
<span className="lgl-icon-reflection-left" ref={reflectIconLeftRef} aria-hidden="true">
<svg viewBox="0 0 24 12" fill="none" width="14" height="8">
<path
d="M12 2.3V7.6M9.4 5.3L12 7.9L14.6 5.3M8 9.7H16"
stroke="#ffffff"
strokeWidth="2.1"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</span>
<span className="lgl-percent-reflection-left" ref={reflectLeftRef} aria-hidden="true">
0%
</span>
<span className="lgl-percent-text" ref={percentTextRef}>
0%
</span>
<span className="lgl-percent-reflection-right" ref={reflectRightRef} aria-hidden="true">
0%
</span>
<span className="lgl-icon-reflection-right" ref={reflectIconRightRef} aria-hidden="true">
<svg viewBox="0 0 24 12" fill="none" width="14" height="8">
<path
d="M8.3 6.2L10.8 8.7L15.8 3.6"
stroke="#ffffff"
strokeWidth="2.2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</span>
</div>
</div>
{/* Right Complete Bar Icon (24x12px matched to 12px bar height, Pure White) */}
<div
className="lgl-track-bar-icon lgl-icon-complete is-idle"
ref={iconRightRef}
aria-hidden="true"
>
<svg viewBox="0 0 24 12" fill="none">
<defs>
<linearGradient id="lgl-done-bar-grad" x1="0" y1="0" x2="24" y2="0" gradientUnits="userSpaceOnUse">
<stop offset="0%" stopColor="#ffffff" stopOpacity="0.22" />
<stop offset="100%" stopColor="#ffffff" stopOpacity="0.38" />
</linearGradient>
</defs>
<rect
x="0.6"
y="0.6"
width="22.8"
height="10.8"
rx="5.4"
fill="url(#lgl-done-bar-grad)"
stroke="#ffffff"
strokeOpacity="0.65"
strokeWidth="1.2"
/>
<path
d="M8.3 6.2L10.8 8.7L15.8 3.6"
stroke="#ffffff"
strokeWidth="1.9"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</div>
</div>
<div className="lgl-replay-anchor">
<button
type="button"
className="lgl-replay-btn"
ref={btnRef}
aria-label="Replay loading animation"
>
<span className="lgl-replay-pill-ring" aria-hidden="true" />
<span className="lgl-replay-surface-clip" aria-hidden="true">
<span className="lgl-replay-radial-ripple" ref={rippleRef} />
<span className="lgl-replay-sheen" />
</span>
<span className="lgl-replay-inner">
<svg
className="lgl-replay-icon"
ref={iconReplayRef}
viewBox="0 0 24 24"
aria-hidden="true"
>
<path d="M3 12a9 9 0 1 0 3-6.7L3 8" />
<path d="M3 3v5h5" />
</svg>
<span>Replay</span>
</span>
</button>
</div>
</div>
</div>
);
};
export default LiquidGlassLoader;















