This page contains all the samples, excercises, and demo code from

# Foundation HTML5 Animation with JavaScript

Everything you need to know about moving things around in the web browser using JavaScript. This is the starting place for creating HTML5 games, animations, and UX effects. Check out the Amazon reviews, I promise I didn't make these up :)

- “One of the best programming books I've read in a long time. Writing is clear, straightforward, and interesting.” —David Kroenke
- “It has been 35 years since I had to even spell sine or cosine yet this book makes perfect sense and has really opened my eyes to what is possible.” —Peter Hoyt
- “MUST OWN for any HTML5 game developer.” —Amazon Customer

Fork this repo on GitHub.

## Buy the Book!

My publisher has graciously allowed me to post all the sample code and exercises below to make it easier to browse and incorporate in your own work. Each example is explained in detail within the book, so check it out if you really want to understand how all this code works. If you're just browsing around and looking to solve a problem, that's cool, but if it helps you out of a jam, please consider buying the book (or ebook) so that I can continue publishing. I promise, it's good!
And if you enjoy the book, please spread the word and write a review.

## The Blurb

Foundation HTML5 Animation with JavaScript, by Billy Lamberta and Keith Peters, covers everything that you need to know to create dynamic, scripted animations using the HTML5 canvas. It provides information on all the relevant math you'll need, before moving on to physics concepts like acceleration, velocity, easing, springs, collision detection, conservation of momentum, 3D, and forward and inverse kinematics. Foundation HTML5 Animation with JavaScript is a fantastic resource for all web developers working in HTML5 or switching over from Flash to create standards-compliant games, applications, and animations that will work across all modern browsers and most mobile devices, including iPhones, iPads, and Android devices.

# Table of Contents and Exercises

This is table of contents and exercises from Foundation HTML5 Animation with JavaScript, which can be found at Amazon, Barnes & Noble, and other booksellers.
The source code for each excercise is available online, and for a detailed explanation, please consult the book. Check the errata page for the latest updates and corrections.

The examples below cover the following:

- All the JavaScript and HTML5 code (including math and trigonometry functions) you'll need to start animating with code.
- Basic motion principles like velocity, acceleration, friction, easing, and bouncing.
- How to handle user interactions via the keyboard, mouse, and touchscreen.
- Advanced motion techniques like springs, coordinate rotation, conservation of momentum, and forward and inverse kinematics.
- All the basic 3D concepts you'll need for 3D in HTML5 (without WebGL)—from simple perspective to full 3D solids, complete with backface culling and dynamic lighting.

1. ## Part One: JavaScript Animation Basics

   1. ### Chapter 1: Basic Animation Concepts

   2. ### Chapter 2: Basics of JavaScript for Animation

      1. Our Basic HTML5 Canvas Setup
      2. JavaScript Event Demonstration
      3. Listening for Mouse Events
      4. Capture the Mouse Position
      5. Listening for Touch Events
      6. Listening for Keyboard Events
      7. Capture a Keyboard Event by Key Code
      8. Capture a Keyboard Event by Key Name

   3. ### Chapter 3: Trigonometry for Animation

      1. Rotate an Object Towards a Point
      2. Smooth Up and Down Motion
      3. Set the Range, Speed, and Center of the Motion
      4. Add Linear Vertical Motion to Make a Wave
      5. Create a Pulsing Motion by Adjusting Scale
      6. Wave Motion Using Two Angles
      7. Draw a Wave to the Canvas Element
      8. Circular Movement
      9. Elliptical Movement
      10. Using the Pythagorean Theorem to Calculate Distance
      11. Find the Distance Between the Center Point and Mouse

   4. ### Chapter 4: Rendering Techniques

      1. Drawing App
      2. Draw Curve Toward Point
      3. Draw Curve Through Point
      4. Joined Multiple Curves
      5. Smoothly Joined Multiple Curves
      6. Smoothly Joined Multiple Curves in Closed Path
      7. Linear Gradient Fill
      8. Linear Gradient Fill with Different Position
      9. Radial Gradient Fill
      10. Load Image with JavaScript and Draw
      11. Draw an HTML Image Element
      12. Draw Frames of an HTML5 Video Element
      13. Invert Color of Canvas Pixels
      14. Covert Canvas Pixels to Grayscale
      15. Dynamic Manipulation of Canvas Pixels
      16. Spray Paint Drawing App

2. ## Part Two: Basic Motion

   1. ### Chapter 5: Velocity and Acceleration

      1. Velocity on One Axis
      2. Velocity on Two Axes
      3. Angular Velocity
      4. Using Angular Velocity to Follow the Mouse
      5. Applying Velocity to an Object's Rotation Property
      6. Acceleration on One Axis
      7. Applying Acceleration Interactively
      8. Acceleration on Two Axis
      9. Gravity as Applied Acceleration
      10. Using Angular Acceleration to Follow the Mouse
      11. Spaceship Simulation

   2. ### Chapter 6: Boundaries and Friction

      1. Remove Object at Boundaries
      2. Fountain of Regenerating Objects
      3. Spaceship Simulation with Screen Wrapping
      4. Bounce Off Walls
      5. Bounce Off Walls with Gravity
      6. Friction, The Right Way
      7. Friction, The Easy Way
      8. Spaceship Simulation with Friction Applied

   3. ### Chapter 7: User Interaction: Moving Objects Around

      1. Listening for Mouse Events on an Object
      2. Listening for Touch Events on an Object
      3. Dragging an Object
      4. Combining Dragging and Motion Code
      5. Drag an Object and Reset its Velocity
      6. Throwing an Object

