The live demo starts when it’s in view.Focus the arena and steer with the arrow keys or WASD, or press and drag · left alone the player circles by itself · Alone switches off the shots
The swarm under a 2D action game: hundreds of agents as typed-array columns (struct of arrays), each kind with its own way of closing in…
The swarm under a 2D action game: hundreds of agents as typed-array columns (struct of arrays), each kind with its own way of closing in (weaving, slithering then lunging, circling then diving, a heavy one that shoves the rest aside), kept apart through a uniform grid with the lighter giving way, clinging round what they chase instead of piling onto one point, hurt and knocked back by mass, and removed by swapping the last into their place. Previous positions are kept for drawing between ticks. Pure, allocation-free per tick, tested; you draw it.
Measured 21 November 2026 on RTX 4060 Laptop, 13th Gen Intel(R) Core(TM) i9-13900H, Chrome 157, 1920×1080 at 1×. 600 frames × 3 rounds, median kept (rounds ranged CPU 1.77–2.28 ms). What ran: Alone: 420 agents of four kinds chase a player circling the arena, keeping apart through the grid and clinging round it, stepped at 60 Hz; the frame includes the Canvas 2D drawing. Phones: tiers are being measured next.
How steadily it runs on RTX 4060 Laptop · 13th Gen Intel(R) Core(TM) i9-13900H · reference
Typical frame ?
16.81 ms
1% low ?
55 fps
Varies by ?
±0.43 ms
All metricsRTX 4060 Laptop · 13th Gen Intel(R) Core(TM) i9-13900H · referenceHow long each frame took. Most frames sit in the tall bars; a long tail to the right is hitching.
Frames run on a 60 Hz timer (a page demo), so most frames take about one tick, 16.7 ms: the spread around it is what matters.
Frames
Median (p50) ?
16.81 ms
p95 ?
17.57 ms
p99 ?
18.04 ms
Slowest ?
18.41 ms
Mean ?
16.83 ms
Standard deviation ?
0.43 ms
1% low ?
55 fps
0.1% low ?
54 fps
Hitches ?
0 of 600
Over budget ?
0 of 600
Main thread
Long tasks ?
0
Long frames ?
0
Event-loop delay ?
0.1 / 2.3 / 2.7 ms
Input
No input during this run.
Memory
JavaScript heap ?
35.4 → 35.9 MB
Growth ?
+0.45 MB
Browser
JavaScript a frame ?
2.03 ms
All tasks a frame ?
2.50 ms
Layout / style ?
0.02 / 0.01 ms
Main thread busy ?
15%
Garbage collection
Collections ?
14 (14 minor, 0 major)
Pauses ?
5.3 ms in all, longest 0.8
What it needs
Renders with
Canvas 2DA 2D canvas: every current browser.
Your browser
Runs hereNo WebGPU needed
Depends on
NothingNo other packages
With an agent
install("crowd-2d")Your coding agent reads its pitfalls and installs it through the library’s MCP server. Set it up
What it’s for
A horde that closes in on the player from every side, in a survivors-like or twin-stick game
Enemies that move differently by kind, so a crowd reads as several threats
A swarm that attacks an escort or a cargo as well as the player
Flocks, shoals or insects that chase a pointer on a landing page
Stress-testing a 2D renderer with hundreds of moving bodies
Pitfalls, in brief
Step at a fixed rate (60 Hz) and draw between ticks with px/py: the steering and the separation are tuned per tick, and a variable dt changes how a crowd packs. play-input’s lockstep gives you the clock.
Indices are slots, not identities: compact swaps the last agent into a dead one’s place.
onDead runs inside compact while the agent is still whole: read its position and kind there.
Separation is one pass a tick with each pair pushed once: a very dense crowd still overlaps a little.
The touch callback fires every tick an agent touches, for each one: scale damage by dt (damage a second times dt), and throttle anything you show or play (a hurt flash every 0.3 s, not every touch).
3 more in brief
The grid is built from where agents were at the start of the tick; nearest() reads it, so call nearest after step in the same tick (it can be off by one tick’s movement, which is a few pixels).
Capacity is fixed at creation and spawn returns -1 when full: keep your own cap under it (a device tier’s budget) and spawn against that, not against capacity.
It is deterministic only if you pass a seeded rand to spawn; the steering itself uses Math.sin, so the same seed gives the same run on one machine, not bit-for-bit across browsers (fine for single player, not for a network lockstep).
8 pitfalls in full, the hook-up guide, the module source and its tests come with the part.
Where it comes from
From another of the maker’s apps. Fully custom code.
swarmcrowdhordeboidssteeringseparationstruct of arraystyped arraysspatial gridno allocationgame2dtwin-sticksurvivors-likecanvas 2dno dependencies