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
|
import { delimiter } from '../modules/core/regex.js'
import { radians } from '../utils/utils.js'
import { register } from '../utils/adopter.js'
import Element from '../elements/Element.js'
import Point from './Point.js'
function closeEnough(a, b, threshold) {
return Math.abs(b - a) < (threshold || 1e-6)
}
export default class Matrix {
constructor(...args) {
this.init(...args)
}
static formatTransforms(o) {
// Get all of the parameters required to form the matrix
const flipBoth = o.flip === 'both' || o.flip === true
const flipX = o.flip && (flipBoth || o.flip === 'x') ? -1 : 1
const flipY = o.flip && (flipBoth || o.flip === 'y') ? -1 : 1
const skewX =
o.skew && o.skew.length
? o.skew[0]
: isFinite(o.skew)
? o.skew
: isFinite(o.skewX)
? o.skewX
: 0
const skewY =
o.skew && o.skew.length
? o.skew[1]
: isFinite(o.skew)
? o.skew
: isFinite(o.skewY)
? o.skewY
: 0
const scaleX =
o.scale && o.scale.length
? o.scale[0] * flipX
: isFinite(o.scale)
? o.scale * flipX
: isFinite(o.scaleX)
? o.scaleX * flipX
: flipX
const scaleY =
o.scale && o.scale.length
? o.scale[1] * flipY
: isFinite(o.scale)
? o.scale * flipY
: isFinite(o.scaleY)
? o.scaleY * flipY
: flipY
const shear = o.shear || 0
const theta = o.rotate || o.theta || 0
const origin = new Point(
o.origin || o.around || o.ox || o.originX,
o.oy || o.originY
)
const ox = origin.x
const oy = origin.y
// We need Point to be invalid if nothing was passed because we cannot default to 0 here. That is why NaN
const position = new Point(
o.position || o.px || o.positionX || NaN,
o.py || o.positionY || NaN
)
const px = position.x
const py = position.y
const translate = new Point(
o.translate || o.tx || o.translateX,
o.ty || o.translateY
)
const tx = translate.x
const ty = translate.y
const relative = new Point(
o.relative || o.rx || o.relativeX,
o.ry || o.relativeY
)
const rx = relative.x
const ry = relative.y
// Populate all of the values
return {
scaleX,
scaleY,
skewX,
skewY,
shear,
theta,
rx,
ry,
tx,
ty,
ox,
oy,
px,
py
}
}
static fromArray(a) {
return { a: a[0], b: a[1], c: a[2], d: a[3], e: a[4], f: a[5] }
}
static isMatrixLike(o) {
return (
o.a != null ||
o.b != null ||
o.c != null ||
o.d != null ||
o.e != null ||
o.f != null
)
}
// left matrix, right matrix, target matrix which is overwritten
static matrixMultiply(l, r, o) {
// Work out the product directly
const a = l.a * r.a + l.c * r.b
const b = l.b * r.a + l.d * r.b
const c = l.a * r.c + l.c * r.d
const d = l.b * r.c + l.d * r.d
const e = l.e + l.a * r.e + l.c * r.f
const f = l.f + l.b * r.e + l.d * r.f
// make sure to use local variables because l/r and o could be the same
o.a = a
o.b = b
o.c = c
o.d = d
o.e = e
o.f = f
return o
}
around(cx, cy, matrix) {
return this.clone().aroundO(cx, cy, matrix)
}
// Transform around a center point
aroundO(cx, cy, matrix) {
const dx = cx || 0
const dy = cy || 0
return this.translateO(-dx, -dy).lmultiplyO(matrix).translateO(dx, dy)
}
// Clones this matrix
clone() {
return new Matrix(this)
}
// Decomposes this matrix into its affine parameters
decompose(cx = 0, cy = 0) {
// Get the parameters from the matrix
const a = this.a
const b = this.b
const c = this.c
const d = this.d
const e = this.e
const f = this.f
// Figure out if the winding direction is clockwise or counterclockwise
const determinant = a * d - b * c
const ccw = determinant > 0 ? 1 : -1
// Since we only shear in x, we can use the x basis to get the x scale
// and the rotation of the resulting matrix
const sx = ccw * Math.sqrt(a * a + b * b)
const thetaRad = Math.atan2(ccw * b, ccw * a)
const theta = (180 / Math.PI) * thetaRad
const ct = Math.cos(thetaRad)
const st = Math.sin(thetaRad)
// We can then solve the y basis vector simultaneously to get the other
// two affine parameters directly from these parameters
const lam = (a * c + b * d) / determinant
const sy = (c * sx) / (lam * a - b) || (d * sx) / (lam * b + a)
// Use the translations
const tx = e - cx + cx * ct * sx + cy * (lam * ct * sx - st * sy)
const ty = f - cy + cx * st * sx + cy * (lam * st * sx + ct * sy)
// Construct the decomposition and return it
return {
// Return the affine parameters
scaleX: sx,
scaleY: sy,
shear: lam,
rotate: theta,
translateX: tx,
translateY: ty,
originX: cx,
originY: cy,
// Return the matrix parameters
a: this.a,
b: this.b,
c: this.c,
d: this.d,
e: this.e,
f: this.f
}
}
// Check if two matrices are equal
equals(other) {
if (other === this) return true
const comp = new Matrix(other)
return (
closeEnough(this.a, comp.a) &&
closeEnough(this.b, comp.b) &&
closeEnough(this.c, comp.c) &&
closeEnough(this.d, comp.d) &&
closeEnough(this.e, comp.e) &&
closeEnough(this.f, comp.f)
)
}
// Flip matrix on x or y, at a given offset
flip(axis, around) {
return this.clone().flipO(axis, around)
}
flipO(axis, around) {
return axis === 'x'
? this.scaleO(-1, 1, around, 0)
: axis === 'y'
? this.scaleO(1, -1, 0, around)
: this.scaleO(-1, -1, axis, around || axis) // Define an x, y flip point
}
// Initialize
init(source) {
const base = Matrix.fromArray([1, 0, 0, 1, 0, 0])
// ensure source as object
source =
source instanceof Element
? source.matrixify()
: typeof source === 'string'
? Matrix.fromArray(source.split(delimiter).map(parseFloat))
: Array.isArray(source)
? Matrix.fromArray(source)
: typeof source === 'object' && Matrix.isMatrixLike(source)
? source
: typeof source === 'object'
? new Matrix().transform(source)
: arguments.length === 6
? Matrix.fromArray([].slice.call(arguments))
: base
// Merge the source matrix with the base matrix
this.a = source.a != null ? source.a : base.a
this.b = source.b != null ? source.b : base.b
this.c = source.c != null ? source.c : base.c
this.d = source.d != null ? source.d : base.d
this.e = source.e != null ? source.e : base.e
this.f = source.f != null ? source.f : base.f
return this
}
inverse() {
return this.clone().inverseO()
}
// Inverses matrix
inverseO() {
// Get the current parameters out of the matrix
const a = this.a
const b = this.b
const c = this.c
const d = this.d
const e = this.e
const f = this.f
// Invert the 2x2 matrix in the top left
const det = a * d - b * c
if (!det) throw new Error('Cannot invert ' + this)
// Calculate the top 2x2 matrix
const na = d / det
const nb = -b / det
const nc = -c / det
const nd = a / det
// Apply the inverted matrix to the top right
const ne = -(na * e + nc * f)
const nf = -(nb * e + nd * f)
// Construct the inverted matrix
this.a = na
this.b = nb
this.c = nc
this.d = nd
this.e = ne
this.f = nf
return this
}
lmultiply(matrix) {
return this.clone().lmultiplyO(matrix)
}
lmultiplyO(matrix) {
const r = this
const l = matrix instanceof Matrix ? matrix : new Matrix(matrix)
return Matrix.matrixMultiply(l, r, this)
}
// Left multiplies by the given matrix
multiply(matrix) {
return this.clone().multiplyO(matrix)
}
multiplyO(matrix) {
// Get the matrices
const l = this
const r = matrix instanceof Matrix ? matrix : new Matrix(matrix)
return Matrix.matrixMultiply(l, r, this)
}
// Rotate matrix
rotate(r, cx, cy) {
return this.clone().rotateO(r, cx, cy)
}
rotateO(r, cx = 0, cy = 0) {
// Convert degrees to radians
r = radians(r)
const cos = Math.cos(r)
const sin = Math.sin(r)
const { a, b, c, d, e, f } = this
this.a = a * cos - b * sin
this.b = b * cos + a * sin
this.c = c * cos - d * sin
this.d = d * cos + c * sin
this.e = e * cos - f * sin + cy * sin - cx * cos + cx
this.f = f * cos + e * sin - cx * sin - cy * cos + cy
return this
}
// Scale matrix
scale() {
return this.clone().scaleO(...arguments)
}
scaleO(x, y = x, cx = 0, cy = 0) {
// Support uniform scaling
if (arguments.length === 3) {
cy = cx
cx = y
y = x
}
const { a, b, c, d, e, f } = this
this.a = a * x
this.b = b * y
this.c = c * x
this.d = d * y
this.e = e * x - cx * x + cx
this.f = f * y - cy * y + cy
return this
}
// Shear matrix
shear(a, cx, cy) {
return this.clone().shearO(a, cx, cy)
}
// eslint-disable-next-line no-unused-vars
shearO(lx, cx = 0, cy = 0) {
const { a, b, c, d, e, f } = this
this.a = a + b * lx
this.c = c + d * lx
this.e = e + f * lx - cy * lx
return this
}
// Skew Matrix
skew() {
return this.clone().skewO(...arguments)
}
skewO(x, y = x, cx = 0, cy = 0) {
// support uniformal skew
if (arguments.length === 3) {
cy = cx
cx = y
y = x
}
// Convert degrees to radians
x = radians(x)
y = radians(y)
const lx = Math.tan(x)
const ly = Math.tan(y)
const { a, b, c, d, e, f } = this
this.a = a + b * lx
this.b = b + a * ly
this.c = c + d * lx
this.d = d + c * ly
this.e = e + f * lx - cy * lx
this.f = f + e * ly - cx * ly
return this
}
// SkewX
skewX(x, cx, cy) {
return this.skew(x, 0, cx, cy)
}
// SkewY
skewY(y, cx, cy) {
return this.skew(0, y, cx, cy)
}
toArray() {
return [this.a, this.b, this.c, this.d, this.e, this.f]
}
// Convert matrix to string
toString() {
return (
'matrix(' +
this.a +
',' +
this.b +
',' +
this.c +
',' +
this.d +
',' +
this.e +
',' +
this.f +
')'
)
}
// Transform a matrix into another matrix by manipulating the space
transform(o) {
// Check if o is a matrix and then left multiply it directly
if (Matrix.isMatrixLike(o)) {
const matrix = new Matrix(o)
return matrix.multiplyO(this)
}
// Get the proposed transformations and the current transformations
const t = Matrix.formatTransforms(o)
const current = this
const { x: ox, y: oy } = new Point(t.ox, t.oy).transform(current)
// Construct the resulting matrix
const transformer = new Matrix()
.translateO(t.rx, t.ry)
.lmultiplyO(current)
.translateO(-ox, -oy)
.scaleO(t.scaleX, t.scaleY)
.skewO(t.skewX, t.skewY)
.shearO(t.shear)
.rotateO(t.theta)
.translateO(ox, oy)
// If we want the origin at a particular place, we force it there
if (isFinite(t.px) || isFinite(t.py)) {
const origin = new Point(ox, oy).transform(transformer)
// TODO: Replace t.px with isFinite(t.px)
// Doesn't work because t.px is also 0 if it wasn't passed
const dx = isFinite(t.px) ? t.px - origin.x : 0
const dy = isFinite(t.py) ? t.py - origin.y : 0
transformer.translateO(dx, dy)
}
// Translate now after positioning
transformer.translateO(t.tx, t.ty)
return transformer
}
// Translate matrix
translate(x, y) {
return this.clone().translateO(x, y)
}
translateO(x, y) {
this.e += x || 0
this.f += y || 0
return this
}
valueOf() {
return {
a: this.a,
b: this.b,
c: this.c,
d: this.d,
e: this.e,
f: this.f
}
}
}
export function ctm() {
return new Matrix(this.node.getCTM())
}
export function screenCTM() {
try {
/* https://bugzilla.mozilla.org/show_bug.cgi?id=1344537
This is needed because FF does not return the transformation matrix
for the inner coordinate system when getScreenCTM() is called on nested svgs.
However all other Browsers do that */
if (typeof this.isRoot === 'function' && !this.isRoot()) {
const rect = this.rect(1, 1)
const m = rect.node.getScreenCTM()
rect.remove()
return new Matrix(m)
}
return new Matrix(this.node.getScreenCTM())
} catch (e) {
console.warn(
`Cannot get CTM from SVG node ${this.node.nodeName}. Is the element rendered?`
)
return new Matrix()
}
}
register(Matrix, 'Matrix')
|