// 008 - Viscous Gyre (fluid)
// 1:1 Original algorithm engine source
function createViscousGyre() {
const MAX_PARTICLES = 1600;
const px = new Float32Array(MAX_PARTICLES);
const py = new Float32Array(MAX_PARTICLES);
return {
setup() {
for (let i = 0; i < MAX_PARTICLES; i++) {
px[i] = Math.random();
py[i] = Math.random();
}
},
render(context, timeState, params) {
const { ctx, width, height } = context;
const count = Math.min(MAX_PARTICLES, Number(params.particleCount || 1200));
const speed = Number(params.speed || 5e-3);
const t = timeState.time * 0.5;
ctx.fillStyle = "rgba(8, 9, 13, 0.15)";
ctx.fillRect(0, 0, width, height);
for (let i = 0; i < count; i++) {
const x = px[i];
const y = py[i];
const u = 2 * Math.PI * Math.sin(Math.PI * x) * Math.cos(2 * Math.PI * y + Math.sin(t) * 0.4);
const v = -Math.PI * Math.cos(Math.PI * x) * Math.sin(2 * Math.PI * y + Math.sin(t) * 0.4);
const prevPx = px[i] * width;
const prevPy = py[i] * height;
px[i] += u * speed;
py[i] += v * speed;
if (px[i] < 0 || px[i] > 1 || py[i] < 0 || py[i] > 1) {
px[i] = Math.random();
py[i] = Math.random();
continue;
}
const currPx = px[i] * width;
const currPy = py[i] * height;
const velMag = Math.sqrt(u * u + v * v);
const hue = (160 + velMag * 15 + t * 20) % 360;
ctx.strokeStyle = hsla(hue, 90, 60, Math.min(0.9, velMag * 0.15 + 0.3));
ctx.lineWidth = 1.2;
ctx.beginPath();
ctx.moveTo(prevPx, prevPy);
ctx.lineTo(currPx, currPy);
ctx.stroke();
}
}
};
}
// Default parameters from content metadata
const defaultParams = [
{
"key": "particleCount",
"label": "Current Tracers",
"type": "range",
"min": 400,
"max": 1600,
"step": 100,
"defaultValue": 1200,
"description": "Particle count"
},
{
"key": "speed",
"label": "Circulation Velocity",
"type": "range",
"min": 0.001,
"max": 0.015,
"step": 0.001,
"defaultValue": 0.005,
"description": "Advection timestep"
}
];
if (!window.__art_instances) window.__art_instances = {};
if (!window.__art_instances['viscous-gyre']) {
const inst = typeof createViscousGyre === 'function' ? createViscousGyre() : null;
if (inst && inst.setup) {
inst.setup({ ctx, width, height, dpr: 1, aspectRatio: width / height }, defaultParams);
}
window.__art_instances['viscous-gyre'] = inst;
}
const instance = window.__art_instances['viscous-gyre'];
if (instance && instance.render) {
instance.render(
{ ctx, width, height, dpr: 1, aspectRatio: width / height },
{ time, deltaTime: dt, frameCount: Math.floor(time * 60), fps: 60 },
defaultParams
);
}