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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
|
<!DOCTYPE HTML>
<html>
<head>
<title>Test DOMMatrix behavior</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<script>
function createMatrix(a, b, c, d, e, f)
{
var m = new DOMMatrix();
m.a = a;
m.b = b;
m.c = c;
m.d = d;
m.e = e;
m.f = f;
return m;
}
function create3DMatrix(a, b, c, d, e, f)
{
var m = new DOMMatrix();
m.a = a;
m.b = b;
m.c = c;
m.d = d;
m.e = e;
m.f = f;
m.m13 = 0;
return m;
}
function cmpMatrix(a, b, msg)
{
if (Array.isArray(a))
a = new DOMMatrix(a);
if (Array.isArray(b))
b = new DOMMatrix(b);
ok(CompareDOMMatrix(a, b),
msg + " - got " + formatMatrix(a)
+ ", expected " + formatMatrix(b));
}
function roughCmpMatrix(a, b, msg)
{
if (Array.isArray(a))
a = new DOMMatrix(a);
if (Array.isArray(b))
b = new DOMMatrix(b);
ok(RoughCompareDOMMatrix(a, b),
msg + " - got " + formatMatrix(a)
+ ", expected " + formatMatrix(b));
}
function formatMatrix(m)
{
m = new DOMMatrix(m);
if (m.is2D)
return "(" + [m.a, m.b, m.c, m.d, m.e, m.f].join(', ') + ")";
else
return "(" + [m.m11, m.m12, m.m13, m.m14,
m.m21, m.m22, m.m23, m.m24,
m.m31, m.m32, m.m33, m.m34,
m.m41, m.m42, m.m43, m.m44,].join(', ') + ")";
}
function CompareMatrix(dm, m)
{
var ma = m.toFloat32Array();
for (var x = 0; x < ma.length; x++) {
if (Math.abs(ma[x] - dm.m[x]) > 0.000001)
return false;
}
return true;
}
function CompareDOMMatrix(dm1, dm2)
{
var m1 = dm1.toFloat32Array();
var m2 = dm2.toFloat32Array();
if (m1.length != m2.length)
return false;
for (var x = 0; x < m1.length; x++) {
if (Math.abs(m1[x] - m2[x]) > 0.000001)
return false;
}
return true;
}
function RoughCompareDOMMatrix(dm1, dm2)
{
var m1 = dm1.toFloat32Array();
var m2 = dm2.toFloat32Array();
if (m1.length != m2.length)
return false;
const tolerance = 1 / 65535;
for (var x = 0; x < m1.length; x++) {
if (Math.abs(m1[x] - m2[x]) > tolerance)
return false;
}
return true;
}
SimpleTest.waitForExplicitFinish();
function main()
{
var tests = [
testCreateMatrix,
testMultiply,
testInverse,
testTranslate,
testScale,
testScaleNonUniform,
testRotate,
testRotateFromVector,
testFlipX,
testFlipY,
testSkewX,
testSkewY,
testMultiplyInPlace,
testInverseInPlace,
testTranslateInPlace,
testScaleInPlace,
testRotateInPlace,
testRotateFromVectorInPlace,
testSkewXInPlace,
testSkewYInPlace,
testCreateMatrix3D,
testMultiply3D,
testInverse3D,
testTranslate3D,
testScale3D,
test3D,
testParsing,
testStringify
];
for (var i = 0; i < tests.length; i++) {
try{
tests[i]();
} catch (e) {
ok(false, "uncaught exception in test " + i + ": " + e.name);
}
}
SimpleTest.finish();
}
function testCreateMatrix()
{
var m = new DOMMatrix();
// Should be initialised to identity
cmpMatrix(m, [1, 0, 0, 1, 0, 0],
"DOMMatrix should produce identity matrix");
m = new DOMMatrix([1,2,3,4,5,6]);
cmpMatrix(m, [1,2,3,4,5,6],
"DOMMatrix should produce the same matrix");
ok(m.is2D, "Failed to mark matrix as 2D.");
m = new DOMMatrix([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
cmpMatrix(m, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
"DOMMatrix should produce the same matrix");
ok(!m.is2D, "Failed to mark matrix as 3D.");
var n = new DOMMatrix(m.toFloat32Array());
cmpMatrix(n, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
"DOMMatrix should produce the same matrix with float32array constructor");
ok(!n.is2D, "Failed to mark matrix as 3D.");
var n = new DOMMatrix(m.toFloat64Array());
cmpMatrix(n, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
"DOMMatrix should produce the same matrix with float64array constructor");
ok(!n.is2D, "Failed to mark matrix as 3D.");
var exn = null;
try {
m = new DOMMatrix([0]);
} catch (e) {
exn = e;
}
ok(exn, "did throw exception with bad DOMMatrix constructor with 1 parameter");
exn = null;
try {
m = new DOMMatrix([1,2,3,4,5,6,7,8,9]);
} catch (e) {
exn = e;
}
ok(exn, "did throw exception with bad DOMMatrix constructor with 9 parameters");
exn = null;
try {
m = new DOMMatrix([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]);
} catch (e) {
exn = e;
}
ok(exn, "did throw exception with bad DOMMatrix constructor with 17 parameters");
}
// DOMMatrix multiply(in DOMMatrix secondMatrix);
function testMultiply()
{
var m1 = createMatrix(1, 0, 0, 1, 50, 90);
var m2 = createMatrix(Math.SQRT1_2, -Math.SQRT1_2, Math.SQRT1_2, Math.SQRT1_2, 0, 0);
var m3 = createMatrix(1, 0, 0, 1, 130, 160);
var result = m1.multiply(m2).multiply(m3);
roughCmpMatrix(result, [Math.SQRT1_2, -Math.SQRT1_2, Math.SQRT1_2, Math.SQRT1_2, 255.060974, 111.213203],
"Unexpected result after multiplying matrices");
// Check orig matrices are unchanged
cmpMatrix(m1, [1, 0, 0, 1, 50, 90], "Matrix changed after multiplication");
roughCmpMatrix(m2, [Math.SQRT1_2, -Math.SQRT1_2, Math.SQRT1_2, Math.SQRT1_2, 0, 0],
"Matrix changed after multiplication");
cmpMatrix(m3, [1, 0, 0, 1, 130, 160], "Matrix changed after multiplication");
}
// DOMMatrix inverse() raises(SVGException);
function testInverse()
{
// Test inversion
var m = createMatrix(2, 0, 0, 4, 110, -50);
roughCmpMatrix(m.inverse(), [0.5, 0, 0, 0.25, -55, 12.5],
"Unexpected result after inverting matrix");
// Test non-invertable
m = createMatrix(0, 0, 1, 0, 0, 0);
m = m.inverse();
ok(isNaN(m.a), "Failed to invalidate inverted singular matrix, got " + m.a);
ok(!m.is2D, "Failed to mark invalidated inverted singular matrix as 3D.");
}
// DOMMatrix translate(in float x, in float y);
function testTranslate()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
roughCmpMatrix(m.translate(100, -50), [2, 0, 0, 1, 320, 50],
"Unexpected result after translate");
}
// DOMMatrix scale(in float scaleFactor);
function testScale()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
roughCmpMatrix(m.scale(0.5), [1, 0, 0, 0.5, 120, 100],
"Unexpected result after scale");
}
// DOMMatrix scaleNonUniform(in float scaleFactorX, in float scaleFactorY);
function testScaleNonUniform()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
roughCmpMatrix(m.scaleNonUniform(0.5, -3), [1, 0, 0, -3, 120, 100],
"Unexpected result after scaleNonUniform");
}
// DOMMatrix rotate(in float angle);
function testRotate()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
roughCmpMatrix(m.rotate(45),
[2*Math.cos(Math.PI/4), Math.sin(Math.PI/4),
2*-Math.sin(Math.PI/4), Math.cos(Math.PI/4),
120, 100],
"Unexpected result after rotate");
}
// DOMMatrix rotateFromVector(in float x, in float y) raises(SVGException);
function testRotateFromVector()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
// Make a 150 degree angle
var result = m.rotateFromVector(-2, 1.1547);
roughCmpMatrix(result,
[2*Math.cos(5*Math.PI/6), Math.sin(5*Math.PI/6),
2*-Math.sin(5*Math.PI/6), Math.cos(5*Math.PI/6),
120, 100],
"Unexpected result after rotateFromVector");
// Test bad input (1)
var exn = null;
try {
m.rotateFromVector(1, 0);
} catch (e) {
exn = e;
}
is(exn, null, "did not throw exception with zero coord for rotateFromVector");
// Test bad input (2)
exn = null;
try {
m.rotateFromVector(0, 1);
} catch (e) {
exn = e;
}
is(exn, null, "did not throw exception with zero coord for rotateFromVector");
}
// DOMMatrix flipX();
function testFlipX()
{
var m = createMatrix(1, 2, 3, 4, 5, 6);
cmpMatrix(m.flipX(), [-1, -2, 3, 4, 5, 6], "Unexpected result after flipX");
}
// DOMMatrix flipY();
function testFlipY()
{
var m = createMatrix(1, 2, 3, 4, 5, 6);
cmpMatrix(m.flipY(), [1, 2, -3, -4, 5, 6], "Unexpected result after flipY");
}
// DOMMatrix skewX(in float angle);
function testSkewX()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
roughCmpMatrix(m.skewX(30), [2, 0, 2*Math.tan(Math.PI/6), 1, 120, 100],
"Unexpected result after skewX");
}
// DOMMatrix skewY(in float angle);
function testSkewY()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
roughCmpMatrix(m.skewY(30), [2, Math.tan(Math.PI/6), 0, 1, 120, 100],
"Unexpected result after skewY");
}
// DOMMatrix multiply(in DOMMatrix secondMatrix);
function testMultiplyInPlace()
{
var m1 = createMatrix(1, 0, 0, 1, 50, 90);
var m2 = createMatrix(Math.SQRT1_2, -Math.SQRT1_2, Math.SQRT1_2, Math.SQRT1_2, 0, 0);
var m3 = createMatrix(1, 0, 0, 1, 130, 160);
m1.multiplySelf(m2).multiplySelf(m3);
roughCmpMatrix(m1, [Math.SQRT1_2, -Math.SQRT1_2, Math.SQRT1_2, Math.SQRT1_2, 255.060974, 111.213203],
"Unexpected result after multiplying matrices");
// Check orig matrices are unchanged
roughCmpMatrix(m2, [Math.SQRT1_2, -Math.SQRT1_2, Math.SQRT1_2, Math.SQRT1_2, 0, 0],
"Matrix changed after multiplication");
cmpMatrix(m3, [1, 0, 0, 1, 130, 160], "Matrix changed after multiplication");
}
// DOMMatrix inverse() raises(SVGException);
function testInverseInPlace()
{
// Test inversion
var m = createMatrix(2, 0, 0, 4, 110, -50);
m.invertSelf();
roughCmpMatrix(m, [0.5, 0, 0, 0.25, -55, 12.5],
"Unexpected result after inverting matrix");
// Test non-invertable
m = createMatrix(0, 0, 1, 0, 0, 0);
m.invertSelf();
ok(isNaN(m.a), "Failed to invalidate inverted singular matrix, got " + m.a);
ok(!m.is2D, "Failed to mark invalidated inverted singular matrix as 3D.");
}
// DOMMatrix translate(in float x, in float y);
function testTranslateInPlace()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
m.translateSelf(100, -50)
roughCmpMatrix(m, [2, 0, 0, 1, 320, 50],
"Unexpected result after translate");
}
// DOMMatrix scale(in float scaleFactor);
function testScaleInPlace()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
m.scaleSelf(0.5);
roughCmpMatrix(m, [1, 0, 0, 0.5, 120, 100],
"Unexpected result after scale");
}
// DOMMatrix rotate(in float angle);
function testRotateInPlace()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
m.rotateSelf(45);
roughCmpMatrix(m,
[2*Math.cos(Math.PI/4), Math.sin(Math.PI/4),
2*-Math.sin(Math.PI/4), Math.cos(Math.PI/4),
120, 100],
"Unexpected result after rotate");
}
// DOMMatrix rotateFromVector(in float x, in float y) raises(SVGException);
function testRotateFromVectorInPlace()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
// Make a 150 degree angle
m.rotateFromVectorSelf(-2, 1.1547);
roughCmpMatrix(m,
[2*Math.cos(5*Math.PI/6), Math.sin(5*Math.PI/6),
2*-Math.sin(5*Math.PI/6), Math.cos(5*Math.PI/6),
120, 100],
"Unexpected result after rotateFromVector");
// Test bad input (1)
try {
m.rotateFromVectorSelf(1, 0);
ok(true, "did not throw exception with zero coord for rotateFromVector");
} catch (e) {
ok(false,
"Got unexpected exception " + e + ", expected NotSupportedError");
}
// Test bad input (2)
try {
m.rotateFromVectorSelf(0, 1);
ok(true, "did not throw exception with zero coord for rotateFromVector");
} catch (e) { }
}
// DOMMatrix skewX(in float angle);
function testSkewXInPlace()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
m.skewXSelf(30);
roughCmpMatrix(m, [2, 0, 2*Math.tan(Math.PI/6), 1, 120, 100],
"Unexpected result after skewX");
}
// DOMMatrix skewY(in float angle);
function testSkewYInPlace()
{
var m = createMatrix(2, 0, 0, 1, 120, 100);
m.skewYSelf(30);
roughCmpMatrix(m, [2, Math.tan(Math.PI/6), 0, 1, 120, 100],
"Unexpected result after skewY");
}
function testCreateMatrix3D()
{
var m = new DOMMatrix([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
// Should be initialised to identity
cmpMatrix(m, [1, 0, 0, 1, 0, 0],
"DOMMatrix should produce identity matrix");
is(m.is2D, false, "should produce 3d matrix");
m = new DOMMatrix([1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]);
// Should be initialised to identity
cmpMatrix(m, [1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
"DOMMatrix should produce identity matrix");
is(m.is2D, false, "should produce 3d matrix");
}
// DOMMatrix multiply(in DOMMatrix secondMatrix);
function testMultiply3D()
{
var m1 = createMatrix(1, 0, 0, 1, 50, 90);
var m2 = create3DMatrix(Math.SQRT1_2, -Math.SQRT1_2, Math.SQRT1_2, Math.SQRT1_2, 0, 0);
var m3 = create3DMatrix(1, 0, 0, 1, 130, 160);
var result = m1.multiply(m2).multiply(m3);
ok(m1.is2D == true, "should produce 3d matrix");
roughCmpMatrix(result, [Math.SQRT1_2, -Math.SQRT1_2, Math.SQRT1_2, Math.SQRT1_2, 255.060974, 111.213203],
"Unexpected result after multiplying matrices");
// Check orig matrices are unchanged
cmpMatrix(m1, [1, 0, 0, 1, 50, 90], "Matrix changed after multiplication");
roughCmpMatrix(m2, [Math.SQRT1_2, -Math.SQRT1_2, Math.SQRT1_2, Math.SQRT1_2, 0, 0],
"Matrix changed after multiplication");
cmpMatrix(m3, [1, 0, 0, 1, 130, 160], "Matrix changed after multiplication");
}
// DOMMatrix inverse() raises(SVGException);
function testInverse3D()
{
// Test inversion
var m = create3DMatrix(2, 0, 0, 4, 110, -50);
roughCmpMatrix(m.inverse(), [0.5, 0, 0, 0.25, -55, 12.5],
"Unexpected result after inverting matrix");
// Test non-invertable
m = createMatrix(0, 0, 1, 0, 0, 0);
m = m.inverse();
ok(isNaN(m.a), "Failed to invalidate inverted singular matrix, got " + m.a);
ok(!m.is2D, "Failed to mark invalidated inverted singular matrix as 3D.");
}
// DOMMatrix translate(in float x, in float y);
function testTranslate3D()
{
var m = create3DMatrix(2, 0, 0, 1, 120, 100);
roughCmpMatrix(m.translate(100, -50), [2, 0, 0, 1, 320, 50],
"Unexpected result after translate");
}
// DOMMatrix scale(in float scaleFactor);
function testScale3D()
{
var m = create3DMatrix(2, 0, 0, 1, 120, 100);
roughCmpMatrix(m.scale(0.5), [1, 0, 0, 0.5, 120, 100],
"Unexpected result after scale");
}
function Matrix3D() {
this.m = new Float32Array([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]);
}
Matrix3D.prototype = {
translate(x, y, z, result) {
result = result || new Matrix3D();
var m = result.m;
m[0] = 1;
m[1] = 0;
m[2] = 0;
m[3] = x;
m[4] = 0;
m[5] = 1;
m[6] = 0;
m[7] = y;
m[8] = 0;
m[9] = 0;
m[10] = 1;
m[11] = z;
m[12] = 0;
m[13] = 0;
m[14] = 0;
m[15] = 1;
return result;
},
inverse(matrix, result) {
result = result || new Matrix3D();
var m = matrix.m, r = result.m;
r[0] = m[5]*m[10]*m[15] - m[5]*m[14]*m[11] - m[6]*m[9]*m[15] + m[6]*m[13]*m[11] + m[7]*m[9]*m[14] - m[7]*m[13]*m[10];
r[1] = -m[1]*m[10]*m[15] + m[1]*m[14]*m[11] + m[2]*m[9]*m[15] - m[2]*m[13]*m[11] - m[3]*m[9]*m[14] + m[3]*m[13]*m[10];
r[2] = m[1]*m[6]*m[15] - m[1]*m[14]*m[7] - m[2]*m[5]*m[15] + m[2]*m[13]*m[7] + m[3]*m[5]*m[14] - m[3]*m[13]*m[6];
r[3] = -m[1]*m[6]*m[11] + m[1]*m[10]*m[7] + m[2]*m[5]*m[11] - m[2]*m[9]*m[7] - m[3]*m[5]*m[10] + m[3]*m[9]*m[6];
r[4] = -m[4]*m[10]*m[15] + m[4]*m[14]*m[11] + m[6]*m[8]*m[15] - m[6]*m[12]*m[11] - m[7]*m[8]*m[14] + m[7]*m[12]*m[10];
r[5] = m[0]*m[10]*m[15] - m[0]*m[14]*m[11] - m[2]*m[8]*m[15] + m[2]*m[12]*m[11] + m[3]*m[8]*m[14] - m[3]*m[12]*m[10];
r[6] = -m[0]*m[6]*m[15] + m[0]*m[14]*m[7] + m[2]*m[4]*m[15] - m[2]*m[12]*m[7] - m[3]*m[4]*m[14] + m[3]*m[12]*m[6];
r[7] = m[0]*m[6]*m[11] - m[0]*m[10]*m[7] - m[2]*m[4]*m[11] + m[2]*m[8]*m[7] + m[3]*m[4]*m[10] - m[3]*m[8]*m[6];
r[8] = m[4]*m[9]*m[15] - m[4]*m[13]*m[11] - m[5]*m[8]*m[15] + m[5]*m[12]*m[11] + m[7]*m[8]*m[13] - m[7]*m[12]*m[9];
r[9] = -m[0]*m[9]*m[15] + m[0]*m[13]*m[11] + m[1]*m[8]*m[15] - m[1]*m[12]*m[11] - m[3]*m[8]*m[13] + m[3]*m[12]*m[9];
r[10] = m[0]*m[5]*m[15] - m[0]*m[13]*m[7] - m[1]*m[4]*m[15] + m[1]*m[12]*m[7] + m[3]*m[4]*m[13] - m[3]*m[12]*m[5];
r[11] = -m[0]*m[5]*m[11] + m[0]*m[9]*m[7] + m[1]*m[4]*m[11] - m[1]*m[8]*m[7] - m[3]*m[4]*m[9] + m[3]*m[8]*m[5];
r[12] = -m[4]*m[9]*m[14] + m[4]*m[13]*m[10] + m[5]*m[8]*m[14] - m[5]*m[12]*m[10] - m[6]*m[8]*m[13] + m[6]*m[12]*m[9];
r[13] = m[0]*m[9]*m[14] - m[0]*m[13]*m[10] - m[1]*m[8]*m[14] + m[1]*m[12]*m[10] + m[2]*m[8]*m[13] - m[2]*m[12]*m[9];
r[14] = -m[0]*m[5]*m[14] + m[0]*m[13]*m[6] + m[1]*m[4]*m[14] - m[1]*m[12]*m[6] - m[2]*m[4]*m[13] + m[2]*m[12]*m[5];
r[15] = m[0]*m[5]*m[10] - m[0]*m[9]*m[6] - m[1]*m[4]*m[10] + m[1]*m[8]*m[6] + m[2]*m[4]*m[9] - m[2]*m[8]*m[5];
var det = m[0]*r[0] + m[1]*r[4] + m[2]*r[8] + m[3]*r[12];
for (var i = 0; i < 16; i++) r[i] /= det;
return result;
},
multiply(left, result) {
result = result || new Matrix3D();
var a = this.m, b = left.m, r = result.m;
r[0] = a[0] * b[0] + a[1] * b[4] + a[2] * b[8] + a[3] * b[12];
r[1] = a[0] * b[1] + a[1] * b[5] + a[2] * b[9] + a[3] * b[13];
r[2] = a[0] * b[2] + a[1] * b[6] + a[2] * b[10] + a[3] * b[14];
r[3] = a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3] * b[15];
r[4] = a[4] * b[0] + a[5] * b[4] + a[6] * b[8] + a[7] * b[12];
r[5] = a[4] * b[1] + a[5] * b[5] + a[6] * b[9] + a[7] * b[13];
r[6] = a[4] * b[2] + a[5] * b[6] + a[6] * b[10] + a[7] * b[14];
r[7] = a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7] * b[15];
r[8] = a[8] * b[0] + a[9] * b[4] + a[10] * b[8] + a[11] * b[12];
r[9] = a[8] * b[1] + a[9] * b[5] + a[10] * b[9] + a[11] * b[13];
r[10] = a[8] * b[2] + a[9] * b[6] + a[10] * b[10] + a[11] * b[14];
r[11] = a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11] * b[15];
r[12] = a[12] * b[0] + a[13] * b[4] + a[14] * b[8] + a[15] * b[12];
r[13] = a[12] * b[1] + a[13] * b[5] + a[14] * b[9] + a[15] * b[13];
r[14] = a[12] * b[2] + a[13] * b[6] + a[14] * b[10] + a[15] * b[14];
r[15] = a[12] * b[3] + a[13] * b[7] + a[14] * b[11] + a[15] * b[15];
return result;
},
scale(x, y, z, result) {
result = result || new Matrix3D();
var m = result.m;
m[0] = x;
m[1] = 0;
m[2] = 0;
m[3] = 0;
m[4] = 0;
m[5] = y;
m[6] = 0;
m[7] = 0;
m[8] = 0;
m[9] = 0;
m[10] = z;
m[11] = 0;
m[12] = 0;
m[13] = 0;
m[14] = 0;
m[15] = 1;
return result;
},
rotate(a, x, y, z, result) {
result = result || new Matrix3D();
var m = result.m;
var d = Math.sqrt(x*x + y*y + z*z);
a *= Math.PI / 180; x /= d; y /= d; z /= d;
var c = Math.cos(a), s = Math.sin(a), t = 1 - c;
m[0] = x * x * t + c;
m[1] = x * y * t - z * s;
m[2] = x * z * t + y * s;
m[3] = 0;
m[4] = y * x * t + z * s;
m[5] = y * y * t + c;
m[6] = y * z * t - x * s;
m[7] = 0;
m[8] = z * x * t - y * s;
m[9] = z * y * t + x * s;
m[10] = z * z * t + c;
m[11] = 0;
m[12] = 0;
m[13] = 0;
m[14] = 0;
m[15] = 1;
return result;
},
swap(result) {
result = result || new Matrix3D();
for (var x = 0; x < 16; x++)
result.m[x] = this.m[Math.floor(x/4) + (x%4)*4];
return result;
}
};
function test3D()
{
var m = new DOMMatrix()
var m2 = new Matrix3D();
m.translateSelf(2,3,4).scaleSelf(1.2, 2.3, 3.4, 0, 0, 0);
m2 = m2.multiply(m2.translate(2,3,4)).multiply(m2.scale(1.2, 2.3, 3.4)).swap();
ok(CompareMatrix(m2, m), "translate + scale in 3d didn't match, expected: " + formatMatrix(m2.m) + ", got: " + formatMatrix(m));
m.invertSelf();
m2 = new Matrix3D();
m2 = m2.multiply(m2.translate(2,3,4)).multiply(m2.scale(1.2, 2.3, 3.4));
m2 = m2.inverse(m2).swap();
ok(CompareMatrix(m2, m), "translate + scale in inverted 3d didn't match, expected: " + formatMatrix(m2.m) + ", got: " + formatMatrix(m));
}
function testParsing()
{
var m = new DOMMatrix("translate(10px, 20px) scale(.5, 2) rotate(45deg)");
var m2 = new DOMMatrix();
m2.translateSelf(10, 20).scaleSelf(.5,2).rotateSelf(45);
ok(CompareDOMMatrix(m2, m), "string parsing didn't match");
m = new DOMMatrix();
m.setMatrixValue("translate(10px, 20px) scale(.5, 2) rotate(45deg)");
ok(CompareDOMMatrix(m2, m), "string parsing didn't match");
}
function testStringify() {
var m = new DOMMatrix();
var s = "" + m;
ok(s == "matrix" + formatMatrix(m), "stringifier 1 produced wrong result: " + s);
m.a = 100;
s = "" + m;
ok(s == "matrix" + formatMatrix(m), "stringifier 2 produced wrong result: " + s);
m.m43 = 200;
s = "" + m;
ok(s == "matrix3d" + formatMatrix(m), "stringifier 3 produced wrong result:" + s);
}
window.addEventListener("load", main);
</script>
</pre>
</body>
</html>
|