|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('matrix', function (Y, NAME) { |
|
9 |
|
10 /** |
|
11 * Matrix utilities. |
|
12 * |
|
13 * @class MatrixUtil |
|
14 * @module matrix |
|
15 **/ |
|
16 |
|
17 var MatrixUtil = { |
|
18 /** |
|
19 * Used as value for the _rounding method. |
|
20 * |
|
21 * @property _rounder |
|
22 * @private |
|
23 */ |
|
24 _rounder: 100000, |
|
25 |
|
26 /** |
|
27 * Rounds values |
|
28 * |
|
29 * @method _round |
|
30 * @private |
|
31 */ |
|
32 _round: function(val) { |
|
33 val = Math.round(val * MatrixUtil._rounder) / MatrixUtil._rounder; |
|
34 return val; |
|
35 }, |
|
36 /** |
|
37 * Converts a radian value to a degree. |
|
38 * |
|
39 * @method rad2deg |
|
40 * @param {Number} rad Radian value to be converted. |
|
41 * @return Number |
|
42 */ |
|
43 rad2deg: function(rad) { |
|
44 var deg = rad * (180 / Math.PI); |
|
45 return deg; |
|
46 }, |
|
47 |
|
48 /** |
|
49 * Converts a degree value to a radian. |
|
50 * |
|
51 * @method deg2rad |
|
52 * @param {Number} deg Degree value to be converted to radian. |
|
53 * @return Number |
|
54 */ |
|
55 deg2rad: function(deg) { |
|
56 var rad = deg * (Math.PI / 180); |
|
57 return rad; |
|
58 }, |
|
59 |
|
60 /** |
|
61 * Converts an angle to a radian |
|
62 * |
|
63 * @method angle2rad |
|
64 * @param {Objecxt} val Value to be converted to radian. |
|
65 * @return Number |
|
66 */ |
|
67 angle2rad: function(val) { |
|
68 if (typeof val === 'string' && val.indexOf('rad') > -1) { |
|
69 val = parseFloat(val); |
|
70 } else { // default to deg |
|
71 val = MatrixUtil.deg2rad(parseFloat(val)); |
|
72 } |
|
73 |
|
74 return val; |
|
75 }, |
|
76 |
|
77 /** |
|
78 * Converts a transform object to an array of column vectors. |
|
79 * |
|
80 * / \ |
|
81 * | matrix[0][0] matrix[1][0] matrix[2][0] | |
|
82 * | matrix[0][1] matrix[1][1] matrix[2][1] | |
|
83 * | matrix[0][2] matrix[1][2] matrix[2][2] | |
|
84 * \ / |
|
85 * |
|
86 * @method getnxn |
|
87 * @return Array |
|
88 */ |
|
89 convertTransformToArray: function(matrix) |
|
90 { |
|
91 var matrixArray = [ |
|
92 [matrix.a, matrix.c, matrix.dx], |
|
93 [matrix.b, matrix.d, matrix.dy], |
|
94 [0, 0, 1] |
|
95 ]; |
|
96 return matrixArray; |
|
97 }, |
|
98 |
|
99 /** |
|
100 * Returns the determinant of a given matrix. |
|
101 * |
|
102 * / \ |
|
103 * | matrix[0][0] matrix[1][0] matrix[2][0] | |
|
104 * | matrix[0][1] matrix[1][1] matrix[2][1] | |
|
105 * | matrix[0][2] matrix[1][2] matrix[2][2] | |
|
106 * | matrix[0][3] matrix[1][3] matrix[2][3] | |
|
107 * \ / |
|
108 * |
|
109 * @method getDeterminant |
|
110 * @param {Array} matrix An nxn matrix represented an array of vector (column) arrays. Each vector array has index for each row. |
|
111 * @return Number |
|
112 */ |
|
113 getDeterminant: function(matrix) |
|
114 { |
|
115 var determinant = 0, |
|
116 len = matrix.length, |
|
117 i = 0, |
|
118 multiplier; |
|
119 |
|
120 if(len == 2) |
|
121 { |
|
122 return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]; |
|
123 } |
|
124 for(; i < len; ++i) |
|
125 { |
|
126 multiplier = matrix[i][0]; |
|
127 if(i % 2 === 0 || i === 0) |
|
128 { |
|
129 determinant += multiplier * MatrixUtil.getDeterminant(MatrixUtil.getMinors(matrix, i, 0)); |
|
130 } |
|
131 else |
|
132 { |
|
133 determinant -= multiplier * MatrixUtil.getDeterminant(MatrixUtil.getMinors(matrix, i, 0)); |
|
134 } |
|
135 } |
|
136 return determinant; |
|
137 }, |
|
138 |
|
139 /** |
|
140 * Returns the inverse of a matrix |
|
141 * |
|
142 * @method inverse |
|
143 * @param Array matrix An array representing an nxn matrix |
|
144 * @return Array |
|
145 * |
|
146 * / \ |
|
147 * | matrix[0][0] matrix[1][0] matrix[2][0] | |
|
148 * | matrix[0][1] matrix[1][1] matrix[2][1] | |
|
149 * | matrix[0][2] matrix[1][2] matrix[2][2] | |
|
150 * | matrix[0][3] matrix[1][3] matrix[2][3] | |
|
151 * \ / |
|
152 */ |
|
153 inverse: function(matrix) |
|
154 { |
|
155 var determinant = 0, |
|
156 len = matrix.length, |
|
157 i = 0, |
|
158 j, |
|
159 inverse, |
|
160 adjunct = [], |
|
161 //vector representing 2x2 matrix |
|
162 minor = []; |
|
163 if(len === 2) |
|
164 { |
|
165 determinant = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]; |
|
166 inverse = [ |
|
167 [matrix[1][1] * determinant, -matrix[1][0] * determinant], |
|
168 [-matrix[0][1] * determinant, matrix[0][0] * determinant] |
|
169 ]; |
|
170 } |
|
171 else |
|
172 { |
|
173 determinant = MatrixUtil.getDeterminant(matrix); |
|
174 for(; i < len; ++i) |
|
175 { |
|
176 adjunct[i] = []; |
|
177 for(j = 0; j < len; ++j) |
|
178 { |
|
179 minor = MatrixUtil.getMinors(matrix, j, i); |
|
180 adjunct[i][j] = MatrixUtil.getDeterminant(minor); |
|
181 if((i + j) % 2 !== 0 && (i + j) !== 0) |
|
182 { |
|
183 adjunct[i][j] *= -1; |
|
184 } |
|
185 } |
|
186 } |
|
187 inverse = MatrixUtil.scalarMultiply(adjunct, 1/determinant); |
|
188 } |
|
189 return inverse; |
|
190 }, |
|
191 |
|
192 /** |
|
193 * Multiplies a matrix by a numeric value. |
|
194 * |
|
195 * @method scalarMultiply |
|
196 * @param {Array} matrix The matrix to be altered. |
|
197 * @param {Number} multiplier The number to multiply against the matrix. |
|
198 * @return Array |
|
199 */ |
|
200 scalarMultiply: function(matrix, multiplier) |
|
201 { |
|
202 var i = 0, |
|
203 j, |
|
204 len = matrix.length; |
|
205 for(; i < len; ++i) |
|
206 { |
|
207 for(j = 0; j < len; ++j) |
|
208 { |
|
209 matrix[i][j] = MatrixUtil._round(matrix[i][j] * multiplier); |
|
210 } |
|
211 } |
|
212 return matrix; |
|
213 }, |
|
214 |
|
215 /** |
|
216 * Returns the transpose for an nxn matrix. |
|
217 * |
|
218 * @method transpose |
|
219 * @param matrix An nxn matrix represented by an array of vector arrays. |
|
220 * @return Array |
|
221 */ |
|
222 transpose: function(matrix) |
|
223 { |
|
224 var len = matrix.length, |
|
225 i = 0, |
|
226 j = 0, |
|
227 transpose = []; |
|
228 for(; i < len; ++i) |
|
229 { |
|
230 transpose[i] = []; |
|
231 for(j = 0; j < len; ++j) |
|
232 { |
|
233 transpose[i].push(matrix[j][i]); |
|
234 } |
|
235 } |
|
236 return transpose; |
|
237 }, |
|
238 |
|
239 /** |
|
240 * Returns a matrix of minors based on a matrix, column index and row index. |
|
241 * |
|
242 * @method getMinors |
|
243 * @param {Array} matrix The matrix from which to extract the matrix of minors. |
|
244 * @param {Number} columnIndex A zero-based index representing the specified column to exclude. |
|
245 * @param {Number} rowIndex A zero-based index represeenting the specified row to exclude. |
|
246 * @return Array |
|
247 */ |
|
248 getMinors: function(matrix, columnIndex, rowIndex) |
|
249 { |
|
250 var minors = [], |
|
251 len = matrix.length, |
|
252 i = 0, |
|
253 j, |
|
254 column; |
|
255 for(; i < len; ++i) |
|
256 { |
|
257 if(i !== columnIndex) |
|
258 { |
|
259 column = []; |
|
260 for(j = 0; j < len; ++j) |
|
261 { |
|
262 if(j !== rowIndex) |
|
263 { |
|
264 column.push(matrix[i][j]); |
|
265 } |
|
266 } |
|
267 minors.push(column); |
|
268 } |
|
269 } |
|
270 return minors; |
|
271 }, |
|
272 |
|
273 /** |
|
274 * Returns the sign of value |
|
275 * |
|
276 * @method sign |
|
277 * @param {Number} val value to be interpreted |
|
278 * @return Number |
|
279 */ |
|
280 sign: function(val) |
|
281 { |
|
282 return val === 0 ? 1 : val/Math.abs(val); |
|
283 }, |
|
284 |
|
285 /** |
|
286 * Multiplies a vector and a matrix |
|
287 * |
|
288 * @method vectorMatrixProduct |
|
289 * @param {Array} vector Array representing a column vector |
|
290 * @param {Array} matrix Array representing an nxn matrix |
|
291 * @return Array |
|
292 */ |
|
293 vectorMatrixProduct: function(vector, matrix) |
|
294 { |
|
295 var i, |
|
296 j, |
|
297 len = vector.length, |
|
298 product = [], |
|
299 rowProduct; |
|
300 for(i = 0; i < len; ++i) |
|
301 { |
|
302 rowProduct = 0; |
|
303 for(j = 0; j < len; ++j) |
|
304 { |
|
305 rowProduct += vector[i] * matrix[i][j]; |
|
306 } |
|
307 product[i] = rowProduct; |
|
308 } |
|
309 return product; |
|
310 }, |
|
311 |
|
312 /** |
|
313 * Breaks up a 2d transform matrix into a series of transform operations. |
|
314 * |
|
315 * @method decompose |
|
316 * @param {Array} 3x3 matrix array |
|
317 * @return Array |
|
318 */ |
|
319 decompose: function(matrix) |
|
320 { |
|
321 var a = parseFloat(matrix[0][0]), |
|
322 b = parseFloat(matrix[1][0]), |
|
323 c = parseFloat(matrix[0][1]), |
|
324 d = parseFloat(matrix[1][1]), |
|
325 dx = parseFloat(matrix[0][2]), |
|
326 dy = parseFloat(matrix[1][2]), |
|
327 rotate, |
|
328 sx, |
|
329 sy, |
|
330 shear; |
|
331 if((a * d - b * c) === 0) |
|
332 { |
|
333 return false; |
|
334 } |
|
335 //get length of vector(ab) |
|
336 sx = MatrixUtil._round(Math.sqrt(a * a + b * b)); |
|
337 //normalize components of vector(ab) |
|
338 a /= sx; |
|
339 b /= sx; |
|
340 shear = MatrixUtil._round(a * c + b * d); |
|
341 c -= a * shear; |
|
342 d -= b * shear; |
|
343 //get length of vector(cd) |
|
344 sy = MatrixUtil._round(Math.sqrt(c * c + d * d)); |
|
345 //normalize components of vector(cd) |
|
346 c /= sy; |
|
347 d /= sy; |
|
348 shear /=sy; |
|
349 shear = MatrixUtil._round(MatrixUtil.rad2deg(Math.atan(shear))); |
|
350 rotate = MatrixUtil._round(MatrixUtil.rad2deg(Math.atan2(matrix[1][0], matrix[0][0]))); |
|
351 |
|
352 return [ |
|
353 ["translate", dx, dy], |
|
354 ["rotate", rotate], |
|
355 ["skewX", shear], |
|
356 ["scale", sx, sy] |
|
357 ]; |
|
358 }, |
|
359 |
|
360 /** |
|
361 * Parses a transform string and returns an array of transform arrays. |
|
362 * |
|
363 * @method getTransformArray |
|
364 * @param {String} val A transform string |
|
365 * @return Array |
|
366 */ |
|
367 getTransformArray: function(transform) { |
|
368 var re = /\s*([a-z]*)\(([\w,\.,\-,\s]*)\)/gi, |
|
369 transforms = [], |
|
370 args, |
|
371 m, |
|
372 decomp, |
|
373 methods = MatrixUtil.transformMethods; |
|
374 |
|
375 while ((m = re.exec(transform))) { |
|
376 if (methods.hasOwnProperty(m[1])) |
|
377 { |
|
378 args = m[2].split(','); |
|
379 args.unshift(m[1]); |
|
380 transforms.push(args); |
|
381 } |
|
382 else if(m[1] == "matrix") |
|
383 { |
|
384 args = m[2].split(','); |
|
385 decomp = MatrixUtil.decompose([ |
|
386 [args[0], args[2], args[4]], |
|
387 [args[1], args[3], args[5]], |
|
388 [0, 0, 1] |
|
389 ]); |
|
390 transforms.push(decomp[0]); |
|
391 transforms.push(decomp[1]); |
|
392 transforms.push(decomp[2]); |
|
393 transforms.push(decomp[3]); |
|
394 } |
|
395 } |
|
396 return transforms; |
|
397 }, |
|
398 |
|
399 /** |
|
400 * Returns an array of transform arrays representing transform functions and arguments. |
|
401 * |
|
402 * @method getTransformFunctionArray |
|
403 * @return Array |
|
404 */ |
|
405 getTransformFunctionArray: function(transform) { |
|
406 var list; |
|
407 switch(transform) |
|
408 { |
|
409 case "skew" : |
|
410 list = [transform, 0, 0]; |
|
411 break; |
|
412 case "scale" : |
|
413 list = [transform, 1, 1]; |
|
414 break; |
|
415 case "scaleX" : |
|
416 list = [transform, 1]; |
|
417 break; |
|
418 case "scaleY" : |
|
419 list = [transform, 1]; |
|
420 break; |
|
421 case "translate" : |
|
422 list = [transform, 0, 0]; |
|
423 break; |
|
424 default : |
|
425 list = [transform, 0]; |
|
426 break; |
|
427 } |
|
428 return list; |
|
429 }, |
|
430 |
|
431 /** |
|
432 * Compares to arrays or transform functions to ensure both contain the same functions in the same |
|
433 * order. |
|
434 * |
|
435 * @method compareTransformSequence |
|
436 * @param {Array} list1 Array to compare |
|
437 * @param {Array} list2 Array to compare |
|
438 * @return Boolean |
|
439 */ |
|
440 compareTransformSequence: function(list1, list2) |
|
441 { |
|
442 var i = 0, |
|
443 len = list1.length, |
|
444 len2 = list2.length, |
|
445 isEqual = len === len2; |
|
446 if(isEqual) |
|
447 { |
|
448 for(; i < len; ++i) |
|
449 { |
|
450 if(list1[i][0] != list2[i][0]) |
|
451 { |
|
452 isEqual = false; |
|
453 break; |
|
454 } |
|
455 } |
|
456 } |
|
457 return isEqual; |
|
458 }, |
|
459 |
|
460 /** |
|
461 * Mapping of possible transform method names. |
|
462 * |
|
463 * @property transformMethods |
|
464 * @type Object |
|
465 */ |
|
466 transformMethods: { |
|
467 rotate: "rotate", |
|
468 skew: "skew", |
|
469 skewX: "skewX", |
|
470 skewY: "skewY", |
|
471 translate: "translate", |
|
472 translateX: "translateX", |
|
473 translateY: "tranlsateY", |
|
474 scale: "scale", |
|
475 scaleX: "scaleX", |
|
476 scaleY: "scaleY" |
|
477 } |
|
478 |
|
479 }; |
|
480 |
|
481 Y.MatrixUtil = MatrixUtil; |
|
482 |
|
483 /** |
|
484 * Matrix is a class that allows for the manipulation of a transform matrix. |
|
485 * This class is a work in progress. |
|
486 * |
|
487 * @class Matrix |
|
488 * @constructor |
|
489 * @module matrix |
|
490 */ |
|
491 var Matrix = function(config) { |
|
492 this.init(config); |
|
493 }; |
|
494 |
|
495 Matrix.prototype = { |
|
496 /** |
|
497 * Used as value for the _rounding method. |
|
498 * |
|
499 * @property _rounder |
|
500 * @private |
|
501 */ |
|
502 _rounder: 100000, |
|
503 |
|
504 /** |
|
505 * Updates the matrix. |
|
506 * |
|
507 * @method multiple |
|
508 * @param {Number} a |
|
509 * @param {Number} b |
|
510 * @param {Number} c |
|
511 * @param {Number} d |
|
512 * @param {Number} dx |
|
513 * @param {Number} dy |
|
514 */ |
|
515 multiply: function(a, b, c, d, dx, dy) { |
|
516 var matrix = this, |
|
517 matrix_a = matrix.a * a + matrix.c * b, |
|
518 matrix_b = matrix.b * a + matrix.d * b, |
|
519 matrix_c = matrix.a * c + matrix.c * d, |
|
520 matrix_d = matrix.b * c + matrix.d * d, |
|
521 matrix_dx = matrix.a * dx + matrix.c * dy + matrix.dx, |
|
522 matrix_dy = matrix.b * dx + matrix.d * dy + matrix.dy; |
|
523 |
|
524 matrix.a = this._round(matrix_a); |
|
525 matrix.b = this._round(matrix_b); |
|
526 matrix.c = this._round(matrix_c); |
|
527 matrix.d = this._round(matrix_d); |
|
528 matrix.dx = this._round(matrix_dx); |
|
529 matrix.dy = this._round(matrix_dy); |
|
530 return this; |
|
531 }, |
|
532 |
|
533 /** |
|
534 * Parses a string and updates the matrix. |
|
535 * |
|
536 * @method applyCSSText |
|
537 * @param {String} val A css transform string |
|
538 */ |
|
539 applyCSSText: function(val) { |
|
540 var re = /\s*([a-z]*)\(([\w,\.,\-,\s]*)\)/gi, |
|
541 args, |
|
542 m; |
|
543 |
|
544 val = val.replace(/matrix/g, "multiply"); |
|
545 while ((m = re.exec(val))) { |
|
546 if (typeof this[m[1]] === 'function') { |
|
547 args = m[2].split(','); |
|
548 this[m[1]].apply(this, args); |
|
549 } |
|
550 } |
|
551 }, |
|
552 |
|
553 /** |
|
554 * Parses a string and returns an array of transform arrays. |
|
555 * |
|
556 * @method getTransformArray |
|
557 * @param {String} val A css transform string |
|
558 * @return Array |
|
559 */ |
|
560 getTransformArray: function(val) { |
|
561 var re = /\s*([a-z]*)\(([\w,\.,\-,\s]*)\)/gi, |
|
562 transforms = [], |
|
563 args, |
|
564 m; |
|
565 |
|
566 val = val.replace(/matrix/g, "multiply"); |
|
567 while ((m = re.exec(val))) { |
|
568 if (typeof this[m[1]] === 'function') { |
|
569 args = m[2].split(','); |
|
570 args.unshift(m[1]); |
|
571 transforms.push(args); |
|
572 } |
|
573 } |
|
574 return transforms; |
|
575 }, |
|
576 |
|
577 /** |
|
578 * Default values for the matrix |
|
579 * |
|
580 * @property _defaults |
|
581 * @private |
|
582 */ |
|
583 _defaults: { |
|
584 a: 1, |
|
585 b: 0, |
|
586 c: 0, |
|
587 d: 1, |
|
588 dx: 0, |
|
589 dy: 0 |
|
590 }, |
|
591 |
|
592 /** |
|
593 * Rounds values |
|
594 * |
|
595 * @method _round |
|
596 * @private |
|
597 */ |
|
598 _round: function(val) { |
|
599 val = Math.round(val * this._rounder) / this._rounder; |
|
600 return val; |
|
601 }, |
|
602 |
|
603 /** |
|
604 * Initializes a matrix. |
|
605 * |
|
606 * @method init |
|
607 * @param {Object} config Specified key value pairs for matrix properties. If a property is not explicitly defined in the config argument, |
|
608 * the default value will be used. |
|
609 */ |
|
610 init: function(config) { |
|
611 var defaults = this._defaults, |
|
612 prop; |
|
613 |
|
614 config = config || {}; |
|
615 |
|
616 for (prop in defaults) { |
|
617 if(defaults.hasOwnProperty(prop)) |
|
618 { |
|
619 this[prop] = (prop in config) ? config[prop] : defaults[prop]; |
|
620 } |
|
621 } |
|
622 |
|
623 this._config = config; |
|
624 }, |
|
625 |
|
626 /** |
|
627 * Applies a scale transform |
|
628 * |
|
629 * @method scale |
|
630 * @param {Number} val |
|
631 */ |
|
632 scale: function(x, y) { |
|
633 this.multiply(x, 0, 0, y, 0, 0); |
|
634 return this; |
|
635 }, |
|
636 |
|
637 /** |
|
638 * Applies a skew transformation. |
|
639 * |
|
640 * @method skew |
|
641 * @param {Number} x The value to skew on the x-axis. |
|
642 * @param {Number} y The value to skew on the y-axis. |
|
643 */ |
|
644 skew: function(x, y) { |
|
645 x = x || 0; |
|
646 y = y || 0; |
|
647 |
|
648 if (x !== undefined) { // null or undef |
|
649 x = Math.tan(this.angle2rad(x)); |
|
650 |
|
651 } |
|
652 |
|
653 if (y !== undefined) { // null or undef |
|
654 y = Math.tan(this.angle2rad(y)); |
|
655 } |
|
656 |
|
657 this.multiply(1, y, x, 1, 0, 0); |
|
658 return this; |
|
659 }, |
|
660 |
|
661 /** |
|
662 * Applies a skew to the x-coordinate |
|
663 * |
|
664 * @method skewX |
|
665 * @param {Number} x x-coordinate |
|
666 */ |
|
667 skewX: function(x) { |
|
668 this.skew(x); |
|
669 return this; |
|
670 }, |
|
671 |
|
672 /** |
|
673 * Applies a skew to the y-coordinate |
|
674 * |
|
675 * @method skewY |
|
676 * @param {Number} y y-coordinate |
|
677 */ |
|
678 skewY: function(y) { |
|
679 this.skew(null, y); |
|
680 return this; |
|
681 }, |
|
682 |
|
683 /** |
|
684 * Returns a string of text that can be used to populate a the css transform property of an element. |
|
685 * |
|
686 * @method toCSSText |
|
687 * @return String |
|
688 */ |
|
689 toCSSText: function() { |
|
690 var matrix = this, |
|
691 text = 'matrix(' + |
|
692 matrix.a + ',' + |
|
693 matrix.b + ',' + |
|
694 matrix.c + ',' + |
|
695 matrix.d + ',' + |
|
696 matrix.dx + ',' + |
|
697 matrix.dy + ')'; |
|
698 return text; |
|
699 }, |
|
700 |
|
701 /** |
|
702 * Returns a string that can be used to populate the css filter property of an element. |
|
703 * |
|
704 * @method toFilterText |
|
705 * @return String |
|
706 */ |
|
707 toFilterText: function() { |
|
708 var matrix = this, |
|
709 text = 'progid:DXImageTransform.Microsoft.Matrix('; |
|
710 text += 'M11=' + matrix.a + ',' + |
|
711 'M21=' + matrix.b + ',' + |
|
712 'M12=' + matrix.c + ',' + |
|
713 'M22=' + matrix.d + ',' + |
|
714 'sizingMethod="auto expand")'; |
|
715 |
|
716 text += ''; |
|
717 |
|
718 return text; |
|
719 }, |
|
720 |
|
721 /** |
|
722 * Converts a radian value to a degree. |
|
723 * |
|
724 * @method rad2deg |
|
725 * @param {Number} rad Radian value to be converted. |
|
726 * @return Number |
|
727 */ |
|
728 rad2deg: function(rad) { |
|
729 var deg = rad * (180 / Math.PI); |
|
730 return deg; |
|
731 }, |
|
732 |
|
733 /** |
|
734 * Converts a degree value to a radian. |
|
735 * |
|
736 * @method deg2rad |
|
737 * @param {Number} deg Degree value to be converted to radian. |
|
738 * @return Number |
|
739 */ |
|
740 deg2rad: function(deg) { |
|
741 var rad = deg * (Math.PI / 180); |
|
742 return rad; |
|
743 }, |
|
744 |
|
745 angle2rad: function(val) { |
|
746 if (typeof val === 'string' && val.indexOf('rad') > -1) { |
|
747 val = parseFloat(val); |
|
748 } else { // default to deg |
|
749 val = this.deg2rad(parseFloat(val)); |
|
750 } |
|
751 |
|
752 return val; |
|
753 }, |
|
754 |
|
755 /** |
|
756 * Applies a rotate transform. |
|
757 * |
|
758 * @method rotate |
|
759 * @param {Number} deg The degree of the rotation. |
|
760 */ |
|
761 rotate: function(deg, x, y) { |
|
762 var rad = this.angle2rad(deg), |
|
763 sin = Math.sin(rad), |
|
764 cos = Math.cos(rad); |
|
765 this.multiply(cos, sin, 0 - sin, cos, 0, 0); |
|
766 return this; |
|
767 }, |
|
768 |
|
769 /** |
|
770 * Applies translate transformation. |
|
771 * |
|
772 * @method translate |
|
773 * @param {Number} x The value to transate on the x-axis. |
|
774 * @param {Number} y The value to translate on the y-axis. |
|
775 */ |
|
776 translate: function(x, y) { |
|
777 x = parseFloat(x) || 0; |
|
778 y = parseFloat(y) || 0; |
|
779 this.multiply(1, 0, 0, 1, x, y); |
|
780 return this; |
|
781 }, |
|
782 |
|
783 /** |
|
784 * Applies a translate to the x-coordinate |
|
785 * |
|
786 * @method translateX |
|
787 * @param {Number} x x-coordinate |
|
788 */ |
|
789 translateX: function(x) { |
|
790 this.translate(x); |
|
791 return this; |
|
792 }, |
|
793 |
|
794 /** |
|
795 * Applies a translate to the y-coordinate |
|
796 * |
|
797 * @method translateY |
|
798 * @param {Number} y y-coordinate |
|
799 */ |
|
800 translateY: function(y) { |
|
801 this.translate(null, y); |
|
802 return this; |
|
803 }, |
|
804 |
|
805 |
|
806 /** |
|
807 * Returns an identity matrix. |
|
808 * |
|
809 * @method identity |
|
810 * @return Object |
|
811 */ |
|
812 identity: function() { |
|
813 var config = this._config, |
|
814 defaults = this._defaults, |
|
815 prop; |
|
816 |
|
817 for (prop in config) { |
|
818 if (prop in defaults) { |
|
819 this[prop] = defaults[prop]; |
|
820 } |
|
821 } |
|
822 return this; |
|
823 }, |
|
824 |
|
825 /** |
|
826 * Returns a 3x3 Matrix array |
|
827 * |
|
828 * / \ |
|
829 * | matrix[0][0] matrix[1][0] matrix[2][0] | |
|
830 * | matrix[0][1] matrix[1][1] matrix[2][1] | |
|
831 * | matrix[0][2] matrix[1][2] matrix[2][2] | |
|
832 * \ / |
|
833 * |
|
834 * @method getMatrixArray |
|
835 * @return Array |
|
836 */ |
|
837 getMatrixArray: function() |
|
838 { |
|
839 var matrix = this, |
|
840 matrixArray = [ |
|
841 [matrix.a, matrix.c, matrix.dx], |
|
842 [matrix.b, matrix.d, matrix.dy], |
|
843 [0, 0, 1] |
|
844 ]; |
|
845 return matrixArray; |
|
846 }, |
|
847 |
|
848 /** |
|
849 * Returns the left, top, right and bottom coordinates for a transformed |
|
850 * item. |
|
851 * |
|
852 * @method getContentRect |
|
853 * @param {Number} width The width of the item. |
|
854 * @param {Number} height The height of the item. |
|
855 * @param {Number} x The x-coordinate of the item. |
|
856 * @param {Number} y The y-coordinate of the item. |
|
857 * @return Object |
|
858 */ |
|
859 getContentRect: function(width, height, x, y) |
|
860 { |
|
861 var left = !isNaN(x) ? x : 0, |
|
862 top = !isNaN(y) ? y : 0, |
|
863 right = left + width, |
|
864 bottom = top + height, |
|
865 matrix = this, |
|
866 a = matrix.a, |
|
867 b = matrix.b, |
|
868 c = matrix.c, |
|
869 d = matrix.d, |
|
870 dx = matrix.dx, |
|
871 dy = matrix.dy, |
|
872 x1 = (a * left + c * top + dx), |
|
873 y1 = (b * left + d * top + dy), |
|
874 //[x2, y2] |
|
875 x2 = (a * right + c * top + dx), |
|
876 y2 = (b * right + d * top + dy), |
|
877 //[x3, y3] |
|
878 x3 = (a * left + c * bottom + dx), |
|
879 y3 = (b * left + d * bottom + dy), |
|
880 //[x4, y4] |
|
881 x4 = (a * right + c * bottom + dx), |
|
882 y4 = (b * right + d * bottom + dy); |
|
883 return { |
|
884 left: Math.min(x3, Math.min(x1, Math.min(x2, x4))), |
|
885 right: Math.max(x3, Math.max(x1, Math.max(x2, x4))), |
|
886 top: Math.min(y2, Math.min(y4, Math.min(y3, y1))), |
|
887 bottom: Math.max(y2, Math.max(y4, Math.max(y3, y1))) |
|
888 }; |
|
889 }, |
|
890 |
|
891 /** |
|
892 * Returns the determinant of the matrix. |
|
893 * |
|
894 * @method getDeterminant |
|
895 * @return Number |
|
896 */ |
|
897 getDeterminant: function() |
|
898 { |
|
899 return Y.MatrixUtil.getDeterminant(this.getMatrixArray()); |
|
900 }, |
|
901 |
|
902 /** |
|
903 * Returns the inverse (in array form) of the matrix. |
|
904 * |
|
905 * @method inverse |
|
906 * @return Array |
|
907 */ |
|
908 inverse: function() |
|
909 { |
|
910 return Y.MatrixUtil.inverse(this.getMatrixArray()); |
|
911 }, |
|
912 |
|
913 /** |
|
914 * Returns the transpose of the matrix |
|
915 * |
|
916 * @method transpose |
|
917 * @return Array |
|
918 */ |
|
919 transpose: function() |
|
920 { |
|
921 return Y.MatrixUtil.transpose(this.getMatrixArray()); |
|
922 }, |
|
923 |
|
924 /** |
|
925 * Returns an array of transform commands that represent the matrix. |
|
926 * |
|
927 * @method decompose |
|
928 * @return Array |
|
929 */ |
|
930 decompose: function() |
|
931 { |
|
932 return Y.MatrixUtil.decompose(this.getMatrixArray()); |
|
933 } |
|
934 }; |
|
935 |
|
936 Y.Matrix = Matrix; |
|
937 |
|
938 |
|
939 }, '3.10.3', {"requires": ["yui-base"]}); |