-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
86 lines (85 loc) · 2.41 KB
/
index.js
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
var Matrix = function() {
this.reset();
};
Matrix.prototype.reset = function() {
this.m = [1, 0, 0, 1, 0, 0];
return this;
};
Matrix.prototype.multiply = function(matrix) {
var m11 = this.m[0] * matrix.m[0] + this.m[2] * matrix.m[1],
m12 = this.m[1] * matrix.m[0] + this.m[3] * matrix.m[1],
m21 = this.m[0] * matrix.m[2] + this.m[2] * matrix.m[3],
m22 = this.m[1] * matrix.m[2] + this.m[3] * matrix.m[3];
var dx = this.m[0] * matrix.m[4] + this.m[2] * matrix.m[5] + this.m[4],
dy = this.m[1] * matrix.m[4] + this.m[3] * matrix.m[5] + this.m[5];
this.m[0] = m11;
this.m[1] = m12;
this.m[2] = m21;
this.m[3] = m22;
this.m[4] = dx;
this.m[5] = dy;
return this;
};
Matrix.prototype.inverse = function() {
var inv = new Matrix();
inv.m = this.m.slice(0);
var d = 1 / (inv.m[0] * inv.m[3] - inv.m[1] * inv.m[2]),
m0 = inv.m[3] * d,
m1 = -inv.m[1] * d,
m2 = -inv.m[2] * d,
m3 = inv.m[0] * d,
m4 = d * (inv.m[2] * inv.m[5] - inv.m[3] * inv.m[4]),
m5 = d * (inv.m[1] * inv.m[4] - inv.m[0] * inv.m[5]);
inv.m[0] = m0;
inv.m[1] = m1;
inv.m[2] = m2;
inv.m[3] = m3;
inv.m[4] = m4;
inv.m[5] = m5;
return inv;
};
Matrix.prototype.rotate = function(rad) {
var c = Math.cos(rad),
s = Math.sin(rad),
m11 = this.m[0] * c + this.m[2] * s,
m12 = this.m[1] * c + this.m[3] * s,
m21 = this.m[0] * -s + this.m[2] * c,
m22 = this.m[1] * -s + this.m[3] * c;
this.m[0] = m11;
this.m[1] = m12;
this.m[2] = m21;
this.m[3] = m22;
return this;
};
Matrix.prototype.translate = function(x, y) {
this.m[4] += this.m[0] * x + this.m[2] * y;
this.m[5] += this.m[1] * x + this.m[3] * y;
return this;
};
Matrix.prototype.scale = function(sx, sy) {
this.m[0] *= sx;
this.m[1] *= sx;
this.m[2] *= sy;
this.m[3] *= sy;
return this;
};
Matrix.prototype.transformPoint = function(px, py) {
var x = px,
y = py;
px = x * this.m[0] + y * this.m[2] + this.m[4];
py = x * this.m[1] + y * this.m[3] + this.m[5];
return [px, py];
};
Matrix.prototype.transformVector = function(px, py) {
var x = px,
y = py;
px = x * this.m[0] + y * this.m[2];
py = x * this.m[1] + y * this.m[3];
return [px, py];
};
if(typeof module !== "undefined") {
module.exports = Matrix;
}
else {
window.Matrix = Matrix;
}