-
Notifications
You must be signed in to change notification settings - Fork 0
/
Line.ts
513 lines (446 loc) · 12.2 KB
/
Line.ts
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
import { getProperBounds, LTTB, createLinearScale } from "./util";
import { Data, RawData, Color } from "./types";
import { vec2, vec3, mat4 } from "gl-matrix";
import Shader from "./Shader";
export interface Options {
canvas: HTMLCanvasElement;
data: RawData[];
color?: Color;
backgroundColor?: Color;
downsample?: boolean | number;
}
// constants
const THICKNESS = 3.2;
const LINE_COLOR: Color = {
r: 217,
g: 21,
b: 78
};
const BG_COLOR: Color = {
r: 248,
g: 248,
b: 248
};
const DefaultOptions: Partial<Options> = {
downsample: true,
color: LINE_COLOR,
backgroundColor: BG_COLOR
};
export default class Line {
private options: Options;
private data: Data[];
private gl: WebGL2RenderingContext;
private lineShader: Shader;
private overlayShader: Shader;
private lineVAO: WebGLVertexArrayObject;
private overlayVAO: WebGLVertexArrayObject;
private buffer: WebGLBuffer;
private numIndices: number;
private frustum: {
left: number;
right: number;
top: number;
bottom: number;
near: number;
far: number;
};
private xScale: (n: number) => number;
private yScale: (n: number) => number;
constructor(options: Options) {
// merge default options with user defined
this.options = Object.assign({}, DefaultOptions, options);
const { data, canvas, downsample } = this.options;
if (data.length < 2) {
// two more points made lines
return;
}
this.gl = canvas.getContext("webgl2");
if (!this.gl) throw new Error("Unable to create webgl2 context.");
const { width, height } = canvas;
// improve resolution
const fact = 2;
canvas.width = width * fact;
canvas.height = height * fact;
canvas.style.width = width "px";
canvas.style.height = height "px";
this.gl.viewport(0, 0, width * fact, height * fact);
this.gl.enable(this.gl.DEPTH_TEST);
this.data = this.processData(data, downsample, width);
this.frustum = {
left: 0,
right: width * fact,
bottom: 0,
top: height * fact,
near: 1,
far: 100
};
this.createScaleMappers();
this.createShaders();
this.buildObjects();
this.draw();
}
private createScaleMappers() {
this.xScale = createLinearScale(
[this.data[0].x, this.data[this.data.length - 1].x],
[this.frustum.left, this.frustum.right]
);
const bounds = getProperBounds(this.data);
this.yScale = createLinearScale(bounds, [
this.frustum.bottom,
this.frustum.top
]);
}
// RawData -> Data
// respect downsampling
private processData(
rawData: RawData[],
downsample: Options["downsample"],
width: number
): Data[] {
const isOneDemensionData = typeof rawData[0] === "number";
const result: Data[] = [];
if (isOneDemensionData)
rawData.forEach((item, index) =>
result.push({ x: index, y: item as number })
);
else rawData.forEach(item => result.push({ x: item[0], y: item[1] }));
if (typeof downsample == "number" || downsample)
return LTTB(
result,
typeof downsample === "number"
? downsample
: this.calThreshold(result, width)
);
else return result;
}
private createShaders() {
const { gl } = this;
const { color, backgroundColor } = this.options;
const lineVShaderSrc = `
attribute vec2 position;
uniform mat4 mvp;
void main() {
gl_Position = mvp * vec4(position, 0.0, 1.0);
}
`;
const lineFShaderSrc = `
precision highp float;
vec3 lineColor = vec3(${color!.r / 255.0}, ${color!.g / 255.0}, ${color!
.b / 255.0});
void main() {
gl_FragColor = vec4(lineColor, 1.0);
}
`;
this.lineShader = new Shader(gl, lineVShaderSrc, lineFShaderSrc);
const overlayVShaderSrc = `
attribute vec2 position;
uniform mat4 mvp;
void main() {
gl_Position = mvp * vec4(position, -0.1, 1.0);
}
`;
const overlayFShaderSrc = `
precision mediump float;
uniform vec2 resolution;
vec3 bg = vec3(${backgroundColor!.r / 255.0}, ${backgroundColor!.g /
255.0}, ${backgroundColor!.b / 255.0});
void main() {
vec2 st = gl_FragCoord.xy / resolution;
float c = smoothstep(1.0, 0.0, st.y);
gl_FragColor = vec4(c * bg, 1.0);
}
`;
this.overlayShader = new Shader(gl, overlayVShaderSrc, overlayFShaderSrc);
}
private calThreshold(data: Data[], width: number): number {
const { length } = data;
if (length < 2) return length;
// The interval of two adjacent points is 15 display pixels
const maxPoints = Math.floor(width / 15 1);
return length > maxPoints ? maxPoints : length;
}
// build vertics and indices that form the line segments
private buildObjects() {
const { gl } = this;
const lineVertices: number[] = [];
const lineIndices: number[] = [];
const overlayVertices: number[] = [];
const overlayIndices: number[] = [];
// build first point
{
const p0 = vec2.fromValues(
this.xScale(this.data[0].x),
this.yScale(this.data[0].y)
);
const p1 = vec2.fromValues(
this.xScale(this.data[1].x),
this.yScale(this.data[1].y)
);
const p0p1 = vec2.sub(vec2.create(), p1, p0);
const normal = vec2.normalize(
vec2.create(),
vec2.fromValues(-p0p1[1], p0p1[0])
);
const t = vec2.fromValues(normal[0] * THICKNESS, normal[1] * THICKNESS);
const t0 = vec2.sub(vec2.create(), p0, t);
const t1 = vec2.add(vec2.create(), p0, t);
lineVertices.push(
// 0
t0[0],
t0[1],
// 1
t1[0],
t1[1]
);
if (t0[0] < t1[0]) {
overlayVertices.push(
// 0
t0[0],
this.frustum.bottom,
// 1
t0[0],
t0[1]
);
} else {
overlayVertices.push(
// 0
t1[0],
this.frustum.bottom,
// 1
t1[0],
t1[1]
);
}
}
// build middle points
for (let i = 1; i < this.data.length - 1; i ) {
const p0 = vec2.fromValues(
this.xScale(this.data[i - 1].x),
this.yScale(this.data[i - 1].y)
);
const p1 = vec2.fromValues(
this.xScale(this.data[i].x),
this.yScale(this.data[i].y)
);
const p2 = vec2.fromValues(
this.xScale(this.data[i 1].x),
this.yScale(this.data[i 1].y)
);
const p0p1 = vec2.sub(vec2.create(), p1, p0);
const p1p2 = vec2.sub(vec2.create(), p2, p1);
const p0p1Norm = vec2.normalize(vec2.create(), p0p1);
const p1p2Norm = vec2.normalize(vec2.create(), p1p2);
const tangent = vec2.add(vec2.create(), p0p1Norm, p1p2Norm);
const tangentNorm = vec2.normalize(vec2.create(), tangent);
const miter = vec2.fromValues(-tangentNorm[1], tangentNorm[0]);
const normal0 = vec2.normalize(
vec2.create(),
vec2.fromValues(-p0p1[1], p0p1[0])
);
const len = THICKNESS / vec2.dot(miter, normal0);
const t2 = vec2.sub(
vec2.create(),
p1,
vec2.fromValues(miter[0] * len, miter[1] * len)
);
const t3 = vec2.add(
vec2.create(),
p1,
vec2.fromValues(miter[0] * len, miter[1] * len)
);
lineVertices.push(
// 2
t2[0],
t2[1],
// 3
t3[0],
t3[1]
);
overlayVertices.push(
// 0
t3[0],
this.frustum.bottom,
// 1
t3[0],
t3[1]
);
lineIndices.push(
1 (i - 1) * 2,
0 (i - 1) * 2,
2 (i - 1) * 2,
1 (i - 1) * 2,
2 (i - 1) * 2,
3 (i - 1) * 2
);
overlayIndices.push(
1 (i - 1) * 2,
0 (i - 1) * 2,
2 (i - 1) * 2,
1 (i - 1) * 2,
2 (i - 1) * 2,
3 (i - 1) * 2
);
}
// build last point
{
const i = this.data.length - 2;
const p1 = vec2.fromValues(
this.xScale(this.data[i].x),
this.yScale(this.data[i].y)
);
const p2 = vec2.fromValues(
this.xScale(this.data[i 1].x),
this.yScale(this.data[i 1].y)
);
const p1p2 = vec2.sub(vec2.create(), p2, p1);
const normal = vec2.normalize(
vec2.create(),
vec2.fromValues(-p1p2[1], p1p2[0])
);
const t = vec2.fromValues(normal[0] * THICKNESS, normal[1] * THICKNESS);
const t4 = vec2.sub(vec2.create(), p2, t);
const t5 = vec2.add(vec2.create(), p2, t);
lineVertices.push(
// 4
t4[0],
t4[1],
// 5
t5[0],
t5[1]
);
if (t4[0] < t5[0]) {
overlayVertices.push(
// 0
t5[0],
this.frustum.bottom,
// 1
t5[0],
t5[1]
);
} else {
overlayVertices.push(
// 0
t4[0],
this.frustum.bottom,
// 1
t4[0],
t4[1]
);
}
lineIndices.push(
1 i * 2,
0 i * 2,
2 i * 2,
1 i * 2,
2 i * 2,
3 i * 2
);
overlayIndices.push(
1 i * 2,
0 i * 2,
2 i * 2,
1 i * 2,
2 i * 2,
3 i * 2
);
}
{
const lineVAO = gl.createVertexArray();
gl.bindVertexArray(lineVAO);
const lineVBO = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, lineVBO);
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array(lineVertices),
gl.STATIC_DRAW
);
const lineEBO = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, lineEBO);
gl.bufferData(
gl.ELEMENT_ARRAY_BUFFER,
new Uint16Array(lineIndices),
gl.STATIC_DRAW
);
const positionLoc = gl.getAttribLocation(
this.lineShader.program,
"position"
);
gl.vertexAttribPointer(positionLoc, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(positionLoc);
this.lineVAO = lineVAO;
this.lineShader.setNumOfIndices(lineIndices.length);
}
{
const overlayVAO = gl.createVertexArray();
gl.bindVertexArray(overlayVAO);
const overlayVBO = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, overlayVBO);
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array(overlayVertices),
gl.STATIC_DRAW
);
const overlayEBO = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, overlayEBO);
gl.bufferData(
gl.ELEMENT_ARRAY_BUFFER,
new Uint16Array(overlayIndices),
gl.STATIC_DRAW
);
const positionLoc = gl.getAttribLocation(
this.overlayShader.program,
"position"
);
gl.vertexAttribPointer(positionLoc, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(positionLoc);
this.overlayVAO = overlayVAO;
this.overlayShader.setNumOfIndices(overlayIndices.length);
}
}
private buildMVP() {
const { gl, frustum } = this;
const p = mat4.ortho(
mat4.create(),
frustum.left,
frustum.right,
frustum.bottom,
frustum.top,
frustum.near,
frustum.far
);
const v = mat4.targetTo(
mat4.create(),
vec3.fromValues(0, 0, 2),
vec3.fromValues(0, 0, -1),
vec3.fromValues(0, 1, 0)
);
const m = mat4.create();
const mvp = mat4.mul(mat4.create(), mat4.mul(mat4.create(), m, v), p);
return mvp;
}
private draw() {
const { gl } = this;
const { backgroundColor } = this.options;
gl.clearColor(
backgroundColor.r / 255.0,
backgroundColor.g / 255.0,
backgroundColor.b / 255.0,
1
);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
const mvp = this.buildMVP();
this.lineShader.use();
this.lineShader.setMat4("mvp", mvp);
this.lineShader.draw(this.lineVAO);
this.overlayShader.use();
this.overlayShader.setMat4("mvp", mvp);
this.overlayShader.set2fv(
"resolution",
new Float32Array([
this.frustum.right - this.frustum.left,
this.frustum.top - this.frustum.bottom
])
);
this.overlayShader.draw(this.overlayVAO);
}
}