UNPKG

10.2 kBJavaScriptView Raw
1'use strict';
2
3var regTransformTypes = /matrix|translate|scale|rotate|skewX|skewY/,
4 regTransformSplit = /\s*(matrix|translate|scale|rotate|skewX|skewY)\s*\(\s*(.+?)\s*\)[\s,]*/,
5 regNumericValues = /[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][-+]?\d+)?/g;
6
7/**
8 * Convert transform string to JS representation.
9 *
10 * @param {String} transformString input string
11 * @param {Object} params plugin params
12 * @return {Array} output array
13 */
14exports.transform2js = function(transformString) {
15
16 // JS representation of the transform data
17 var transforms = [],
18 // current transform context
19 current;
20
21 // split value into ['', 'translate', '10 50', '', 'scale', '2', '', 'rotate', '-45', '']
22 transformString.split(regTransformSplit).forEach(function(item) {
23 /*jshint -W084 */
24 var num;
25
26 if (item) {
27 // if item is a translate function
28 if (regTransformTypes.test(item)) {
29 // then collect it and change current context
30 transforms.push(current = { name: item });
31 // else if item is data
32 } else {
33 // then split it into [10, 50] and collect as context.data
34 while (num = regNumericValues.exec(item)) {
35 num = Number(num);
36 if (current.data)
37 current.data.push(num);
38 else
39 current.data = [num];
40 }
41 }
42 }
43 });
44
45 // return empty array if broken transform (no data)
46 return current && current.data ? transforms : [];
47};
48
49/**
50 * Multiply transforms into one.
51 *
52 * @param {Array} input transforms array
53 * @return {Array} output matrix array
54 */
55exports.transformsMultiply = function(transforms) {
56
57 // convert transforms objects to the matrices
58 transforms = transforms.map(function(transform) {
59 if (transform.name === 'matrix') {
60 return transform.data;
61 }
62 return transformToMatrix(transform);
63 });
64
65 // multiply all matrices into one
66 transforms = {
67 name: 'matrix',
68 data: transforms.length > 0 ? transforms.reduce(multiplyTransformMatrices) : []
69 };
70
71 return transforms;
72
73};
74
75/**
76 * Do math like a schoolgirl.
77 *
78 * @type {Object}
79 */
80var mth = exports.mth = {
81
82 rad: function(deg) {
83 return deg * Math.PI / 180;
84 },
85
86 deg: function(rad) {
87 return rad * 180 / Math.PI;
88 },
89
90 cos: function(deg) {
91 return Math.cos(this.rad(deg));
92 },
93
94 acos: function(val, floatPrecision) {
95 return +(this.deg(Math.acos(val)).toFixed(floatPrecision));
96 },
97
98 sin: function(deg) {
99 return Math.sin(this.rad(deg));
100 },
101
102 asin: function(val, floatPrecision) {
103 return +(this.deg(Math.asin(val)).toFixed(floatPrecision));
104 },
105
106 tan: function(deg) {
107 return Math.tan(this.rad(deg));
108 },
109
110 atan: function(val, floatPrecision) {
111 return +(this.deg(Math.atan(val)).toFixed(floatPrecision));
112 }
113
114};
115
116/**
117 * Decompose matrix into simple transforms. See
118 * http://frederic-wang.fr/decomposition-of-2d-transform-matrices.html
119 *
120 * @param {Object} data matrix transform object
121 * @return {Object|Array} transforms array or original transform object
122 */
123exports.matrixToTransform = function(transform, params) {
124 var floatPrecision = params.floatPrecision,
125 data = transform.data,
126 transforms = [],
127 sx = +Math.hypot(data[0], data[1]).toFixed(params.transformPrecision),
128 sy = +((data[0] * data[3] - data[1] * data[2]) / sx).toFixed(params.transformPrecision),
129 colsSum = data[0] * data[2] + data[1] * data[3],
130 rowsSum = data[0] * data[1] + data[2] * data[3],
131 scaleBefore = rowsSum != 0 || sx == sy;
132
133 // [..., ..., ..., ..., tx, ty] → translate(tx, ty)
134 if (data[4] || data[5]) {
135 transforms.push({ name: 'translate', data: data.slice(4, data[5] ? 6 : 5) });
136 }
137
138 // [sx, 0, tan(a)·sy, sy, 0, 0] → skewX(a)·scale(sx, sy)
139 if (!data[1] && data[2]) {
140 transforms.push({ name: 'skewX', data: [mth.atan(data[2] / sy, floatPrecision)] });
141
142 // [sx, sx·tan(a), 0, sy, 0, 0] → skewY(a)·scale(sx, sy)
143 } else if (data[1] && !data[2]) {
144 transforms.push({ name: 'skewY', data: [mth.atan(data[1] / data[0], floatPrecision)] });
145 sx = data[0];
146 sy = data[3];
147
148 // [sx·cos(a), sx·sin(a), sy·-sin(a), sy·cos(a), x, y] → rotate(a[, cx, cy])·(scale or skewX) or
149 // [sx·cos(a), sy·sin(a), sx·-sin(a), sy·cos(a), x, y] → scale(sx, sy)·rotate(a[, cx, cy]) (if !scaleBefore)
150 } else if (!colsSum || (sx == 1 && sy == 1) || !scaleBefore) {
151 if (!scaleBefore) {
152 sx = (data[0] < 0 ? -1 : 1) * Math.hypot(data[0], data[2]);
153 sy = (data[3] < 0 ? -1 : 1) * Math.hypot(data[1], data[3]);
154 transforms.push({ name: 'scale', data: [sx, sy] });
155 }
156 var rotate = [mth.acos(data[0] / sx, floatPrecision) * ((scaleBefore ? 1 : sy) * data[1] < 0 ? -1 : 1)];
157
158 if (rotate[0]) transforms.push({ name: 'rotate', data: rotate });
159
160 if (rowsSum && colsSum) transforms.push({
161 name: 'skewX',
162 data: [mth.atan(colsSum / (sx * sx), floatPrecision)]
163 });
164
165 // rotate(a, cx, cy) can consume translate() within optional arguments cx, cy (rotation point)
166 if (rotate[0] && (data[4] || data[5])) {
167 transforms.shift();
168 var cos = data[0] / sx,
169 sin = data[1] / (scaleBefore ? sx : sy),
170 x = data[4] * (scaleBefore || sy),
171 y = data[5] * (scaleBefore || sx),
172 denom = (Math.pow(1 - cos, 2) + Math.pow(sin, 2)) * (scaleBefore || sx * sy);
173 rotate.push(((1 - cos) * x - sin * y) / denom);
174 rotate.push(((1 - cos) * y + sin * x) / denom);
175 }
176
177 // Too many transformations, return original matrix if it isn't just a scale/translate
178 } else if (data[1] || data[2]) {
179 return transform;
180 }
181
182 if (scaleBefore && (sx != 1 || sy != 1) || !transforms.length) transforms.push({
183 name: 'scale',
184 data: sx == sy ? [sx] : [sx, sy]
185 });
186
187 return transforms;
188};
189
190/**
191 * Convert transform to the matrix data.
192 *
193 * @param {Object} transform transform object
194 * @return {Array} matrix data
195 */
196function transformToMatrix(transform) {
197
198 if (transform.name === 'matrix') return transform.data;
199
200 var matrix;
201
202 switch (transform.name) {
203 case 'translate':
204 // [1, 0, 0, 1, tx, ty]
205 matrix = [1, 0, 0, 1, transform.data[0], transform.data[1] || 0];
206 break;
207 case 'scale':
208 // [sx, 0, 0, sy, 0, 0]
209 matrix = [transform.data[0], 0, 0, transform.data[1] || transform.data[0], 0, 0];
210 break;
211 case 'rotate':
212 // [cos(a), sin(a), -sin(a), cos(a), x, y]
213 var cos = mth.cos(transform.data[0]),
214 sin = mth.sin(transform.data[0]),
215 cx = transform.data[1] || 0,
216 cy = transform.data[2] || 0;
217
218 matrix = [cos, sin, -sin, cos, (1 - cos) * cx + sin * cy, (1 - cos) * cy - sin * cx];
219 break;
220 case 'skewX':
221 // [1, 0, tan(a), 1, 0, 0]
222 matrix = [1, 0, mth.tan(transform.data[0]), 1, 0, 0];
223 break;
224 case 'skewY':
225 // [1, tan(a), 0, 1, 0, 0]
226 matrix = [1, mth.tan(transform.data[0]), 0, 1, 0, 0];
227 break;
228 }
229
230 return matrix;
231
232}
233
234/**
235 * Applies transformation to an arc. To do so, we represent ellipse as a matrix, multiply it
236 * by the transformation matrix and use a singular value decomposition to represent in a form
237 * rotate(θ)·scale(a b)·rotate(φ). This gives us new ellipse params a, b and θ.
238 * SVD is being done with the formulae provided by Wolffram|Alpha (svd {{m0, m2}, {m1, m3}})
239 *
240 * @param {Array} arc [a, b, rotation in deg]
241 * @param {Array} transform transformation matrix
242 * @return {Array} arc transformed input arc
243 */
244exports.transformArc = function(arc, transform) {
245
246 var a = arc[0],
247 b = arc[1],
248 rot = arc[2] * Math.PI / 180,
249 cos = Math.cos(rot),
250 sin = Math.sin(rot),
251 h = Math.pow(arc[5] * cos + arc[6] * sin, 2) / (4 * a * a) +
252 Math.pow(arc[6] * cos - arc[5] * sin, 2) / (4 * b * b);
253 if (h > 1) {
254 h = Math.sqrt(h);
255 a *= h;
256 b *= h;
257 }
258 var ellipse = [a * cos, a * sin, -b * sin, b * cos, 0, 0],
259 m = multiplyTransformMatrices(transform, ellipse),
260 // Decompose the new ellipse matrix
261 lastCol = m[2] * m[2] + m[3] * m[3],
262 squareSum = m[0] * m[0] + m[1] * m[1] + lastCol,
263 root = Math.hypot(m[0] - m[3], m[1] + m[2]) * Math.hypot(m[0] + m[3], m[1] - m[2]);
264
265 if (!root) { // circle
266 arc[0] = arc[1] = Math.sqrt(squareSum / 2);
267 arc[2] = 0;
268 } else {
269 var majorAxisSqr = (squareSum + root) / 2,
270 minorAxisSqr = (squareSum - root) / 2,
271 major = Math.abs(majorAxisSqr - lastCol) > 1e-6,
272 sub = (major ? majorAxisSqr : minorAxisSqr) - lastCol,
273 rowsSum = m[0] * m[2] + m[1] * m[3],
274 term1 = m[0] * sub + m[2] * rowsSum,
275 term2 = m[1] * sub + m[3] * rowsSum;
276 arc[0] = Math.sqrt(majorAxisSqr);
277 arc[1] = Math.sqrt(minorAxisSqr);
278 arc[2] = ((major ? term2 < 0 : term1 > 0) ? -1 : 1) *
279 Math.acos((major ? term1 : term2) / Math.hypot(term1, term2)) * 180 / Math.PI;
280 }
281
282 if ((transform[0] < 0) !== (transform[3] < 0)) {
283 // Flip the sweep flag if coordinates are being flipped horizontally XOR vertically
284 arc[4] = 1 - arc[4];
285 }
286
287 return arc;
288
289};
290
291/**
292 * Multiply transformation matrices.
293 *
294 * @param {Array} a matrix A data
295 * @param {Array} b matrix B data
296 * @return {Array} result
297 */
298function multiplyTransformMatrices(a, b) {
299
300 return [
301 a[0] * b[0] + a[2] * b[1],
302 a[1] * b[0] + a[3] * b[1],
303 a[0] * b[2] + a[2] * b[3],
304 a[1] * b[2] + a[3] * b[3],
305 a[0] * b[4] + a[2] * b[5] + a[4],
306 a[1] * b[4] + a[3] * b[5] + a[5]
307 ];
308
309}