-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble.js
75 lines (62 loc) · 1.73 KB
/
bubble.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
"use strict";
// Random movement (aka vibration) mode settings.
Bubble.vibration = true;
Bubble.vibrationStep = 5;
Bubble.toggleVibration = function() {
Bubble.vibration = !Bubble.vibration;
}
Bubble.increaseVibrationStep = function() {
if (Bubble.vibrationStep < 40) {
Bubble.vibrationStep++;
}
}
Bubble.decreaseVibrationStep = function() {
if (Bubble.vibrationStep > 1) {
Bubble.vibrationStep--;
}
}
// Two modes: 'time' and 'random'.
Bubble.colorGenerationMode = 'time';
Bubble.toggleColorGenerationMode = function() {
Bubble.colorGenerationMode = (
Bubble.colorGenerationMode === 'time'
? 'random'
: 'time'
);
}
// Time of bubble's existence (in frames).
const LIFESPAN_MAX = 200;
function Bubble(x, y) {
this.x = x;
this.y = y;
this.diameter = 20;
if (Bubble.colorGenerationMode === 'time') {
// Determined at the time of creation.
this.fillColor = Math.floor(Date.now() / 100) % 100;
} else if (Bubble.colorGenerationMode === 'random') {
// Just a random rainbow-ish color.
this.fillColor = random(0, 100);
}
this.lifespan = LIFESPAN_MAX;
this.display = function() {
const colorBrightness = map(this.lifespan, 0, LIFESPAN_MAX, 0, 100);
const strokeShade = (this.lifespan > 255 ? 255 : this.lifespan);
// Hue is the "rainbow" component.
colorMode(HSB, 100);
fill(this.fillColor, 100, colorBrightness, 60);
noStroke();
ellipse(this.x, this.y, this.diameter);
};
this.update = function() {
if (Bubble.vibration) {
const step = Bubble.vibrationStep;
this.x += random(-step, step);
this.y += random(-step, step);
}
this.lifespan--;
this.diameter += 2;
};
this.isFaded = function() {
return this.lifespan < 0;
};
}