feat(solidjs): implement fullscreen Canvas2D flow-field particle system driven by ML outputs
Transplant FlowFieldVisualizer from playground/js/ui/visualizer.js with TypeScript types. Create SolidJS FlowField component with own rAF render loop, reactive to output mode changes. Canvas resizes via ResizeObserver. Add 8 e2e tests covering VAL-VIS-001 (particles render), VAL-VIS-002 (joystick changes flow), VAL-VIS-003 (canvas resizes with window). Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
parent
d85f85f032
commit
b9e330beb4
5 changed files with 724 additions and 2 deletions
11
src/App.tsx
11
src/App.tsx
|
|
@ -4,6 +4,7 @@ import { createInputStore, type InputStore } from './stores/input-store';
|
||||||
import { exposeDebugProbe } from './probe/debug-probe';
|
import { exposeDebugProbe } from './probe/debug-probe';
|
||||||
import bus from './bus';
|
import bus from './bus';
|
||||||
import Joystick from './components/input/Joystick';
|
import Joystick from './components/input/Joystick';
|
||||||
|
import FlowField from './components/visual/FlowField';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Root application component for the NISPS immersive app.
|
* Root application component for the NISPS immersive app.
|
||||||
|
|
@ -43,13 +44,15 @@ export default function App() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set mlStore BEFORE setReady(true) so the FlowField component
|
||||||
|
// receives a valid store reference when it first mounts.
|
||||||
|
mlStore = store;
|
||||||
|
|
||||||
setReady(true);
|
setReady(true);
|
||||||
setStatus(`Ready — ${outputs.length} outputs loaded`);
|
setStatus(`Ready — ${outputs.length} outputs loaded`);
|
||||||
|
|
||||||
// Expose debug probe if ?debug=1
|
// Expose debug probe if ?debug=1
|
||||||
exposeDebugProbe(store);
|
exposeDebugProbe(store);
|
||||||
|
|
||||||
mlStore = store;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setStatus(`Error: ${err}`);
|
setStatus(`Error: ${err}`);
|
||||||
console.error('WASM init failed:', err);
|
console.error('WASM init failed:', err);
|
||||||
|
|
@ -97,6 +100,10 @@ export default function App() {
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</Show>
|
</Show>
|
||||||
|
{/* Fullscreen flow field canvas (background, active in visual mode) */}
|
||||||
|
<Show when={ready() && mlStore !== null}>
|
||||||
|
<FlowField mlStore={mlStore!} />
|
||||||
|
</Show>
|
||||||
<Show when={ready() && inputStore()}>
|
<Show when={ready() && inputStore()}>
|
||||||
{(store) => <Joystick inputStore={store()} />}
|
{(store) => <Joystick inputStore={store()} />}
|
||||||
</Show>
|
</Show>
|
||||||
|
|
|
||||||
96
src/components/visual/FlowField.tsx
Normal file
96
src/components/visual/FlowField.tsx
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
/**
|
||||||
|
* FlowField Component — Fullscreen Canvas2D particle system.
|
||||||
|
*
|
||||||
|
* Renders a flow-field visualization driven by the first 20 ML outputs.
|
||||||
|
* Owns its own rAF render loop (per architecture: "each canvas owns its rAF").
|
||||||
|
* Reads the ML store's outputs signal each frame to update particle parameters.
|
||||||
|
*
|
||||||
|
* Canvas resizes with window via ResizeObserver.
|
||||||
|
* Only active when outputMode === 'visual'.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
onMount,
|
||||||
|
onCleanup,
|
||||||
|
createEffect,
|
||||||
|
Show,
|
||||||
|
} from 'solid-js';
|
||||||
|
import type { MLStore } from '../../stores/ml-store';
|
||||||
|
import { FlowFieldVisualizer } from '../../core/ui/flow-field';
|
||||||
|
import './flowfield.css';
|
||||||
|
|
||||||
|
export interface FlowFieldProps {
|
||||||
|
mlStore: MLStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function FlowField(props: FlowFieldProps) {
|
||||||
|
let canvasRef: HTMLCanvasElement | undefined;
|
||||||
|
let visualizer: FlowFieldVisualizer | undefined;
|
||||||
|
let rafId: number = 0;
|
||||||
|
|
||||||
|
// Resize handler — updates canvas buffer dimensions
|
||||||
|
function handleResize(): void {
|
||||||
|
if (visualizer && canvasRef) {
|
||||||
|
visualizer.resize();
|
||||||
|
visualizer.initParticles();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// rAF render loop — only runs when in visual mode
|
||||||
|
function animate(): void {
|
||||||
|
if (!visualizer) return;
|
||||||
|
const outputs = props.mlStore.outputs();
|
||||||
|
visualizer.setParams(outputs);
|
||||||
|
visualizer.draw();
|
||||||
|
rafId = requestAnimationFrame(animate);
|
||||||
|
}
|
||||||
|
|
||||||
|
function startLoop(): void {
|
||||||
|
stopLoop();
|
||||||
|
rafId = requestAnimationFrame(animate);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopLoop(): void {
|
||||||
|
if (rafId) {
|
||||||
|
cancelAnimationFrame(rafId);
|
||||||
|
rafId = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start/stop render loop based on output mode
|
||||||
|
createEffect(() => {
|
||||||
|
const mode = props.mlStore.state.outputMode;
|
||||||
|
if (mode === 'visual' && canvasRef) {
|
||||||
|
if (!visualizer) {
|
||||||
|
visualizer = new FlowFieldVisualizer(canvasRef);
|
||||||
|
}
|
||||||
|
startLoop();
|
||||||
|
} else {
|
||||||
|
stopLoop();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
// Set up resize observer
|
||||||
|
if (canvasRef) {
|
||||||
|
const observer = new ResizeObserver(() => {
|
||||||
|
handleResize();
|
||||||
|
});
|
||||||
|
observer.observe(canvasRef);
|
||||||
|
onCleanup(() => observer.disconnect());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
onCleanup(() => {
|
||||||
|
stopLoop();
|
||||||
|
visualizer = undefined;
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<canvas
|
||||||
|
ref={canvasRef}
|
||||||
|
id="flowfield-canvas"
|
||||||
|
class="flowfield-canvas"
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
11
src/components/visual/flowfield.css
Normal file
11
src/components/visual/flowfield.css
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
/* FlowField canvas — fullscreen background */
|
||||||
|
|
||||||
|
.flowfield-canvas {
|
||||||
|
position: fixed;
|
||||||
|
inset: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
display: block;
|
||||||
|
z-index: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
364
src/core/ui/flow-field.ts
Normal file
364
src/core/ui/flow-field.ts
Normal file
|
|
@ -0,0 +1,364 @@
|
||||||
|
/**
|
||||||
|
* FlowFieldVisualizer — Canvas2D particle system driven by ML outputs.
|
||||||
|
*
|
||||||
|
* Transplanted from playground/js/ui/visualizer.js with TypeScript types.
|
||||||
|
* Uses the first 20 ML outputs to control flow field parameters:
|
||||||
|
* p0: angleOffset — flow direction
|
||||||
|
* p1: scale — pattern size
|
||||||
|
* p2: speed — particle speed
|
||||||
|
* p3: hueBase — base color
|
||||||
|
* p4: hueSpread — color variation
|
||||||
|
* p5: particleSize — dot radius
|
||||||
|
* p6: fadeRate — trail length
|
||||||
|
* p7: turbulence — chaos
|
||||||
|
* p8: attractStrength — pull toward screen center
|
||||||
|
* p9: attractRadius — radius where attraction is strongest
|
||||||
|
* p10: dispersionRate — speed of outward dispersion pulses
|
||||||
|
* p11: dispersionAmount — strength of outward dispersion
|
||||||
|
* p12: particleLifetime — average frames before respawn
|
||||||
|
* p13: respawnStyle — 0=random, 1=edge, 2=center-burst
|
||||||
|
* p14: advectionMode — flow→orbit→radial blend
|
||||||
|
* p15: inertia — velocity memory
|
||||||
|
* p16: drag — velocity damping
|
||||||
|
* p17: repulsorStrength — repulsor force amount
|
||||||
|
* p18: repulsorCount — number of active repulsors
|
||||||
|
* p19: repulsorOrbitRate — repulsor orbital speed
|
||||||
|
*/
|
||||||
|
|
||||||
|
const TWO_PI = Math.PI * 2;
|
||||||
|
|
||||||
|
// ─── Value noise (no dependencies) ────────────────────────────────────
|
||||||
|
|
||||||
|
const PERM = new Uint8Array(512);
|
||||||
|
{
|
||||||
|
const p = new Uint8Array(256);
|
||||||
|
for (let i = 0; i < 256; i++) p[i] = i;
|
||||||
|
for (let i = 255; i > 0; i--) {
|
||||||
|
const j = Math.floor(Math.random() * (i + 1));
|
||||||
|
[p[i], p[j]] = [p[j], p[i]];
|
||||||
|
}
|
||||||
|
for (let i = 0; i < 512; i++) PERM[i] = p[i & 255];
|
||||||
|
}
|
||||||
|
|
||||||
|
function fade(t: number): number { return t * t * t * (t * (t * 6 - 15) + 10); }
|
||||||
|
function lerp(a: number, b: number, t: number): number { return a + t * (b - a); }
|
||||||
|
|
||||||
|
function grad(hash: number, x: number, y: number): number {
|
||||||
|
const h = hash & 3;
|
||||||
|
const u = h < 2 ? x : y;
|
||||||
|
const v = h < 2 ? y : x;
|
||||||
|
return ((h & 1) ? -u : u) + ((h & 2) ? -v : v);
|
||||||
|
}
|
||||||
|
|
||||||
|
function noise2D(x: number, y: number): number {
|
||||||
|
const X = Math.floor(x) & 255;
|
||||||
|
const Y = Math.floor(y) & 255;
|
||||||
|
const xf = x - Math.floor(x);
|
||||||
|
const yf = y - Math.floor(y);
|
||||||
|
const u = fade(xf);
|
||||||
|
const v = fade(yf);
|
||||||
|
|
||||||
|
const aa = PERM[PERM[X] + Y];
|
||||||
|
const ab = PERM[PERM[X] + Y + 1];
|
||||||
|
const ba = PERM[PERM[X + 1] + Y];
|
||||||
|
const bb = PERM[PERM[X + 1] + Y + 1];
|
||||||
|
|
||||||
|
return lerp(
|
||||||
|
lerp(grad(aa, xf, yf), grad(ba, xf - 1, yf), u),
|
||||||
|
lerp(grad(ab, xf, yf - 1), grad(bb, xf - 1, yf - 1), u),
|
||||||
|
v
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Particle interface ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
interface Particle {
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
id: number;
|
||||||
|
age: number;
|
||||||
|
life: number;
|
||||||
|
vx: number;
|
||||||
|
vy: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Parameters ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export interface FlowFieldParams {
|
||||||
|
angleOffset: number;
|
||||||
|
scale: number;
|
||||||
|
speed: number;
|
||||||
|
hueBase: number;
|
||||||
|
hueSpread: number;
|
||||||
|
particleSize: number;
|
||||||
|
fadeRate: number;
|
||||||
|
turbulence: number;
|
||||||
|
attractStrength: number;
|
||||||
|
attractRadius: number;
|
||||||
|
dispersionRate: number;
|
||||||
|
dispersionAmount: number;
|
||||||
|
particleLifetime: number;
|
||||||
|
respawnStyle: number;
|
||||||
|
advectionMode: number;
|
||||||
|
inertia: number;
|
||||||
|
drag: number;
|
||||||
|
repulsorStrength: number;
|
||||||
|
repulsorCount: number;
|
||||||
|
repulsorOrbitRate: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Visualizer ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export class FlowFieldVisualizer {
|
||||||
|
canvas: HTMLCanvasElement;
|
||||||
|
ctx: CanvasRenderingContext2D;
|
||||||
|
particles: Particle[];
|
||||||
|
numParticles: number;
|
||||||
|
time: number;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
params: FlowFieldParams;
|
||||||
|
|
||||||
|
constructor(canvas: HTMLCanvasElement) {
|
||||||
|
this.canvas = canvas;
|
||||||
|
this.ctx = canvas.getContext('2d')!;
|
||||||
|
this.particles = [];
|
||||||
|
this.numParticles = 400;
|
||||||
|
this.time = 0;
|
||||||
|
this.width = 0;
|
||||||
|
this.height = 0;
|
||||||
|
|
||||||
|
// Default parameters
|
||||||
|
this.params = {
|
||||||
|
angleOffset: 0,
|
||||||
|
scale: 0.005,
|
||||||
|
speed: 2,
|
||||||
|
hueBase: 180,
|
||||||
|
hueSpread: 60,
|
||||||
|
particleSize: 3,
|
||||||
|
fadeRate: 0.05,
|
||||||
|
turbulence: 1,
|
||||||
|
attractStrength: 0.8,
|
||||||
|
attractRadius: 200,
|
||||||
|
dispersionRate: 2.0,
|
||||||
|
dispersionAmount: 1.0,
|
||||||
|
particleLifetime: 220,
|
||||||
|
respawnStyle: 0.0,
|
||||||
|
advectionMode: 0.0,
|
||||||
|
inertia: 0.2,
|
||||||
|
drag: 0.02,
|
||||||
|
repulsorStrength: 0.0,
|
||||||
|
repulsorCount: 0,
|
||||||
|
repulsorOrbitRate: 0.8,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.resize();
|
||||||
|
this.initParticles();
|
||||||
|
}
|
||||||
|
|
||||||
|
resize(): void {
|
||||||
|
const rect = this.canvas.getBoundingClientRect();
|
||||||
|
const dpr = window.devicePixelRatio || 1;
|
||||||
|
this.canvas.width = rect.width * dpr;
|
||||||
|
this.canvas.height = rect.height * dpr;
|
||||||
|
this.ctx.scale(dpr, dpr);
|
||||||
|
this.width = rect.width;
|
||||||
|
this.height = rect.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
initParticles(): void {
|
||||||
|
this.particles = [];
|
||||||
|
for (let i = 0; i < this.numParticles; i++) {
|
||||||
|
this.particles.push(this.makeParticle(i));
|
||||||
|
}
|
||||||
|
// Clear canvas to black
|
||||||
|
this.ctx.fillStyle = '#0d0d0d';
|
||||||
|
this.ctx.fillRect(0, 0, this.width, this.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
makeParticle(id: number): Particle {
|
||||||
|
return {
|
||||||
|
x: Math.random() * this.width,
|
||||||
|
y: Math.random() * this.height,
|
||||||
|
id,
|
||||||
|
age: Math.floor(Math.random() * this.params.particleLifetime),
|
||||||
|
life: this.computeLifetime(),
|
||||||
|
vx: 0,
|
||||||
|
vy: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
computeLifetime(): number {
|
||||||
|
const variance = 0.65 + Math.random() * 0.7;
|
||||||
|
return Math.max(10, Math.floor(this.params.particleLifetime * variance));
|
||||||
|
}
|
||||||
|
|
||||||
|
respawnParticle(p: Particle): void {
|
||||||
|
const mode = Math.min(2, Math.floor(this.params.respawnStyle * 2.999));
|
||||||
|
const { width, height } = this;
|
||||||
|
|
||||||
|
if (mode === 1) {
|
||||||
|
// Edge respawn
|
||||||
|
const side = Math.floor(Math.random() * 4);
|
||||||
|
if (side === 0) { p.x = Math.random() * width; p.y = 0; }
|
||||||
|
if (side === 1) { p.x = width; p.y = Math.random() * height; }
|
||||||
|
if (side === 2) { p.x = Math.random() * width; p.y = height; }
|
||||||
|
if (side === 3) { p.x = 0; p.y = Math.random() * height; }
|
||||||
|
const towardCenterX = width * 0.5 - p.x;
|
||||||
|
const towardCenterY = height * 0.5 - p.y;
|
||||||
|
const inwardDist = Math.hypot(towardCenterX, towardCenterY) + 1e-6;
|
||||||
|
p.vx = (towardCenterX / inwardDist) * 2.0;
|
||||||
|
p.vy = (towardCenterY / inwardDist) * 2.0;
|
||||||
|
} else if (mode === 2) {
|
||||||
|
// Center-burst respawn
|
||||||
|
const angle = Math.random() * TWO_PI;
|
||||||
|
const r = Math.random() * Math.min(width, height) * 0.08;
|
||||||
|
p.x = width * 0.5 + Math.cos(angle) * r;
|
||||||
|
p.y = height * 0.5 + Math.sin(angle) * r;
|
||||||
|
p.vx = Math.cos(angle) * 2.5;
|
||||||
|
p.vy = Math.sin(angle) * 2.5;
|
||||||
|
} else {
|
||||||
|
// Random respawn
|
||||||
|
p.x = Math.random() * width;
|
||||||
|
p.y = Math.random() * height;
|
||||||
|
p.vx = (Math.random() * 2 - 1) * 0.5;
|
||||||
|
p.vy = (Math.random() * 2 - 1) * 0.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
p.age = 0;
|
||||||
|
p.life = this.computeLifetime();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Set parameters from ML output (all values 0-1, first 20 outputs) */
|
||||||
|
setParams(outputs: Float32Array | number[]): void {
|
||||||
|
if (!outputs || outputs.length < 20) return;
|
||||||
|
this.params.angleOffset = outputs[0] * TWO_PI;
|
||||||
|
this.params.scale = 0.001 + outputs[1] * 0.009;
|
||||||
|
this.params.speed = 0.5 + outputs[2] * 4.5;
|
||||||
|
this.params.hueBase = outputs[3] * 360;
|
||||||
|
this.params.hueSpread = outputs[4] * 120;
|
||||||
|
this.params.particleSize = 1 + outputs[5] * 5;
|
||||||
|
this.params.fadeRate = 0.01 + outputs[6] * 0.14;
|
||||||
|
this.params.turbulence = outputs[7] * 2;
|
||||||
|
this.params.attractStrength = 0.1 + outputs[8] * 2.9;
|
||||||
|
this.params.attractRadius = 40 + outputs[9] * 420;
|
||||||
|
this.params.dispersionRate = 0.2 + outputs[10] * 8;
|
||||||
|
this.params.dispersionAmount = outputs[11] * 3;
|
||||||
|
this.params.particleLifetime = 30 + outputs[12] * 470;
|
||||||
|
this.params.respawnStyle = outputs[13];
|
||||||
|
this.params.advectionMode = outputs[14];
|
||||||
|
this.params.inertia = outputs[15] * 0.98;
|
||||||
|
this.params.drag = outputs[16] * 0.35;
|
||||||
|
this.params.repulsorStrength = outputs[17] * 4.5;
|
||||||
|
this.params.repulsorCount = Math.floor(outputs[18] * 4.999);
|
||||||
|
this.params.repulsorOrbitRate = 0.1 + outputs[19] * 2.9;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Draw one frame of the particle system */
|
||||||
|
draw(): void {
|
||||||
|
const { ctx, width, height, params } = this;
|
||||||
|
if (width === 0 || height === 0) return;
|
||||||
|
this.time += 0.003;
|
||||||
|
|
||||||
|
// Fade existing content (creates trails)
|
||||||
|
ctx.fillStyle = `rgba(13, 13, 13, ${params.fadeRate})`;
|
||||||
|
ctx.fillRect(0, 0, width, height);
|
||||||
|
|
||||||
|
for (const p of this.particles) {
|
||||||
|
const cx = width * 0.5;
|
||||||
|
const cy = height * 0.5;
|
||||||
|
|
||||||
|
// Sample flow field
|
||||||
|
const nx = p.x * params.scale;
|
||||||
|
const ny = p.y * params.scale;
|
||||||
|
const angle = noise2D(nx + this.time, ny) * TWO_PI + params.angleOffset;
|
||||||
|
const curl = noise2D(nx + 100, ny + 100 + this.time * 0.5) * params.turbulence;
|
||||||
|
|
||||||
|
// Mix between three advection fields
|
||||||
|
const flowVx = Math.cos(angle + curl) * params.speed;
|
||||||
|
const flowVy = Math.sin(angle + curl) * params.speed;
|
||||||
|
const fromCenterX = p.x - cx;
|
||||||
|
const fromCenterY = p.y - cy;
|
||||||
|
const centerDist = Math.hypot(fromCenterX, fromCenterY) + 1e-6;
|
||||||
|
const radialX = fromCenterX / centerDist;
|
||||||
|
const radialY = fromCenterY / centerDist;
|
||||||
|
const orbitX = -radialY;
|
||||||
|
const orbitY = radialX;
|
||||||
|
const orbitVx = orbitX * params.speed;
|
||||||
|
const orbitVy = orbitY * params.speed;
|
||||||
|
const radialVx = radialX * params.speed;
|
||||||
|
const radialVy = radialY * params.speed;
|
||||||
|
|
||||||
|
const modeBlend = params.advectionMode * 2;
|
||||||
|
let targetVx: number;
|
||||||
|
let targetVy: number;
|
||||||
|
if (modeBlend < 1) {
|
||||||
|
targetVx = lerp(flowVx, orbitVx, modeBlend);
|
||||||
|
targetVy = lerp(flowVy, orbitVy, modeBlend);
|
||||||
|
} else {
|
||||||
|
targetVx = lerp(orbitVx, radialVx, modeBlend - 1);
|
||||||
|
targetVy = lerp(orbitVy, radialVy, modeBlend - 1);
|
||||||
|
}
|
||||||
|
p.vx = p.vx * params.inertia + targetVx * (1 - params.inertia);
|
||||||
|
p.vy = p.vy * params.inertia + targetVy * (1 - params.inertia);
|
||||||
|
p.vx *= (1 - params.drag);
|
||||||
|
p.vy *= (1 - params.drag);
|
||||||
|
let nextX = p.x + p.vx;
|
||||||
|
let nextY = p.y + p.vy;
|
||||||
|
|
||||||
|
// Central attractor
|
||||||
|
const dx = cx - nextX;
|
||||||
|
const dy = cy - nextY;
|
||||||
|
const dist = Math.hypot(dx, dy) + 1e-6;
|
||||||
|
const nxCenter = dx / dist;
|
||||||
|
const nyCenter = dy / dist;
|
||||||
|
const normalizedDist = Math.min(dist / params.attractRadius, 2);
|
||||||
|
const falloff = 1 / (1 + normalizedDist * normalizedDist);
|
||||||
|
nextX += nxCenter * params.attractStrength * falloff;
|
||||||
|
nextY += nyCenter * params.attractStrength * falloff;
|
||||||
|
|
||||||
|
// Time-varying dispersion
|
||||||
|
const dispersionPulse = 0.5 + 0.5 * Math.sin(this.time * params.dispersionRate + p.id * 0.07);
|
||||||
|
const dispersionForce = params.dispersionAmount * dispersionPulse * falloff;
|
||||||
|
nextX -= nxCenter * dispersionForce;
|
||||||
|
nextY -= nyCenter * dispersionForce;
|
||||||
|
|
||||||
|
// Orbiting repulsor points
|
||||||
|
const repulsorRadius = Math.min(width, height) * 0.28;
|
||||||
|
for (let r = 0; r < params.repulsorCount; r++) {
|
||||||
|
const phase = this.time * params.repulsorOrbitRate + (r / 4) * TWO_PI;
|
||||||
|
const wobble = 0.6 + 0.15 * r;
|
||||||
|
const rx = cx + Math.cos(phase * (1.0 + wobble)) * repulsorRadius;
|
||||||
|
const ry = cy + Math.sin(phase * (1.3 + wobble)) * repulsorRadius;
|
||||||
|
const repulseDx = nextX - rx;
|
||||||
|
const repulseDy = nextY - ry;
|
||||||
|
const distSq = repulseDx * repulseDx + repulseDy * repulseDy + 160;
|
||||||
|
const distInv = 1 / Math.sqrt(distSq);
|
||||||
|
const force = params.repulsorStrength * (650 / distSq);
|
||||||
|
nextX += repulseDx * distInv * force;
|
||||||
|
nextY += repulseDy * distInv * force;
|
||||||
|
}
|
||||||
|
|
||||||
|
p.x = nextX;
|
||||||
|
p.y = nextY;
|
||||||
|
|
||||||
|
// Wrap around edges
|
||||||
|
if (p.x < 0) p.x += width;
|
||||||
|
if (p.x > width) p.x -= width;
|
||||||
|
if (p.y < 0) p.y += height;
|
||||||
|
if (p.y > height) p.y -= height;
|
||||||
|
|
||||||
|
p.age += 1;
|
||||||
|
if (p.age >= p.life) this.respawnParticle(p);
|
||||||
|
|
||||||
|
// Color based on particle id + hue params
|
||||||
|
const hue = (params.hueBase + (p.id / this.numParticles) * params.hueSpread) % 360;
|
||||||
|
const lightness = 50 + Math.sin(p.id * 0.1 + this.time) * 15;
|
||||||
|
|
||||||
|
ctx.fillStyle = `hsl(${hue}, 75%, ${lightness}%)`;
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(p.x, p.y, params.particleSize, 0, TWO_PI);
|
||||||
|
ctx.fill();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
244
tests/solidjs/flowfield.spec.ts
Normal file
244
tests/solidjs/flowfield.spec.ts
Normal file
|
|
@ -0,0 +1,244 @@
|
||||||
|
/**
|
||||||
|
* e2e tests for f07-flowfield-canvas.
|
||||||
|
* Validates:
|
||||||
|
* VAL-VIS-001: Particles render in visual mode (flow-field on fullscreen canvas)
|
||||||
|
* VAL-VIS-002: Particles respond to ML outputs (changing joystick changes flow)
|
||||||
|
* VAL-VIS-003: Canvas resizes with window
|
||||||
|
*
|
||||||
|
* Approach:
|
||||||
|
* - Switch to visual mode via __nispsStore.setOutputMode('visual')
|
||||||
|
* - Wait for mode switch to take effect (outputCount = 20)
|
||||||
|
* - Verify canvas renders particles (screenshot + canvas pixel analysis)
|
||||||
|
* - Move joystick, verify particles change
|
||||||
|
* - Resize window, verify canvas fills viewport
|
||||||
|
*/
|
||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
import { loadSolidApp } from './helpers';
|
||||||
|
|
||||||
|
test.describe('FlowField Canvas (f07)', () => {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper: switch to visual mode and wait for output count to change.
|
||||||
|
*/
|
||||||
|
async function switchToVisualMode(page: import('@playwright/test').Page) {
|
||||||
|
await page.evaluate(async () => {
|
||||||
|
const store = (window as any).__nispsStore;
|
||||||
|
await store.setOutputMode('visual');
|
||||||
|
});
|
||||||
|
// Wait for outputCount to reflect visual mode
|
||||||
|
await page.waitForFunction(
|
||||||
|
() => (window as any).__nispsStore.outputCount() === 20,
|
||||||
|
{ timeout: 10_000 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── VAL-VIS-001: Particles render in visual mode ───
|
||||||
|
|
||||||
|
test('VAL-VIS-001a: fullscreen canvas exists in DOM in visual mode', async ({ page }) => {
|
||||||
|
await loadSolidApp(page);
|
||||||
|
await switchToVisualMode(page);
|
||||||
|
|
||||||
|
const canvas = page.locator('#flowfield-canvas');
|
||||||
|
await expect(canvas).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('VAL-VIS-001b: canvas fills entire viewport in visual mode', async ({ page }) => {
|
||||||
|
await loadSolidApp(page);
|
||||||
|
await switchToVisualMode(page);
|
||||||
|
|
||||||
|
const canvas = page.locator('#flowfield-canvas');
|
||||||
|
const box = await canvas.boundingBox();
|
||||||
|
|
||||||
|
expect(box).not.toBeNull();
|
||||||
|
const viewport = page.viewportSize()!;
|
||||||
|
// Canvas should fill the viewport (allow small rounding differences)
|
||||||
|
expect(Math.abs(box!.width - viewport.width)).toBeLessThan(5);
|
||||||
|
expect(Math.abs(box!.height - viewport.height)).toBeLessThan(5);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('VAL-VIS-001c: canvas has colored pixels (particles render)', async ({ page }) => {
|
||||||
|
await loadSolidApp(page);
|
||||||
|
await switchToVisualMode(page);
|
||||||
|
|
||||||
|
// Wait for particles to render (400 particles need a few frames)
|
||||||
|
await page.waitForTimeout(1000);
|
||||||
|
|
||||||
|
// Check that the canvas has non-black, non-uniform pixels.
|
||||||
|
// Sample a large grid area since particles are sparse (400 on 1280x720).
|
||||||
|
const hasColoredPixels = await page.evaluate(() => {
|
||||||
|
const canvas = document.getElementById('flowfield-canvas') as HTMLCanvasElement;
|
||||||
|
if (!canvas) return false;
|
||||||
|
const ctx = canvas.getContext('2d')!;
|
||||||
|
const w = canvas.width;
|
||||||
|
const h = canvas.height;
|
||||||
|
// Sample a 10x10 grid of 5x5 pixel blocks
|
||||||
|
let coloredCount = 0;
|
||||||
|
for (let gy = 0; gy < 10; gy++) {
|
||||||
|
for (let gx = 0; gx < 10; gx++) {
|
||||||
|
const px = Math.floor(w * (gx + 0.5) / 10);
|
||||||
|
const py = Math.floor(h * (gy + 0.5) / 10);
|
||||||
|
const size = 5;
|
||||||
|
const data = ctx.getImageData(px, py, size, size).data;
|
||||||
|
// Check if any pixel in this block has color
|
||||||
|
for (let i = 0; i < data.length; i += 4) {
|
||||||
|
if (data[i] > 10 || data[i + 1] > 10 || data[i + 2] > 10) {
|
||||||
|
coloredCount++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coloredCount > 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(hasColoredPixels).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── VAL-VIS-002: Particles respond to ML outputs ───
|
||||||
|
|
||||||
|
test('VAL-VIS-002a: changing joystick position changes canvas pixels', async ({ page }) => {
|
||||||
|
await loadSolidApp(page);
|
||||||
|
await switchToVisualMode(page);
|
||||||
|
|
||||||
|
// Set initial position and wait for render
|
||||||
|
await page.evaluate(() => {
|
||||||
|
(window as any).__nisps.setInputs(0.2, 0.2);
|
||||||
|
});
|
||||||
|
await page.waitForTimeout(500);
|
||||||
|
|
||||||
|
// Capture canvas state at position 1
|
||||||
|
const pixelsBefore = await page.evaluate(() => {
|
||||||
|
const canvas = document.getElementById('flowfield-canvas') as HTMLCanvasElement;
|
||||||
|
const ctx = canvas.getContext('2d')!;
|
||||||
|
const w = canvas.width;
|
||||||
|
const h = canvas.height;
|
||||||
|
// Sample a grid of pixels
|
||||||
|
const data = ctx.getImageData(0, 0, w, h).data;
|
||||||
|
let sum = 0;
|
||||||
|
for (let i = 0; i < data.length; i += 4) {
|
||||||
|
sum += data[i] + data[i + 1] + data[i + 2];
|
||||||
|
}
|
||||||
|
return { avgBrightness: sum / (data.length / 4), width: w, height: h };
|
||||||
|
});
|
||||||
|
|
||||||
|
// Move to a very different position
|
||||||
|
await page.evaluate(() => {
|
||||||
|
(window as any).__nisps.setInputs(0.8, 0.8);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Wait for particles to respond and move
|
||||||
|
await page.waitForTimeout(1000);
|
||||||
|
|
||||||
|
// Capture canvas state at position 2
|
||||||
|
const pixelsAfter = await page.evaluate(() => {
|
||||||
|
const canvas = document.getElementById('flowfield-canvas') as HTMLCanvasElement;
|
||||||
|
const ctx = canvas.getContext('2d')!;
|
||||||
|
const w = canvas.width;
|
||||||
|
const h = canvas.height;
|
||||||
|
const data = ctx.getImageData(0, 0, w, h).data;
|
||||||
|
let sum = 0;
|
||||||
|
for (let i = 0; i < data.length; i += 4) {
|
||||||
|
sum += data[i] + data[i + 1] + data[i + 2];
|
||||||
|
}
|
||||||
|
return { avgBrightness: sum / (data.length / 4), width: w, height: h };
|
||||||
|
});
|
||||||
|
|
||||||
|
// The average brightness should be different after moving the joystick
|
||||||
|
// (particles flow differently, trails change)
|
||||||
|
expect(Math.abs(pixelsBefore.avgBrightness - pixelsAfter.avgBrightness)).toBeGreaterThan(0.01);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('VAL-VIS-002b: ML outputs are routed to flow field in visual mode', async ({ page }) => {
|
||||||
|
await loadSolidApp(page);
|
||||||
|
await switchToVisualMode(page);
|
||||||
|
|
||||||
|
// Verify output count is 20 in visual mode
|
||||||
|
const outputCount = await page.evaluate(() => {
|
||||||
|
return (window as any).__nispsStore.outputCount();
|
||||||
|
});
|
||||||
|
expect(outputCount).toBe(20);
|
||||||
|
|
||||||
|
// Verify outputs are all bounded [0,1]
|
||||||
|
const outputs = await page.evaluate(() => {
|
||||||
|
const arr = (window as any).__nisps.getOutputs();
|
||||||
|
return Array.from(arr) as number[];
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(outputs.length).toBe(20);
|
||||||
|
expect(outputs.every(v => v >= 0 && v <= 1)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── VAL-VIS-003: Canvas resizes with window ───
|
||||||
|
|
||||||
|
test('VAL-VIS-003a: canvas resizes when window is resized', async ({ page }) => {
|
||||||
|
await loadSolidApp(page);
|
||||||
|
await switchToVisualMode(page);
|
||||||
|
|
||||||
|
// Get initial size
|
||||||
|
const initialSize = await page.evaluate(() => {
|
||||||
|
const canvas = document.getElementById('flowfield-canvas') as HTMLCanvasElement;
|
||||||
|
return {
|
||||||
|
cssWidth: canvas.clientWidth,
|
||||||
|
cssHeight: canvas.clientHeight,
|
||||||
|
bufferWidth: canvas.width,
|
||||||
|
bufferHeight: canvas.height,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// Resize viewport
|
||||||
|
await page.setViewportSize({ width: 800, height: 600 });
|
||||||
|
await page.waitForTimeout(200);
|
||||||
|
|
||||||
|
// Check canvas resized
|
||||||
|
const resizedSize = await page.evaluate(() => {
|
||||||
|
const canvas = document.getElementById('flowfield-canvas') as HTMLCanvasElement;
|
||||||
|
return {
|
||||||
|
cssWidth: canvas.clientWidth,
|
||||||
|
cssHeight: canvas.clientHeight,
|
||||||
|
bufferWidth: canvas.width,
|
||||||
|
bufferHeight: canvas.height,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// CSS size should match new viewport
|
||||||
|
expect(resizedSize.cssWidth).toBe(800);
|
||||||
|
expect(resizedSize.cssHeight).toBe(600);
|
||||||
|
|
||||||
|
// Buffer should be different from initial
|
||||||
|
expect(resizedSize.bufferWidth).not.toBe(initialSize.bufferWidth);
|
||||||
|
expect(resizedSize.bufferHeight).not.toBe(initialSize.bufferHeight);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('VAL-VIS-003b: canvas fills viewport after multiple resize events', async ({ page }) => {
|
||||||
|
await loadSolidApp(page);
|
||||||
|
await switchToVisualMode(page);
|
||||||
|
|
||||||
|
// Resize multiple times
|
||||||
|
for (const [w, h] of [[1024, 768], [640, 480], [1920, 1080]]) {
|
||||||
|
await page.setViewportSize({ width: w, height: h });
|
||||||
|
await page.waitForTimeout(150);
|
||||||
|
|
||||||
|
const size = await page.evaluate(() => {
|
||||||
|
const canvas = document.getElementById('flowfield-canvas') as HTMLCanvasElement;
|
||||||
|
return { cssWidth: canvas.clientWidth, cssHeight: canvas.clientHeight };
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(size.cssWidth).toBe(w);
|
||||||
|
expect(size.cssHeight).toBe(h);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── Integration: flow field hidden when not in visual mode ───
|
||||||
|
|
||||||
|
test('flow field canvas is visible only in visual mode', async ({ page }) => {
|
||||||
|
await loadSolidApp(page);
|
||||||
|
// App starts in synth mode — flow field should be hidden
|
||||||
|
const canvasInSynth = page.locator('#flowfield-canvas');
|
||||||
|
// In synth mode, canvas may exist but shouldn't be actively rendering
|
||||||
|
// (it may be hidden or just not have the visualizer running)
|
||||||
|
|
||||||
|
// Switch to visual mode
|
||||||
|
await switchToVisualMode(page);
|
||||||
|
await expect(canvasInSynth).toBeVisible();
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue