Animation Tutorial

First the 4 key ideas behind every character. Then make Pip, a round orange friend, and make Pip breathe, walk and jump. Each of the 5 steps is a whole character: paste it into the Character Studio, then change its numbers and watch what happens.

This is part 1 of 2. Part 2, the Animation Reference, explains the JavaScript, when each animation plays, and the formulas behind every move.

Key ideas

Four ideas are all you need. Each picture on this page is drawn by the code beside it.

1. A picture is a list of drawing commands

Each line draws one shape with the pen. The lines run from the top down, so a later shape covers an earlier one: the eyes come after the body, or the body would hide them. To change the picture, change a number, a colour or a line, and watch.

pen.circle(0, -34, 26, { fill: '#ffb000' }); // the body
pen.circle(-9, -40, 4, { fill: '#1d1530' }); // the left eye
pen.circle(9, -40, 4, { fill: '#1d1530' }); // the right eye
pen.curve(-8, -26, 0, -19, 8, -26, { line: 2 }); // the smile
4 lines, 4 shapes. Swap the first and last line: the smile disappears behind the body.

2. Numbers say where, how big and how it looks

In pen.circle(-9, -40, 4, { fill: '#1d1530' }), -9, -40 is where the centre is, 4 is how big, and { fill: … } is the style. (0, 0) is the ground under the feet; x goes right, and a negative y goes up. Every shape and what its numbers mean is in the Blocks part of the reference, and every style in Styles, each with its picture.

3. t is the animation's clock: it goes from 0 to 1

An animation draws the same picture again and again, and each time t is a little bigger: 0 at the start, 1 at the end. A number that never uses t never moves. A number worked out from t changes from picture to picture, and that is the movement.

4. To move a part: choose a number, a direction and how far, then write it with t

  1. Which number? x moves left and right, y moves up and down, a size grows and shrinks, and rotate in a pen.group turns a part.
  2. Which way, and how far? For example, 14 pixels each way, or 30 degrees.
  3. Write it with t. In an animation that loops, use pen.wave(t): it goes 0, 1, 0, −1, 0 while t goes from 0 to 1, so the part ends where it started and the loop is smooth. Multiply it by how far.
const sway = 14 * pen.wave(t); // from -14 to 14 and back
pen.circle(sway, -34, 18, { fill: '#ffb000' });
Left and right: x is 14 times the wave.
const turn = 30 * pen.wave(t); // degrees: -30 to 30 and back
pen.group({ x: 0, y: -44, rotate: turn }, () => {
  pen.rect(-26, -5, 52, 10, { fill: '#06d6a0', corner: 5 });
});
Turning: the group's rotate is 30 degrees times the wave.
const hop = Math.sin(Math.PI * t); // 0, up to 1, back to 0
pen.circle(0, -18 - 24 * hop, 14, { fill: '#ff8fab' });
Up and back down: y gets smaller (higher) in the middle of the loop. Math.sin(Math.PI * t) is 0 at both ends.
const size = 18 + 6 * pen.wave(t); // from 12 to 24 and back
pen.circle(0, -40, size, { fill: '#c77dff' });
Bigger and smaller: the size is 18, plus 6 times the wave.

In an animation that plays once (loop: false), t itself works too: 20 * t goes from 0 at the start to 20 at the end. The Animation Reference has the formulas the example characters use for every animation.

Try: in the Character Studio, add the bob up and down block (under Time), then change its 3 to 10: it bobs further. Change one number at a time, and Ctrl+Z (⌘Z on a Mac) takes it back.

Your first character

Make Pip, a round orange friend, in 5 steps. Each step is a whole character. In the Character Studio, press New character, click in the code, select all of it (Ctrl+A, or ⌘A on a Mac), and paste the step's code over it (Ctrl+V). Then change the numbers and watch what happens.

4 rules for every character

  1. The ground under the feet is (0, 0). x goes left and right. y goes up and down, and going up makes y smaller: −26 is 26 pixels above the ground.
  2. Stay in the box. width and height make the box, the dashed frame in the Studio. A shape outside it is cut off in the game.
  3. A loop ends where it starts. t goes from 0 to 1 and then starts again, so the picture at t = 1 must match the one at t = 0.
  4. Face right. The game flips the picture when your character walks left.

Step 1: a still picture

Each shape is a pen command with numbers: where the shape is, how big it is, and a style. The body is a circle with a radius of 20 pixels. Its centre is 26 pixels above the ground, so it rests on the feet.

// Pip: my first character.
export default {
  name: 'Pip',
  width: 60,
  height: 80,
  animations: {
    idle: { frames: 1, fps: 1, draw: (pen, t) => drawPip(pen) },
  },
};

