-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwave-text-01.html
283 lines (247 loc) · 8.34 KB
/
wave-text-01.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>twgl.js - wave text</title>
<style>
* {
box-sizing: border-box;
}
html, body {
margin: 0px;
width: 100%;
height: 100%;
overflow: hidden;
font-family: monospace;
}
canvas {
width: 100%;
height: 100%;
}
#b {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 2;
color: white;
}
a:visited, a:link, a:active {
color: white;
}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="b"><a href="http://twgljs.org">twgl.js - wave text</a></div>
</body>
<script id="vs" type="notjs">
uniform mat4 u_viewProjection;
uniform mat4 u_world;
attribute vec4 a_position;
void main() {
gl_Position = u_viewProjection * u_world * a_position;
}
</script>
<script id="fs" type="notjs">
precision mediump float;
uniform vec4 u_color;
void main() {
gl_FragColor = u_color;
}
</script>
<script src="js/twgl-full.min.js"></script>
<script src="js/chroma.min.js"></script>
<script>
"use strict";
var settings = {
msg: "webglfundamentals.org",
zSpacing: 0.5,
start: [0.0, 0.0, 1.0, 'gl'],
end: [ 0, 0, 0.2, 'gl'],
steps: 7,
setOff: 0.15,
letterOff: 0.5,
speed: 2,
vRange: 4,
bk: [0, 0, 0, 'gl'],
};
try {
var str = decodeURIComponent(window.location.search.substring(1));
var userSettings = JSON.parse(str);
Object.keys(userSettings).forEach(function(key) {
settings[key] = userSettings[key];
});
} catch (e) {
//console.error(e);
//console.log(str);
}
var m4 = twgl.m4;
twgl.setAttributePrefix("a_");
var gl = twgl.getWebGLContext(document.getElementById("c"));
var programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]);
function deg2Rad(d) {
return d * Math.PI / 180;
}
function rad2Deg(r) {
return r * 180 / Math.PI;
}
var radius = 50;
/**
* 0-6-1
* | |
* 2-7-3
* | |
* 4-8-5
*/
var charPositions = [
-1, 1, 1, 1,
-1, 0, 1, 0,
-1, -1, 1, -1,
0, 1, 0, 0,
0, -1,
];
var charIndices = [
{ c: '0', indices: [ 0, 1, 1, 5, 5, 4, 4, 0, 0, 5 ], },
{ c: '1', indices: [ 6, 8 ], },
{ c: '2', indices: [ 0, 1, 1, 3, 3, 2, 2, 4, 4, 5 ], },
{ c: '3', indices: [ 0, 1, 1, 5, 5, 4, 2, 3 ], },
{ c: '4', indices: [ 0, 2, 2, 3, 1, 5 ], },
{ c: '5', indices: [ 1, 0, 0, 2, 2, 3, 3, 5, 5, 4 ], },
{ c: '6', indices: [ 1, 0, 0, 4, 4, 5, 5, 3, 3, 2 ], },
{ c: '7', indices: [ 0, 1, 1, 5 ], },
{ c: '8', indices: [ 0, 1, 1, 5, 5, 4, 4, 0, 2, 3 ], },
{ c: '9', indices: [ 3, 2, 2, 0, 0, 1, 1, 5 ], },
{ c: 'a', indices: [ 4, 0, 0, 1, 1, 5, 2, 3 ], },
{ c: 'b', indices: [ 0, 4, 4, 5, 5, 3, 3, 2 ], },
{ c: 'c', indices: [ 3, 2, 2, 4, 4, 5 ], },
{ c: 'd', indices: [ 1, 5, 5, 4, 4, 2, 2, 3 ], },
{ c: 'e', indices: [ 1, 0, 0, 4, 4, 5, 2, 3 ], },
{ c: 'f', indices: [ 1, 0, 0, 4, 2, 3 ], },
{ c: 'g', indices: [ 3, 2, 2, 0, 0, 1, 1, 5, 5, 4 ], },
{ c: 'h', indices: [ 0, 4, 2, 3, 3, 5 ], },
{ c: 'i', indices: [ 0, 1, 6, 8, 4, 5 ], },
{ c: 'j', indices: [ 2, 4, 4, 5, 5, 1 ], },
{ c: 'k', indices: [ 0, 4, 1, 2, 2, 5 ], },
{ c: 'l', indices: [ 0, 4, 4, 5 ], },
{ c: 'm', indices: [ 4, 0, 0, 7, 7, 1, 1, 5 ], },
{ c: 'n', indices: [ 4, 0, 0, 6, 6, 3, 3, 5 ], },
{ c: 'o', indices: [ 0, 1, 1, 5, 5, 4, 4, 0 ], },
{ c: 'p', indices: [ 2, 3, 3, 1, 1, 0, 0, 4 ], },
{ c: 'q', indices: [ 0, 4, 4, 8, 8, 3, 3, 1, 1, 0, 7, 5 ], },
{ c: 'r', indices: [ 4, 0, 0, 1, 1, 3, 3, 2, 2, 5 ], },
{ c: 's', indices: [ 1, 6, 6, 2, 2, 3, 3, 8, 8, 4 ], },
{ c: 't', indices: [ 0, 1, 6, 8 ], },
{ c: 'u', indices: [ 0, 4, 4, 5, 5, 1 ], },
{ c: 'v', indices: [ 0, 8, 8, 1 ], },
{ c: 'w', indices: [ 0, 4, 4, 7, 7, 5, 5, 1 ], },
{ c: 'x', indices: [ 0, 5, 1, 4 ], },
{ c: 'y', indices: [ 0, 7, 7, 1, 7, 8 ], },
{ c: 'z', indices: [ 0, 1, 1, 4, 4, 5 ], },
{ c: '.', indices: [ 2, 7, 7, 8, 8, 4, 4, 2 ], },
];
var total = 0;
var charInfos = {};
var indices = charIndices.map(function(char) {
var indices = char.indices;
var offset = total;
var count = indices.length;
total += count;
charInfos[char.c] = {
offset: offset * 2,
count: count,
};
return indices;
});
var charBufferInfo = twgl.createBufferInfoFromArrays(gl, {
position: { numComponents: 2, data: charPositions },
indices: { numComponents: 2, data: Array.prototype.concat.apply([], indices) },
});
var fieldOfViewY = deg2Rad(30);
var viewProjection = m4.identity();
var drawObjects = [
];
function addString(str, world, color) {
var str = str.toString();
var length = str.length;
var letterSize = 2.0;
var padding = 0.6;
var spacing = letterSize + padding;
var xOffset = (length - 1) * (spacing) * -0.5;
return Array.prototype.map.call(str, function(char, ndx) {
var mat = m4.copy(world);
m4.translate(mat, [xOffset + ndx * spacing, 0, 0], mat);
var charInfo = charInfos[char];
var drawObject;
if (charInfo) {
drawObject = {
programInfo: programInfo,
bufferInfo: charBufferInfo,
type: gl.LINES,
offset: charInfo.offset,
count: charInfo.count,
uniforms: {
u_world: mat,
u_viewProjection: viewProjection,
u_color: color || [1, 1, 1, 1],
},
};
drawObjects.push(drawObject);
}
return drawObject;
});
}
var letterSets = [];
function addStuff(color, zOff) {
var world = m4.identity();
m4.rotateX(world, deg2Rad(0), world);
m4.translate(world, [0, 0, -radius - zOff], world);
letterSets.push(addString(settings.msg, world, color));
}
var zSpacing = settings.zSpacing;
var cStart = chroma.apply(null, settings.start);
var cEnd = chroma.apply(null, settings.end);
var cScale = chroma.scale([cStart, cEnd]).mode('lab');
for (var s = 0; s < settings.steps; ++s) {
var lvl = settings.steps - s - 1;
addStuff(cScale(lvl / settings.steps).gl(), lvl * zSpacing);
}
var frameCount = 0;
var bc = chroma.apply(null, settings.bk).gl();
function render(time) {
time *= 0.001;
++frameCount;
twgl.resizeCanvasToDisplaySize(gl.canvas);//, window.devicePixelRatio || 1);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.clearColor(bc[0], bc[1], bc[2], 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.lineWidth(2);
var aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
var zNear = 0.1;
var zFar = radius + 10 + settings.steps * Math.abs(settings.zSpacing);
var fieldOfViewX = deg2Rad(70);
var fieldOfViewY = 2 * Math.atan(Math.tan(fieldOfViewX * 0.5) * (1 / aspect));
m4.perspective(fieldOfViewY, aspect, zNear, zFar, viewProjection);
//m4.multiply(m4.translation([0, 0, -40]), matrix, matrix);
letterSets.forEach(function(set, setNdx) {
if (setNdx === letterSets.length - 1) {
var color = set[0].uniforms.u_color;
color[0] = (frameCount / 1.5 % 2) * 0.8;
color[1] = (frameCount / 1.7 % 2) * 0.8;
}
set.forEach(function(letter, letterNdx) {
if (letter) {
var uni = letter.uniforms;
letter.translation = letter.translation || m4.getTranslation(uni.u_world);
letter.translation[1] = Math.sin(setNdx * settings.setOff+ letterNdx * settings.letterOff + time * settings.speed) * settings.vRange;
m4.translation(letter.translation, uni.u_world);
}
});
});
twgl.drawObjectList(gl, drawObjects);
requestAnimationFrame(render, gl.canvas);
}
requestAnimationFrame(render);
</script>
</html>