-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchzrxl.js
92 lines (83 loc) · 2.65 KB
/
chzrxl.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
81
82
83
84
85
86
87
88
89
90
91
92
Blob = function() {
this.init = function(config) {
this.index = config.index;
this.x = config.x;
this.y = config.y;
this.pt1 = config.pt1;
this.pt2 = config.pt2;
this.rate = config.rate;
this.phase = config.phase;
this.colour = config.colour;
this.blobs = config.blobs;
return this;
};
this.update = function(t) {
// p is between 0.0 (at pt1) and 1.0 (at pt2)
var p = (Math.sin((t + this.phase) / this.rate) + 1) / 2;
var blob1 = this.blobs[this.pt1];
var blob2 = this.blobs[this.pt2];
var x1 = blob1.x;
var x2 = blob2.x;
this.x = (x2 - x1) * p + x1;
var y1 = blob1.y;
var y2 = blob2.y;
this.y = (y2 - y1) * p + y1;
};
};
Chzrxl = function() {
this.init = function(config) {
this.width = config.width;
this.height = config.height;
this.blobs = [];
this.numBlobs = 200;
this.numToHoldFixed = Math.floor(this.numBlobs * 0.20);
this.onUpdateBlob = config.onUpdateBlob;
this.reset();
return this;
};
this.forEachBlob = function(callback) {
for (var i = 0; i < this.blobs.length; i++) {
callback(this.blobs[i]);
}
};
this.reset = function() {
this.blobs = [];
this.tick = 0;
for (var i = 0; i < this.numBlobs; i++) {
var x = Math.random() * this.width;
var y = Math.random() * this.height;
var pt1 = i;
var pt2;
while (pt1 === i || pt2 === i || pt2 === pt1) {
pt1 = Math.floor(Math.random() * this.numBlobs);
pt2 = Math.floor(Math.random() * this.numBlobs);
}
var rate = Math.random() * 100 + 100;
var phase = Math.floor(Math.random() * 110);
this.blobs.push(new Blob().init({
index: i,
x: x,
y: y,
pt1: pt1,
pt2: pt2,
rate: rate,
phase: phase,
colour: 0xff0000 + i,
blobs: this.blobs,
onInitBlob: this.onInitBlob
}));
}
};
this.update = function() {
for (var i = 0; i < this.blobs.length; i++) {
if (i < this.numToHoldFixed) continue;
var blob = this.blobs[i];
blob.update(this.tick);
if (this.onUpdateBlob) this.onUpdateBlob(blob);
}
this.tick += 1;
};
this.setPercentToHoldFixed = function(percent) {
this.numToHoldFixed = Math.floor(this.numBlobs * (percent / 100));
};
};