function drawPip(pen) {
  // Two feet on the ground: flat ellipses, 8 pixels left and right of the middle.
  pen.ellipse(-8, -3, 7, 3, { fill: '#e8590c' });
  pen.ellipse(8, -3, 7, 3, { fill: '#e8590c' });
  // The body: a circle with radius 20, its centre 26 pixels above the ground.
  pen.circle(0, -26, 20, { fill: '#ff9f43' });
  // Two eyes: small dark circles with no outline.
  pen.circle(-7, -30, 3, { fill: '#1d1530', line: 0 });
  pen.circle(7, -30, 3, { fill: '#1d1530', line: 0 });
  // A smile: a curve from (-6, -21) to (6, -21), pulled down toward (0, -16).
  pen.curve(-6, -21, 0, -16, 6, -21, { line: 2 });
}

Try: make the body bigger with a radius of 24. Move the eyes further apart. Change '#ff9f43' to '#4dabf7' for a blue Pip.

Step 2: a pose

Before Pip can move, its picture needs a few numbers to change. A pose is a set of numbers, and drawPip draws any pose. Here the numbers are 0 and 1, so Pip looks the same as in step 1. pen.group moves everything that is drawn inside it, all together.

// Pip: a pose is a few numbers, and one function draws any pose.
export default {
  name: 'Pip',
  width: 60,
  height: 80,
  animations: {
    idle: { frames: 1, fps: 1, draw: (pen, t) => drawPip(pen, { bob: 0, look: 0, eyes: 1 }) },
  },
};

// The pose p:
//   bob   how far the body rises, in pixels
//   look  how far the eyes look to the right
//   eyes  1 for open, 0 for shut
function drawPip(pen, p) {
  pen.ellipse(-8, -3, 7, 3, { fill: '#e8590c' });
  pen.ellipse(8, -3, 7, 3, { fill: '#e8590c' });
  // pen.group moves everything drawn inside it: here, up by p.bob.
  pen.group({ y: -p.bob }, () => {
    pen.circle(0, -26, 20, { fill: '#ff9f43' });
    // Open eyes are 3 pixels high; shut eyes are a thin line.
    const eyeHeight = 0.5 + 2.5 * p.eyes;
    pen.ellipse(-7 + p.look, -30, 3, eyeHeight, { fill: '#1d1530', line: 0 });
    pen.ellipse(7 + p.look, -30, 3, eyeHeight, { fill: '#1d1530', line: 0 });
    pen.curve(-6 + p.look, -21, p.look, -16, 6 + p.look, -21, { line: 2 });
  });
}

Try: set bob to 10, and Pip's body floats above its feet. Set eyes to 0, then look to 3.

Step 3: breathing (idle)

Now the numbers change with time. The game plays idle while your character stands still: 24 pictures at 12 a second make a loop of 2 seconds. pen.wave(t) goes up and down once, smoothly, so bob goes from 0 up to 1.5, down to −1.5 and back to 0. One picture in the middle has the eyes shut: a blink.

// Pip: breathing and blinking.
export default {
  name: 'Pip',
  width: 60,
  height: 80,
  animations: {
    // 24 pictures, 12 a second: the loop lasts 2 seconds.
    idle: { frames: 24, fps: 12, draw: (pen, t) => drawPip(pen, idle(pen, t)) },
  },
};

// t goes from 0 to 1 during the loop.
function idle(pen, t) {
  return {
    // pen.wave(t) goes 0, 1, 0, -1, 0: the body rises and sinks 1.5 pixels.
    bob: 1.5 * pen.wave(t),
    look: 0,
    // Shut for a moment in the middle of the loop: a blink.
    eyes: t >= 0.5 && t < 0.55 ? 0 : 1,
  };
}

function drawPip(pen, p) {
  pen.ellipse(-8, -3, 7, 3, { fill: '#e8590c' });
  pen.ellipse(8, -3, 7, 3, { fill: '#e8590c' });
  pen.group({ y: -p.bob }, () => {
    pen.circle(0, -26, 20, { fill: '#ff9f43' });
    const eyeHeight = 0.5 + 2.5 * p.eyes;
    pen.ellipse(-7 + p.look, -30, 3, eyeHeight, { fill: '#1d1530', line: 0 });
    pen.ellipse(7 + p.look, -30, 3, eyeHeight, { fill: '#1d1530', line: 0 });
    pen.curve(-6 + p.look, -21, p.look, -16, 6 + p.look, -21, { line: 2 });
  });
}

Try: 3 * pen.wave(t) for deep breaths, or pen.wave(t, 2) for two breaths in each loop. Drag the t slider in the Studio to see each picture.

Step 4: walking

The game plays walk while your character moves, and slides it along for you. So walk only moves the parts. The pose gets 2 new numbers, footL and footR: how high each foot is lifted. step is more than 0 in the first half of the loop and less than 0 in the second half. So Math.max(0, step) lifts the left foot first, and Math.max(0, -step) lifts the right foot next.

// Pip: walking.
export default {
  name: 'Pip',
  width: 60,
  height: 80,
  animations: {
    idle: { frames: 24, fps: 12, draw: (pen, t) => drawPip(pen, idle(pen, t)) },
    walk: { frames: 12, fps: 12, draw: (pen, t) => drawPip(pen, walk(pen, t)) },
  },
};

