Matrix.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-draw-Matrix'>/**
  19. </span> * @private
  20. */
  21. Ext.define('Ext.draw.Matrix', {
  22. /* Begin Definitions */
  23. requires: ['Ext.draw.Draw'],
  24. /* End Definitions */
  25. constructor: function(a, b, c, d, e, f) {
  26. if (a != null) {
  27. this.matrix = [[a, c, e], [b, d, f], [0, 0, 1]];
  28. }
  29. else {
  30. this.matrix = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
  31. }
  32. },
  33. add: function(a, b, c, d, e, f) {
  34. var me = this,
  35. out = [[], [], []],
  36. matrix = [[a, c, e], [b, d, f], [0, 0, 1]],
  37. x,
  38. y,
  39. z,
  40. res;
  41. for (x = 0; x &lt; 3; x++) {
  42. for (y = 0; y &lt; 3; y++) {
  43. res = 0;
  44. for (z = 0; z &lt; 3; z++) {
  45. res += me.matrix[x][z] * matrix[z][y];
  46. }
  47. out[x][y] = res;
  48. }
  49. }
  50. me.matrix = out;
  51. },
  52. prepend: function(a, b, c, d, e, f) {
  53. var me = this,
  54. out = [[], [], []],
  55. matrix = [[a, c, e], [b, d, f], [0, 0, 1]],
  56. x,
  57. y,
  58. z,
  59. res;
  60. for (x = 0; x &lt; 3; x++) {
  61. for (y = 0; y &lt; 3; y++) {
  62. res = 0;
  63. for (z = 0; z &lt; 3; z++) {
  64. res += matrix[x][z] * me.matrix[z][y];
  65. }
  66. out[x][y] = res;
  67. }
  68. }
  69. me.matrix = out;
  70. },
  71. invert: function() {
  72. var matrix = this.matrix,
  73. a = matrix[0][0],
  74. b = matrix[1][0],
  75. c = matrix[0][1],
  76. d = matrix[1][1],
  77. e = matrix[0][2],
  78. f = matrix[1][2],
  79. x = a * d - b * c;
  80. return new Ext.draw.Matrix(d / x, -b / x, -c / x, a / x, (c * f - d * e) / x, (b * e - a * f) / x);
  81. },
  82. clone: function() {
  83. var matrix = this.matrix,
  84. a = matrix[0][0],
  85. b = matrix[1][0],
  86. c = matrix[0][1],
  87. d = matrix[1][1],
  88. e = matrix[0][2],
  89. f = matrix[1][2];
  90. return new Ext.draw.Matrix(a, b, c, d, e, f);
  91. },
  92. translate: function(x, y) {
  93. this.prepend(1, 0, 0, 1, x, y);
  94. },
  95. scale: function(x, y, cx, cy) {
  96. var me = this;
  97. if (y == null) {
  98. y = x;
  99. }
  100. me.add(x, 0, 0, y, cx * (1 - x), cy * (1 - y));
  101. },
  102. rotate: function(a, x, y) {
  103. a = Ext.draw.Draw.rad(a);
  104. var me = this,
  105. cos = +Math.cos(a).toFixed(9),
  106. sin = +Math.sin(a).toFixed(9);
  107. me.add(cos, sin, -sin, cos, x - cos * x + sin * y, -(sin * x) + y - cos * y);
  108. },
  109. x: function(x, y) {
  110. var matrix = this.matrix;
  111. return x * matrix[0][0] + y * matrix[0][1] + matrix[0][2];
  112. },
  113. y: function(x, y) {
  114. var matrix = this.matrix;
  115. return x * matrix[1][0] + y * matrix[1][1] + matrix[1][2];
  116. },
  117. get: function(i, j) {
  118. return + this.matrix[i][j].toFixed(4);
  119. },
  120. toString: function() {
  121. var me = this;
  122. return [me.get(0, 0), me.get(0, 1), me.get(1, 0), me.get(1, 1), 0, 0].join();
  123. },
  124. toSvg: function() {
  125. var me = this;
  126. return &quot;matrix(&quot; + [me.get(0, 0), me.get(1, 0), me.get(0, 1), me.get(1, 1), me.get(0, 2), me.get(1, 2)].join() + &quot;)&quot;;
  127. },
  128. toFilter: function(dx, dy) {
  129. var me = this;
  130. dx = dx || 0;
  131. dy = dy || 0;
  132. return &quot;progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', filterType='bilinear', M11=&quot; + me.get(0, 0) +
  133. &quot;, M12=&quot; + me.get(0, 1) + &quot;, M21=&quot; + me.get(1, 0) + &quot;, M22=&quot; + me.get(1, 1) +
  134. &quot;, Dx=&quot; + (me.get(0, 2) + dx) + &quot;, Dy=&quot; + (me.get(1, 2) + dy) + &quot;)&quot;;
  135. },
  136. offset: function() {
  137. var matrix = this.matrix;
  138. return [(matrix[0][2] || 0).toFixed(4), (matrix[1][2] || 0).toFixed(4)];
  139. },
  140. // Split matrix into Translate Scale, Shear, and Rotate
  141. split: function () {
  142. function norm(a) {
  143. return a[0] * a[0] + a[1] * a[1];
  144. }
  145. function normalize(a) {
  146. var mag = Math.sqrt(norm(a));
  147. a[0] /= mag;
  148. a[1] /= mag;
  149. }
  150. var matrix = this.matrix,
  151. out = {
  152. translateX: matrix[0][2],
  153. translateY: matrix[1][2]
  154. },
  155. row;
  156. // scale and shear
  157. row = [[matrix[0][0], matrix[0][1]], [matrix[1][1], matrix[1][1]]];
  158. out.scaleX = Math.sqrt(norm(row[0]));
  159. normalize(row[0]);
  160. out.shear = row[0][0] * row[1][0] + row[0][1] * row[1][1];
  161. row[1] = [row[1][0] - row[0][0] * out.shear, row[1][1] - row[0][1] * out.shear];
  162. out.scaleY = Math.sqrt(norm(row[1]));
  163. normalize(row[1]);
  164. out.shear /= out.scaleY;
  165. // rotation
  166. out.rotate = Math.asin(-row[0][1]);
  167. out.isSimple = !+out.shear.toFixed(9) &amp;&amp; (out.scaleX.toFixed(9) == out.scaleY.toFixed(9) || !out.rotate);
  168. return out;
  169. }
  170. });
  171. </pre>
  172. </body>
  173. </html>