transform.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* transformjs
  2. * By dntzhang
  3. */
  4. ;(function () {
  5. var Matrix3D = function (n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44) {
  6. this.elements =window.Float32Array ? new Float32Array(16) : [];
  7. var te = this.elements;
  8. te[0] = (n11 !== undefined) ? n11 : 1; te[4] = n12 || 0; te[8] = n13 || 0; te[12] = n14 || 0;
  9. te[1] = n21 || 0; te[5] = (n22 !== undefined) ? n22 : 1; te[9] = n23 || 0; te[13] = n24 || 0;
  10. te[2] = n31 || 0; te[6] = n32 || 0; te[10] = (n33 !== undefined) ? n33 : 1; te[14] = n34 || 0;
  11. te[3] = n41 || 0; te[7] = n42 || 0; te[11] = n43 || 0; te[15] = (n44 !== undefined) ? n44 : 1;
  12. };
  13. Matrix3D.DEG_TO_RAD = Math.PI / 180;
  14. Matrix3D.prototype = {
  15. set: function (n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44) {
  16. var te = this.elements;
  17. te[0] = n11; te[4] = n12; te[8] = n13; te[12] = n14;
  18. te[1] = n21; te[5] = n22; te[9] = n23; te[13] = n24;
  19. te[2] = n31; te[6] = n32; te[10] = n33; te[14] = n34;
  20. te[3] = n41; te[7] = n42; te[11] = n43; te[15] = n44;
  21. return this;
  22. },
  23. identity: function () {
  24. this.set(
  25. 1, 0, 0, 0,
  26. 0, 1, 0, 0,
  27. 0, 0, 1, 0,
  28. 0, 0, 0, 1
  29. );
  30. return this;
  31. },
  32. multiplyMatrices: function (a, be) {
  33. var ae = a.elements;
  34. var te = this.elements;
  35. var a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
  36. var a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
  37. var a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
  38. var a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
  39. var b11 = be[0], b12 = be[1], b13 = be[2], b14 = be[3];
  40. var b21 = be[4], b22 = be[5], b23 = be[6], b24 = be[7];
  41. var b31 = be[8], b32 = be[9], b33 = be[10], b34 = be[11];
  42. var b41 = be[12], b42 = be[13], b43 = be[14], b44 = be[15];
  43. te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  44. te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  45. te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  46. te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  47. te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  48. te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  49. te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  50. te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  51. te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  52. te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  53. te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  54. te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  55. te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  56. te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  57. te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  58. te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  59. return this;
  60. },
  61. // 解决角度为90的整数倍导致Math.cos得到极小的数,其实是0。导致不渲染
  62. _rounded: function(value,i){
  63. i= Math.pow(10, i || 15);
  64. // default
  65. return Math.round(value*i)/i;
  66. },
  67. appendTransform: function (x, y, z, scaleX, scaleY, scaleZ, rotateX, rotateY, rotateZ,skewX,skewY, originX, originY, originZ) {
  68. var rx = rotateX * Matrix3D.DEG_TO_RAD;
  69. var cosx =this._rounded( Math.cos(rx));
  70. var sinx = this._rounded(Math.sin(rx));
  71. var ry = rotateY * Matrix3D.DEG_TO_RAD;
  72. var cosy =this._rounded( Math.cos(ry));
  73. var siny = this._rounded(Math.sin(ry));
  74. var rz = rotateZ * Matrix3D.DEG_TO_RAD;
  75. var cosz =this._rounded( Math.cos(rz * -1));
  76. var sinz =this._rounded( Math.sin(rz * -1));
  77. this.multiplyMatrices(this, [
  78. 1, 0, 0, x,
  79. 0, cosx, sinx, y,
  80. 0, -sinx, cosx, z,
  81. 0, 0, 0, 1
  82. ]);
  83. this.multiplyMatrices(this, [
  84. cosy, 0, siny, 0,
  85. 0, 1, 0, 0,
  86. -siny, 0, cosy, 0,
  87. 0, 0, 0, 1
  88. ]);
  89. this.multiplyMatrices(this,[
  90. cosz * scaleX, sinz * scaleY, 0, 0,
  91. -sinz * scaleX, cosz * scaleY, 0, 0,
  92. 0, 0, 1 * scaleZ, 0,
  93. 0, 0, 0, 1
  94. ]);
  95. if(skewX||skewY){
  96. this.multiplyMatrices(this,[
  97. this._rounded(Math.cos(skewX* Matrix3D.DEG_TO_RAD)), this._rounded( Math.sin(skewX* Matrix3D.DEG_TO_RAD)), 0, 0,
  98. -1*this._rounded(Math.sin(skewY* Matrix3D.DEG_TO_RAD)), this._rounded( Math.cos(skewY* Matrix3D.DEG_TO_RAD)), 0, 0,
  99. 0, 0, 1, 0,
  100. 0, 0, 0, 1
  101. ]);
  102. }
  103. if (originX || originY || originZ) {
  104. this.elements[12] -= originX * this.elements[0] + originY * this.elements[4] + originZ * this.elements[8];
  105. this.elements[13] -= originX * this.elements[1] + originY * this.elements[5] + originZ * this.elements[9];
  106. this.elements[14] -= originX * this.elements[2] + originY * this.elements[6] + originZ * this.elements[10];
  107. }
  108. return this;
  109. }
  110. };
  111. function observe(target, props, callback) {
  112. for (var i = 0, len = props.length; i < len; i++) {
  113. var prop = props[i];
  114. watch(target, prop, callback);
  115. }
  116. }
  117. function watch(target, prop, callback) {
  118. Object.defineProperty(target, prop, {
  119. get: function () {
  120. return this["__" + prop];
  121. },
  122. set: function (value) {
  123. if (value !== this["__" + prop]) {
  124. this["__" + prop] = value;
  125. callback();
  126. }
  127. }
  128. });
  129. }
  130. window.Transform = function (element) {
  131. observe(
  132. element,
  133. ["translateX", "translateY", "translateZ", "scaleX", "scaleY", "scaleZ" , "rotateX", "rotateY", "rotateZ","skewX","skewY", "originX", "originY", "originZ"],
  134. function () {
  135. var mtx = element.matrix3D.identity().appendTransform( element.translateX, element.translateY, element.translateZ, element.scaleX, element.scaleY, element.scaleZ, element.rotateX, element.rotateY, element.rotateZ,element.skewX,element.skewY, element.originX, element.originY, element.originZ);
  136. element.style.transform = element.style.msTransform = element.style.OTransform = element.style.MozTransform = element.style.webkitTransform = "perspective("+element.perspective+"px) matrix3d(" + Array.prototype.slice.call(mtx.elements).join(",") + ")";
  137. });
  138. observe(
  139. element,
  140. [ "perspective"],
  141. function () {
  142. element.style.transform = element.style.msTransform = element.style.OTransform = element.style.MozTransform = element.style.webkitTransform = "perspective("+element.perspective+"px) matrix3d(" + Array.prototype.slice.call(element.matrix3D.elements).join(",") + ")";
  143. });
  144. element.matrix3D = new Matrix3D();
  145. element.perspective = 500;
  146. element.scaleX = element.scaleY = element.scaleZ = 1;
  147. //由于image自带了x\y\z,所有加上translate前缀
  148. element.translateX = element.translateY = element.translateZ = element.rotateX = element.rotateY = element.rotateZ =element.skewX=element.skewY= element.originX = element.originY = element.originZ = 0;
  149. }
  150. })();