Skip to content

Your first animation

Bounce a ball with two classes, then throw one along an arc on a canvas.

Edit this page

This page assumes toon is on your page; see Install toon. toon.css animates page elements with classes, and toon.js animates on <canvas>. Use either one, or both.

With toon.css included, two classes make a bouncing ball. A second element under it makes its contact shadow (the shadow on the ground, which shrinks as the ball rises). The ball jumps 140px, so the box around it leaves that much room above. That’s the left ball, and the first block of code.

The right ball is the same ball, tuned with custom properties on the element (or on :root, to change everything at once). It gets more squash (it flattens further when it lands), a higher bounce and a slower tempo. --toon-tempo multiplies every duration, so a higher tempo is slower:

With toon.js included, this throws a ball back and forth along an arc (the curved path a thrown thing follows). Each frame works out the ball’s speed and direction, so it stretches the way it’s moving:

You’ve made a ball bounce and a ball fly along an arc. Pick what to build next: