-
Notifications
You must be signed in to change notification settings - Fork 0
/
starfield.js
63 lines (50 loc) · 1.18 KB
/
starfield.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
/* Credits to The Coding Train */
let speed = 2.5;
window.wallpaperPropertyListener = {
applyUserProperties: function(properties) {
if (properties.speed) {
if (properties.speed.value !== "") {
speed = properties.speed.value;
}
}
}
}
class Star {
constructor() {
this.x = random(-width, width);
this.y = random(-height, height);
this.z = random(width);
this.b = random(100, 255);
}
update() {
this.z -= speed;
if (this.z < 1) {
this.z = width;
this.x = random(-width, width);
this.y = random(-height, height);
}
}
show() {
fill(this.b, this.b, this.b);
noStroke();
let sx = map(this.x / this.z, 0, 1, 0, width);
let sy = map(this.y / this.z, 0, 1, 0, height);
let r = map(this.z, 0, width, 4, 0);
square(sx, sy, r);
}
}
let stars = new Array(2000);
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
for (let i = 0; i < stars.length; i++) {
stars[i] = new Star();
}
}
function draw() {
background(0);
translate(width/2, height/2);
for (let i = 0; i < stars.length; i++) {
stars[i].update();
stars[i].show();
}
}