Hands On: Vivifying the Canvas
In this lesson, we will bring our canvas to life!
The ball still stays on the surface, so we need to add calculations that move it as time goes on and redraw the ball periodically in its new position.
Exercise: Moving the ball
// --- Constants const HEIGHT = 250; const WIDTH = 500; const BALL = 12; const SOIL = 17; const GRASS = 3; const BALLCOLOR = '#CC333F'; const SKYCOLOR = '#9CC4E4'; const SOILCOLOR = '#6A4A3C'; const GRASSCOLOR = '#93A42A'; // --- Drawing context var ctx; var ballX; var ballY; function initDraw(elemId) { ctx = document.getElementById(elemId).getContext('2d'); ballX = BALL; ballY = HEIGHT - BALL - SOIL - GRASS; draw(); } function drawArea() { // Draw sky ctx.fillStyle = SKYCOLOR; ctx.beginPath(); ctx.rect(0, 0, WIDTH, HEIGHT - SOIL - GRASS); ctx.fill(); // Draw soil ctx.fillStyle = SOILCOLOR; ctx.beginPath(); ctx.rect(0, HEIGHT - SOIL, WIDTH, SOIL); ctx.fill(); // Draw grass ctx.fillStyle = GRASSCOLOR; ctx.beginPath(); ctx.rect(0, HEIGHT - SOIL - GRASS, WIDTH, GRASS); ctx.fill(); } function draw() { drawArea(); // Draw ball ctx.fillStyle = BALLCOLOR; ctx.beginPath(); ctx.arc(ballX, ballY, BALL, 0, Math.PI * 2); ctx.closePath(); ctx.fill(); }
In this exercise, you add the calculation that will compute the new position of the ball. This uses the current position and velocity of the ball, calculates the new ball coordinates, and modifies the vertical velocity component with the effect of gravity. To ...