forked from mattdesl/canvas-sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
animated-three-text-canvas.js
136 lines (114 loc) · 3.47 KB
/
animated-three-text-canvas.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
/**
* A WebGL example of a basic rotating cube with text, using ThreeJS.
*
* This is similar to canvas-in-canvas.js example, in that it uses
* a second sketch to hold the canvas. You may or may not consider
* this to be overkill, depending on your application...
*
* @author Matt DesLauriers (@mattdesl)
*/
const canvasSketch = require('canvas-sketch');
const random = require('canvas-sketch-util/random');
// Import THREE and assign it to global scope
global.THREE = require('three');
// Now import any ThreeJS example utilities
require('three/examples/js/controls/OrbitControls');
// A sketch that simply renders the passed 'text' setting
// into the center of the canvas
const textSketch = () => {
return ({ context, width, height, settings }) => {
const { text } = settings;
// Clear canvas
context.clearRect(0, 0, width, height);
// Draw background
context.fillStyle = 'black';
context.fillRect(0, 0, width, height);
// Draw text
const fontSize = 80;
context.fillStyle = 'white';
context.textAlign = 'center';
context.textBaseline = 'middle';
context.font = `${fontSize}px monospace`;
context.fillText(text || '', width / 2, height / 2);
};
};
// Setup our sketch
const settings = {
// Make the loop animated
animate: true,
// Get a WebGL canvas rather than 2D
context: 'webgl',
// Turn on MSAA
attributes: { antialias: true }
};
const sketch = async ({ context }) => {
// Wait for text sketch to load up
const textManager = await canvasSketch(textSketch, {
dimensions: [ 512, 512 ],
// Do not attach keyboard shortcuts
hotkeys: false,
// Do not attach to parent
parent: false
});
// Get the other canvas
const otherCanvas = textManager.props.canvas;
// Create a renderer
const renderer = new THREE.WebGLRenderer({
context
});
// Black background
renderer.setClearColor('hsl(0, 0%, 20%)', 1);
// create a camera
const camera = new THREE.PerspectiveCamera(45, 1, 0.01, 100);
camera.position.set(2, 2, -4);
camera.lookAt(new THREE.Vector3());
// set up some orbit controls
const controls = new THREE.OrbitControls(camera, context.canvas);
// setup your scene
const scene = new THREE.Scene();
const map = new THREE.Texture(otherCanvas);
// A cube with basic mamterial
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshBasicMaterial({
map
})
);
scene.add(mesh);
// Update the text with a new string
const setText = (text) => {
// Pass in new settings, this triggers a re-render
textManager.update({
text
});
// Make sure WebGL gets the new texture
map.needsUpdate = true;
};
// Set some random characters
const remix = () => {
const maxChars = 6;
const chars = Array.from(new Array(maxChars)).map(() => {
return String.fromCharCode(random.rangeFloor(33, 127));
}).join('');
setText(chars);
};
remix();
setInterval(remix, 100);
// draw each frame
return {
// Handle resize events here
resize ({ pixelRatio, viewportWidth, viewportHeight }) {
renderer.setPixelRatio(pixelRatio);
renderer.setSize(viewportWidth, viewportHeight);
camera.aspect = viewportWidth / viewportHeight;
camera.updateProjectionMatrix();
},
// And render events here
render ({ time, deltaTime }) {
mesh.rotation.y += deltaTime * (5 * Math.PI / 180);
controls.update();
renderer.render(scene, camera);
}
};
};
canvasSketch(sketch, settings);