Something went wrong. Try again.
Your thinking partner.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164import { MODE_FRAMES, resolvePreset } from "thinking-orbs/engine";
export type Glyph = "ring" | "orb" | "plus" | "minus";export type Dot = { x: number; y: number; r: number; alpha: number };export const MORPH_MS = 225;const MODES = ["weaving", "working", "breathing"] as const;const ease = (p: number) => { const q = Math.max(0, Math.min(1, p)); return q * q * (3 - 2 * q);};const mix = (a: number, b: number, p: number) => a + (b - a) * p;
function orb(time: number): Dot[] { const step = Math.floor(time / 4000); const get = (index: number, weight: number) => { const { mode, speed, opts } = resolvePreset( MODES[index % MODES.length]!, 64, ); return MODE_FRAMES[mode]( 36, (0.6 + time / 1000) * speed * 0.7, opts, ).dots.map((d) => ({ x: d.x + 4, y: d.y + 4, r: Math.max(0.48, d.r * 1.18), alpha: weight * (0.55 + (1 - Math.min(1, Math.max(0, d.white))) * 0.45) * Math.sqrt(d.a ?? 1), })); }; const p = step === 0 ? 1 : ease((time % 4000) / 450); return [...(p < 1 ? get(step - 1, 1 - p) : []), ...get(step, p)];}function mark(glyph: Exclude<Glyph, "orb">, count = 180): Dot[] { if (glyph === "ring") return Array.from({ length: count }, (_, i) => { const angle = -Math.PI + ((i + 0.5) * Math.PI * 2) / count; return { x: 22 + 14.75 * Math.cos(angle), y: 22 + 14.75 * Math.sin(angle), r: 1.25, alpha: 1, }; }); return Array.from({ length: 56 }, (_, i) => { const d = -11.667 + ((i % 28) * 23.334) / 27; return { x: 22 + (glyph === "plus" && i >= 28 ? 0 : d), y: 22 + (glyph === "plus" && i >= 28 ? d : 0), r: 1.25, alpha: 1, }; });}const angular = (dots: Dot[]) => [...dots].sort( (a, b) => Math.atan2(a.y - 22, a.x - 22) - Math.atan2(b.y - 22, b.x - 22), );const sample = (dots: Dot[], count: number) => Array.from( { length: count }, (_, i) => dots[Math.min(dots.length - 1, Math.floor((i * dots.length) / count))]!, );
/** Local animation clock. Network run state controls the target; there is no demo timer. */export class ParticleMotion { target: Glyph; private time = 0; private age = 0; private from: Dot[] = []; private to: Dot[] = []; private current: Dot[] = []; private moving = false; constructor(initial: Glyph) { this.target = initial; this.current = initial === "orb" ? orb(0) : mark(initial); } get active() { return this.moving || this.target === "orb"; } setTarget(target: Glyph, reduced = false) { if (target === this.target) return; const previous = this.target; this.target = target; this.age = 0; this.time = 0; let destination = target === "orb" ? orb(0) : mark(target); let source = this.current; if ( target === "ring" || target === "orb" || previous === "ring" || previous === "orb" ) { source = angular(source); destination = angular(destination); const n = Math.max(source.length, destination.length); source = previous === "ring" && !this.moving ? mark("ring", n) : sample(source, n); destination = target === "ring" ? mark("ring", n) : sample(destination, n); } this.from = source; this.to = destination; this.moving = !reduced; if (reduced) this.current = destination; } frame(delta: number, reduced = false): { glyph: Glyph | null; dots: Dot[] } { if (reduced) { this.moving = false; this.current = this.target === "orb" ? orb(0) : mark(this.target); } else if (this.moving) { this.age += delta; const p = ease(this.age / MORPH_MS), lift = Math.sin(Math.PI * p); const signMorph = this.target === "plus" || this.target === "minus"; this.current = this.to.map((to, i) => { const from = this.from[i]!; return { x: mix(from.x, to.x, p) + (signMorph ? Math.sin(i * 2.4) * lift * 1.8 : 0), y: mix(from.y, to.y, p) + (signMorph ? (i % 2 ? 1 : -1) * lift * 2.4 : 0), r: Math.max( 0.35, mix(from.r, to.r, p) - (signMorph ? 0.65 * lift : 0), ), alpha: mix(from.alpha, to.alpha, p), }; }); if (this.age >= MORPH_MS) this.moving = false; } else if (this.target === "orb") { this.time += delta; this.current = orb(this.time); } return { glyph: !this.moving && this.target !== "orb" ? this.target : null, dots: this.current, }; }}
/** Batch particles into eight SVG paths instead of hundreds of native views. */export function particlePaths(dots: Dot[]) { const paths = Array.from({ length: 8 }, () => ""); for (const d of dots) { if (d.alpha < 0.02) continue; const r = d.r.toFixed(2), diameter = (2 * d.r).toFixed(2); const index = Math.min(7, Math.max(0, Math.round(d.alpha * 8) - 1)); paths[index] += `M${(d.x - d.r).toFixed(2)} ${d.y.toFixed(2)}a${r} ${r} 0 1 0 ${diameter} 0a${r} ${r} 0 1 0 -${diameter} 0 `; } return paths;}