In this recipe, we'll explore the third major type of motion—oscillation. Some good examples of oscillations are a bouncing weight attached to a spring, an oscillating bubble, or a pendulum that swings back and forth.
Follow these steps to oscillate a box back and forth:
Link to the
Animation
class:<head> <script src="animation.js"> </script>
Instantiate an
Animation
object and get the canvas context:<script> window.onload = function(){ var anim = new Animation("myCanvas"); var canvas = anim.getCanvas(); var context = anim.getContext();
Create a
box
object that contains the box's position and size:Define the parameters required for the harmonic oscillation equation:
var centerX = canvas.width / 2 - box.width...