-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathStar.js
More file actions
58 lines (51 loc) · 1.49 KB
/
Star.js
File metadata and controls
58 lines (51 loc) · 1.49 KB
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
function random(min, max) {
if (arguments.length < 2) {
max = min;
min = 0;
}
if (min > max) {
var hold = max;
max = min;
min = hold;
}
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function maxOrbit(x, y) {
var max = Math.max(x, y),
diameter = Math.round(Math.sqrt(max * max + max * max));
return diameter / 2;
//星星移动范围,值越大范围越小,
}
class Star {
constructor(p, ctx, canvas2) {
this.maxStars = 1100; //星星数量,默认1100
this.orbitRadius = random(maxOrbit(p.w, p.h));
this.ctx = ctx;
this.canvas2 = canvas2;
this.radius = random(60, this.orbitRadius) / 20; //星星大小,值越大星星越小,默认8
this.orbitX = p.w / 2;
this.orbitY = p.h / 2;
this.timePassed = random(0, this.maxStars);
this.speed = random(this.orbitRadius) / 400000; //星星移动速度,值越大越慢,默认5W
this.alpha = random(2, 10) / 10;
}
draw() {
const x = Math.sin(this.timePassed) * this.orbitRadius + this.orbitX;
const y = Math.cos(this.timePassed) * this.orbitRadius + this.orbitY;
let twinkle = random(10);
if (twinkle === 1 && this.alpha > 0) {
this.alpha -= 0.05;
} else if (twinkle === 2 && this.alpha < 1) {
this.alpha += 0.05;
}
this.ctx.globalAlpha = this.alpha;
this.ctx.drawImage(
this.canvas2,
x - this.radius / 2,
y - this.radius / 2,
this.radius,
this.radius
);
this.timePassed += this.speed;
}
}