Chapter 5. Math, Physics, and Animation
Impressing users with animation involves more than knowing how to move objects—you also need to know how to move them in ways that users expect. That requires understanding some common algorithms for math-based movement and physics interactions. Simple movement based on points and vectors provides a foundation, and then it’s time to create objects that bounce off walls and one another with a bit of friction added to the mix. After that, we will step back and talk about movement that goes beyond straight lines: circles, spirals, and complex Bezier curves. We will then cover how adding gravity can affect movement. Finally, we will finish this chapter by discussing easing and how it can have a positive effect on math-based animations.
Moving in a Straight Line
For the simplest kinds of animations—moving objects in a straight
line up and down the canvas—this can take the form of adding a constant
value to the x or y position of an object every time it is
drawn.
So, to animate graphics, we will need to create an interval and then
call a function that will display
our updated graphics on every frame. Each example in this chapter will be built in a similar way. The first
step is to set up the necessary variables in our canvasApp() function. For this first, basic
example of movement, we will create a variable named speed. We will apply this value to the y position of our object on every call to
drawScreen(). The x and y variables set up the initial position ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access