critter-fx

Canvas critters for the browser.

Bats, crows, owls, moths, bees, ghosts, fish, spiders, balloons and more that fly by, roost, swarm, wander, crawl and scatter.

One file, no dependencies.

Presets
Playback
Kinds
This kind
State

Getting started

Drop the file in and call Critters.start().

That is the whole setup, everything else is optional.

<script src="critter.js"></script>
<script>Critters.start('bats');</script>

Or install it from npm, where it is also served over unpkg:

npm install critter-fx
<script src="https://unpkg.com/critter-fx"></script>

Live config

Every option the library has is in the panel, and every control writes the real thing.

Sweep them until it looks right, then take the call below and paste it into your own page.

Only what differs from the defaults is listed, down to the single key inside a group and the single key on a kind.

This page starts with a faint glow so the dark kinds show up on it, which is why that one is there from the start.

Kinds and modes

A kind is what it looks like and how it moves: frames out of the atlas plus a gait.

A mode is what it is doing: crossing the screen, roosting, flocking, drifting, trailing the cursor or circling something.

Give several modes weights and each critter takes one, spread evenly through the field.

Critters.start({
	kinds: [{type: 'bat', weight: 3}, {type: 'ghost', weight: 1}],
	modes: {flyby: 2, roost: 1, wander: 1},
});

A kind is the whole animal

Each kind carries its own gait and its own behaviour, not just its own art.

A paper plane glides, stalls and drops its nose, a balloon rises and leaves by the top, a crow comes round in an arc, a lantern does not care about the pointer at all.

Pick a kind under This kind in the panel and change any key it has without touching the others, or write the same thing in the kinds list.

Critters.start({
	kinds: [
		{type: 'crow', flyby: {group: [4, 7]}, roost: {wake: 300}},
		{type: 'lantern', from: 'bottom', skittish: 0},
		{type: 'firefly', reaction: 'curious'},
	],
});

Who chases whom

Kinds notice each other.

Bats hunt moths, songbirds go after butterflies, bees and dragonflies, and all of them get out of the way of an owl.

Nothing is eaten: a catch sends the prey fleeing and fires a catch event.

Critters.start({kinds: ['bat', 'moth'], modes: 'wander'});
Critters.on('catch', ({hunter, prey}) => console.log(hunter, 'caught a', prey));

Crawlers

Spiders never fly.

They walk the edge they live on, drop on a thread or spin a web under it, and climb back up when the pointer comes close.

Put .card h3 in Roost on in the panel and they move under the card headings.

Critters.start({kinds: 'spider', modes: 'roost', roostOn: '.card h3', roost: {web: 0.5}});

Your own kinds

registerKind() adds a kind for every instance: start from an existing one with extends, or hand it an art function that draws one frame into a 100 by 100 box.

Critters.registerKind('albino', {extends: 'bat', colour: '#f2f0ea', accent: '#ff5a5a', skittish: 2});
Critters.registerKind('orb', {
	size: [10, 22], speed: [20, 40], flap: 1, rise: 0.5, from: 'bottom', skittish: 0, blend: 'lighter',
	art(ctx, spec, frame, frames) {
		ctx.fillStyle = spec.colour;
		ctx.beginPath();
		ctx.arc(50, 50, 30 + 8 * Math.sin((frame / frames) * Math.PI * 2), 0, Math.PI * 2);
		ctx.fill();
	},
});

Roosting on the page

Roosting bats hang from the top of the screen by default.

Point roostOn at some elements and they hang beneath those instead, following them as the page scrolls and taking off when one scrolls away.

Move the pointer close to one and it wakes.

Critters.start({modes: 'roost', roostOn: '.card h3'});

Every kind takes the holds it has a pose for: bats hang, crows stand, moths and butterflies stand and cling to the sides, and spiders do all three.

roost.where picks between the window, the elements and both, roost.sides which edges are on offer, and a kind can carry its own copy of either.

Critters.start({
	kinds: [{type: 'bat', roost: {where: 'window'}}, 'butterfly', 'spider'],
	modes: 'roost',
	roostOn: '.card h3',
});

Bursts and scares

burst() launches a one-off group from a point or an element.

scare() sends everything near a point fleeing, perched or not.

Moth to a flame

Orbit circles the pointer by default, or any point or element.

Set the orbit Target in the panel to #Lantern, the light below, and give orbit some weight.

Or put it in Attract to: attractTo pulls in whatever wanders within reach, whatever mode it is in, and avoidOn keeps them out of elements they should not cross.

Several at once

Critters.create() returns an independent instance with the same API and its own canvas.

This one adds fireflies on top of whatever the panel is running.

const fireflies = Critters.create({kinds: 'firefly', modes: 'wander', count: 30});
fireflies.start();

Scoped to an element

Critters can be bound to a single element rather than the viewport, with a ResizeObserver keeping the canvas on the box.

The edges they roost on and enter by become the edges of the box.

Critters.start({bounds: '#ScopedBox'});
scoped critter target

Option reference

Every control in the panel, the option it writes, and the value it starts on.

The same text sits on each control as a tooltip, and the box at the top of the panel finds one by either name.

Control Option Default What it does

Under This kind, every key a kind entry can carry.

Each one starts on the picked kind's own value, so there is no single default to list.

Control Key What it does