-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.js
129 lines (112 loc) · 3.13 KB
/
transform.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
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
function Transform() {
this.reset();
}
Transform.prototype.copy = function() {
var other = new Transform();
other.m[0] = this.m[0];
other.m[1] = this.m[1];
other.m[2] = this.m[2];
other.m[3] = this.m[3];
other.m[4] = this.m[4];
other.m[5] = this.m[5];
return other;
}
Transform.prototype.reset = function() {
this.m = [1, 0, 0, 1, 0, 0];
};
Transform.prototype.multiply = function(matrix) {
var m11 = this.m[0] * matrix.m[0] + this.m[2] * matrix.m[1];
var m12 = this.m[1] * matrix.m[0] + this.m[3] * matrix.m[1];
var m21 = this.m[0] * matrix.m[2] + this.m[2] * matrix.m[3];
var 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];
var 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;
};
Transform.prototype.invert = function() {
var d = 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
var m0 = this.m[3] * d;
var m1 = -this.m[1] * d;
var m2 = -this.m[2] * d;
var m3 = this.m[0] * d;
var m4 = d * (this.m[2] * this.m[5] - this.m[3] * this.m[4]);
var m5 = d * (this.m[1] * this.m[4] - this.m[0] * this.m[5]);
this.m[0] = m0;
this.m[1] = m1;
this.m[2] = m2;
this.m[3] = m3;
this.m[4] = m4;
this.m[5] = m5;
};
Transform.prototype.rotate = function(rad) {
var c = Math.cos(rad);
var s = Math.sin(rad);
var m11 = this.m[0] * c + this.m[2] * s;
var m12 = this.m[1] * c + this.m[3] * s;
var m21 = this.m[0] * -s + this.m[2] * c;
var 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;
};
Transform.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;
};
Transform.prototype.scale = function(sx, sy) {
this.m[0] *= sx;
this.m[1] *= sx;
this.m[2] *= sy;
this.m[3] *= sy;
};
Transform.prototype.transformPoint = function(px, py) {
var x = px;
var 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];
};
Transform.prototype.transformPointInverse = function(px, py) {
other = this.copy();
other.invert();
return other.transformPoint(px, py);
};
Transform.prototype.clamp = function(width, height, canvas) {
width *= this.m[0];
height *= this.m[3];
width = (canvas.width - width);
height = (canvas.height - height);
if (width >= 0) {
if (this.m[4] < 0) {
this.m[4] = 0;
} else if (this.m[4] > width) {
this.m[4] = width;
}
}
if (width < 0) {
if (this.m[4] > 0) {
this.m[4] = 0;
} else if (this.m[4] < width) {
this.m[4] = width;
}
}
if (height >= 0) {
if (this.m[5] < 0) {
this.m[5] = 0;
} else if (this.m[5] > height) {
this.m[5] = height;
}
} else {
if (this.m[5] > 0) {
this.m[5] = 0;
} else if (this.m[5] < height) {
this.m[5] = height;
}
}
};