-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaffine-transform.js
58 lines (56 loc) · 1.84 KB
/
affine-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
function AffineTransform(v, m) {
if(v instanceof Matrix)
m = v, v = null;
this.matrix = m || Matrix.identity;
this.translation = v || Vector.zero;
}
AffineTransform.prototype = {
clone: function() {
return new AffineTransform(this.translation, this.matrix);
},
evaluate: function() {
return this;
},
toWorldSpace: function(objectVector) {
return this.matrix.times(objectVector).plus(this.translation);
},
toObjectSpace: function(worldVector) {
return this.matrix.inverse().times(worldVector.minus(this.translation));
},
vectorToWorldSpace: function(objectVector) {
return this.matrix.times(objectVector);
},
vectorToObjectSpace: function(worldVector) {
return this.matrix.inverse().times(worldVector);
},
inverse: function() {
var inv = this.matrix.inverse();
return new AffineTransform(inv.times(this.translation.minus()), inv);
},
combine: function(that) {
return new AffineTransform(this.toWorldSpace(that.translation), this.matrix.times(that.matrix));
},
translate: function(v) {
return new AffineTransform(this.translation.plus(v), this.matrix)
},
rotate: function(angle, center) {
var m = Matrix.fromRotation(angle);
if(!center) return new AffineTransform(this.translation, this.matrix.times(m))
else return new AffineTransform(m.times(this.translation.minus(center)).plus(center), this.matrix.times(m))
},
scale: function(factor, center) {
var scaled = this.clone();
if(factor instanceof Vector)
factor = factor.toDiagonalMatrix();
scaled.matrix = this.matrix.times(factor);
if(center) {
scaled.translation = this.translation.minus(center).times(factor).plus(center);
}
return scaled;
},
toSVGTransformString: function() {
var m = this.matrix, v = this.translation;
return 'M'+[m.a, m.c, m.b, m.d, v.x, v.y];//[m.a, m.b, v.x, m.c, m.d, v.y]
},
};
AffineTransform.identity = new AffineTransform();