buildmymcpserver/apps/web/components/particle-hero/index.tsx
Marco Sadjadi 6197ee7f5e
All checks were successful
Deploy to Production / deploy (push) Successful in 1m1s
feat: particle cloud (no discrete dots) + geo-IP country preselect on login
Two coordinated polish moves the owner asked for.

## 1. Hero particle field — "no white dots, just a glow that follows the mouse and is always in motion"

Previous tuning (uPointSize 2.8, uBaseAlpha 0.6) gave discrete indigo
dots that additively saturated to near-white in dense clusters. The
owner wanted no granular dots visible at all — a continuous indigo
cloud that the cursor pulls toward itself.

Changes:

- **Render fragment**: replaced the anti-aliased disc SDF
  (`smoothstep(0.5, 0.42, d)` — hard edge) with a Gaussian falloff
  (`exp(-d * d * 6.0)` — smooth blob, no edge). Each particle is now
  a soft volume that blends seamlessly with neighbours.

- **Sim fragment**: replaced the outward-gradient ring push with a
  mouse-halo attraction. Particles drift toward an ideal radius
  (~0.20) around the cursor, with exp-bell falloff so they don't
  collapse onto the cursor or feel influenced from across the canvas.
  `ringField()` helper is now unused but kept for future use.

- **JS uniforms**: `uPointSize` 2.8→14 (256-tier) / 3.6→20 (128-tier);
  `uBaseAlpha` 0.6→0.055. Individual particles are below the
  perception threshold for "dot" but 65k of them additively composite
  into a continuous cloud. With the much lower per-particle alpha,
  the cumulative brightness never saturates to white.

- **ParticleField tick loop**: asymmetric ring-active fade — `alpha
  = 0.14` ramping in (fast cursor response), `0.012` decaying out
  (slow glow trail after the pointer moves away). Matches the brief
  "glow longer + attractive to mouse but always in motion".