function idle(pen, t) {
  return { bob: 1.5 * pen.wave(t), look: 0, eyes: t >= 0.5 && t < 0.55 ? 0 : 1, footL: 0, footR: 0 };
}

function walk(pen, t) {
  // step goes from -1 to 1 and back: it says which foot is up.
  const step = pen.wave(t);
  return {
    bob: 2 * Math.abs(step), // the body rises on each step
    look: 2, // the eyes look the way Pip walks
    eyes: 1,
    footL: 5 * Math.max(0, step), // the left foot lifts in the first half...
    footR: 5 * Math.max(0, -step), // ...and the right foot in the second half
  };
}

// New in the pose: footL and footR, how high each foot is lifted.
function drawPip(pen, p) {
  pen.ellipse(-8, -3 - p.footL, 7, 3, { fill: '#e8590c' });
  pen.ellipse(8, -3 - p.footR, 7, 3, { fill: '#e8590c' });
  pen.group({ y: -p.bob }, () => {
    pen.circle(0, -26, 20, { fill: '#ff9f43' });
    const eyeHeight = 0.5 + 2.5 * p.eyes;
    pen.ellipse(-7 + p.look, -30, 3, eyeHeight, { fill: '#1d1530', line: 0 });
    pen.ellipse(7 + p.look, -30, 3, eyeHeight, { fill: '#1d1530', line: 0 });
    pen.curve(-6 + p.look, -21, p.look, -16, 6 + p.look, -21, { line: 2 });
  });
}

Try: lift the feet higher, with 8 instead of 5. What happens with look: -2?

Step 5: jumping and in the air

jump plays once (loop: false) when you press Space in the Test Lab. 4 * t * (1 - t) is a parabola: 0 at the start, 1 in the middle and 0 at the end. So Pip goes up 30 pixels and lands where it started. A new number, up, lifts all of Pip, feet and all. Quiz Climb plays air instead while your character is off the ground, and moves it for you, so air only tucks the feet up.

// Pip: jumping, and in the air.
export default {
  name: 'Pip',
  width: 60,
  height: 80,
  animations: {
    idle: { frames: 24, fps: 12, draw: (pen, t) => drawPip(pen, idle(pen, t)) },
    walk: { frames: 12, fps: 12, draw: (pen, t) => drawPip(pen, walk(pen, t)) },
    // jump plays once (loop: false): t goes from 0 to 1 a single time.
    jump: { frames: 16, fps: 16, loop: false, draw: (pen, t) => drawPip(pen, jump(t)) },
    // air loops while Quiz Climb moves Pip through the air.
    air: { frames: 12, fps: 12, draw: (pen, t) => drawPip(pen, air(pen, t)) },
  },
};

function idle(pen, t) {
  return { up: 0, bob: 1.5 * pen.wave(t), look: 0, eyes: t >= 0.5 && t < 0.55 ? 0 : 1, footL: 0, footR: 0 };
}

function walk(pen, t) {
  const step = pen.wave(t);
  return { up: 0, bob: 2 * Math.abs(step), look: 2, eyes: 1, footL: 5 * Math.max(0, step), footR: 5 * Math.max(0, -step) };
}

function jump(t) {
  // 4 * t * (1 - t) is a parabola: 0 at the start, 1 in the middle, 0 at the end.
  const height = 4 * t * (1 - t);
  return { up: 30 * height, bob: 0, look: 0, eyes: 1, footL: 3 * height, footR: 3 * height };
}

function air(pen, t) {
  // The game does the moving: air only tucks the feet up and wobbles a little.
  return { up: 0, bob: 1 + pen.wave(t, 2), look: 0, eyes: 1, footL: 4, footR: 4 };
}

// New in the pose: up lifts the whole of Pip, feet and all.
function drawPip(pen, p) {
  pen.group({ y: -p.up }, () => {
    pen.ellipse(-8, -3 - p.footL, 7, 3, { fill: '#e8590c' });
    pen.ellipse(8, -3 - p.footR, 7, 3, { fill: '#e8590c' });
    pen.group({ y: -p.bob }, () => {
      pen.circle(0, -26, 20, { fill: '#ff9f43' });
      const eyeHeight = 0.5 + 2.5 * p.eyes;
      pen.ellipse(-7 + p.look, -30, 3, eyeHeight, { fill: '#1d1530', line: 0 });
      pen.ellipse(7 + p.look, -30, 3, eyeHeight, { fill: '#1d1530', line: 0 });
      pen.curve(-6 + p.look, -21, p.look, -16, 6 + p.look, -21, { line: 2 });
    });
  });
}

Try: jump higher with 40 * height, but stay inside the box: it is 80 pixels high. Then give Pip a hat, arms or a tail, and play it in the Test Lab.

That is the whole recipe: draw a still picture, turn its numbers into a pose, then work out the pose from t for each animation. The Animation Reference shows what each animation can do.