Last active
April 27, 2020 19:15
-
-
Save sandrabosk/199ef82056d464a0e602ff8cfa24438d to your computer and use it in GitHub Desktop.
solution-canvas-animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const canvas = document.querySelector('#canvas'); | |
const ctx = canvas.getContext('2d'); | |
const cWidth = canvas.width; | |
const cHeight = canvas.height; | |
let speed1 = 300; | |
let speed2 = 300; | |
let speed3 = 300; | |
function clearCanvas() { | |
ctx.clearRect(0, 0, cWidth, cHeight); | |
} | |
function drawCanvas(x, y, w, h, color) { | |
ctx.fillStyle = color; | |
ctx.fillRect(x, y, w, h); | |
} | |
function updateCanvas() { | |
speed1 -= 1; | |
speed2 -= 2; | |
speed3 -= 3; | |
// clear the canvas | |
clearCanvas(); | |
// redraw the canvas | |
// drawCanvas(x, y, width, height, "color"); | |
drawCanvas(50, speed1, 50, 50, 'red'); | |
drawCanvas(150, speed2, 50, 50, 'green'); | |
drawCanvas(250, speed3, 50, 50, 'yellow'); | |
requestAnimationFrame(updateCanvas); | |
} | |
updateCanvas(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> | |
<title>Canvas Animations</title> | |
</head> | |
<body> | |
<canvas id="canvas" width="300" height="300"></canvas> | |
<!-- add js file in the end when the DOM is already loaded --> | |
<script src="index.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment