-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsky.js
More file actions
79 lines (70 loc) · 2.08 KB
/
Copy pathsky.js
File metadata and controls
79 lines (70 loc) · 2.08 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function Sky(width, height, cloudChance, color) {
this.width = width;
this.height = height;
this.cloudChance = cloudChance;
this.color = color;
this.clouds = [];
}
Sky.prototype.map = function(v, vi, vx, ri, rx) {
const vd = Math.abs(vx - vi);
const vr = Math.abs(v - vi);
const rd = Math.abs(rx - ri);
return ((vr / vd) * rd) + ri;
};
Sky.prototype.run = function() {
sketch.translate(sketch.width/2, sketch.height/2);
sketch.blendMode(sketch.BLEND);
sketch.fill(this.color);
sketch.stroke(0);
sketch.strokeWeight(10);
sketch.ellipseMode(sketch.center);
sketch.ellipse(0, 0, this.width, this.height);
sketch.blendMode(sketch.OVERLAY);
const cc = Math.random();
if (cc <= this.cloudChance) {
const d = this.map(Math.random(), 0, 1, this.width * .1, this.width * .6);
const a = this.map(Math.random(), 0, 1, 0, Math.PI*2);
const x = Math.cos(a) * (this.width / 2 + d/2);
const y = Math.sin(a) * (this.height / 2 + d/2);
let vel = this.map(Math.random(), 0, 1, 0.05, 1.25);
if (x >= 0) {
vel *= -1;
}
const newCloud = new Cloud(x, y, vel, d, sketch.color(255,255,255,200));
this.clouds.push(newCloud);
}
const toDelete = [];
for (let i = 0; i < this.clouds.length; i += 1) {
const cloud = this.clouds[i];
cloud.update();
cloud.draw(sketch);
const p = cloud.pos;
const r = cloud.diameter / 2;
const dist = Math.sqrt(p[0]*p[0] + p[1]*p[1]);
if (dist > (this.width / 2) + r) {
toDelete.push(i);
}
}
for (let i of toDelete) {
this.clouds.splice(i, 1);
}
console.log(this.clouds.length);
}
function Cloud(x, y, v, d, c) {
this.pos = [x, y];
this.velX = v;
this.diameter = d;
this.col = c;
this.yOffset = 0;
this.age = 0;
}
Cloud.prototype.update = function() {
this.yOffset = Math.sin(this.age) * this.diameter * .125;
this.pos = [this.pos[0] + this.velX, this.pos[1]];
this.age += 0.01;
}
Cloud.prototype.draw = function(sketch) {
sketch.noStroke();
sketch.fill(this.col);
sketch.ellipse(this.pos[0], this.pos[1] + this.yOffset, this.diameter, this.diameter);
}