-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
74 lines (59 loc) · 1.3 KB
/
sketch.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
const
dZ = 8,
maxR = 4,
starCount = 400;
class Star {
constructor() {
this.reset()
}
reset = () => {
this.x = random(-width, width);
this.y = random(-height, height);
this.z = random(width);
this.pz = this.z;
}
show = () => {
fill(255);
noStroke();
this.sx = map(this.x / this.z, 0, 1, 0, width);
this.sy = map(this.y / this.z, 0, 1, 0, height);
// let r = map(this.z, 0, width / 2, maxR, 1);
// ellipse(this.sx, this.sy, r, r);
this.px = map(this.x / this.pz, 0, 1, 0, width);
this.py = map(this.y / this.pz, 0, 1, 0, height);
stroke(255);
strokeWeight(0.5)
line(this.px, this.py, this.sx, this.sy)
}
tick = () => {
this.pz = this.z;
this.z = this.z - dZ;
if (
this.z < 1 ||
this.sx < -(width / 2) ||
this.sx > width / 2 ||
this.sy < -(height / 2) ||
this.sy > height / 2
) {
this.reset()
}
}
}
var stars = [];
function setup() {
createCanvas(windowWidth, windowHeight);
for (let i = 0; i < starCount; i++) {
stars.push(new Star());
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight)
}
function draw() {
background(0);
translate(windowWidth / 2, windowHeight / 2)
stars.forEach((star) => {
star.tick();
star.show();
})
}