// 032 - Barnsley Fern (botany)
// 1:1 Original algorithm engine source
function createBarnsleyFern() {
const SAMPLES_PER_FRAME = 3500;
return {
setup() {
},
render(context, timeState, params) {
const { ctx, width, height } = context;
const speed = Number(params.swaySpeed || 0.6);
const t = timeState.time * speed;
ctx.fillStyle = "rgba(6, 9, 10, 0.2)";
ctx.fillRect(0, 0, width, height);
let x = 0;
let y = 0;
const scale = Math.min(width, height) * 0.088;
const cx = width * 0.5 + Math.sin(t * 1.2) * 12;
const cy = height * 0.95;
const sway = Math.sin(t * 1.5) * 0.04;
for (let i = 0; i < SAMPLES_PER_FRAME; i++) {
const r = Math.random();
let nextX = 0;
let nextY = 0;
if (r < 0.01) {
nextX = 0;
nextY = 0.16 * y;
} else if (r < 0.86) {
nextX = 0.85 * x + (0.04 + sway) * y;
nextY = -0.04 * x + 0.85 * y + 1.6;
} else if (r < 0.93) {
nextX = 0.2 * x - 0.26 * y;
nextY = 0.23 * x + 0.22 * y + 1.6;
} else {
nextX = -0.15 * x + 0.28 * y;
nextY = 0.26 * x + 0.24 * y + 0.44;
}
x = nextX;
y = nextY;
const px = cx + x * scale;
const py = cy - y * scale;
const hue = (115 + y / 10 * 45 + t * 10) % 360;
ctx.fillStyle = hsla(hue, 90, 60, 0.7);
ctx.fillRect(px, py, 1.2, 1.2);
}
}
};
}
// Default parameters from content metadata
const defaultParams = [
{
"key": "swaySpeed",
"label": "Stem Sway Speed",
"type": "range",
"min": 0.2,
"max": 2,
"step": 0.1,
"defaultValue": 0.6,
"description": "Harmonic leaf sway rate"
}
];
if (!window.__art_instances) window.__art_instances = {};
if (!window.__art_instances['barnsley-fern']) {
const inst = typeof createBarnsleyFern === 'function' ? createBarnsleyFern() : null;
if (inst && inst.setup) {
inst.setup({ ctx, width, height, dpr: 1, aspectRatio: width / height }, defaultParams);
}
window.__art_instances['barnsley-fern'] = inst;
}
const instance = window.__art_instances['barnsley-fern'];
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
);
}