-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsketchtext01.html
695 lines (475 loc) · 16 KB
/
sketchtext01.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
<!DOCTYPE html>
<html lang="en">
<head>
<title>sketchtext</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
html, body {
font-family: Monospace;
color: #666;
margin: 0px;
overflow: hidden;
width: 100%;
height: 100%;
}
canvas {
width: 100%;
height: 100%;
}
#info {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
</style>
</head>
<body>
<div id="info">
type to enter new text, drag to spin the text
<br/>
<span class="button" id="font">change font</span>,
<span class="button" id="weight">change weight</span>,
<span class="button" id="postprocessing">change postprocessing</span>,
<a id="permalink" href="#">permalink</a>
</div>
<script src="js/three.min.js"></script>
<script src="js/three/js/utils/GeometryUtils.js"></script>
<script src="js/three/js/shaders/ConvolutionShader.js"></script>
<script src="js/three/js/shaders/CopyShader.js"></script>
<script src="js/three/js/shaders/FilmShader.js"></script>
<script src="js/three/js/shaders/FXAAShader.js"></script>
<script src="js/three/js/postprocessing/EffectComposer.js"></script>
<script src="js/three/js/postprocessing/RenderPass.js"></script>
<script src="js/three/js/postprocessing/ShaderPass.js"></script>
<script src="js/three/js/postprocessing/MaskPass.js"></script>
<script src="js/three/js/postprocessing/BloomPass.js"></script>
<script src="js/three/js/postprocessing/FilmPass.js"></script>
<!-- load the font files -->
<script src="js/three/fonts/gentilis_bold.typeface.js"></script>
<script src="js/three/fonts/gentilis_regular.typeface.js"></script>
<script src="js/three/fonts/optimer_bold.typeface.js"></script>
<script src="js/three/fonts/optimer_regular.typeface.js"></script>
<script src="js/three/fonts/helvetiker_bold.typeface.js"></script>
<script src="js/three/fonts/helvetiker_regular.typeface.js"></script>
<script src="js/three/fonts/droid/droid_sans_regular.typeface.js"></script>
<script src="js/three/fonts/droid/droid_sans_bold.typeface.js"></script>
<script src="js/three/fonts/droid/droid_serif_regular.typeface.js"></script>
<script src="js/three/fonts/droid/droid_serif_bold.typeface.js"></script>
<!-- todo async loader for fonts -->
<script type="not-js" id="vertexshader">
uniform sampler2D randomTexture;
uniform vec2 randomOffset;
varying vec4 v_position;
void main() {
vec4 pos = vec4(position, 1.0);
vec4 mvPosition = modelViewMatrix * pos;
vec4 rand = texture2D(randomTexture, randomOffset + fract(mvPosition.xy * 0.01));
mvPosition.xy += rand.xy * 2.0;
gl_Position = projectionMatrix * mvPosition;
v_position = pos;
}
</script>
<script type="not-js" id="fragmentshader">
uniform vec3 color;
uniform sampler2D randomTexture;
uniform vec2 randomOffset2;
varying vec4 v_position;
void main() {
vec4 rand = texture2D(randomTexture, randomOffset2 + fract(v_position.xy * 0.002));
float bw = rand.x * rand.y;
bw = pow(1.0 - bw, 8.0);
gl_FragColor = vec4(color + vec3(bw, bw, bw), 1.0);
}
</script>
<script>
/**
* @author zz85 / http://www.lab4games.net/zz85/blog
*
* Creates extruded geometry from a path shape.
*
* parameters = {
*
* curveSegments: <int>, // number of points on the curves
* steps: <int>, // number of points for z-side extrusions / used for subdividing segements of extrude spline too
*
* }
**/
THREE.LineGeometry = function (shapes, options) {
if (typeof(shapes) === "undefined") {
shapes = [];
return;
}
THREE.BufferGeometry.call(this);
this.type = 'LineGeometry';
shapes = shapes instanceof Array ? shapes : [ shapes ];
this.verts = [];
this.addShapeList(shapes, options);
var numVerts = this.verts.reduce(function(prev, curr) {
return prev + curr.length * 2;
}, 0);
var numComponents = 3;
var positions = new Float32Array(numVerts * numComponents);
var d = 0;
this.verts.forEach(function(verts, ndx) {
for (var p = 0, pl = verts.length; p < pl; ++p) {
positions[ d + 0 ] = verts[ p ].x;
positions[ d + 1 ] = verts[ p ].y;
var p2 = (p + 1) % pl;
d += numComponents;
positions[ d + 0 ] = verts[ p2 ].x;
positions[ d + 1 ] = verts[ p2 ].y;
d += numComponents;
}
});
this.verts = undefined;
this.addAttribute('position', new THREE.BufferAttribute(positions, numComponents));
this.computeBoundingSphere();
this.computeBoundingBox();
};
THREE.LineGeometry.prototype = Object.create(THREE.BufferGeometry.prototype);
THREE.LineGeometry.prototype.constructor = THREE.LineGeometry;
THREE.LineGeometry.prototype.addShapeList = function (shapes, options) {
var sl = shapes.length;
for (var s = 0; s < sl; s ++) {
var shape = shapes[ s ];
this.addShape(shape, options);
}
};
THREE.LineGeometry.prototype.addShape = function (shape, options) {
var amount = options.amount !== undefined ? options.amount : 100;
var curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12;
var steps = options.steps !== undefined ? options.steps : 1;
// Variables initalization
var shapePoints = shape.extractPoints(curveSegments);
var vertices = shapePoints.shape;
var holes = shapePoints.holes;
var reverse = ! THREE.Shape.Utils.isClockWise(vertices) ;
if (reverse) {
vertices = vertices.reverse();
// Maybe we should also check if holes are in the opposite direction, just to be safe ...
for (var h = 0, hl = holes.length; h < hl; h ++) {
var ahole = holes[ h ];
if (THREE.Shape.Utils.isClockWise(ahole)) {
holes[ h ] = ahole.reverse();
}
}
reverse = false; // If vertices are in order now, we shouldn't need to worry about them again (hopefully)!
}
this.verts.push(vertices);
this.verts = this.verts.concat(holes);
};
THREE.LineTextGeometry = function (text, parameters) {
parameters = parameters || {};
var textShapes = THREE.FontUtils.generateShapes(text, parameters);
THREE.LineGeometry.call(this, textShapes, parameters);
this.type = 'LineTextGeometry';
};
THREE.LineTextGeometry.prototype = Object.create(THREE.LineGeometry.prototype);
THREE.LineTextGeometry.prototype.constructor = THREE.LineTextGeometry;
var container;
var permalink;
var hex;
var color;
var uniforms;
var camera;
var cameraTarget;
var scene;
var renderer;
var composer;
var effectFXAA;
var group;
var textMesh1;
var textGeo;
var material;
var firstLetter = true;
var text = "sqiggle";
var height = 20;
var size = 130;
var hover = 30;
var curveSegments = 4;
var font = "optimer"; // helvetiker, optimer, gentilis, droid sans, droid serif
var weight = "bold"; // normal bold
var style = "normal"; // normal italic
var fontMap = {
"helvetiker": 0,
"optimer": 1,
"gentilis": 2,
"droid sans": 3,
"droid serif": 4
};
var weightMap = {
"normal": 0,
"bold": 1
};
var reverseFontMap = {};
var reverseWeightMap = {};
for (var i in fontMap) {
reverseFontMap[ fontMap[i] ] = i;
}
for (var i in weightMap) {
reverseWeightMap[ weightMap[i] ] = i;
}
var targetRotation = 0;
var targetRotationOnMouseDown = 0;
var mouseX = 0;
var mouseXOnMouseDown = 0;
var postprocessing = { enabled : false };
var glow = 0.9;
init();
animate();
function capitalize(txt) {
return txt.substring(0, 1).toUpperCase() + txt.substring(1);
}
function decimalToHex(d) {
var hex = Number(d).toString(16);
hex = "000000".substr(0, 6 - hex.length) + hex;
return hex.toUpperCase();
}
function init() {
permalink = document.getElementById("permalink");
// CAMERA
camera = new THREE.PerspectiveCamera(30, 1, 1, 1500);
camera.position.set(0, 400, 700);
cameraTarget = new THREE.Vector3(0, 150, 0);
// SCENE
scene = new THREE.Scene();
// scene.fog = new THREE.Fog(0x000000, 250, 1400);
// LIGHTS
//text = capitalize(font) + " " + capitalize(weight);
//text = "abcdefghijklmnopqrstuvwxyz0123456789";
//text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
// Get text from hash
var hash = document.location.hash.substr(1);
if (hash.length !== 0) {
var fonthash = hash.substr(0, 1);
var weighthash = hash.substr(1, 1);
var pphash = hash.substr(2, 1);
var texthash = hash.substring(4);
font = reverseFontMap[ parseInt(fonthash) ];
weight = reverseWeightMap[ parseInt(weighthash) ];
postprocessing.enabled = parseInt(pphash);
text = decodeURI(texthash);
updatePermalink();
}
var r = {
width: 256,
height: 256,
};
var s = r.width * r.height * 4;
r.data = new Uint8Array(s);
for (var o = 0; o < s; ++o) {
r.data[o] = Math.random() * 256;
}
var randomTexture = new THREE.DataTexture(
r.data, r.width, r.height, THREE.RGBAFormat, THREE.UnsignedByteType, THREE.Texture.DEFAULT_MAPPING, THREE.RepeatWrapping, THREE.RepeatWrapping);
randomTexture.needsUpdate = true;
uniforms = {
color: { type: "c", value: new THREE.Color(0x606060) },
randomTexture: { type: "t", value: randomTexture },
randomOffset: { type: "v2", value: new THREE.Vector2(0, 0) },
randomOffset2: { type: "v2", value: new THREE.Vector2(0, 0) },
};
material = new THREE.ShaderMaterial({
uniforms: uniforms,
attributes: {
// size: { type: 'f', value: null },
// customColor: { type: 'c', value: null }
},
vertexShader: document.getElementById('vertexshader').text,
fragmentShader: document.getElementById('fragmentshader').text,
// blending: THREE.AdditiveBlending,
depthTest: true,
transparent: false,
});
group = new THREE.Group();
group.position.y = 100;
scene.add(group);
createText();
// RENDERER
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setClearColor(0xFFFFFF);
document.body.insertBefore(renderer.domElement, document.body.firstChild);
// EVENTS
document.addEventListener('mousedown', onDocumentMouseDown, false);
document.addEventListener('touchstart', onDocumentTouchStart, false);
document.addEventListener('touchmove', onDocumentTouchMove, false);
document.addEventListener('keypress', onDocumentKeyPress, false);
document.addEventListener('keydown', onDocumentKeyDown, false);
document.getElementById("font").addEventListener('click', function() {
if (font == "helvetiker") {
font = "optimer";
} else if (font == "optimer") {
font = "gentilis";
} else if (font == "gentilis") {
font = "droid sans";
} else if (font == "droid sans") {
font = "droid serif";
} else {
font = "helvetiker";
}
refreshText();
}, false);
document.getElementById("weight").addEventListener('click', function() {
weight = (weight == "bold") ? "normal" : "bold";
refreshText();
}, false);
document.getElementById("postprocessing").addEventListener('click', function() {
postprocessing.enabled = !postprocessing.enabled;
updatePermalink();
}, false);
// POSTPROCESSING
renderer.autoClear = false;
var renderModel = new THREE.RenderPass(scene, camera);
var effectBloom = new THREE.BloomPass(0.25);
var effectFilm = new THREE.FilmPass(0.5, 0.125, 2048, false);
effectFXAA = new THREE.ShaderPass(THREE.FXAAShader);
effectFilm.renderToScreen = true;
composer = new THREE.EffectComposer(renderer);
composer.addPass(renderModel);
composer.addPass(effectFXAA);
composer.addPass(effectBloom);
composer.addPass(effectFilm);
}
function resize() {
var canvas = renderer.context.canvas;
var width = canvas.clientWidth;
var height = canvas.clientHeight;
if (renderer.width != width || renderer.height != height) {
renderer.setSize(width, height, false);
renderer.width = width;
renderer.height = height;
windowHalfX = width / 2;
windowHalfY = height / 2;
camera.aspect = width / height;
camera.updateProjectionMatrix();
composer.reset();
effectFXAA.uniforms[ 'resolution' ].value.set(1 / width, 1 / height);
}
}
//
function boolToNum(b) {
return b ? 1 : 0;
}
function updatePermalink() {
var link = "" + fontMap[ font ] + weightMap[ weight ] + boolToNum(postprocessing.enabled) + "#" + encodeURI(text);
permalink.href = "#" + link;
window.location.hash = link;
}
function onDocumentKeyDown(event) {
if (firstLetter) {
firstLetter = false;
text = "";
}
var keyCode = event.keyCode;
// backspace
if (keyCode == 8) {
event.preventDefault();
text = text.substring(0, text.length - 1);
refreshText();
return false;
}
}
function onDocumentKeyPress(event) {
var keyCode = event.which;
// backspace
if (keyCode == 8) {
event.preventDefault();
} else {
var ch = String.fromCharCode(keyCode);
text += ch;
refreshText();
}
}
function createText() {
textGeo = new THREE.LineTextGeometry(text, {
size: size,
height: height,
curveSegments: curveSegments,
font: font,
weight: weight,
style: style,
});
textGeo.computeBoundingBox();
var centerOffset = -0.5 * (textGeo.boundingBox.max.x - textGeo.boundingBox.min.x);
textMesh1 = new THREE.Line(textGeo, material, THREE.LinePieces);
textMesh1.position.x = centerOffset;
textMesh1.position.y = hover;
textMesh1.position.z = 0;
textMesh1.rotation.x = 0;
textMesh1.rotation.y = Math.PI * 2;
group.add(textMesh1);
}
function refreshText() {
updatePermalink();
group.remove(textMesh1);
if (!text) return;
createText();
}
function onDocumentMouseDown(event) {
event.preventDefault();
document.addEventListener('mousemove', onDocumentMouseMove, false);
document.addEventListener('mouseup', onDocumentMouseUp, false);
document.addEventListener('mouseout', onDocumentMouseOut, false);
mouseXOnMouseDown = event.clientX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
}
function onDocumentMouseMove(event) {
mouseX = event.clientX - windowHalfX;
targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.02;
}
function onDocumentMouseUp(event) {
document.removeEventListener('mousemove', onDocumentMouseMove, false);
document.removeEventListener('mouseup', onDocumentMouseUp, false);
document.removeEventListener('mouseout', onDocumentMouseOut, false);
}
function onDocumentMouseOut(event) {
document.removeEventListener('mousemove', onDocumentMouseMove, false);
document.removeEventListener('mouseup', onDocumentMouseUp, false);
document.removeEventListener('mouseout', onDocumentMouseOut, false);
}
function onDocumentTouchStart(event) {
if (event.touches.length == 1) {
event.preventDefault();
mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
}
}
function onDocumentTouchMove(event) {
if (event.touches.length == 1) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
}
}
//
var frameCount = 0;
function animate() {
++frameCount;
render();
requestAnimationFrame(animate);
}
function render() {
resize();
group.rotation.y += (targetRotation - group.rotation.y) * 0.05;
camera.lookAt(cameraTarget);
if (frameCount % 5 == 0) {
material.uniforms.randomOffset.value.x += 0.001;
material.uniforms.randomOffset.value.y += 0.0013;
material.uniforms.randomOffset2.value.x = frameCount % 3 / 3;
material.uniforms.randomOffset2.value.y = frameCount % 3 / 3;
}
renderer.clear();
if (postprocessing.enabled) {
composer.render(0.05);
} else {
renderer.render(scene, camera);
}
}
</script>
</body>
</html>