This page contains all the samples, excercises, and demo code from
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.
-
Part One: JavaScript Animation Basics
-
Chapter 1: Basic Animation Concepts
-
Chapter 2: Basics of JavaScript for Animation
-
Chapter 3: Trigonometry for Animation
-
Rotate an Object Towards a Point
-
Smooth Up and Down Motion
-
Set the Range, Speed, and Center of the Motion
-
Add Linear Vertical Motion to Make a Wave
-
Create a Pulsing Motion by Adjusting Scale
-
Wave Motion Using Two Angles
-
Draw a Wave to the Canvas Element
-
Circular Movement
-
Elliptical Movement
-
Using the Pythagorean Theorem to Calculate Distance
-
Find the Distance Between the Center Point and Mouse
-
Rotate an Object Towards a Point
-
Chapter 4: Rendering Techniques
-
Drawing App
-
Draw Curve Toward Point
-
Draw Curve Through Point
-
Joined Multiple Curves
-
Smoothly Joined Multiple Curves
-
Smoothly Joined Multiple Curves in Closed Path
-
Linear Gradient Fill
-
Linear Gradient Fill with Different Position
-
Radial Gradient Fill
-
Load Image with JavaScript and Draw
-
Draw an HTML Image Element
-
Draw Frames of an HTML5 Video Element
-
Invert Color of Canvas Pixels
-
Covert Canvas Pixels to Grayscale
-
Dynamic Manipulation of Canvas Pixels
-
Spray Paint Drawing App
-
Drawing App
-
-
Part Two: Basic Motion
-
Chapter 5: Velocity and Acceleration
-
Velocity on One Axis
-
Velocity on Two Axes
-
Angular Velocity
-
Using Angular Velocity to Follow the Mouse
-
Applying Velocity to an Object's Rotation Property
-
Acceleration on One Axis
-
Applying Acceleration Interactively
-
Acceleration on Two Axis
-
Gravity as Applied Acceleration
-
Using Angular Acceleration to Follow the Mouse
-
Spaceship Simulation
-
Velocity on One Axis
-
Chapter 6: Boundaries and Friction
-
Chapter 7: User Interaction: Moving Objects Around
-
-
Part Three: Advanced Motion
-
Chapter 8: Easing and Springing
-
Simple Easing
-
Dragging and Easing
-
Turn Off Easing
-
Ease to a Moving Target
-
Springing in One Dimension
-
Springing with Friction Applied
-
Springing in Two Dimensions
-
Springing to a Moving Target
-
Add Gravity and Draw Spring
-
Chaining Multiple Springs
-
Chaining Many Springs
-
Springing to Multiple Targets
-
Offset the Spring Target
-
Two Objects Connected by a Spring
-
Three Objects Connected by a Spring
-
Simple Easing
-
Chapter 9: Collision Detection
-
Hit Testing With the Bounding Box of an Object
-
Stacking Boxes
-
Hit Testing a Point and a Bounding Box
-
Distance-based Collision Detection
-
Distance-based Collision Detection With Arbitrary Size
-
Collision-based Springing
-
Multiple Object Collision Detection and Springing
-
Optimized Multiple Object Collision Detection and Springing
-
Hit Testing With the Bounding Box of an Object
-
Chapter 10: Coordinate Rotation and Bouncing Off Angles
-
Using Simple Coordinate Rotation to Rotate Around Point
-
Rotating Around a Point Using Advanced Coordinate Rotation
-
Rotating Multiple Objects
-
Bouncing Off an Angle
-
Optimized Bouncing Off an Angle
-
Bouncing Off a Dynamic Angle
-
Bouncing Off Angle and Hit Test Object
-
Bouncing Off Angle and Check Bounding Box
-
Bouncing Off Angle and Walls
-
Bouncing Off Multiple Angles
-
Using Simple Coordinate Rotation to Rotate Around Point
-
Chapter 11: Billiard Ball Physics
-
Chapter 12: Particle Attraction and Gravity
-
Gravitational Force Between Particles
-
Gravitational Force and Collision Detection
-
Gravitational Force and Collision Detection With Random Sizes
-
Object in Orbit
-
Drawing Along the Orbital Path
-
Connecting Particles With Springs
-
Drawing Lines Between Connected Particles
-
Applying Mass to Connected Particles
-
Gravitational Force Between Particles
-
Chapter 13: Forward Kinematics: Making Things Walk
-
Chapter 14: Inverse Kinematics: Dragging and Reaching
-
-
Part Four: 3D Animation
-
Chapter 15: 3D Basics
-
3D Perspective
-
3D Perspective without Negative Scale
-
3D Velocity and Acceleration
-
Single Object 3D Bounce
-
Multiple Object 3D Bounce
-
Multiple Object 3D Bounce with Z-Sorting
-
Bouncy Balls and Gravity
-
Running Through a Forest with Screen-Wrapping
-
Flying Through a Forest
-
3D Easing
-
3D Springing
-
Coordinate Rotation Around Y-Axis
-
Coordinate Rotation Around X and Y Axes
-
3D Collision Detection
-
3D Perspective
-
Chapter 16: 3D Lines and Fills
-
Chapter 17: Backface Culling and 3D Lighting
-
-
Part Five: Additional Techniques
-
Chapter 18: Matrix Math
-
Chapter 19: Tips and Tricks
-
Brownian (Random) Motion
-
Brownian Motion with Trails
-
Square Distribution
-
Square Distribution Over Smaller Area
-
Circular Distribution
-
Smoothed Circular Distribution
-
Biased Distribution
-
Biased Distribution with Multiple Iterations
-
Two-Dimensional Biased Distribution
-
Collision-Based Distribution
-
Timer-Based Animation
-
Time-Based Animation
-
Time-Based Animation using setInterval
-
Time-Based Animation with Gravity
-
Collisions Between Same-Mass Objects
-
Using Sound Effects
-
Brownian (Random) Motion
-
-
Extras
-
Easing Equations
-
Find a Position on a Motion Path
-
Putting Web Workers to Work
-
Character Animation Using a Sprite Sheet
-
Helper Functions
-
JavaScript Key Codes
-
Easing Equations