@nexterm/canvas 0.0.0-stage → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/geometry.js ADDED
@@ -0,0 +1,1006 @@
1
+ const { inspect } = require('util')
2
+ /*
3
+ * vendored in order to fix its dependence on the window global [cds 2020/08/04]
4
+ * otherwise unchanged from https://github.com/jarek-foksa/geometry-polyfill/tree/f36bbc8f4bc43539d980687904ce46c8e915543d
5
+ */
6
+
7
+ // @info
8
+ // DOMPoint polyfill
9
+ // @src
10
+ // https://drafts.fxtf.org/geometry/#DOMPoint
11
+ // https://github.com/chromium/chromium/blob/master/third_party/blink/renderer/core/geometry/dom_point_read_only.cc
12
+ class DOMPoint {
13
+ constructor(x = 0, y = 0, z = 0, w = 1) {
14
+ this.x = x
15
+ this.y = y
16
+ this.z = z
17
+ this.w = w
18
+ }
19
+
20
+ static fromPoint(otherPoint) {
21
+ return new DOMPoint(
22
+ otherPoint.x,
23
+ otherPoint.y,
24
+ otherPoint.z !== undefined ? otherPoint.z : 0,
25
+ otherPoint.w !== undefined ? otherPoint.w : 1,
26
+ )
27
+ }
28
+
29
+ // matrixTransform(optional DOMMatrixInit matrix = {})
30
+ matrixTransform(matrix) {
31
+ const { values: m, is2D } = matrixFromInit(matrix, MATRIX_TRANSFORM)
32
+ if (is2D && this.z === 0 && this.w === 1) {
33
+ return new DOMPoint(this.x * m[M11] + this.y * m[M21] + m[M41], this.x * m[M12] + this.y * m[M22] + m[M42], 0, 1)
34
+ } else {
35
+ return new DOMPoint(
36
+ this.x * m[M11] + this.y * m[M21] + this.z * m[M31] + this.w * m[M41],
37
+ this.x * m[M12] + this.y * m[M22] + this.z * m[M32] + this.w * m[M42],
38
+ this.x * m[M13] + this.y * m[M23] + this.z * m[M33] + this.w * m[M43],
39
+ this.x * m[M14] + this.y * m[M24] + this.z * m[M34] + this.w * m[M44],
40
+ )
41
+ }
42
+ }
43
+
44
+ toJSON() {
45
+ return {
46
+ x: this.x,
47
+ y: this.y,
48
+ z: this.z,
49
+ w: this.w,
50
+ }
51
+ }
52
+ }
53
+
54
+ // @info
55
+ // DOMRect polyfill
56
+ // @src
57
+ // https://drafts.fxtf.org/geometry/#DOMRect
58
+ // https://github.com/chromium/chromium/blob/master/third_party/blink/renderer/core/geometry/dom_rect_read_only.cc
59
+
60
+ class DOMRect {
61
+ constructor(x = 0, y = 0, width = 0, height = 0) {
62
+ this.x = x
63
+ this.y = y
64
+ this.width = width
65
+ this.height = height
66
+ }
67
+
68
+ static fromRect(otherRect) {
69
+ return new DOMRect(otherRect.x, otherRect.y, otherRect.width, otherRect.height)
70
+ }
71
+
72
+ get top() {
73
+ return this.y
74
+ }
75
+
76
+ get left() {
77
+ return this.x
78
+ }
79
+
80
+ get right() {
81
+ return this.x + this.width
82
+ }
83
+
84
+ get bottom() {
85
+ return this.y + this.height
86
+ }
87
+
88
+ toJSON() {
89
+ return {
90
+ x: this.x,
91
+ y: this.y,
92
+ width: this.width,
93
+ height: this.height,
94
+ top: this.top,
95
+ left: this.left,
96
+ right: this.right,
97
+ bottom: this.bottom,
98
+ }
99
+ }
100
+ }
101
+
102
+ for (const propertyName of ['top', 'right', 'bottom', 'left']) {
103
+ const propertyDescriptor = Object.getOwnPropertyDescriptor(DOMRect.prototype, propertyName)
104
+ propertyDescriptor.enumerable = true
105
+ Object.defineProperty(DOMRect.prototype, propertyName, propertyDescriptor)
106
+ }
107
+
108
+ // @info
109
+ // DOMMatrix polyfill (SVG 2)
110
+ // @src
111
+ // https://github.com/chromium/chromium/blob/master/third_party/blink/renderer/core/geometry/dom_matrix_read_only.cc
112
+ // https://github.com/tocharomera/generativecanvas/blob/master/node-canvas/lib/DOMMatrix.js
113
+
114
+ const M11 = 0,
115
+ M12 = 1,
116
+ M13 = 2,
117
+ M14 = 3
118
+ const M21 = 4,
119
+ M22 = 5,
120
+ M23 = 6,
121
+ M24 = 7
122
+ const M31 = 8,
123
+ M32 = 9,
124
+ M33 = 10,
125
+ M34 = 11
126
+ const M41 = 12,
127
+ M42 = 13,
128
+ M43 = 14,
129
+ M44 = 15
130
+
131
+ const A = M11,
132
+ B = M12
133
+ const C = M21,
134
+ D = M22
135
+ const E = M41,
136
+ F = M42
137
+
138
+ const DEGREE_PER_RAD = 180 / Math.PI
139
+ const RAD_PER_DEGREE = Math.PI / 180
140
+
141
+ const VALUES = Symbol('values')
142
+ const IS_2D = Symbol('is2D')
143
+
144
+ function parseMatrix(init) {
145
+ let parsed = init.replace(/matrix\(/, '').split(/,/, 7)
146
+
147
+ if (parsed.length !== 6) {
148
+ throw new Error(`Failed to parse ${init}`)
149
+ }
150
+
151
+ parsed = parsed.map(parseFloat)
152
+
153
+ return [parsed[0], parsed[1], 0, 0, parsed[2], parsed[3], 0, 0, 0, 0, 1, 0, parsed[4], parsed[5], 0, 1]
154
+ }
155
+
156
+ function parseMatrix3d(init) {
157
+ const parsed = init.replace(/matrix3d\(/, '').split(/,/, 17)
158
+
159
+ if (parsed.length !== 16) {
160
+ throw new Error(`Failed to parse ${init}`)
161
+ }
162
+
163
+ return parsed.map(parseFloat)
164
+ }
165
+
166
+ function parseTransform(tform) {
167
+ const type = tform.split(/\(/, 1)[0]
168
+
169
+ if (type === 'matrix') {
170
+ return parseMatrix(tform)
171
+ } else if (type === 'matrix3d') {
172
+ return parseMatrix3d(tform)
173
+ } else {
174
+ throw new Error(`${type} parsing not implemented`)
175
+ }
176
+ }
177
+
178
+ const setNumber2D = (receiver, index, value) => {
179
+ if (typeof value !== 'number') {
180
+ throw new TypeError('Expected number')
181
+ }
182
+
183
+ receiver[VALUES][index] = value
184
+ }
185
+
186
+ const setNumber3D = (receiver, index, value) => {
187
+ if (typeof value !== 'number') {
188
+ throw new TypeError('Expected number')
189
+ }
190
+
191
+ if (index === M33 || index === M44) {
192
+ if (value !== 1) {
193
+ receiver[IS_2D] = false
194
+ }
195
+ } else if (value !== 0) {
196
+ receiver[IS_2D] = false
197
+ }
198
+
199
+ receiver[VALUES][index] = value
200
+ }
201
+
202
+ const newInstance = (values) => {
203
+ const instance = Object.create(DOMMatrix.prototype)
204
+ instance.constructor = DOMMatrix
205
+ instance[IS_2D] = true
206
+ instance[VALUES] = values
207
+
208
+ return instance
209
+ }
210
+
211
+ // DOMMatrixInit dictionaries (Geometry Interfaces 1: "validate and fixup",
212
+ // "create a DOMMatrix from the dictionary"), with Chrome's errors.
213
+ //
214
+ // matrixFromInit(init, prefix)
215
+ // DOMMatrix -> its values and is2D
216
+ // undefined / null -> {}
217
+ // other non-object -> TypeError "not of type 'DOMMatrixInit'"
218
+ // object -> read members (toMatrixInit), validate and fix up
219
+
220
+ const FROM_MATRIX = "Failed to execute 'fromMatrix' on 'DOMMatrix': "
221
+ const MULTIPLY = "Failed to execute 'multiply' on 'DOMMatrixReadOnly': "
222
+ const MULTIPLY_SELF = "Failed to execute 'multiplySelf' on 'DOMMatrix': "
223
+ const PRE_MULTIPLY_SELF = "Failed to execute 'preMultiplySelf' on 'DOMMatrix': "
224
+ const MATRIX_TRANSFORM = "Failed to execute 'matrixTransform' on 'DOMPointReadOnly': "
225
+
226
+ // The members in the order Chrome reads them: the inherited dictionary's
227
+ // first, each dictionary's in lexicographic order.
228
+ const MATRIX_2D_INIT_MEMBERS = ['a', 'b', 'c', 'd', 'e', 'f', 'm11', 'm12', 'm21', 'm22', 'm41', 'm42']
229
+ const MATRIX_INIT_MEMBERS = ['m13', 'm14', 'm23', 'm24', 'm31', 'm32', 'm33', 'm34', 'm43', 'm44']
230
+ // [alias, member, default]: validate and fixup (2D).
231
+ const MATRIX_2D_ALIASES = [
232
+ ['a', 'm11', 1],
233
+ ['b', 'm12', 0],
234
+ ['c', 'm21', 0],
235
+ ['d', 'm22', 1],
236
+ ['e', 'm41', 0],
237
+ ['f', 'm42', 0],
238
+ ]
239
+
240
+ const isObject = (value) => value !== null && (typeof value === 'object' || typeof value === 'function')
241
+
242
+ // ECMAScript ToNumber, written out so that the errors it throws itself can
243
+ // get Chrome's context (`fail`), while the errors thrown by user code
244
+ // (valueOf, toString, @@toPrimitive) pass through unchanged, as in Chrome.
245
+ // V8's messages; an object in a message is shown as #<Object>.
246
+ function toNumber(value, fail) {
247
+ if (!isObject(value)) {
248
+ if (typeof value === 'bigint') throw fail('Cannot convert a BigInt value to a number')
249
+ if (typeof value === 'symbol') throw fail('Cannot convert a Symbol value to a number')
250
+ return +value
251
+ }
252
+ const toPrimitive = value[Symbol.toPrimitive]
253
+ if (toPrimitive !== undefined && toPrimitive !== null) {
254
+ if (typeof toPrimitive !== 'function') {
255
+ const shown = isObject(toPrimitive) ? '#<Object>' : String(toPrimitive)
256
+ throw fail(
257
+ `'${shown}' returned for property 'Symbol(Symbol.toPrimitive)' of object '#<Object>' is not a function`,
258
+ )
259
+ }
260
+ const primitive = toPrimitive.call(value, 'number')
261
+ if (isObject(primitive)) throw fail('Cannot convert object to primitive value')
262
+ return toNumber(primitive, fail)
263
+ }
264
+ for (const name of ['valueOf', 'toString']) {
265
+ const method = value[name]
266
+ if (typeof method === 'function') {
267
+ const primitive = method.call(value)
268
+ if (!isObject(primitive)) return toNumber(primitive, fail)
269
+ }
270
+ }
271
+ throw fail('Cannot convert object to primitive value')
272
+ }
273
+
274
+ // WebIDL conversion to a DOMMatrixInit: the present members only.
275
+ function toMatrixInit(init, prefix) {
276
+ const dict = Object.create(null)
277
+ if (init === undefined || init === null) return dict
278
+ if (!isObject(init)) throw new TypeError(`${prefix}The provided value is not of type 'DOMMatrixInit'.`)
279
+ const read = (key, dictionary) => {
280
+ const value = init[key]
281
+ if (value !== undefined) {
282
+ dict[key] = toNumber(
283
+ value,
284
+ (msg) => new TypeError(`${prefix}Failed to read the '${key}' property from '${dictionary}': ${msg}`),
285
+ )
286
+ }
287
+ }
288
+ for (const key of MATRIX_2D_INIT_MEMBERS) read(key, 'DOMMatrix2DInit')
289
+ const is2D = init.is2D
290
+ if (is2D !== undefined) dict.is2D = Boolean(is2D)
291
+ for (const key of MATRIX_INIT_MEMBERS) read(key, 'DOMMatrixInit')
292
+ return dict
293
+ }
294
+
295
+ const sameValueZero = (x, y) => x === y || (x !== x && y !== y)
296
+
297
+ // { values, is2D } of the matrix `init` describes. `values` may be the
298
+ // DOMMatrix's own array: read it, do not write it.
299
+ function matrixFromInit(init, prefix) {
300
+ if (init instanceof DOMMatrix && init[VALUES] instanceof Float64Array) {
301
+ return { values: init[VALUES], is2D: init[IS_2D] }
302
+ }
303
+ const dict = toMatrixInit(init, prefix)
304
+ // validate and fixup (2D)
305
+ for (const [alias, member] of MATRIX_2D_ALIASES) {
306
+ if (alias in dict && member in dict && !sameValueZero(dict[alias], dict[member])) {
307
+ throw new TypeError(`${prefix}Property mismatch on matrix initialization.`)
308
+ }
309
+ }
310
+ for (const [alias, member, defaultValue] of MATRIX_2D_ALIASES) {
311
+ if (!(member in dict)) dict[member] = alias in dict ? dict[alias] : defaultValue
312
+ }
313
+ // validate and fixup: the members default to the identity.
314
+ const value = (key) => (key in dict ? dict[key] : key === 'm33' || key === 'm44' ? 1 : 0)
315
+ const is3D =
316
+ ['m13', 'm14', 'm23', 'm24', 'm31', 'm32', 'm34', 'm43'].some((key) => value(key) !== 0) ||
317
+ value('m33') !== 1 ||
318
+ value('m44') !== 1
319
+ if (dict.is2D === true && is3D) {
320
+ throw new TypeError(`${prefix}The is2D member is set to true but the input matrix is a 3d matrix.`)
321
+ }
322
+ const is2D = dict.is2D === undefined ? !is3D : dict.is2D
323
+ const { m11, m12, m21, m22, m41, m42 } = dict
324
+ const values = is2D
325
+ ? [m11, m12, 0, 0, m21, m22, 0, 0, 0, 0, 1, 0, m41, m42, 0, 1]
326
+ : [
327
+ m11,
328
+ m12,
329
+ value('m13'),
330
+ value('m14'),
331
+ m21,
332
+ m22,
333
+ value('m23'),
334
+ value('m24'),
335
+ value('m31'),
336
+ value('m32'),
337
+ value('m33'),
338
+ value('m34'),
339
+ m41,
340
+ m42,
341
+ value('m43'),
342
+ value('m44'),
343
+ ]
344
+ return { values: new Float64Array(values), is2D }
345
+ }
346
+
347
+ // `target` times (post) or after (pre) the matrix `other` describes.
348
+ function multiplyByInit(target, other, prefix, post) {
349
+ const { values, is2D } = matrixFromInit(other, prefix)
350
+ target[VALUES] = post ? multiply(values, target[VALUES]) : multiply(target[VALUES], values)
351
+ if (!is2D) {
352
+ target[IS_2D] = false
353
+ }
354
+ return target
355
+ }
356
+
357
+ const multiply = (first, second) => {
358
+ const dest = new Float64Array(16)
359
+
360
+ for (let i = 0; i < 4; i++) {
361
+ for (let j = 0; j < 4; j++) {
362
+ let sum = 0
363
+
364
+ for (let k = 0; k < 4; k++) {
365
+ sum += first[i * 4 + k] * second[k * 4 + j]
366
+ }
367
+
368
+ dest[i * 4 + j] = sum
369
+ }
370
+ }
371
+
372
+ return dest
373
+ }
374
+
375
+ class DOMMatrix {
376
+ get m11() {
377
+ return this[VALUES][M11]
378
+ }
379
+ set m11(value) {
380
+ setNumber2D(this, M11, value)
381
+ }
382
+ get m12() {
383
+ return this[VALUES][M12]
384
+ }
385
+ set m12(value) {
386
+ setNumber2D(this, M12, value)
387
+ }
388
+ get m13() {
389
+ return this[VALUES][M13]
390
+ }
391
+ set m13(value) {
392
+ setNumber3D(this, M13, value)
393
+ }
394
+ get m14() {
395
+ return this[VALUES][M14]
396
+ }
397
+ set m14(value) {
398
+ setNumber3D(this, M14, value)
399
+ }
400
+ get m21() {
401
+ return this[VALUES][M21]
402
+ }
403
+ set m21(value) {
404
+ setNumber2D(this, M21, value)
405
+ }
406
+ get m22() {
407
+ return this[VALUES][M22]
408
+ }
409
+ set m22(value) {
410
+ setNumber2D(this, M22, value)
411
+ }
412
+ get m23() {
413
+ return this[VALUES][M23]
414
+ }
415
+ set m23(value) {
416
+ setNumber3D(this, M23, value)
417
+ }
418
+ get m24() {
419
+ return this[VALUES][M24]
420
+ }
421
+ set m24(value) {
422
+ setNumber3D(this, M24, value)
423
+ }
424
+ get m31() {
425
+ return this[VALUES][M31]
426
+ }
427
+ set m31(value) {
428
+ setNumber3D(this, M31, value)
429
+ }
430
+ get m32() {
431
+ return this[VALUES][M32]
432
+ }
433
+ set m32(value) {
434
+ setNumber3D(this, M32, value)
435
+ }
436
+ get m33() {
437
+ return this[VALUES][M33]
438
+ }
439
+ set m33(value) {
440
+ setNumber3D(this, M33, value)
441
+ }
442
+ get m34() {
443
+ return this[VALUES][M34]
444
+ }
445
+ set m34(value) {
446
+ setNumber3D(this, M34, value)
447
+ }
448
+ get m41() {
449
+ return this[VALUES][M41]
450
+ }
451
+ set m41(value) {
452
+ setNumber2D(this, M41, value)
453
+ }
454
+ get m42() {
455
+ return this[VALUES][M42]
456
+ }
457
+ set m42(value) {
458
+ setNumber2D(this, M42, value)
459
+ }
460
+ get m43() {
461
+ return this[VALUES][M43]
462
+ }
463
+ set m43(value) {
464
+ setNumber3D(this, M43, value)
465
+ }
466
+ get m44() {
467
+ return this[VALUES][M44]
468
+ }
469
+ set m44(value) {
470
+ setNumber3D(this, M44, value)
471
+ }
472
+
473
+ get a() {
474
+ return this[VALUES][A]
475
+ }
476
+ set a(value) {
477
+ setNumber2D(this, A, value)
478
+ }
479
+ get b() {
480
+ return this[VALUES][B]
481
+ }
482
+ set b(value) {
483
+ setNumber2D(this, B, value)
484
+ }
485
+ get c() {
486
+ return this[VALUES][C]
487
+ }
488
+ set c(value) {
489
+ setNumber2D(this, C, value)
490
+ }
491
+ get d() {
492
+ return this[VALUES][D]
493
+ }
494
+ set d(value) {
495
+ setNumber2D(this, D, value)
496
+ }
497
+ get e() {
498
+ return this[VALUES][E]
499
+ }
500
+ set e(value) {
501
+ setNumber2D(this, E, value)
502
+ }
503
+ get f() {
504
+ return this[VALUES][F]
505
+ }
506
+ set f(value) {
507
+ setNumber2D(this, F, value)
508
+ }
509
+
510
+ get is2D() {
511
+ return this[IS_2D]
512
+ }
513
+
514
+ get isIdentity() {
515
+ const values = this[VALUES]
516
+
517
+ return (
518
+ values[M11] === 1 &&
519
+ values[M12] === 0 &&
520
+ values[M13] === 0 &&
521
+ values[M14] === 0 &&
522
+ values[M21] === 0 &&
523
+ values[M22] === 1 &&
524
+ values[M23] === 0 &&
525
+ values[M24] === 0 &&
526
+ values[M31] === 0 &&
527
+ values[M32] === 0 &&
528
+ values[M33] === 1 &&
529
+ values[M34] === 0 &&
530
+ values[M41] === 0 &&
531
+ values[M42] === 0 &&
532
+ values[M43] === 0 &&
533
+ values[M44] === 1
534
+ )
535
+ }
536
+
537
+ // fromMatrix(optional DOMMatrixInit other = {})
538
+ static fromMatrix(init) {
539
+ const { values, is2D } = matrixFromInit(init, FROM_MATRIX)
540
+ const matrix = new DOMMatrix()
541
+ matrix[VALUES] = new Float64Array(values)
542
+ matrix[IS_2D] = is2D
543
+ return matrix
544
+ }
545
+
546
+ static fromFloat32Array(init) {
547
+ if (!(init instanceof Float32Array)) throw new TypeError('Expected Float32Array')
548
+ return new DOMMatrix(init)
549
+ }
550
+
551
+ static fromFloat64Array(init) {
552
+ if (!(init instanceof Float64Array)) throw new TypeError('Expected Float64Array')
553
+ return new DOMMatrix(init)
554
+ }
555
+
556
+ // @type
557
+ // (Float64Array) => void
558
+ constructor(init) {
559
+ this[IS_2D] = true
560
+
561
+ this[VALUES] = new Float64Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1])
562
+
563
+ // Parse CSS transformList
564
+ if (typeof init === 'string') {
565
+ if (init === '') {
566
+ return
567
+ } else {
568
+ const tforms = init.split(/\)\s+/, 20).map(parseTransform)
569
+
570
+ if (tforms.length === 0) {
571
+ return
572
+ }
573
+
574
+ init = tforms[0]
575
+
576
+ for (let i = 1; i < tforms.length; i++) {
577
+ init = multiply(tforms[i], init)
578
+ }
579
+ }
580
+ }
581
+
582
+ let i = 0
583
+
584
+ if (init && init.length === 6) {
585
+ setNumber2D(this, A, init[i++])
586
+ setNumber2D(this, B, init[i++])
587
+ setNumber2D(this, C, init[i++])
588
+ setNumber2D(this, D, init[i++])
589
+ setNumber2D(this, E, init[i++])
590
+ setNumber2D(this, F, init[i++])
591
+ } else if (init && init.length === 16) {
592
+ setNumber2D(this, M11, init[i++])
593
+ setNumber2D(this, M12, init[i++])
594
+ setNumber3D(this, M13, init[i++])
595
+ setNumber3D(this, M14, init[i++])
596
+ setNumber2D(this, M21, init[i++])
597
+ setNumber2D(this, M22, init[i++])
598
+ setNumber3D(this, M23, init[i++])
599
+ setNumber3D(this, M24, init[i++])
600
+ setNumber3D(this, M31, init[i++])
601
+ setNumber3D(this, M32, init[i++])
602
+ setNumber3D(this, M33, init[i++])
603
+ setNumber3D(this, M34, init[i++])
604
+ setNumber2D(this, M41, init[i++])
605
+ setNumber2D(this, M42, init[i++])
606
+ setNumber3D(this, M43, init[i++])
607
+ setNumber3D(this, M44, init[i])
608
+ } else if (init !== undefined) {
609
+ throw new TypeError('Expected string or array.')
610
+ }
611
+ }
612
+
613
+ dump() {
614
+ const mat = this[VALUES]
615
+ console.info([mat.slice(0, 4), mat.slice(4, 8), mat.slice(8, 12), mat.slice(12, 16)])
616
+ }
617
+
618
+ [inspect.custom](depth) {
619
+ if (depth < 0) return '[DOMMatrix]'
620
+
621
+ const { a, b, c, d, e, f, is2D, isIdentity } = this
622
+ if (this.is2D) {
623
+ return `DOMMatrix ${inspect({ a, b, c, d, e, f, is2D, isIdentity }, { colors: true })}`
624
+ } else {
625
+ const { m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44, is2D, isIdentity } = this
626
+ return `DOMMatrix ${inspect(
627
+ {
628
+ a,
629
+ b,
630
+ c,
631
+ d,
632
+ e,
633
+ f,
634
+ m11,
635
+ m12,
636
+ m13,
637
+ m14,
638
+ m21,
639
+ m22,
640
+ m23,
641
+ m24,
642
+ m31,
643
+ m32,
644
+ m33,
645
+ m34,
646
+ m41,
647
+ m42,
648
+ m43,
649
+ m44,
650
+ is2D,
651
+ isIdentity,
652
+ },
653
+ { colors: true },
654
+ )}`
655
+ }
656
+ }
657
+
658
+ // multiply, multiplySelf, preMultiplySelf(optional DOMMatrixInit other = {})
659
+ multiply(other) {
660
+ const result = newInstance(this[VALUES])
661
+ result[IS_2D] = this[IS_2D]
662
+ return multiplyByInit(result, other, MULTIPLY, true)
663
+ }
664
+
665
+ multiplySelf(other) {
666
+ return multiplyByInit(this, other, MULTIPLY_SELF, true)
667
+ }
668
+
669
+ preMultiplySelf(other) {
670
+ return multiplyByInit(this, other, PRE_MULTIPLY_SELF, false)
671
+ }
672
+
673
+ translate(tx, ty, tz) {
674
+ return newInstance(this[VALUES]).translateSelf(tx, ty, tz)
675
+ }
676
+
677
+ translateSelf(tx = 0, ty = 0, tz = 0) {
678
+ this[VALUES] = multiply([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, tx, ty, tz, 1], this[VALUES])
679
+
680
+ if (tz !== 0) {
681
+ this[IS_2D] = false
682
+ }
683
+
684
+ return this
685
+ }
686
+
687
+ scale(scaleX, scaleY, scaleZ, originX, originY, originZ) {
688
+ return newInstance(this[VALUES]).scaleSelf(scaleX, scaleY, scaleZ, originX, originY, originZ)
689
+ }
690
+
691
+ scale3d(scale, originX, originY, originZ) {
692
+ return newInstance(this[VALUES]).scale3dSelf(scale, originX, originY, originZ)
693
+ }
694
+
695
+ scale3dSelf(scale, originX, originY, originZ) {
696
+ return this.scaleSelf(scale, scale, scale, originX, originY, originZ)
697
+ }
698
+
699
+ scaleSelf(scaleX, scaleY, scaleZ, originX, originY, originZ) {
700
+ // Not redundant with translate's checks because we need to negate the values later.
701
+ if (typeof originX !== 'number') originX = 0
702
+ if (typeof originY !== 'number') originY = 0
703
+ if (typeof originZ !== 'number') originZ = 0
704
+
705
+ this.translateSelf(originX, originY, originZ)
706
+
707
+ if (typeof scaleX !== 'number') scaleX = 1
708
+ if (typeof scaleY !== 'number') scaleY = scaleX
709
+ if (typeof scaleZ !== 'number') scaleZ = 1
710
+
711
+ this[VALUES] = multiply([scaleX, 0, 0, 0, 0, scaleY, 0, 0, 0, 0, scaleZ, 0, 0, 0, 0, 1], this[VALUES])
712
+
713
+ this.translateSelf(-originX, -originY, -originZ)
714
+
715
+ if (scaleZ !== 1 || originZ !== 0) {
716
+ this[IS_2D] = false
717
+ }
718
+
719
+ return this
720
+ }
721
+
722
+ rotateFromVector(x, y) {
723
+ return newInstance(this[VALUES]).rotateFromVectorSelf(x, y)
724
+ }
725
+
726
+ rotateFromVectorSelf(x = 0, y = 0) {
727
+ const theta = x === 0 && y === 0 ? 0 : Math.atan2(y, x) * DEGREE_PER_RAD
728
+ return this.rotateSelf(theta)
729
+ }
730
+
731
+ rotate(rotX, rotY, rotZ) {
732
+ return newInstance(this[VALUES]).rotateSelf(rotX, rotY, rotZ)
733
+ }
734
+
735
+ rotateSelf(rotX, rotY, rotZ) {
736
+ if (rotY === undefined && rotZ === undefined) {
737
+ rotZ = rotX
738
+ rotX = rotY = 0
739
+ }
740
+
741
+ if (typeof rotY !== 'number') rotY = 0
742
+ if (typeof rotZ !== 'number') rotZ = 0
743
+
744
+ if (rotX !== 0 || rotY !== 0) {
745
+ this[IS_2D] = false
746
+ }
747
+
748
+ rotX *= RAD_PER_DEGREE
749
+ rotY *= RAD_PER_DEGREE
750
+ rotZ *= RAD_PER_DEGREE
751
+
752
+ let c = Math.cos(rotZ)
753
+ let s = Math.sin(rotZ)
754
+
755
+ this[VALUES] = multiply([c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], this[VALUES])
756
+
757
+ c = Math.cos(rotY)
758
+ s = Math.sin(rotY)
759
+
760
+ this[VALUES] = multiply([c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1], this[VALUES])
761
+
762
+ c = Math.cos(rotX)
763
+ s = Math.sin(rotX)
764
+
765
+ this[VALUES] = multiply([1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1], this[VALUES])
766
+
767
+ return this
768
+ }
769
+
770
+ rotateAxisAngle(x, y, z, angle) {
771
+ return newInstance(this[VALUES]).rotateAxisAngleSelf(x, y, z, angle)
772
+ }
773
+
774
+ rotateAxisAngleSelf(x = 0, y = 0, z = 0, angle = 0) {
775
+ const length = Math.sqrt(x * x + y * y + z * z)
776
+
777
+ if (length === 0) {
778
+ return this
779
+ }
780
+
781
+ if (length !== 1) {
782
+ x /= length
783
+ y /= length
784
+ z /= length
785
+ }
786
+
787
+ angle *= RAD_PER_DEGREE
788
+
789
+ const c = Math.cos(angle)
790
+ const s = Math.sin(angle)
791
+ const t = 1 - c
792
+ const tx = t * x
793
+ const ty = t * y
794
+
795
+ this[VALUES] = multiply(
796
+ [
797
+ tx * x + c,
798
+ tx * y + s * z,
799
+ tx * z - s * y,
800
+ 0,
801
+ tx * y - s * z,
802
+ ty * y + c,
803
+ ty * z + s * x,
804
+ 0,
805
+ tx * z + s * y,
806
+ ty * z - s * x,
807
+ t * z * z + c,
808
+ 0,
809
+ 0,
810
+ 0,
811
+ 0,
812
+ 1,
813
+ ],
814
+ this[VALUES],
815
+ )
816
+
817
+ if (x !== 0 || y !== 0) {
818
+ this[IS_2D] = false
819
+ }
820
+
821
+ return this
822
+ }
823
+
824
+ skewX(sx) {
825
+ return newInstance(this[VALUES]).skewXSelf(sx)
826
+ }
827
+
828
+ skewXSelf(sx) {
829
+ if (typeof sx !== 'number') {
830
+ return this
831
+ }
832
+
833
+ const t = Math.tan(sx * RAD_PER_DEGREE)
834
+
835
+ this[VALUES] = multiply([1, 0, 0, 0, t, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], this[VALUES])
836
+
837
+ return this
838
+ }
839
+
840
+ skewY(sy) {
841
+ return newInstance(this[VALUES]).skewYSelf(sy)
842
+ }
843
+
844
+ skewYSelf(sy) {
845
+ if (typeof sy !== 'number') {
846
+ return this
847
+ }
848
+
849
+ const t = Math.tan(sy * RAD_PER_DEGREE)
850
+
851
+ this[VALUES] = multiply([1, t, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], this[VALUES])
852
+
853
+ return this
854
+ }
855
+
856
+ flipX() {
857
+ return newInstance(multiply([-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], this[VALUES]))
858
+ }
859
+
860
+ flipY() {
861
+ return newInstance(multiply([1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], this[VALUES]))
862
+ }
863
+
864
+ inverse() {
865
+ return newInstance(this[VALUES].slice()).invertSelf()
866
+ }
867
+
868
+ invertSelf() {
869
+ if (this[IS_2D]) {
870
+ const det = this[VALUES][A] * this[VALUES][D] - this[VALUES][B] * this[VALUES][C]
871
+
872
+ // Invertable
873
+ if (det !== 0) {
874
+ const newA = this[VALUES][D] / det
875
+ const newB = -this[VALUES][B] / det
876
+ const newC = -this[VALUES][C] / det
877
+ const newD = this[VALUES][A] / det
878
+ const newE = (this[VALUES][C] * this[VALUES][F] - this[VALUES][D] * this[VALUES][E]) / det
879
+ const newF = (this[VALUES][B] * this[VALUES][E] - this[VALUES][A] * this[VALUES][F]) / det
880
+
881
+ this.a = newA
882
+ this.b = newB
883
+ this.c = newC
884
+ this.d = newD
885
+ this.e = newE
886
+ this.f = newF
887
+
888
+ return this
889
+ }
890
+
891
+ // Not invertable
892
+ else {
893
+ this[IS_2D] = false
894
+
895
+ this[VALUES] = [NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]
896
+
897
+ return this
898
+ }
899
+ } else {
900
+ throw new Error('3D matrix inversion is not implemented.')
901
+ }
902
+ }
903
+
904
+ setMatrixValue(transformList) {
905
+ const temp = new DOMMatrix(transformList)
906
+
907
+ this[VALUES] = temp[VALUES]
908
+ this[IS_2D] = temp[IS_2D]
909
+
910
+ return this
911
+ }
912
+
913
+ transformPoint(point) {
914
+ const x = point.x || 0
915
+ const y = point.y || 0
916
+ const z = point.z || 0
917
+ const w = point.w || 1
918
+
919
+ const values = this[VALUES]
920
+
921
+ const nx = values[M11] * x + values[M21] * y + values[M31] * z + values[M41] * w
922
+ const ny = values[M12] * x + values[M22] * y + values[M32] * z + values[M42] * w
923
+ const nz = values[M13] * x + values[M23] * y + values[M33] * z + values[M43] * w
924
+ const nw = values[M14] * x + values[M24] * y + values[M34] * z + values[M44] * w
925
+
926
+ return new DOMPoint(nx, ny, nz, nw)
927
+ }
928
+
929
+ toFloat32Array() {
930
+ return Float32Array.from(this[VALUES])
931
+ }
932
+
933
+ toFloat64Array() {
934
+ return this[VALUES].slice(0)
935
+ }
936
+
937
+ toJSON() {
938
+ return {
939
+ a: this.a,
940
+ b: this.b,
941
+ c: this.c,
942
+ d: this.d,
943
+ e: this.e,
944
+ f: this.f,
945
+ m11: this.m11,
946
+ m12: this.m12,
947
+ m13: this.m13,
948
+ m14: this.m14,
949
+ m21: this.m21,
950
+ m22: this.m22,
951
+ m23: this.m23,
952
+ m24: this.m24,
953
+ m31: this.m31,
954
+ m32: this.m32,
955
+ m33: this.m33,
956
+ m34: this.m34,
957
+ m41: this.m41,
958
+ m42: this.m42,
959
+ m43: this.m43,
960
+ m44: this.m44,
961
+ is2D: this.is2D,
962
+ isIdentity: this.isIdentity,
963
+ }
964
+ }
965
+
966
+ toString() {
967
+ if (this.is2D) {
968
+ return `matrix(${this.a}, ${this.b}, ${this.c}, ${this.d}, ${this.e}, ${this.f})`
969
+ } else {
970
+ return `matrix3d(${this[VALUES].join(', ')})`
971
+ }
972
+ }
973
+ }
974
+
975
+ for (const propertyName of [
976
+ 'a',
977
+ 'b',
978
+ 'c',
979
+ 'd',
980
+ 'e',
981
+ 'f',
982
+ 'm11',
983
+ 'm12',
984
+ 'm13',
985
+ 'm14',
986
+ 'm21',
987
+ 'm22',
988
+ 'm23',
989
+ 'm24',
990
+ 'm31',
991
+ 'm32',
992
+ 'm33',
993
+ 'm34',
994
+ 'm41',
995
+ 'm42',
996
+ 'm43',
997
+ 'm44',
998
+ 'is2D',
999
+ 'isIdentity',
1000
+ ]) {
1001
+ const propertyDescriptor = Object.getOwnPropertyDescriptor(DOMMatrix.prototype, propertyName)
1002
+ propertyDescriptor.enumerable = true
1003
+ Object.defineProperty(DOMMatrix.prototype, propertyName, propertyDescriptor)
1004
+ }
1005
+
1006
+ module.exports = { DOMPoint, DOMMatrix, DOMRect }