-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
72 lines (58 loc) · 1.45 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 设置画布
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const width = canvas.width;
const height = canvas.height;
// 生成随机数的函数
function random(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// 为程序中的小球建立模型
function Ball(x, velX, color, size) {
this.x = x;
this.velX = velX;
this.color = color;
this.size = size;
}
//画小球
Ball.prototype.draw = function () {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
ctx.fill();
};
//
let testBall = new Ball(150, 4, "green", 10);
testBall.draw();
Ball.prototype.update = function () {
this.x += this.velX;
if (this.x + this.size >= width) {
this.x = 0;
} else {
this.y = Math.sin((this.x / 800) * 2 * Math.PI) * 280 + 300;
}
};
// 让球动起来
// 首先我们需要一个地方储存小球
let balls = [];
while (balls.length < 1) {
let size = 3;
let ball = new Ball(random(1, 1), 0.5, "rgb(198, 95, 251)", size);
balls.push(ball);
}
times = 0;
function loop() {
if (times > 2000) {
// ctx.fillStyle = "rgba(0, 0, 0, 0.1)";
ctx.clearRect(0, 0, 1200, 600);
times = 0;
}
for (let i = 0; i < balls.length; i++) {
balls[i].draw();
balls[i].update();
}
requestAnimationFrame(loop);
times += 1;
// console.log(times);
}
loop();