- **ParticleHero index.tsx**: added an always-on indigo radial
  gradient behind the WebGL canvas, so the hero never reads as
  visually empty between frames — the canvas additively paints the
  dynamic cloud on top. Removed the white-dot stipple from the
  static fallback (it was the most likely source of the "weisse
  punkte" complaint for any visitor on the fallback path).

## 2. SMS login — pre-select country picker from visitor's geo-IP

The country picker on `/login` previously defaulted to `'CH'` for
everyone. Visitors from DE / AT / US / etc. had to manually scroll
to their dial code — small friction but it sits on the highest-stakes
conversion step in the funnel.

- **New API route** `apps/api/src/routes/geo.ts` →
  `GET /v1/geo/country` returns `{ country: 'CH' | 'DE' | … | null }`
  by reading Cloudflare's `CF-IPCountry` header. Public, no auth —
  reading a 2-letter country code from a geo-IP header isn't PII
  under GDPR / DSG. `'XX'` and `'T1'` (CF's "unknown" + Tor) are
  normalised to `null`. Outside CF (dev), header is missing → null.

- **Login page** picks up the result in the existing `useEffect`,
  guards against codes not in our country list, and calls `setCountry`
  to override the `'CH'` default. Stays at `'CH'` if the detection
  fails or the visitor is on a Tor exit. Verified live: the endpoint
  returns `{"country":"DE"}` from CF's German edge.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 13:17:20 +02:00

177 lines
6.5 KiB
TypeScript

'use client';
/**
* ParticleHero — public entry to the WebGL particle background.
*
* Responsibilities (kept OUT of ParticleField so that component can
* assume happy-path WebGL2):
*
* 1. WebGL2 missing → static gradient.
* 2. Mobile / low-power profile → either 16k particles or skip.
* 3. prefers-reduced-motion → still WebGL + cursor tracking, but
* capped at 16k particles with halved drift and halved push
* velocity. The ring still follows the cursor at full fidelity
* because that's the interaction the user explicitly wants; we
* only damp the *ambient* motion so the field reads as calm.
* 4. Lazy-load Three.js via next/dynamic so the hero LCP text isn't
* blocked by ~150kb of WebGL plumbing.
*
* The static fallback is a CSS-only radial gradient + faint dot mask.
* It looks intentional, not broken — same color story as the live
* particle field, just without motion.
*/
import dynamic from 'next/dynamic';
import { useEffect, useState } from 'react';
type Capability =
| { kind: 'unknown' }
| { kind: 'fallback' }
| { kind: 'webgl'; textureSize: 128 | 256; motionScale: number };
// Dynamic import keeps three out of the initial bundle. ssr:false
// because there's no DOM/Canvas during SSR anyway.
const ParticleField = dynamic(
() => import('./ParticleField').then((m) => ({ default: m.ParticleField })),
{
ssr: false,
// No loading UI — the static gradient already lives at z-0 above
// until this resolves, and the canvas paints into the same slot.
loading: () => null,
},
);
/**
* Detect WebGL2 + float-render-target support without keeping the
* context around. We create a throwaway canvas, ask for `webgl2`, and
* probe `EXT_color_buffer_float`. If anything fails we fall back.
*
* Runs sync-only in the browser; never during SSR.
*/
function detectWebGL2(): boolean {
try {
const c = document.createElement('canvas');
const gl = c.getContext('webgl2');
if (!gl) return false;
const ext = gl.getExtension('EXT_color_buffer_float');
// Losing context to ensure we don't leak the probe.
const loseExt = gl.getExtension('WEBGL_lose_context');
loseExt?.loseContext();
return Boolean(ext);
} catch {
return false;
}
}
export function ParticleHero() {
// Start in 'unknown' so SSR markup matches the first client render —
// the fallback gradient is rendered until we resolve capability, so
// there's no flash either way.
const [cap, setCap] = useState<Capability>({ kind: 'unknown' });
useEffect(() => {
// 1. WebGL2 + float targets — hard gate. Without these the sim
// can't run at all, fall through to the static gradient.
if (!detectWebGL2()) {
setCap({ kind: 'fallback' });
return;
}
const reduce = window.matchMedia('(prefers-reduced-motion: reduce)');
/**
* Pick the right particle tier for the device.
*
* Returns a non-fallback WebGL config OR null when the device is
* too constrained to render the field at all (low-core phones).
* Reduced-motion does NOT shrink the tier here — it's applied as
* a separate motion scalar on top, because the user still wants
* the cursor-tracking interaction.
*/
const pickTier = (): Capability => {
// Heuristic: small viewport OR an absurd DPR (low-DPI phones lying
// about retina) with no high-end signal. hardwareConcurrency is a
// rough but free proxy; logical cores <= 4 on a small viewport is
// a strong hint we shouldn't push 65k particles.
const isNarrow = window.matchMedia('(max-width: 768px)').matches;
const dpr = window.devicePixelRatio || 1;
const cores = navigator.hardwareConcurrency ?? 4;
const reduced = reduce.matches;
// Motion-reduce caps drift + ring-push velocity at 50% but keeps
// pointer position fidelity at 100%. 1.0 means "default motion".
const motionScale = reduced ? 0.5 : 1.0;
if (isNarrow) {
// Phones: drop to 16k. Going lower than that and the field
// visibly thins out; going higher and we cook batteries.
// 4-core phones get the static fallback — those are the
// budget Androids most likely to thermal-throttle.
if (cores <= 4) {
return { kind: 'fallback' };
}
return { kind: 'webgl', textureSize: 128, motionScale };
}
if (dpr > 2.5 && cores <= 4) {
// High-DPI low-core — likely a low-end tablet.
return { kind: 'webgl', textureSize: 128, motionScale };
}
// Desktop / capable tablet. Reduced-motion users get the same 128
// tier as mobile — fewer particles means less ambient activity in
// peripheral vision, which is what the motion preference is for.
if (reduced) {
return { kind: 'webgl', textureSize: 128, motionScale };
}
return { kind: 'webgl', textureSize: 256, motionScale };
};
setCap(pickTier());
// Respond to motion-preference changes mid-session — re-pick the
// tier so toggling the OS setting takes effect without a reload.
const onReduceChange = () => setCap(pickTier());
reduce.addEventListener('change', onReduceChange);
return () => reduce.removeEventListener('change', onReduceChange);
}, []);
// Static fallback: pure indigo radial glow, no dot grid. The
// dot-mask was confusing — it read as "stippled white texture"
// against the indigo glow rather than as resting particles. The
// cleaner, dotless gradient holds up better as a fallback.
if (cap.kind !== 'webgl') {
return (
<div
aria-hidden="true"
className="absolute inset-0 size-full overflow-hidden"
style={{
background:
'radial-gradient(65% 80% at 50% 45%, rgba(99,102,241,0.22), rgba(99,102,241,0) 72%)',
}}
/>
);
}
// WebGL path: an always-on indigo radial behind the canvas so the
// hero never feels visually empty (even between frames, even when the
// pointer is far away). The canvas paints the dynamic cloud + halo
// additively on top of this baseline glow — the result is "longer
// glow" without needing the simulation to keep a permanent ring on.
return (
<>
<div
aria-hidden="true"
className="pointer-events-none absolute inset-0 size-full"
style={{
background:
'radial-gradient(70% 85% at 50% 48%, rgba(99,102,241,0.22), rgba(99,102,241,0) 72%)',
}}
/>
<ParticleField textureSize={cap.textureSize} motionScale={cap.motionScale} />
</>
);
}
export default ParticleHero;