Ball pool
A pit of glossy balls that tumble into a pile; shove them with the pointer, grab one and throw it, or click to blast them apart.
- element
- physics
- balls
- pointer
- playful
- canvas
- since 0.2
Background Studio: tune it below, then copy the code
Your system asks for reduced motion: the demo shows its final state and nothing moves.
Live demo
Ball pool example
Code
It follows the controls above: change an option and the code changes with it.
<script src="https://search.axi0.io/axi0/motion/motion.js" defer></script>
<axi0-ball-pool cover></axi0-ball-pool>
import { BallPool } from "./motion/react/BallPool";
<BallPool />
<script setup lang="ts">
import BallPool from "./motion/vue/BallPool.vue";
</script>
<template>
<BallPool />
</template>
const fx = document.createElement("axi0-ball-pool");
document.querySelector("#hero").append(fx);
The wrappers: BallPool.tsx (React, TypeScript) and BallPool.vue (Vue). Both render the same attributes as the HTML; the loader does the rest.
Options
| Attribute | Default | What it does |
|---|---|---|
count | 34 | How many balls |
gravity | 1 | How hard they fall (0 lets them float) |
size | 1 | Ball size |
colors | auto | Colors, comma separated (empty: the palette for the page's theme) |
background | auto | Background color (empty: the theme's) |
speed | 1 | How fast it moves |
theme | auto | Light or dark palette (auto: from the page behind it) |
cover | false | Fill the nearest positioned parent, behind its content |
Accessibility
Decorative: the canvas is aria-hidden and takes no focus; with cover it ignores the pointer for clicks, so the content above it stays clickable and readable. Under reduced motion the balls show already settled in their heap and nothing moves. Drawn on a 2D canvas at device resolution (capped at 2); it starts when it comes within 300 px of the screen, the frame loop stops off screen and in hidden tabs, and reduced motion draws one still frame with no movement.
Size
ball-pool.js is 1.8 KB gzip, with backgrounds-kit.js 3.4 KB it needs. A page loads the loader (2.9 KB) and core.js (1.4 KB) once, whatever it uses.