MICHAEL KAHENCOMPUTER ENGINEER

Biosphere Unit · PROJECT GUIDE

ASCII ecosystem simulation

I built Biosphere Unit as an autonomous ecosystem simulation in JavaScript. Watch ASCII wildlife find food, avoid threats, hunt, and reproduce in a procedural world, with Canvas 2D rendering and live population telemetry.

By Michael Kahen ·

Agent-based simulation · Procedural generation · JavaScript · Canvas 2D

The interactive demo opens in my portfolio and requires JavaScript. This guide is readable without it.

Run the ecosystem

Open the simulation and watch the time, generation, and population readouts alongside the world. If motion is paused by your display preference, use the simulation's pause control to resume.

  • Space / Pause: freeze the world to inspect the current scene.
  • S / Speed: cycle through the available simulation speeds.
  • R / Reset: restart the ecosystem and observe a new run.

Observe a group of deer on land, then compare their movement with fish in the lake. Watch how population counts change as feeding, hunting, aging, and reproduction occur. The system runs autonomously; you do not need to direct each animal.

Agent behavior and food

Each simulated animal has its own position, velocity, energy, age, species, and inherited traits. Its behavior combines wandering, seeking, fleeing, and separation. That separation keeps nearby agents from behaving like one overlapping point, while habitat boundaries constrain where they can move.

The world includes deer, snakes, and a tiger on land, plus fish and an alligator in the lake. Food resources include berries on land and plankton in water. Seeking and hunting connect movement to survival: animals consume energy over time and gain it from food or prey.

Hunger changes behavior. An animal's energy reserve affects when it seeks food or hunts, so the same species can act differently at different moments. Population changes emerge from these local decisions rather than a prewritten animation of the whole scene.

Inheritance and population changes

Reproduction depends on conditions such as a parent's energy and reproduction timing. Creating offspring costs energy. New agents inherit traits with bounded mutation, creating variation across generations instead of identical copies.

Energy depletion, aging, and predation remove animals. The generation counter tracks the highest generation reached; it does not mean every living agent belongs to that generation. The population readout tracks the modeled wildlife, while decorative birds are handled separately.

The demo also includes a recovery rule: when a species disappears, it is reseeded after a short delay. In the implementation, that delay is six seconds of simulation time. This keeps an interactive run active, but it means the world is not a closed ecological model where extinction is permanent.

Procedural terrain and habitats

The terrain generator creates one substantial lake with an irregular shoreline, alongside vegetation and structures. Habitat checks keep fish and alligators in water and terrestrial wildlife on land. Boundary correction prevents an agent's movement from carrying it into the wrong habitat.

Terrain generation uses seeded randomness, while animal behavior also uses runtime randomness. A seeded landscape alone therefore does not make the entire simulation deterministic. Resetting can produce a different sequence of encounters and population changes.

The world responds to viewport changes. Animal and resource positions scale with the canvas, and habitat checks keep them in valid regions after a resize. This lets the same simulation fit a desktop workspace or a smaller mobile viewport.

Fixed-step simulation and Canvas rendering

The simulation updates in fixed steps of 1 / 30 second. Rendering runs through requestAnimationFrame, which follows the browser's display schedule. An accumulator connects elapsed time to simulation updates, keeping behavior from depending on one update per drawn frame.

Separating the update loop from drawing lets the system change simulation speed without changing the meaning of each behavior step. The loop limits catch-up work so a delayed frame does not trigger an unbounded number of updates.

I cache background terrain instead of redrawing all static details from scratch each frame. The responsive Canvas 2D scene renders animated ASCII wildlife, weather, resources, and visual effects on top. The canvas caps its pixel ratio at two to limit the backing buffer's size on dense displays.

The portfolio loads the simulation code when you open it and suspends it when you leave the view or hide the browser tab. Read the ecosystem source and view lifecycle controller. MDN explains the browser's requestAnimationFrame API.

Scope and limitations

Biosphere Unit is an interactive software experiment. Its species rules, mutation ranges, food supply, and reseeding behavior are design choices for the demo. It is not calibrated to measured ecosystems or intended to predict real wildlife populations. Birds are decorative and do not feed, hunt, or reproduce in the modeled food web.