-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.js
49 lines (35 loc) · 967 Bytes
/
canvas.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
const canvas = document.createElement("canvas");
canvas.width = screenWidth;
canvas.height = screenHeight;
document.body.appendChild(canvas);
const ctx = canvas.getContext("2d");
const pokeball = new Image();
pokeball.src = "img/poke.png";
const pokeballs = [];
for (let i = 0; i < 10; i++) {
pokeballs.push({
x: Math.random() * screenWidth,
y: Math.random() * screenHeight,
vx: (Math.random() - 0.5) * 4,
vy: (Math.random() - 0.5) * 4,
});
}
function draw() {
ctx.clearRect(0, 0, screenWidth, screenHeight);
for (let i = 0; i < pokeballs.length; i++) {
const p = pokeballs[i];
ctx.drawImage(pokeball, p.x, p.y);
p.x += p.vx;
p.y += p.vy;
if (p.x < 0 || p.x > screenWidth) {
p.vx = -p.vx;
}
if (p.y < 0 || p.y > screenHeight) {
p.vy = -p.vy;
}
}
requestAnimationFrame(draw);
}
pokeball.onload = draw;