Animation Reference

An animation is a draw(pen, t) function. A game picks the animation from what the character is doing. This page shows what changes in each animation, with the formulas from the example characters. Point at a graph, or focus it and press ← →, to read its values.

This is part 2 of 2: the reference. Part 1, the Animation Tutorial, makes a character move in 5 steps. New to JavaScript? Start with JavaScript in 10 minutes, and try the blocks.

JavaScript in 10 minutes

A character is a small program written in JavaScript, the language of web pages. You do not need all of it: these nine ideas are enough to change any character or to make your own. Open the Character Studio next to this page and try each one. To start from a ready character, press New character there.

1. A script is a list of instructions

The computer reads a script from the top down, and each instruction does one thing. A character has a draw(pen, t) function for each animation. The game runs it once for every frame, and each line in it draws a shape with the pen. Shapes drawn later go on top of shapes drawn earlier.

draw(pen, t) {
  pen.circle(0, -26, 24, { fill: '#7fc8f8' }); // the body
  pen.circle(-8, -32, 3, { fill: '#1d1530' }); // an eye on top of it
}

Try it: Pebble, a character with one frame

2. Numbers, text and notes

3. Names for values: const

const gives a value a name. Use the name in many places, and change the value in one:

const BODY = '#7fc8f8';
const size = 5;
pen.circle(-10, -26, size, { fill: BODY });
pen.circle(10, -26, size, { fill: BODY });

A name is letters and digits with no spaces, and starts with a letter. Capitals count: size and Size are different names. A name can be made only once inside the same { }, so a second const size there is an error.

4. Lists and settings

Square brackets make a list: [-1, 1]. A point is a list of two numbers, [x, y], so a shape with corners takes a list of points:

pen.polygon([[0, -46], [10, -30], [-10, -30]], { fill: '#f15bb5' });

Curly brackets with labels make an object: settings with names, like a form. { fill: '#f15bb5', line: 2 } has two settings: fill is the colour inside, and line is how thick the outline is. A colon goes between a label and its value, and a comma between settings.

5. Calling a command

pen.circle(0, -30, 10, { fill: '#ffd166' }) calls the pen's circle command. The values in the round brackets are its arguments, separated by commas, and their order matters: for a circle it is x, y, radius, then the style. The blocks below explain the arguments of each command.

In the Studio, click a shape to see its handles and drag them: the numbers in its call change for you. That works for numbers written out, like -30. A number worked out from others, like side * 8, has a grey handle: change it in the code.

Every (, [ and { needs its partner ), ] or }. A semicolon ; ends an instruction.

6. Where things go

7. Repeat and choose

for repeats lines. Here i counts 0, 1, 2, 3, and i++ adds 1 each time:

for (let i = 0; i < 4; i++) {
  pen.circle(-12 + 8 * i, -14, 3, { fill: '#ffffff' });
}

for … of goes through a list. With [-1, 1] it draws a left side and a right side, like two eyes:

for (const side of [-1, 1]) {
  pen.circle(side * 8, -32, 3, { fill: '#1d1530' });
}

if chooses: the lines in its { } run only when its test is true, and the lines after else run when it is false. Tests compare numbers: < less than, > more than, === equal; && means "and".

8. Time: t and pen.wave

In draw(pen, t), t is the time. It goes from 0 to 1 during one pass of the animation, so a number made from t makes things move:

The rest of this page shows how each animation moves: idle, walk, jump and air, and then what the trail picture is.

Try it: Robo's walk, then drag the t slider

9. When it goes red

When the Studio cannot run the script, it shows a message under the code and marks the line, and the last good picture stays. The usual causes:

Two mistakes do not turn red, so check them by eye. A shape outside the dashed box gets cut off in the game. A loop that does not end where it starts makes a jump at its end: drag the t slider to 1 and back to 0.

Ctrl+Z (⌘Z on a Mac) takes back the last change, even a block you added. Back to original brings back a built-in character as it was.

Blocks

Each card shows what its code draws. The Blocks box above the code in the Studio adds these to your script: click a block to add it, or its ? to read about it. A block goes where the caret is when it can go there, and otherwise into the function that draws the whole character. Then change its numbers and watch the picture.

Styles: how a shape looks

Every shape ends with a style in { }: its colours and outline. Each picture below is drawn by the code beside it. Mix them: { fill: '#7cc4ff', line: 4, shade: true }. Shade and shine need a #rrggbb fill.

pen.circle(0, -44, 30, { fill: '#7cc4ff' });
fill: the colour inside, as a #rrggbb code.
pen.circle(0, -44, 30, { line: 4 });
No fill: only the outline.
pen.circle(0, -44, 30, { fill: '#7cc4ff', line: 0 });
line: 0: no outline of its own. (See Two outlines below.)
pen.circle(0, -44, 30, { fill: '#7cc4ff', line: 8 });
line: how thick the outline is. Left out, it is the character's line (3).
pen.circle(0, -44, 30, { fill: '#7cc4ff', ink: '#e63946' });
ink: the colour of the outline.
pen.circle(0, -44, 30, { fill: '#7cc4ff', shade: true });
shade: a darker side, away from the light, so it looks round.
pen.circle(0, -44, 30, { fill: '#7cc4ff', shine: true });
shine: a light spot, like a reflection.
pen.circle(-12, -44, 26, { fill: '#ffd166' });
pen.circle(12, -44, 26, { fill: '#7cc4ff', alpha: 0.5 });
alpha: see-through, from 0 (invisible) to 1 (solid).
pen.rect(-30, -74, 60, 60, { fill: '#c77dff', corner: 16 });
corner (rectangles only): how round the corners are.
pen.circle(-10, -40, 26, { fill: '#ffd166' });
pen.circle(12, -50, 20, { fill: '#ff8fab' });
Order: a later shape goes on top. The pink circle is drawn second.

Two outlines

A character can also have a thick outer outline: silhouette: 2 at the top of its script draws one round the whole figure, so its edge stands out. Every shape is part of that figure, even one with line: 0, so a shape at the edge still gets the thick outline (that is how Cloud's puffs share one outline). To leave a shape out of it, add silhouette: false; for no border at all, write { line: 0, silhouette: false }. A see-through shape (alpha below 1) is always left out.

When each animation plays

Two games play the characters: the play field in the Test Lab, and Quiz Climb. Both find animations by name, and only idle is required. The table shows when each name plays.

Name Lab play field Quiz Climb Plays
idle Standing still. Standing still. In a loop
walk Moving in any direction. Running on the ground. In a loop
jump When you press Space. Only the character that you control jumps. Never: the physics makes the jump. Once
wave Now and then, for the characters that you do not control. Every 2 to 5 s, each one picks a new place to go, or waves instead (about 3 times in 10). Never. Once
air Never. In the air, going up or coming down. In a loop
any other name Never. The Character Studio and the Test Lab show it. Never. –

A missing animation shows idle instead; a missing jump or wave is never started. The trail is not an animation: it is a small picture of its own, which both games scatter behind a moving character.

Five rules follow from the table:

Time: t, frames and fps

The baker calls draw(pen, t) once for each frame and keeps the picture. The game then shows fps pictures each second, so one pass of an animation lasts frames ÷ fps seconds. The loop setting decides which values of t the frames get:

Loop (the default) Once (loop: false)
t of frame i i ÷ frames. t never reaches 1. i ÷ (frames − 1). The last frame is t = 1.
4 frames t = 0, 0.25, 0.5, 0.75 t = 0, 0.333, 0.667, 1
Rule t = 1 must give the same picture as t = 0, because frame 0 comes after the last frame. t = 0 is the start pose, and t = 1 is the end pose.

The example characters use these timings:

Animation frames fps Seconds
idle 12 to 24 10 to 12 1.2 to 2.0
walk 8 to 12 12 to 14 0.67 to 1.0
jump 14 to 18 16 to 18 0.78 to 1.0
wave 16 to 20 12 to 15 1.33
air 8 to 12 10 to 12 0.67 to 1.0

Idle loops are slow, so that the character looks calm. Walks and jumps are short and fast.

idle: small signs of life

A picture that never moves looks dead. An idle loop adds 2 or 3 small, slow motions, and each motion has its own timing. Keep each motion small: 1 or 2 pixels, or a few degrees.

Robo's idle. The antenna follows the body 0.2 of a loop later, and the eyes shut once. Lines show the shape of each motion; the formula gives its size.

These values change in Robo's idle:

Other ideas: squash and stretch the body (Sprout), move the pupils on a small circle with pen.polar(1.6, 360 * t) (Octopus), or twitch an ear in a short window of t (Cat).

See it in the Studio: Robo Sprout Octopus Cat

walk: two steps per loop

One walk loop is two steps: first the left foot, then the right foot. Most parts follow one number, step = pen.wave(t), which goes from 0 up to 1, down to −1 and back to 0 once per loop.

Robo's walk. Each foot is up for one half of the loop, and swings forward while it is up. The body rises with each step, so it bobs twice per loop.

These values change in Robo's walk:

Characters without legs move the whole body. Sprout hops once per loop with up = Math.sin(Math.PI * t) and squashes when it lands. Ghost floats with 8 + 2 * pen.wave(t, 2) and leans 8 degrees into the walk. Octopus moves its tentacle wave twice as fast as in idle.

See it in the Studio: Robo Sprout Ghost Octopus

jump: crouch, fly, land

In the Test Lab, a jump plays once, so t goes from 0 to 1 one time. The Test Lab does not lift the character, so the animation must. Split t into 3 phases, and give each phase a number that goes from 0 to 1 while the phase runs.

Robo's jump. The crouch and the landing are short dips. The height is a parabola, highest in the middle of the flight.

These values change in Robo's jump:

The highest frame must still fit in the box, so make height big enough for the jump. Star buddy spins once with easing, 360 * t * t * (3 - 2 * t). Dragon flaps 3 times in the air with 35 * Math.sin(2 * Math.PI * 3 * fly).

See it in the Studio: Robo Star buddy Dragon

wave: raise, wave, lower

A wave is also an action: it plays once while the character stands still. It starts right after idle, so the arm must go up first and come down again at the end.

Robo's wave. The arm goes up in the first fifth of t, waves twice, and comes down in the last fifth.

These values change in Robo's wave:

An arm raised sideways reaches far from the body, so make width big enough for it. To see a wave in the game, open the Test Lab and look at the characters that you do not control. Only they wave.

See it in the Studio: Robo

air: in the air (Quiz Climb)

Quiz Climb plays air while the character is off the ground, going up or coming down. The physics moves the character, so air is a short loop on the spot. Without it, Quiz Climb shows idle.

See it in the Studio: Robo Penguin Star buddy

trail: what it leaves behind

A trail is a small picture of its own, not part of the character: a nut, a paw print, a sparkle. While the character moves, both games scatter copies of it where the character has been. Each copy drifts off in a random direction and fades away within 1 to 2 seconds. Standing still leaves nothing new.

The game does the moving and the fading, so your script draws only the picture. It goes next to animations, not inside them:

animations: { … },
trail: { width: 12, height: 12, frames: 8, fps: 12, draw: (pen, t) => nut(pen, t) },

Every example character has one. Some of them:

See it in the Studio: Robo Cat Tomato Rainbow Drop

Other animation names

An animation can have any name but trail (that name is the trail picture's own setting), for example to try an idea. The Character Studio and the Test Lab show every animation, but the games play only the names above. The examples have three more:

For programmers: to play one in the game, start it like the jump in src/app/field.ts, with startAction('boo') on a key.

Motion shapes

Most motion is built from a few formulas. Each one turns t into a number with a useful shape. Multiply the number by a size, and add it to a position or an angle.

pen.wave(t): back and forth once per loop. Breathing, swaying.
pen.wave(t, 2): twice per loop. Use a whole number of cycles, or the loop does not close.
pen.wave(t, 1, 0.2): the same wave, 0.2 of a loop later. Parts that follow the body.
Math.abs(pen.wave(t)): two bumps per loop. A bob on each step.
Math.max(0, pen.wave(t)): only the top half. A foot that lifts and then rests.
Math.sin(Math.PI * t): 0 → 1 → 0 once. A hop, or a swell in an action.
4 * t * (1 - t): a parabola. The height of a thrown ball.
t * t * (3 - 2 * t): smoothstep, from 0 to 1 with a slow start and a slow end. Spins and arm moves.
Math.min(1, Math.max(0, (t - a) / (b - a))): 0 until a, then up to 1 at b. One phase of an action (here a = 0.2, b = 0.6).
t >= a && t < b: true only in a short window. A blink (here a = 0.6, b = 0.66).
(2 * t + offset) % 1: from 0 to 1, then back to 0, twice per loop. Things that fall or repeat.

Check your animation

  1. In the Studio, pick the animation under the picture and drag t. Turn on Snap to frames to see the frames that the baker keeps, and press Play to see the speed. Add pen.watch('name', value) to show a number under the timeline.
  2. Run the tests: npm test draws every frame of each character in characters/cast.json. A test fails when a shape leaves the box, when a loop does not end where it starts, or when a jump does not land. The trail picture is checked the same way, in its own small box.
  3. See every frame at once: npm run render -- characters/03-robo.js writes renders/03-robo.png, with each frame, its t, the box and the ground line. Add --anim walk --scale 3 to see one animation bigger.
  4. Play it in the Test Lab: walk with the arrow keys or W A S D, press Space to jump, and watch the other characters wave. Then try it in Quiz Climb, where it walks, jumps and flies.