math_Matrix.js.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: math/Matrix.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: math/Matrix.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>"use strict";
  20. import {Vector2} from "./Vector2.js";
  21. /**
  22. * 2D 3x2 transformation matrix, applied to the canvas elements.
  23. *
  24. * @class
  25. */
  26. function Matrix(values)
  27. {
  28. if(values !== undefined)
  29. {
  30. this.m = values;
  31. }
  32. else
  33. {
  34. this.identity();
  35. }
  36. }
  37. /**
  38. * Copy the content of another matrix and store in this one.
  39. */
  40. Matrix.prototype.copy = function(mat)
  41. {
  42. this.m = mat.m.slice(0);
  43. };
  44. /**
  45. * Create a new matrix object with a copy of the content of this one.
  46. */
  47. Matrix.prototype.clone = function()
  48. {
  49. return new Matrix(this.m.slice(0))
  50. };
  51. /**
  52. * Reset this matrix to indentity.
  53. */
  54. Matrix.prototype.identity = function()
  55. {
  56. this.m = [1, 0, 0, 1, 0, 0];
  57. };
  58. /**
  59. * Multiply another matrix by this one and store the result.
  60. *
  61. * @param mat Matrix array.
  62. */
  63. Matrix.prototype.multiply = function(mat)
  64. {
  65. var m0 = this.m[0] * mat.m[0] + this.m[2] * mat.m[1];
  66. var m1 = this.m[1] * mat.m[0] + this.m[3] * mat.m[1];
  67. var m2 = this.m[0] * mat.m[2] + this.m[2] * mat.m[3];
  68. var m3 = this.m[1] * mat.m[2] + this.m[3] * mat.m[3];
  69. var m4 = this.m[0] * mat.m[4] + this.m[2] * mat.m[5] + this.m[4];
  70. var m5 = this.m[1] * mat.m[4] + this.m[3] * mat.m[5] + this.m[5];
  71. this.m = [m0, m1, m2, m3, m4, m5];
  72. };
  73. /**
  74. * Premultiply another matrix by this one and store the result.
  75. *
  76. * @param mat Matrix array to multiply.
  77. */
  78. Matrix.prototype.premultiply = function(mat)
  79. {
  80. var m0 = mat.m[0] * this.m[0] + mat.m[2] * this.m[1];
  81. var m1 = mat.m[1] * this.m[0] + mat.m[3] * this.m[1];
  82. var m2 = mat.m[0] * this.m[2] + mat.m[2] * this.m[3];
  83. var m3 = mat.m[1] * this.m[2] + mat.m[3] * this.m[3];
  84. var m4 = mat.m[0] * this.m[4] + mat.m[2] * this.m[5] + mat.m[4];
  85. var m5 = mat.m[1] * this.m[4] + mat.m[3] * this.m[5] + mat.m[5];
  86. this.m = [m0, m1, m2, m3, m4, m5];
  87. };
  88. /**
  89. * Compose this transformation matrix with position scale and rotation and origin point.
  90. */
  91. Matrix.prototype.compose = function(px, py, sx, sy, ox, oy, a)
  92. {
  93. this.m = [1, 0, 0, 1, px, py];
  94. if(a !== 0)
  95. {
  96. var c = Math.cos(a);
  97. var s = Math.sin(a);
  98. this.multiply(new Matrix([c, s, -s, c, 0, 0]));
  99. }
  100. if(ox !== 0 || oy !== 0)
  101. {
  102. this.multiply(new Matrix([1, 0, 0, 1, -ox, -oy]));
  103. }
  104. if(sx !== 1 || sy !== 1)
  105. {
  106. this.scale(sx, sy);
  107. }
  108. };
  109. /**
  110. * Apply translation to this matrix.
  111. */
  112. Matrix.prototype.translate = function(x, y)
  113. {
  114. this.m[4] += this.m[0] * x + this.m[2] * y;
  115. this.m[5] += this.m[1] * x + this.m[3] * y;
  116. };
  117. /**
  118. * Apply rotation to this matrix.
  119. *
  120. * @param angle Angle in radians.
  121. */
  122. Matrix.prototype.rotate = function(rad)
  123. {
  124. var c = Math.cos(rad);
  125. var s = Math.sin(rad);
  126. var m11 = this.m[0] * c + this.m[2] * s;
  127. var m12 = this.m[1] * c + this.m[3] * s;
  128. var m21 = this.m[0] * -s + this.m[2] * c;
  129. var m22 = this.m[1] * -s + this.m[3] * c;
  130. this.m[0] = m11;
  131. this.m[1] = m12;
  132. this.m[2] = m21;
  133. this.m[3] = m22;
  134. };
  135. /**
  136. * Apply scale to this matrix.
  137. */
  138. Matrix.prototype.scale = function(sx, sy)
  139. {
  140. this.m[0] *= sx;
  141. this.m[1] *= sx;
  142. this.m[2] *= sy;
  143. this.m[3] *= sy;
  144. };
  145. /**
  146. * Set the position of the transformation matrix.
  147. */
  148. Matrix.prototype.setPosition = function(x, y)
  149. {
  150. this.m[4] = x;
  151. this.m[5] = y;
  152. };
  153. /**
  154. * Get the scale from the transformation matrix.
  155. */
  156. Matrix.prototype.getScale = function()
  157. {
  158. return new Vector2(this.m[0], this.m[3]);
  159. };
  160. /**
  161. * Get the position from the transformation matrix.
  162. */
  163. Matrix.prototype.getPosition = function()
  164. {
  165. return new Vector2(this.m[4], this.m[5]);
  166. };
  167. /**
  168. * Apply skew to this matrix.
  169. */
  170. Matrix.prototype.skew = function(radianX, radianY)
  171. {
  172. this.multiply(new Matrix([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]));
  173. };
  174. /**
  175. * Get the matrix determinant.
  176. */
  177. Matrix.prototype.determinant = function()
  178. {
  179. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  180. };
  181. /**
  182. * Get the inverse matrix.
  183. */
  184. Matrix.prototype.getInverse = function()
  185. {
  186. var d = this.determinant();
  187. return new Matrix([this.m[3] * d, -this.m[1] * d, -this.m[2] * d, this.m[0] * d, d * (this.m[2] * this.m[5] - this.m[3] * this.m[4]), d * (this.m[1] * this.m[4] - this.m[0] * this.m[5])]);
  188. };
  189. /**
  190. * Transform a point using this matrix.
  191. */
  192. Matrix.prototype.transformPoint = function(p)
  193. {
  194. var px = p.x * this.m[0] + p.y * this.m[2] + this.m[4];
  195. var py = p.x * this.m[1] + p.y * this.m[3] + this.m[5];
  196. return new Vector2(px, py);
  197. };
  198. /**
  199. * Set a canvas context to use this transformation.
  200. */
  201. Matrix.prototype.setContextTransform = function(context)
  202. {
  203. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  204. };
  205. /**
  206. * Transform on top of the current context transformation.
  207. */
  208. Matrix.prototype.tranformContext = function(context)
  209. {
  210. context.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  211. };
  212. Matrix.prototype.cssTransform = function()
  213. {
  214. return "matrix(" + this.m[0] + "," + this.m[1] + "," + this.m[2] + "," + this.m[3] + "," + this.m[4] + "," + this.m[5] + ")";
  215. };
  216. export {Matrix};
  217. </code></pre>
  218. </article>
  219. </section>
  220. </div>
  221. <nav>
  222. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BezierCurve.html">BezierCurve</a></li><li><a href="Box.html">Box</a></li><li><a href="Box2.html">Box2</a></li><li><a href="BoxMask.html">BoxMask</a></li><li><a href="Circle.html">Circle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</a></li><li><a href="Graph.html">Graph</a></li><li><a href="Helpers.html">Helpers</a></li><li><a href="Image.html">Image</a></li><li><a href="Key.html">Key</a></li><li><a href="Line.html">Line</a></li><li><a href="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="Text.html">Text</a></li><li><a href="UUID.html">UUID</a></li><li><a href="Vector2.html">Vector2</a></li><li><a href="Viewport.html">Viewport</a></li></ul>
  223. </nav>
  224. <br class="clear">
  225. <footer>
  226. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Mon Jun 10 2019 15:53:44 GMT+0100 (Western European Summer Time)
  227. </footer>
  228. <script> prettyPrint(); </script>
  229. <script src="scripts/linenumber.js"> </script>
  230. </body>
  231. </html>