UNPKG

5.05 kBJavaScriptView Raw
1'use strict'
2
3document.body.style.margin = '0'
4
5
6const t = require('tape')
7const Text = require('./index')
8const fps = require('fps-indicator')()
9const gl = require('gl-util/context')()
10const panzoom = require('pan-zoom')
11
12
13
14let q = []
15
16
17t('font', t => {
18 let matrix = []
19
20 let family = ['Roboto', 'sans-serif']
21 let weights = require('css-font-weight-keywords')
22 let stretches = require('css-font-stretch-keywords')
23
24 for (let i = 4; i < weights.length; i++) {
25 let weight = weights[i]
26
27 for (let j = 1; j < stretches.length; j++) {
28 let stretch = stretches[j]
29 let normal = new Text(gl)
30 normal.update({
31 font: { family, weight, stretch },
32 position: [j * 40, i*20],
33 text: weight
34 })
35
36 q.push(normal)
37
38 // <text gl={gl} position={[j * 40, i * 20]} font={{family, weight, stretch}} text={weight}/>
39 }
40
41 for (let j = 1; j < stretches.length; j++) {
42 let stretch = stretches[j]
43 let italic = new Text(gl)
44 italic.update({
45 font: { family, weight, stretch, style: 'italic' },
46 position: [(stretches.length - 1 + j) * 40, i*20],
47 text: weight
48 })
49
50 q.push(italic)
51 }
52 }
53
54 t.end()
55})
56
57t('alignment', t => {
58 q.push(new Text({
59 gl,
60 offset: .5,
61 baseline: 'top',
62 align: 'left',
63 color: 'black',
64 font: 'Minion Pro',
65 position: [1, 1],
66 range: [0,0,2,2],
67 text: 'Middle',
68 kerning: true
69 }))
70 t.end()
71})
72
73t.skip('1e6 letters', t => {
74 let chars = 'abc'
75
76 let positions = [], text = []
77
78 for (let i = 0; i < 1e3; i++) {
79 for (let j = 0; j < 1e2; j++) {
80 text.push(chars)
81 positions.push([Math.random() * 1e3, Math.random() * 1e3])
82 }
83 }
84
85 q.push(new Text({
86 font: '16px Roboto',
87 color: 'black',
88 gl,
89 positions,
90 text
91 }))
92})
93
94t('changing font-size does not trigger text offsets recalc')
95
96t('spacing')
97
98t('color')
99
100t('baseline')
101
102t('array align, position, color, baseline, font, offset', t => {
103 let text = new Text(gl)
104
105 text.update({
106 text: ['red', 'green', 'blue'],
107 position: [[0,50], [150,50], [300,50]],
108 // color: 0x00ff00,
109 color: [0xff0000, 'green', 0x0000ff],
110 // baseline: 'bottom',
111 baseline: ['top', 'middle', 'bottom'],
112 // align: 'right',
113 align: ['left', 'center', 'right'],
114 font: ['sans-serif', 'serif', 'monospace'],
115
116 offset: [[0,0], [-1,-1], [-2,-2]],
117 opacity: [.5, .75, 1],
118 })
119 // text.update({opacity: [.1, .45, 1]})
120 // text.render()
121
122 q.push(text)
123
124 t.end()
125})
126
127t.skip('kerning', t => {
128 q.push(new Text({
129 gl,
130 offset: .5,
131 baseline: 'top',
132 align: 'left',
133 color: 'black',
134 font: 'Minion Pro',
135 position: [1, 1],
136 range: [0,0,2,2],
137 text: 'Middle',
138 kerning: true
139 }))
140})
141
142t('tracking (spacing)')
143
144t('viewport')
145
146t('range')
147
148t('offset', t => {
149 // numeric offset
150 q.push(new Text({
151 gl,
152 text: 'Offset',
153 range: [0,0,1,1],
154 position: [.5,.5],
155 fontSize: 24,
156 offset: 0
157 }))
158
159 // array offset
160 q.push(new Text({
161 gl,
162 text: 'Line2',
163 range: [0,0,1,1],
164 position: [.5,.5],
165 fontSize: 24,
166 offset: [0, 1]
167 }))
168})
169
170t('ignore spaces')
171
172t('canvas2d performance')
173
174t('Augment chars', t => {
175 q.push(new Text({
176 gl,
177 font: {
178 family: 'Minion Pro',
179 weight: 200,
180 size: 24
181 },
182 text: 'ABC',
183 position: [0, 100]
184 }))
185
186 q.push(new Text({
187 gl,
188 font: {
189 family: 'Minion Pro',
190 weight: 200,
191 size: 24
192 },
193 text: 'DEFG',
194 position: [0, 200]
195 }))
196
197 q.push(new Text({
198 gl,
199 font: {
200 family: 'Minion Pro',
201 weight: 200,
202 size: 32
203 },
204 text: 'HIJK',
205 position: [0, 300]
206 }))
207
208 t.end()
209})
210
211t.skip('updating offset twice does not invert sign')
212
213
214
215q.render = function (opts) {
216 if (opts) q.forEach(text => text.update(opts))
217 q.forEach(text => text.render())
218}
219
220setTimeout(() => {
221 let vp = q[0].viewport
222 let range = q[0].range || [vp.x, vp.y, vp.x + vp.width, vp.y + vp.height]
223 q.render()
224
225 panzoom(document.body, e => {
226 let canvas = gl.canvas
227
228 let w = canvas.offsetWidth
229 let h = canvas.offsetHeight
230
231 let rx = e.x / w
232 let ry = e.y / h
233
234 let xrange = range[2] - range[0],
235 yrange = range[3] - range[1]
236
237 if (e.dz) {
238 let dz = e.dz / w
239 range[0] -= rx * xrange * dz
240 range[2] += (1 - rx) * xrange * dz
241
242 // range[1] -= ry * yrange * dz
243 // range[3] += (1 - ry) * yrange * dz
244
245 range[1] -= (1 - ry) * yrange * dz
246 range[3] += ry * yrange * dz
247 }
248
249 range[0] -= xrange * e.dx / w
250 range[2] -= xrange * e.dx / w
251 // range[1] -= yrange * e.dy / h
252 // range[3] -= yrange * e.dy / h
253 range[1] += yrange * e.dy / h
254 range[3] += yrange * e.dy / h
255
256 q.render({ range })
257 })
258}, 50)
259
260
261
262
263// center cross
264let canvas = document.body.appendChild(
265 document.createElement('canvas')
266)
267canvas.style.position = 'absolute'
268canvas.style.left = 0
269canvas.style.top = 0
270canvas.width = window.innerWidth
271canvas.height = window.innerHeight
272let ctx = canvas.getContext('2d')
273
274// +
275ctx.fillStyle = 'blue'
276ctx.fillRect(canvas.width / 2 - 25, canvas.height / 2, 100, 1)
277ctx.fillRect(canvas.width / 2, canvas.height / 2 - 25, 1, 50)
278
279// ctx.fillRect(0, canvas.height - 50, 100, 1)
280
281// ctx.font = '48px Roboto'
282// ctx.textBaseline = 'top'
283// ctx.fillText('MiddleDitchThomas', canvas.width / 2, canvas.height / 2)
284