-
Notifications
You must be signed in to change notification settings - Fork 0
/
star.js
80 lines (67 loc) · 1.76 KB
/
star.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
73
74
75
76
77
78
79
80
class Star {
constructor() {
this.reset();
}
reset() {
this.x = random(-5000, 5000);
this.y = random(-5000, 5000);
this.z = random(0, 2000);
this.r = random(5, 20);
this.v = random(1, 15);
this.hue = random(0, 60);
this.saturation = random(0, 40);
this.brightness = 100;
}
update() {
this.z -= this.v;
if (this.z <= 0) this.reset();
}
draw() {
let sx = 100.0 * (this.x / this.z);
let sy = 100.0 * (this.y / this.z);
let sr = 0.0001 * this.r * (2000.0 - this.z);
let px = 100.0 * (this.x / (this.z + this.v));
let py = 100.0 * (this.y / (this.z + this.v));
stroke(this.hue, this.saturation, this.brightness);
strokeWeight(sr);
line(sx, sy, px, py);
}
}
class BackgroundStar extends Star {
reset() {
this.noise = new OpenSimplexNoise2D(Date.now());
this.xoff = 0.5;
this.yoff = 0.5;
[this.x, this.y] = this.newPosition();
this.z = random(0, 2000);
this.r = random(5, 20);
this.v = random(1, 15);
this.hue = random(0, 60);
this.saturation = random(0, 40);
this.brightness = 100;
}
newPosition() {
while (true) {
let r = random(-1, 1);
let x = random(-5000, 5000);
let y = random(-5000, 5000);
let n = this.noise.noise(x * this.xoff, y * this.yoff);
if (r > n) {
return [x, y];
}
}
}
draw() {
let sx = (this.x * width) / 10000;
let sy = (this.y * height) / 10000;
let sr = 0.0001 * this.r * (2000.0 - this.z);
stroke(this.hue, this.saturation, this.brightness);
strokeWeight(sr);
noStroke();
fill(this.hue, this.saturation, this.brightness);
circle(sx, sy, sr);
}
update() {
// No need to implement anything here since we don't want background stars to move
}
}