-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
151 lines (124 loc) · 3.27 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"use strict";
// Array containing all the bubbles currently on screen.
const bubbles = [];
// Global state variables.
let rainingMode = false;
let textIsShowing = true;
function setup() {
createCanvas(800, 480);
background(0);
frameRate(50);
}
function draw() {
background(0);
for (let i = 0; i < bubbles.length; i++) {
bubbles[i].display();
bubbles[i].update();
if (bubbles[i].isFaded()) {
bubbles.splice(i--, 1);
}
}
if (rainingMode) {
// Create a randomly positioned bubble.
const x = random(0, width);
const y = random(0, height);
bubbles.push(new Bubble(x, y));
} else if (mouseIsPressed) {
// Works only with raining mode off.
bubbles.push(new Bubble(mouseX, mouseY));
}
if (textIsShowing) {
showText();
}
print("#\ttext: " + textIsShowing + "\t\training: " + rainingMode);
}
function keyPressed() {
switch (keyCode) {
case ' '.charCodeAt(): {
for (let i = 0; i < bubbles.length; i++) {
if (bubbles[i].lifespan > 40) {
bubbles[i].lifespan = 40;
}
}
break;
}
case 'X'.charCodeAt(): {
Bubble.toggleVibration();
break;
}
case 'M'.charCodeAt(): {
Bubble.toggleColorGenerationMode();
break;
}
case 'L'.charCodeAt(): {
const old = textIsShowing;
textIsShowing = !textIsShowing;
print("*L\ttext: " + textIsShowing + "\t\training: " + rainingMode);
break;
}
case 'R'.charCodeAt(): {
const old = rainingMode;
rainingMode = !rainingMode;
print("*R\ttext: " + textIsShowing + "\t\training: " + rainingMode);
break;
}
case UP_ARROW: {
Bubble.increaseVibrationStep();
break;
}
case DOWN_ARROW: {
Bubble.decreaseVibrationStep();
break;
}
}
}
// Alternative for UP_ARROW and DOWN_ARROW.
function mouseWheel(event) {
// Only works if vibration is on.
if (Bubble.vibration) {
if (event.delta > 0) {
// Scrolled down.
Bubble.decreaseVibrationStep();
} else {
// Scrolled up.
Bubble.increaseVibrationStep();
}
}
}
// Show text layers.
function showText() {
fill(255);
stroke(0);
textSize(12);
showTextLegend(); // Upper-left.
showTextVibration(); // Upper-right.
showTextBubbleInfo(); // Bottom-left.
function showTextLegend() {
const keys = [
["Place a bubble", "LMB"],
["Toggle vibration", "X"],
["Toggle colour mode", "M"],
["Increase vibration", "Wheel Up, \u2191"],
["Decrease vibration", "Wheel Down, \u2193"],
["Toggle raining mode", "R"],
["Show or hide text", "L"],
["Clear all bubbles", "Spacebar"]
];
const lineHeight = 17;
for (let i = 0; i < keys.length; i++) {
textAlign(RIGHT);
text(keys[i][0], 120, 20 + i*lineHeight);
textAlign(LEFT);
text(keys[i][1], 130, 20 + i*lineHeight);
}
}
function showTextVibration() {
const state = Bubble.vibration ? Bubble.vibrationStep.toString() : "off";
text("Vibration: " + state, width - 80, 20);
}
function showTextBubbleInfo() {
let bubbleCountText = "Bubble count: " + bubbles.length.toString();
bubbleCountText += ", new bubbles' colour depends on " + Bubble.colorGenerationMode + ".";
text(bubbleCountText, 10, height - 10);
}
}