-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
148 lines (122 loc) · 2.33 KB
/
index.js
File metadata and controls
148 lines (122 loc) · 2.33 KB
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
/**
* Module dependencies
*/
var computed = require('computed-style');
/**
* Expose `unmatrix`
*/
module.exports = unmatrix;
/**
* Unmatrix
*
* @param {Element} el
* @return {Object}
*/
function unmatrix(el) {
return 'string' != typeof el
? parse(style(el))
: parse(el);
}
/**
* Unmatrix: parse the values of the matrix
*
* Algorithm from:
*
* - http://hg.mozilla.org/mozilla-central/file/7cb3e9795d04/layout/style/nsStyleAnimation.cpp
*
* @param {String} str
* @return {Object}
* @api public
*/
function parse(str) {
var m = stom(str);
var A = m[0];
var B = m[1];
var C = m[2];
var D = m[3];
if (A * D == B * C) throw new Error('transform#unmatrix: matrix is singular');
// step (3)
var scaleX = Math.sqrt(A * A + B * B);
A /= scaleX;
B /= scaleX;
// step (4)
var skew = A * C + B * D;
C -= A * skew;
D -= B * skew;
// step (5)
var scaleY = Math.sqrt(C * C + D * D);
C /= scaleY;
D /= scaleY;
skew /= scaleY;
// step (6)
if ( A * D < B * C ) {
A = -A;
B = -B;
skew = -skew;
scaleX = -scaleX;
}
return {
translateX: m[4],
translateY: m[5],
rotate: rtod(Math.atan2(B, A)),
skew: rtod(Math.atan(skew)),
scaleX: round(scaleX),
scaleY: round(scaleY)
};
};
/**
* Get the computed style
*
* @param {Element} el
* @return {String}
* @api private
*/
function style(el) {
var style = computed(el);
return style.getPropertyValue('transform')
|| style.getPropertyValue('-webkit-transform')
|| style.getPropertyValue('-moz-transform')
|| style.getPropertyValue('-ms-transform')
|| style.getPropertyValue('-o-transform');
};
/**
* String to matrix
*
* @param {String} style
* @return {Array}
* @api private
*/
function stom(str) {
var m = [];
if (window.WebKitCSSMatrix) {
m = new window.WebKitCSSMatrix(str);
return [m.a, m.b, m.c, m.d, m.e, m.f];
}
var rdigit = /[\d\.\-]+/g;
var n;
while(n = rdigit.exec(str)) {
m.push(+n);
}
return m;
};
/**
* Radians to degrees
*
* @param {Number} radians
* @return {Number} degrees
* @api private
*/
function rtod(radians) {
var deg = radians * 180 / Math.PI;
return round(deg);
}
/**
* Round to the nearest hundredth
*
* @param {Number} n
* @return {Number}
* @api private
*/
function round(n) {
return Math.round(n * 100) / 100;
}