Search⌘ K

How it Works: Drawing the Trajectory

Explore how to use JavaScript arrays to save and draw the trajectory of an object on an HTML canvas. Understand coordinate storage, path creation, and efficient redrawing methods to create smooth animations and visual effects.

We'll cover the following...

How it works

The trajectory variable, var trajectory = [];, you added in step 1, represents an empty array.

In step 2, you saved the current ball position with the following code:

Node.js
trajectory.push({ x: ballX, y: ballY })

This code, which is the invocation of push(), added a new object to the trajectory array. This object is created with JavaScript’s object notation as a tuple of two coordinates, ...