motion.jsCourse

Motion

Time and distance as numbers, then an animation object, then why you move a transform, what easing is, and why reduced motion sets the duration to zero.

A price label slides 24px in 320ms
  1. durationnumber320
  2. distancenumber24

6 lessons, in orderFree to read

Syllabus

Basics

The types, explained, then an object you can name and call.

  1. A duration is a number of milliseconds. A distance is a number of pixels. 320 and 24 slide a price label. The program prints 320 and 24.

    const duration = 320;
    const distance = 24;
    console.log(duration, distance);
    320 24
    
  2. One object holds the slide of a price label: the duration, the distance, and the opacity. The program builds one and prints 320, 24, and 1.

    Builds on lesson 1.

    const move = {
      durationMs: 320,
      x: 24,
      opacity: 1,
    };
    console.log(move.durationMs, move.x, move.opacity);
    320 24 1
    

Practice

A rule that prevents a bug, then a program that uses the object.

  1. Changing width makes the browser lay the page out again. Changing a transform does not. The price label moves with translateX(24px). Its width stays.

    Builds on lessons 1 through 2.

    const distance = 24;
    const transform = "translateX(" + distance + "px)";
    console.log(transform);
    translateX(24px)
    
  2. Easing maps a time from 0 to 1 onto a progress from 0 to 1. easeOut is slow at the end. The program prints 0, 0.75, and 1.

    Builds on lessons 1 through 3.

    function easeOut(t) {
      return 1 - (1 - t) * (1 - t);
    }
    
    console.log(easeOut(0), easeOut(0.5), easeOut(1));
    0 0.75 1
    

Further

One more idea, then a program that puts the pieces together.

  1. Some readers ask the system for less motion. Honor it by using a duration of 0. The program prints 320 for a normal run and 0 when motion is reduced.

    Builds on lessons 1 through 4.

    function duration(reduce) {
      return reduce ? 0 : 320;
    }
    
    console.log(duration(false), duration(true));
    320 0
    
  2. Two steps share one object. The second step's fromX is the first step's x. The program prints 0 then 24, then 24 then 48.

    Builds on lessons 1 through 5.

    const steps = [
      { fromX: 0, x: 24, durationMs: 320 },
      { fromX: 24, x: 48, durationMs: 320 },
    ];
    
    for (const step of steps) {
      console.log(step.fromX, step.x);
    }
    0 24
    24 48
    

Readings

Shorter pieces in the same language. Any order is fine once the lessons feel familiar.

  1. Easing: a function from progress to progress 2 min

    GoByte Skills #21: an ease is just a function from progress to progress. Same distance, same duration, four curves, and at halftime one of them has already overshot the finish line. On purpose.

  2. Animate transform and opacity only 2 min

    GoByte Skills #20: animate transform and opacity, not top or width. Traced in Chrome: 57 layouts for left, 1 for x, same move. Plus why GSAP still freezes when your main thread does.

  3. GSAP timelines: animation as data 2 min

    GoByte Skills #19: a GSAP timeline is a seekable program. Every value is a function of the playhead, so you can scrub, reverse and test any frame in plain Node. Plus the three ways it quietly stops being pure.