3. ## Part Three: Advanced Motion

   1. ### Chapter 8: Easing and Springing

      1. Simple Easing
      2. Dragging and Easing
      3. Turn Off Easing
      4. Ease to a Moving Target
      5. Springing in One Dimension
      6. Springing with Friction Applied
      7. Springing in Two Dimensions
      8. Springing to a Moving Target
      9. Add Gravity and Draw Spring
      10. Chaining Multiple Springs
      11. Chaining Many Springs
      12. Springing to Multiple Targets
      13. Offset the Spring Target
      14. Two Objects Connected by a Spring
      15. Three Objects Connected by a Spring

   2. ### Chapter 9: Collision Detection

      1. Hit Testing With the Bounding Box of an Object
      2. Stacking Boxes
      3. Hit Testing a Point and a Bounding Box
      4. Distance-based Collision Detection
      5. Distance-based Collision Detection With Arbitrary Size
      6. Collision-based Springing
      7. Multiple Object Collision Detection and Springing
      8. Optimized Multiple Object Collision Detection and Springing

   3. ### Chapter 10: Coordinate Rotation and Bouncing Off Angles

      1. Using Simple Coordinate Rotation to Rotate Around Point
      2. Rotating Around a Point Using Advanced Coordinate Rotation
      3. Rotating Multiple Objects
      4. Bouncing Off an Angle
      5. Optimized Bouncing Off an Angle
      6. Bouncing Off a Dynamic Angle
      7. Bouncing Off Angle and Hit Test Object
      8. Bouncing Off Angle and Check Bounding Box
      9. Bouncing Off Angle and Walls
      10. Bouncing Off Multiple Angles

   4. ### Chapter 11: Billiard Ball Physics

      1. Conservation of Momentum on One Axis
      2. Optimized Conservation of Momentum on One Axis
      3. Conservation of Momentum on Two Axes
      4. Optimized Conservation of Momentum on Two Axes
      5. Multiple Object Collisions
      6. Multiple Object Collisions With Problem Fixed

   5. ### Chapter 12: Particle Attraction and Gravity

      1. Gravitational Force Between Particles
      2. Gravitational Force and Collision Detection
      3. Gravitational Force and Collision Detection With Random Sizes
      4. Object in Orbit
      5. Drawing Along the Orbital Path
      6. Connecting Particles With Springs
      7. Drawing Lines Between Connected Particles
      8. Applying Mass to Connected Particles

   6. ### Chapter 13: Forward Kinematics: Making Things Walk

      1. Segment Class Demo
      2. Single Segment and Slider
      3. Moving Two Segments 1
      4. Moving Two Segments 2
      5. Automate Walk
      6. Natural Walk
      7. Natural Walk with Two Legs
      8. Natural Walk with Two Visible Legs
      9. Dynamic Walk Controls
      10. Real Walk on the Ground

   7. ### Chapter 14: Inverse Kinematics: Dragging and Reaching

      1. Reaching with a Single Segment
      2. Dragging a Single Segment
      3. Dragging Two Segments
      4. Dragging Many Segments
      5. Reaching with Two Segments
      6. Reaching with Many Segments
      7. Reaching for an Object
      8. Reaching with the Law of Cosines
      9. Reaching with the Law of Cosines, Reverse

4. ## Part Four: 3D Animation

   1. ### Chapter 15: 3D Basics

      1. 3D Perspective
      2. 3D Perspective without Negative Scale
      3. 3D Velocity and Acceleration
      4. Single Object 3D Bounce
      5. Multiple Object 3D Bounce
      6. Multiple Object 3D Bounce with Z-Sorting
      7. Bouncy Balls and Gravity
      8. Running Through a Forest with Screen-Wrapping
      9. Flying Through a Forest
      10. 3D Easing
      11. 3D Springing
      12. Coordinate Rotation Around Y-Axis
      13. Coordinate Rotation Around X and Y Axes
      14. 3D Collision Detection

   2. ### Chapter 16: 3D Lines and Fills

      1. Lines and Balls
      2. Lines and Points
      3. Square
      4. Letter E
      5. Filled Letter E
      6. Creating Shapes with Triangles
      7. Modeling a Cube
      8. Modeling a Pyramid
      9. More Complex Models
      10. Generating a Cylinder Model
      11. Move Model, Rotate Around Origin
      12. Move Model, Rotate Around Self

   3. ### Chapter 17: Backface Culling and 3D Lighting

      1. Backface Culling
      2. Enhanced Depth Sorting
      3. 3D Lighting Effect

5. ## Part Five: Additional Techniques

   1. ### Chapter 18: Matrix Math

      1. Coordinate Rotation Around X and Y Axes
      2. Rotate Transformation
      3. Skew X-Axis Transformation
      4. Skew X and Y Axes Transformation

   2. ### Chapter 19: Tips and Tricks

      1. Brownian (Random) Motion
      2. Brownian Motion with Trails
      3. Square Distribution
      4. Square Distribution Over Smaller Area
      5. Circular Distribution
      6. Smoothed Circular Distribution
      7. Biased Distribution
      8. Biased Distribution with Multiple Iterations
      9. Two-Dimensional Biased Distribution
      10. Collision-Based Distribution
      11. Timer-Based Animation
      12. Time-Based Animation
      13. Time-Based Animation using setInterval
      14. Time-Based Animation with Gravity
      15. Collisions Between Same-Mass Objects
      16. Using Sound Effects

6. ## Extras

   1. Easing Equations
   2. Find a Position on a Motion Path
   3. Putting Web Workers to Work
   4. Character Animation Using a Sprite Sheet
   5. Helper Functions
   6. JavaScript Key Codes