-
Notifications
You must be signed in to change notification settings - Fork 1
/
gltoy.html
294 lines (269 loc) · 9.59 KB
/
gltoy.html
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!doctype html>
<html><head>
<title>GLToyJS</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="deps/game-shim/game-shim.js"></script>
<script type="text/javascript" src="deps/gl-matrix/gl-matrix.js"></script>
<script type="text/javascript" src="deps/webgl-debug.js"></script>
<script type="text/javascript" src="gltoy.js"></script>
<script type="text/javascript">
var exports = {};
var effects = Object.create(null);
function store(name) {
effects[name] = exports;
exports = {};
}
</script>
<script type="text/javascript" src="effects/chain/effect.js"></script>
<script type="text/javascript">store("chain")</script>
<script type="text/javascript" src="effects/orchard/effect.js"></script>
<script type="text/javascript">store("orchard")</script>
<script type="text/javascript" src="effects/particles/effect.js"></script>
<script type="text/javascript">store("particles")</script>
<script type="text/javascript" src="effects/plot/effect.js"></script>
<script type="text/javascript">store("plot")</script>
<script type="text/javascript" src="effects/specular/effect.js"></script>
<script type="text/javascript">store("specular")</script>
<script type="text/javascript" src="effects/voronoi/effect.js"></script>
<script type="text/javascript">store("voronoi")</script>
<style type="text/css">
html, body, canvas {
display: block;
margin: 0;
border: none;
padding: 0;
width: 100%;
height: 100%;
}
body {
background: #777;
}
.overlay {
position: absolute;
font: status-bar;
color: white;
text-shadow: 0 0 .3em #000, 0 0 .3em #000;
margin: .5em .5em;
display: table;
pointer-events: none;
}
.overlay pre {
margin: 0;
max-width: 30em;
max-height: 30em;
overflow: hidden;
text-overflow: ellipsis;
}
.overlay select, .overlay button, .overlay input {
pointer-events: auto;
vertical-align: middle;
}
.overlay select {
color: inherit;
background: none;
}
</style>
<script type="text/javascript">
var toy;
function gltoyMain() {
"use strict";
var stateDisplay = document.getElementById("stateDisplay");
var canvas = document.getElementById("canvas");
var effectMenu = document.getElementById("effectMenu");
toy = new gltoy.EffectManager(canvas, effects);
Object.keys(effects).forEach(function (name) {
var item = document.createElement("option");
item.value = name;
item.textContent = name;
effectMenu.appendChild(item);
});
function switchEffect(name, optParameters, push) {
//console.log("switchEffect", name, optParameters, push);
var parameters = optParameters || effects[name].configure();
toy.switchEffect(name, parameters);
var desc = " ";
function prettyprint(o, indent) {
var nextIndent = indent + " ";
switch (typeof o) {
case "object":
if (Array.isArray(o)) {
desc += "[";
var length = o.length;
if (length === 1) {
desc += " ";
prettyprint(o[0], nextIndent);
desc += " ]";
} else if (length > 0) {
if (o.every(function (v) { return typeof(v) === "number"; })) {
for (var key = 0; key < length; key++) {
desc += " ";
prettyprint(o[key], nextIndent);
desc += ",";
}
desc += "]";
} else {
desc += "\n";
for (var key = 0; key < length; key++) {
desc += nextIndent;
prettyprint(o[key], nextIndent);
desc += "\n";
}
desc += indent + "]";
}
}
} else if (o !== null) {
desc += "{";
var keys = Object.keys(o);
if (keys.length === 1) {
var key = keys[0];
desc += " " + key + ": ";
prettyprint(o[key], nextIndent);
desc += " }";
} else if (keys.length > 0) {
desc += "\n";
keys.forEach(function (key) {
desc += nextIndent + key + ": ";
prettyprint(o[key], nextIndent);
desc += "\n";
});
desc += indent + "}";
} else {
desc += "}";
}
} else {
desc += String(o);
}
break;
case "number":
desc += o % 1 ? o.toFixed(3) : String(o);
break;
default:
desc += String(o);
break;
}
}
prettyprint(parameters, "");
stateDisplay.textContent = desc;
stateDisplay.insertBefore(effectMenu, stateDisplay.firstChild);
effectMenu.value = name;
setTimeout(function () { // isolate failure
var kvs = [["effect", name]];
Object.keys(parameters).forEach(function (pk) {
var value = parameters[pk];
if (typeof value == "string") {
kvs.push(["ps." + pk, parameters[pk]]);
} else {
kvs.push(["pj." + pk, JSON.stringify(parameters[pk])]);
}
});
window.history[push ? "pushState" : "replaceState"](undefined, name,
"#" + kvs.map(function (kv) {
return encodeURIComponent(kv[0]) + "=" + encodeURIComponent(kv[1]);
}).join("&"));
}, 0);
}
function randomEffect() {
var names = Object.keys(effects);
switchEffect(names[Math.floor(Math.random() * names.length)], null, true);
}
// --- ---
function readFragment() {
//console.group("readFragment");
var fragment = window.location.hash;
if (fragment[0] !== "#") return;
fragment = fragment.substring(1);
var effect = null;
var parameters = {};
fragment.split("&").forEach(function (fragkv) {
var kv = fragkv.split("=").map(decodeURIComponent);
if (kv.length !== 2) return;
var match;
if (kv[0] === "effect") {
effect = kv[1];
} else if (match = /^p([js])\.(.*)$/.exec(kv[0])) {
var valueKind = match[1];
var prop = match[2];
try {
var value;
switch (valueKind) {
case "j": value = JSON.parse(kv[1]); break;
case "s": value = kv[1]; break;
}
parameters[prop] = value;
} catch (e) {
if (typeof console !== "undefined")
console.warn("Bad ", kv[0], " in URL; ignoring. ", e);
}
} else {
if (typeof console !== "undefined")
console.warn("Ignoring URL parameter ", kv[0]);
}
});
if (effect != null) {
if (!(effect === toy.currentEffectName &&
JSON.stringify(parameters) === JSON.stringify(toy.currentEffectParameters))) {
if (Object.keys(parameters).length === 0) parameters = null;
switchEffect(effect, parameters, false);
} else {
//console.log("skipping");
}
}
//console.groupEnd();
}
window.addEventListener("popstate", function (event) {
readFragment();
});
effectMenu.addEventListener("change", function (event) {
setTimeout(function () { // in random case don't re-change synchronously
if (effectMenu.value === "") {
randomEffect();
} else {
switchEffect(effectMenu.value, null, true);
}
}, 0);
});
document.addEventListener("keydown", function (event) {
if (event.altKey || event.metaKey) return true; // leave to browser
switch (String.fromCharCode(event.keyCode)) {
case " ": randomEffect(); break;
case "R":
switchEffect(toy.currentEffectName, null, false);
break;
default:
return true;
}
return false;
}, false);
document.getElementById("timerate").addEventListener("change", function (event) {
toy.timeRate = event.target.value;
});
// --- setup ---
readFragment();
if (!toy.currentEffectName) {
//console.log("startup random");
randomEffect();
}
setInterval(randomEffect, 1000 * 60 * 5);
}
</script>
</head>
<body onload="gltoyMain();">
<canvas id="canvas"></canvas>
<div style="top: 0; left: 0;" class="overlay">
<div>
<button onclick="toy.timeRate = document.getElementById('timerate').value = 0; return true;">❚❚</button>
<button onclick="toy.timeRate = document.getElementById('timerate').value = 1; return true;">▶</button>
<input type="range" id="timerate" value="1" min="-3" max="3" step="any" list="timerates" style="width: 10em">
<datalist id="timerates">
<option value="0">
<option value="1">
</datalist>
</div>
<pre id="stateDisplay">
<select id="effectMenu">
<option value="">Random</option>
</select>
</pre>
</div>
</body>
</html>