-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.js
More file actions
33 lines (28 loc) · 702 Bytes
/
timer.js
File metadata and controls
33 lines (28 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Create a canvas element
let canvas = document.createElement('canvas');
canvas.width = 600;
canvas.height = 600;
document.body.appendChild(canvas);
// Get the canvas 2D context
let ctx = canvas.getContext('2d');
// Setup a few variables
let x = 0;
let y = 0;
let stepX = 10;
let stepY = 10;
let color = '#000';
// Animation loop
let animationLoop = () => {
ctx.fillStyle = color;
ctx.fillRect(x, y, 10, 10);
// Update x and y
x += stepX;
y += stepY;
// Change direction when hitting a wall
if (x > canvas.width || x < 0) {
stepX = -stepX;
color = '#' + Math.floor(Math.random() * 16777215).toString(16);
}
if (y > canvas.height || y < 0) {
stepY = -stepY;
}