-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebgl2-2d.js
More file actions
594 lines (513 loc) · 21.3 KB
/
webgl2-2d.js
File metadata and controls
594 lines (513 loc) · 21.3 KB
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
((() => {
class Path {
constructor(ctx) {
this.paths = []
this.ctx = ctx
}
rect(x, y, width, height) {
this.paths.push([
[x, y],
[x, y + height],
[x + width, y + height],
[x + width, y],
])
this.paths[this.paths.length - 1].closed = true
}
arc(x, y, radius, startAngle, endAngle, anticlockwise = false) {
const arcPath = createArc(x, y, radius, startAngle, endAngle, 30, anticlockwise)
const last = this.paths.length - 1
if (last >= 0)
this.paths[last] = this.paths[last].concat(arcPath.slice(1)) // concat, remove duplicate arc start point
else
this.paths.push(arcPath)
}
arcTo(x1, y1, x2, y2, radius) {
const last = this.paths.length - 1
if (this.paths[last].closed) {
// path closed at start point
const [x0, y0] = this.paths[last][0]
const path = createTangentArc(x0, y0, x1, y1, x2, y2, radius)
this.paths.push(path)
} else {
const [x0, y0] = this.paths[last][this.paths[last].length - 1] // last point
const path = createTangentArc(x0, y0, x1, y1, x2, y2, radius)
this.paths[last] = this.paths[last].concat(path)
}
}
moveTo(x, y) {
this.paths.push([[x, y]])
}
lineTo(x, y) {
const last = this.paths.length - 1
if (this.paths[last].closed) {
// path closed at start point
const prevPoint = this.paths[last][0].slice()
this.paths.push([prevPoint, [x, y]])
} else {
this.paths[last].push([x, y])
}
}
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
const last = this.paths.length - 1
if (this.paths[last].closed) {
// path closed at start point
const startPoint = this.paths[last][0].slice()
const path = createBezier(startPoint, [cp1x, cp1y], [cp2x, cp2y], [x, y], 30)
this.paths.push(path)
} else {
const startPoint = this.paths[last][this.paths[last].length - 1].slice() // last point
const path = createBezier(startPoint, [cp1x, cp1y], [cp2x, cp2y], [x, y], 30)
this.paths[last] = this.paths[last].concat(path.splice(1)) // prevent duplicate join point
}
}
quadraticCurveTo(cpx, cpy, x, y) {
const last = this.paths.length - 1
if (this.paths[last].closed) {
// path closed at start point
const startPoint = this.paths[last][0].slice()
const path = createQuadratic(startPoint, [cpx, cpy], [x, y], 30)
this.paths.push(path)
} else {
const startPoint = this.paths[last][this.paths[last].length - 1].slice() // last point
const path = createQuadratic(startPoint, [cpx, cpy], [x, y], 30)
this.paths[last] = this.paths[last].concat(path.splice(1)) // prevent duplicate join point
}
}
closePath() {
const last = this.paths.length - 1
const startPoint = [this.paths[last][0][0], this.paths[last][0][1]]
const lastIdx = this.paths[last].length - 1
const lastPoint = [this.paths[last][lastIdx][0], this.paths[last][lastIdx][1]]
if (startPoint[0] === lastPoint[0] && startPoint[1] === lastPoint[1]) {
// remove duplicate point
this.paths[last].pop()
}
// NOTE: hack, add closed attribute to array
this.paths[last].closed = true
}
getStrokeBufferData() {
let positions = []
let indices = []
const width = this.ctx.lineWidth
const lineJoin = this.ctx.lineJoin
const lineCap = this.ctx.lineCap
for (const path of this.paths) {
if (path.length < 2) continue
const indexOffset = positions.length / 2 // index offset when combine all path's position and index, divided by 2: 2 term (x, y) mapping to 1 index
const pathData = getPathStrokeBufferData(path, width, path.closed, indexOffset, lineJoin, lineCap)
positions = positions.concat(pathData.positions)
indices = indices.concat(pathData.indices)
}
return {
positions,
indices
}
}
getShapeBufferData() {
let positions = []
let indices = []
for (const path of this.paths) {
const indexOffset = positions.length / 2 // index offset when combine all path's position and index, divided by 2: 2 term (x, y) mapping to 1 index
const data = getPathShapeBufferData(path, indexOffset)
positions = positions.concat(data.positions)
indices = indices.concat(data.indices)
}
return {
positions,
indices
}
}
}
class ImageData {
constructor(width, height, data) {
this.width = width
this.height = height
this.data = data
if (!this.data) {
this.data = new Uint8Array(width * height * 4)
}
}
}
class CanvasGradient {
constructor() {
this.stops = []
}
addColorStop(offset, color) {
this.stops.push({
offset,
color: colorParser(color)
})
}
imageData(width, height) { }
}
class CanvasLinearGradient extends CanvasGradient {
constructor(x1, y1, x2, y2) {
super()
this.x1 = x1
this.y1 = y1
this.x2 = x2
this.y2 = y2
}
imageData(width, height) {
const imageData = new ImageData(width, height)
const [x1, x2, y1, y2] = [this.x1, this.x2, this.y1, this.y2]
const normal = normalize([x2 - x1, y2 - y1])
// const dirX = x2 - x1
// const dirY = y2 - y1
const length = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
const tableLen = parseInt(Math.sqrt(width ** 2 + height ** 2))
const colorTable = new Array(tableLen).fill(0)
this.stops.sort((a, b) => {
return a.offset - b.offset
})
for (let i = 0; i < this.stops.length - 1; i++) {
const grad1 = this.stops[i]
const grad2 = this.stops[i + 1]
for (let p = parseInt((grad1.offset * tableLen)); p <= parseInt((grad2.offset * tableLen)); p++) {
const k = (p - parseInt((grad1.offset * tableLen))) / ((grad2.offset - grad1.offset) * tableLen)
colorTable[p] = lerpColor(grad1.color, grad2.color, k)
}
}
for (let r = 0; r < height; r++) {
for (let c = 0; c < width; c++) {
// translate
let xx = c - x1
let yy = r - y1
// rotate
// let xxx = dirX * xx + dirY * yy
let xxx = normal[0] * xx + normal[1] * yy
// let yyy = -normal[1] * xx + normal[0] * yy
// scale
xxx /= length
const colorTableIdx = Math.min(Math.max(parseInt(xxx * tableLen), 0), tableLen - 1);
imageData.data[(r * width + c) * 4] = parseInt(colorTable[colorTableIdx].r * 255)
imageData.data[(r * width + c) * 4 + 1] = parseInt(colorTable[colorTableIdx].g * 255)
imageData.data[(r * width + c) * 4 + 2] = parseInt(colorTable[colorTableIdx].b * 255)
imageData.data[(r * width + c) * 4 + 3] = parseInt(colorTable[colorTableIdx].a * 255)
}
}
return imageData
}
}
class CanvasRadialGradient extends CanvasGradient {
constructor(x1, y1, r1, x2, y2, r2) {
super()
this.x1 = x1
this.y1 = y1
this.r1 = r1
this.x2 = x2
this.y2 = y2
this.r2 = r2
}
imageData(width, height) {
// TODO: ...
const imageData = new ImageData(width, height)
const [x1, y1, r1, x2, y2, r2] = [this.x1, this.y1, this.r1, this.x2, this.y2, this.r2]
}
}
class WebGL2RenderingContext2D {
constructor(canvas) {
this._renderer = new Renderer(canvas)
this._width = canvas.width
this._height = canvas.height
this._zIdx = 0 // NOTE: z offset of geometry, due to three's geometry limitation, not need when using pure WebGL
this._path = null
/*
consider WebGL's uniforms
4d, column major
[
a, b, 0, 0,
c, d, 0, 0,
0, 0, 1, 0,
e, f, 0, 1,
]
*/
this._transform = [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
]
this._stateStack = []
// public attributes
this.canvas = canvas
this.lineWidth = 1
this.lineJoin = 'miter'
this.lineCap = 'butt'
this._strokeStyle = { r: 0, g: 0, b: 0, a: 1 } // setter&getter
this._fillStyle = { r: 0, g: 0, b: 0, a: 1 } // setter&getter
}
_createGradientTexture(gradient, width, height) {
// TODO: ...
}
_createGradientImageData(gradient, width, height) {
// TODO: bad implementation, create texture instead
// TODO: need refactor
if (gradient.type === 'linear') {
} else if (gradient.type === 'radial') {
const imageData = new ImageData(width, height)
const { x1, x2, y1, y2 } = gradient
const normal = normalize([x2 - x1, y2 - y1])
// const dirX = x2 - x1
// const dirY = y2 - y1
const length = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
const tableLen = parseInt(Math.sqrt(width ** 2 + height ** 2))
const colorTable = new Array(tableLen).fill(0)
gradient.stops.sort((a, b) => {
return a.offset - b.offset
})
for (let i = 0; i < gradient.stops.length - 1; i++) {
const grad1 = gradient.stops[i]
const grad2 = gradient.stops[i + 1]
for (let p = parseInt((grad1.offset * tableLen)); p <= parseInt((grad2.offset * tableLen)); p++) {
const k = (p - parseInt((grad1.offset * tableLen))) / ((grad2.offset - grad1.offset) * tableLen)
colorTable[p] = lerpColor(grad1.color, grad2.color, k)
}
}
for (let r = 0; r < height; r++) {
for (let c = 0; c < width; c++) {
// translate
let xx = c - x1
let yy = r - y1
// rotate
// let xxx = dirX * xx + dirY * yy
let xxx = normal[0] * xx + normal[1] * yy
// let yyy = -normal[1] * xx + normal[0] * yy
// scale
xxx /= length
const colorTableIdx = Math.min(Math.max(parseInt(xxx * tableLen), 0), tableLen - 1);
imageData.data[(r * width + c) * 4] = parseInt(colorTable[colorTableIdx].r * 255)
imageData.data[(r * width + c) * 4 + 1] = parseInt(colorTable[colorTableIdx].g * 255)
imageData.data[(r * width + c) * 4 + 2] = parseInt(colorTable[colorTableIdx].b * 255)
imageData.data[(r * width + c) * 4 + 3] = parseInt(colorTable[colorTableIdx].a * 255)
}
}
return imageData
}
}
_draw(positions, indices, fillStyle) {
if (fillStyle instanceof CanvasGradient) {
// TODO: maybe not work with transform?
const texCoords = positions.map((v, i) => {
return i % 2 == 0 ? v / this._width : v / this._height
})
// const imageData = this._createGradientImageData(fillStyle, this._width, this._height)
const imageData = fillStyle.imageData(this._width, this._height)
this._renderer.drawTexture(
positions,
indices,
texCoords,
createTextureFromUint8Array(this._renderer.gl, imageData.data, this._renderer.gl.RGBA, this._width, this._height)
)
} else {
this._renderer.draw(positions, indices, [fillStyle.r, fillStyle.g, fillStyle.b, fillStyle.a])
}
}
// API
beginPath() {
this.path = new Path(this)
}
closePath() {
this.path.closePath()
}
moveTo(x, y) {
// TODO: consider call moveTo without beginPath
this.path.moveTo(x, y)
}
lineTo(x, y) {
this.path.lineTo(x, y)
}
arc(x, y, radius, startAngle, endAngle, anticlockwise = false) {
this.path.arc(x, y, radius, startAngle, endAngle, anticlockwise)
}
arcTo(x1, y1, x2, y2, radius) {
this.path.arcTo(x1, y1, x2, y2, radius)
}
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
this.path.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
}
quadraticCurveTo(cpx, cpy, x, y) {
this.path.quadraticCurveTo(cpx, cpy, x, y)
}
stroke() {
const { positions, indices } = this.path.getStrokeBufferData()
this._draw(positions, indices, this._strokeStyle)
}
clip(path, fillStyle) {
// TODO: support specify path and fillStyle
// TODO: save/restore
this._renderer.prepareStencil()
this.fill()
this._renderer.useStencil()
}
rect(x, y, width, height) {
this.path.rect(x, y, width, height)
}
fill() {
const { positions, indices } = this.path.getShapeBufferData()
this._draw(positions, indices, this._fillStyle)
}
fillRect(x, y, width, height) {
const { positions, indices } = generateRectBufferData(x, y, width, height)
this._draw(positions, indices, this._fillStyle)
}
strokeRect(x, y, width, height) {
const rectPath = new Path(this)
rectPath.rect(x, this._height - y - height, width, height)
const { positions, indices } = rectPath.getStrokeBufferData()
this._draw(positions, indices, this._strokeStyle)
}
clearRect(x, y, width, height) {
const { positions, indices } = generateRectBufferData(x, y, width, height)
const transparent = { r: 0, g: 0, b: 0, a: 0 }
this._draw(positions, indices, transparent)
}
// Transformations
transform(a, b, c, d, e, f) {
const newMat = [
a, b, 0, 0,
c, d, 0, 0,
0, 0, 1, 0,
e, f, 0, 1
]
// NOTE: post-multiply!!
// it's not directly transform shapes, it's canvas!
this._transform = mat4Multiply(this._transform, newMat)
this._renderer.setTransform(this._transform)
}
setTransform(a, b, c, d, e, f) {
this._transform = [
a, b, 0, 0,
c, d, 0, 0,
0, 0, 1, 0,
e, f, 0, 1
]
this._renderer.setTransform(this._transform)
}
translate(x, y) {
this.transform(1, 0, 0, 1, x, y)
}
rotate(angle) {
this.transform(
Math.cos(angle),
Math.sin(angle),
-Math.sin(angle),
Math.cos(angle),
0,
0
)
}
scale(x, y) {
this.transform(x, 0, 0, y, 0, 0)
}
// save & restore states
/*
Each context maintains a stack of drawing states. Drawing states consist of:
* The current transformation matrix.
* The current clipping region.
* The current values of the following attributes: strokeStyle, fillStyle, globalAlpha, lineWidth, lineCap, lineJoin, miterLimit, shadowOffsetX, shadowOffsetY, shadowBlur, shadowColor, globalCompositeOperation, font, textAlign, textBaseline.
The current path and the current bitmap are not part of the drawing state. The current path is persistent, and can only be reset using the beginPath() method. The current bitmap is a property of the canvas, not the context.
*/
save() {
this._stateStack.push({
transform: this._transform.slice(),
strokeStyle: this._strokeStyle,
fillStyle: this._fillStyle,
lineWidth: this.lineWidth
})
}
restore() {
if (this._stateStack.length === 0) return;
const state = this._stateStack.pop()
this._transform = state.transform
this._renderer.setTransform(this._transform)
this._strokeStyle = state.strokeStyle
this._fillStyle = state.fillStyle
this.lineWidth = state.lineWidth
}
isPointInPath(x, y, fillRule) {
// TODO: support pass path: isPointInPath(path, x, y, fillRule)
if (this.path.paths.length === 0)
return false
const currPath = this.path.paths[this.path.paths.length - 1].slice()
if (currPath.length === 0)
return false
// close path
const startPoint = [currPath[0][0], currPath[0][1]]
const lastIdx = currPath.length - 1
const lastPoint = [currPath[lastIdx][0], currPath[lastIdx][1]]
if (startPoint[0] === lastPoint[0] && startPoint[1] === lastPoint[1]) {
currPath.pop()
}
return pointInPolygon(currPath, x, y, fillRule)
}
drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) {
if (arguments.length === 3) {
this._renderer.drawImage(image, 0, 0, image.width, image.height, sx, sy, image.width, image.height)
} else if (arguments.length === 5) {
this._renderer.drawImage(image, 0, 0, image.width, image.height, sx, sy, sWidth, sHeight)
} else if (arguments.length === 9) {
this._renderer.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)
} else {
throw new Error('Invalid parameters')
}
}
// pixel manipulation
getImageData(sx, sy, sw, sh) {
return this._renderer.getImageData(sx, sy, sw, sh)
}
putImageData(imageData, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight) {
this._renderer.putImageData(imageData, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight)
}
createImageData(width, height) {
if (arguments.length === 1) {
const imageData = width
width = imageData.width
height = imagedata.height
}
return {
width,
height,
data: new Uint8ClampedArray(width * height * 4)
}
}
// gradient
createLinearGradient(x1, y1, x2, y2) {
return new CanvasLinearGradient(x1, y1, x2, y2)
}
createRadialGradient(x1, y1, r1, x2, y2, r2) {
return new CanvasRadialGradient(x1, y1, r1, x2, y2, r2)
}
set fillStyle(style) {
if (style instanceof CanvasGradient) {
this._fillStyle = style
} else {
this._fillStyle = colorParser(style)
}
}
get fillStyle() {
// TODO: format
return this._fillStyle
}
set strokeStyle(style) {
if (style instanceof CanvasGradient) {
this._strokeStyle = style
} else {
this._strokeStyle = colorParser(style)
}
}
get strokeStyle() {
// TODO: format
return this._strokeStyle
}
}
// mixin getContext
const originGetContext = HTMLCanvasElement.prototype.getContext
HTMLCanvasElement.prototype.getContext = function (contextType) {
if (contextType === 'webgl2-2d') {
return new WebGL2RenderingContext2D(this) // TODO: consider arguments
} else {
return originGetContext.apply(this, arguments) // TODO: ..
}
}
})()) // TODO: more proper way