Ball Class

This tutorial explains how to use JavaScript classes in canvas animations and is not intended for beginners. I recommend going through our Lighting Engine section first and trying the Color Cycle tutorial.

That said, I won't be using strict class syntax here because it's just JavaScript "syntactic sugar," and the underlying concepts remain the same. If you are interested in learning about more complex animations, I suggest checking out this page for a thorough introduction. Otherwise, let's stick with the basics.

To begin, a class is simply a regular JavaScript function. It works the same way, but we add the ability to bind values to a specific instance of the function. In this example, I've created a ball that behaves according to basic physics. It tracks its position and velocity, as well as the "gravity" acting on it.

Simple Bounce

All
Head/Body
Script
Update
Ball Class
Copy

Full Bounce

Once we've worked through the initial complexity, it's easier to add more advanced behavior. In the next example, I've incorporated wall collisions and introduced some random variations in the x-direction to keep the ball in motion.

All
Head/Body
Script
Update
Ball Class
Copy

Multiple Balls

I'm going to be honest with you, this example is Complicated with a capital C, and it likely requires more effort than what you'd need for most basic effects. To save myself from adding too many comments in the code, we'll go over a few tricky concepts here.

First, how does a ball know it will collide with another? Each frame, every instance of the ball class must check the position of every other ball instance. By comparing their positions, we can determine if two balls are within each other’s radius, and then reverse their x- and y-velocities accordingly.

Sometimes, a ball may gain enough velocity to pass through another ball, making them occupy nearly the same space. The math required to adjust both ball positions would be too complex, so I simply remove the ball instance we're currently checking. Since the update function ensures a minimum number of balls, a new ball instance will be created, and the simulation will continue.

Finally, when should we calculate the change in velocity and position for each ball? Does it matter whether the ball is drawn before or after the variables are updated? The issue in programming is that there are multiple answers to both questions. To handle this, I’ve introduced a slightly different method of tracking collisions. It’s up to you to choose the one you prefer, but both methods can and do work.

All
Head/Body
Script
Update
Ball Class
Copy

User Controls

Finally, we'll add some basic user controls to make things more interesting. I've already discussed hue changes in detail, so to keep the code from becoming even more complicated, we'll focus on adjusting the number and radius of the balls. This only requires a few modifications to the existing functions.

Head
Update
Ball Class
Copy

I hope you've enjoyed our JavaScript class animation tutorial! Even at this level of complexity, this effect is just the tip of the iceberg in terms of what you can achieve with the HTML canvas. Additionally, I wouldn’t consider it finished just yet. If we aim for perfection, you’ll notice that ball collisions are detected one frame before they happen, which means they often don’t actually collide. The collisions are also simplified in terms of energy transfer, since the resulting velocity only considers the ball's current velocity and doesn't account for the energy of the other ball. Lastly, checking each ball’s position multiple times per loop is inefficient in terms of time complexity and could be optimized with basic memoization.

In the end, always be critical of your effects. They can usually be improved in one way or another, and the difference between a mediocre effect and a great one often comes from simply staring at it and allowing yourself to dislike it at least once.

For one final step-by-step tutorial, check out our Callbacks section, where I walk through the process of capturing meter data to activate and deactivate effects.

Type to search, ESC to discard
Type to search, ESC to discard
Type to search, ESC to discard