| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var 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 | */
|
|---|
| 14 | exports.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 | */
|
|---|
| 55 | exports.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 | */
|
|---|
| 80 | var 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 | */
|
|---|
| 123 | exports.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 angle = Math.min(Math.max(-1, data[0] / sx), 1),
|
|---|
| 157 | rotate = [mth.acos(angle, floatPrecision) * ((scaleBefore ? 1 : sy) * data[1] < 0 ? -1 : 1)];
|
|---|
| 158 |
|
|---|
| 159 | if (rotate[0]) transforms.push({ name: 'rotate', data: rotate });
|
|---|
| 160 |
|
|---|
| 161 | if (rowsSum && colsSum) transforms.push({
|
|---|
| 162 | name: 'skewX',
|
|---|
| 163 | data: [mth.atan(colsSum / (sx * sx), floatPrecision)]
|
|---|
| 164 | });
|
|---|
| 165 |
|
|---|
| 166 | // rotate(a, cx, cy) can consume translate() within optional arguments cx, cy (rotation point)
|
|---|
| 167 | if (rotate[0] && (data[4] || data[5])) {
|
|---|
| 168 | transforms.shift();
|
|---|
| 169 | var cos = data[0] / sx,
|
|---|
| 170 | sin = data[1] / (scaleBefore ? sx : sy),
|
|---|
| 171 | x = data[4] * (scaleBefore || sy),
|
|---|
| 172 | y = data[5] * (scaleBefore || sx),
|
|---|
| 173 | denom = (Math.pow(1 - cos, 2) + Math.pow(sin, 2)) * (scaleBefore || sx * sy);
|
|---|
| 174 | rotate.push(((1 - cos) * x - sin * y) / denom);
|
|---|
| 175 | rotate.push(((1 - cos) * y + sin * x) / denom);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | // Too many transformations, return original matrix if it isn't just a scale/translate
|
|---|
| 179 | } else if (data[1] || data[2]) {
|
|---|
| 180 | return transform;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | if (scaleBefore && (sx != 1 || sy != 1) || !transforms.length) transforms.push({
|
|---|
| 184 | name: 'scale',
|
|---|
| 185 | data: sx == sy ? [sx] : [sx, sy]
|
|---|
| 186 | });
|
|---|
| 187 |
|
|---|
| 188 | return transforms;
|
|---|
| 189 | };
|
|---|
| 190 |
|
|---|
| 191 | /**
|
|---|
| 192 | * Convert transform to the matrix data.
|
|---|
| 193 | *
|
|---|
| 194 | * @param {Object} transform transform object
|
|---|
| 195 | * @return {Array} matrix data
|
|---|
| 196 | */
|
|---|
| 197 | function transformToMatrix(transform) {
|
|---|
| 198 |
|
|---|
| 199 | if (transform.name === 'matrix') return transform.data;
|
|---|
| 200 |
|
|---|
| 201 | var matrix;
|
|---|
| 202 |
|
|---|
| 203 | switch (transform.name) {
|
|---|
| 204 | case 'translate':
|
|---|
| 205 | // [1, 0, 0, 1, tx, ty]
|
|---|
| 206 | matrix = [1, 0, 0, 1, transform.data[0], transform.data[1] || 0];
|
|---|
| 207 | break;
|
|---|
| 208 | case 'scale':
|
|---|
| 209 | // [sx, 0, 0, sy, 0, 0]
|
|---|
| 210 | matrix = [transform.data[0], 0, 0, transform.data[1] || transform.data[0], 0, 0];
|
|---|
| 211 | break;
|
|---|
| 212 | case 'rotate':
|
|---|
| 213 | // [cos(a), sin(a), -sin(a), cos(a), x, y]
|
|---|
| 214 | var cos = mth.cos(transform.data[0]),
|
|---|
| 215 | sin = mth.sin(transform.data[0]),
|
|---|
| 216 | cx = transform.data[1] || 0,
|
|---|
| 217 | cy = transform.data[2] || 0;
|
|---|
| 218 |
|
|---|
| 219 | matrix = [cos, sin, -sin, cos, (1 - cos) * cx + sin * cy, (1 - cos) * cy - sin * cx];
|
|---|
| 220 | break;
|
|---|
| 221 | case 'skewX':
|
|---|
| 222 | // [1, 0, tan(a), 1, 0, 0]
|
|---|
| 223 | matrix = [1, 0, mth.tan(transform.data[0]), 1, 0, 0];
|
|---|
| 224 | break;
|
|---|
| 225 | case 'skewY':
|
|---|
| 226 | // [1, tan(a), 0, 1, 0, 0]
|
|---|
| 227 | matrix = [1, mth.tan(transform.data[0]), 0, 1, 0, 0];
|
|---|
| 228 | break;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | return matrix;
|
|---|
| 232 |
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | /**
|
|---|
| 236 | * Applies transformation to an arc. To do so, we represent ellipse as a matrix, multiply it
|
|---|
| 237 | * by the transformation matrix and use a singular value decomposition to represent in a form
|
|---|
| 238 | * rotate(θ)·scale(a b)·rotate(φ). This gives us new ellipse params a, b and θ.
|
|---|
| 239 | * SVD is being done with the formulae provided by Wolffram|Alpha (svd {{m0, m2}, {m1, m3}})
|
|---|
| 240 | *
|
|---|
| 241 | * @param {Array} arc [a, b, rotation in deg]
|
|---|
| 242 | * @param {Array} transform transformation matrix
|
|---|
| 243 | * @return {Array} arc transformed input arc
|
|---|
| 244 | */
|
|---|
| 245 | exports.transformArc = function(arc, transform) {
|
|---|
| 246 |
|
|---|
| 247 | var a = arc[0],
|
|---|
| 248 | b = arc[1],
|
|---|
| 249 | rot = arc[2] * Math.PI / 180,
|
|---|
| 250 | cos = Math.cos(rot),
|
|---|
| 251 | sin = Math.sin(rot),
|
|---|
| 252 | h = Math.pow(arc[5] * cos + arc[6] * sin, 2) / (4 * a * a) +
|
|---|
| 253 | Math.pow(arc[6] * cos - arc[5] * sin, 2) / (4 * b * b);
|
|---|
| 254 | if (h > 1) {
|
|---|
| 255 | h = Math.sqrt(h);
|
|---|
| 256 | a *= h;
|
|---|
| 257 | b *= h;
|
|---|
| 258 | }
|
|---|
| 259 | var ellipse = [a * cos, a * sin, -b * sin, b * cos, 0, 0],
|
|---|
| 260 | m = multiplyTransformMatrices(transform, ellipse),
|
|---|
| 261 | // Decompose the new ellipse matrix
|
|---|
| 262 | lastCol = m[2] * m[2] + m[3] * m[3],
|
|---|
| 263 | squareSum = m[0] * m[0] + m[1] * m[1] + lastCol,
|
|---|
| 264 | root = Math.hypot(m[0] - m[3], m[1] + m[2]) * Math.hypot(m[0] + m[3], m[1] - m[2]);
|
|---|
| 265 |
|
|---|
| 266 | if (!root) { // circle
|
|---|
| 267 | arc[0] = arc[1] = Math.sqrt(squareSum / 2);
|
|---|
| 268 | arc[2] = 0;
|
|---|
| 269 | } else {
|
|---|
| 270 | var majorAxisSqr = (squareSum + root) / 2,
|
|---|
| 271 | minorAxisSqr = (squareSum - root) / 2,
|
|---|
| 272 | major = Math.abs(majorAxisSqr - lastCol) > 1e-6,
|
|---|
| 273 | sub = (major ? majorAxisSqr : minorAxisSqr) - lastCol,
|
|---|
| 274 | rowsSum = m[0] * m[2] + m[1] * m[3],
|
|---|
| 275 | term1 = m[0] * sub + m[2] * rowsSum,
|
|---|
| 276 | term2 = m[1] * sub + m[3] * rowsSum;
|
|---|
| 277 | arc[0] = Math.sqrt(majorAxisSqr);
|
|---|
| 278 | arc[1] = Math.sqrt(minorAxisSqr);
|
|---|
| 279 | arc[2] = ((major ? term2 < 0 : term1 > 0) ? -1 : 1) *
|
|---|
| 280 | Math.acos((major ? term1 : term2) / Math.hypot(term1, term2)) * 180 / Math.PI;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | if ((transform[0] < 0) !== (transform[3] < 0)) {
|
|---|
| 284 | // Flip the sweep flag if coordinates are being flipped horizontally XOR vertically
|
|---|
| 285 | arc[4] = 1 - arc[4];
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | return arc;
|
|---|
| 289 |
|
|---|
| 290 | };
|
|---|
| 291 |
|
|---|
| 292 | /**
|
|---|
| 293 | * Multiply transformation matrices.
|
|---|
| 294 | *
|
|---|
| 295 | * @param {Array} a matrix A data
|
|---|
| 296 | * @param {Array} b matrix B data
|
|---|
| 297 | * @return {Array} result
|
|---|
| 298 | */
|
|---|
| 299 | function multiplyTransformMatrices(a, b) {
|
|---|
| 300 |
|
|---|
| 301 | return [
|
|---|
| 302 | a[0] * b[0] + a[2] * b[1],
|
|---|
| 303 | a[1] * b[0] + a[3] * b[1],
|
|---|
| 304 | a[0] * b[2] + a[2] * b[3],
|
|---|
| 305 | a[1] * b[2] + a[3] * b[3],
|
|---|
| 306 | a[0] * b[4] + a[2] * b[5] + a[4],
|
|---|
| 307 | a[1] * b[4] + a[3] * b[5] + a[5]
|
|---|
| 308 | ];
|
|---|
| 309 |
|
|---|
| 310 | }
|
|---|