math_Matrix.js.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. * The values of the matrix are stored in a numeric array.
  25. *
  26. * @class
  27. * @param {array} [values]
  28. */
  29. function Matrix(values)
  30. {
  31. if(values !== undefined)
  32. {
  33. this.m = values;
  34. }
  35. else
  36. {
  37. this.identity();
  38. }
  39. }
  40. /**
  41. * Copy the content of another matrix and store in this one.
  42. */
  43. Matrix.prototype.copy = function(mat)
  44. {
  45. this.m = mat.m.slice(0);
  46. };
  47. /**
  48. * Create a new matrix object with a copy of the content of this one.
  49. */
  50. Matrix.prototype.clone = function()
  51. {
  52. return new Matrix(this.m.slice(0))
  53. };
  54. /**
  55. * Reset this matrix to indentity.
  56. */
  57. Matrix.prototype.identity = function()
  58. {
  59. this.m = [1, 0, 0, 1, 0, 0];
  60. };
  61. /**
  62. * Multiply another matrix by this one and store the result.
  63. *
  64. * @param mat Matrix array.
  65. */
  66. Matrix.prototype.multiply = function(mat)
  67. {
  68. var m0 = this.m[0] * mat.m[0] + this.m[2] * mat.m[1];
  69. var m1 = this.m[1] * mat.m[0] + this.m[3] * mat.m[1];
  70. var m2 = this.m[0] * mat.m[2] + this.m[2] * mat.m[3];
  71. var m3 = this.m[1] * mat.m[2] + this.m[3] * mat.m[3];
  72. var m4 = this.m[0] * mat.m[4] + this.m[2] * mat.m[5] + this.m[4];
  73. var m5 = this.m[1] * mat.m[4] + this.m[3] * mat.m[5] + this.m[5];
  74. this.m = [m0, m1, m2, m3, m4, m5];
  75. };
  76. /**
  77. * Premultiply another matrix by this one and store the result.
  78. *
  79. * @param mat Matrix array to multiply.
  80. */
  81. Matrix.prototype.premultiply = function(mat)
  82. {
  83. var m0 = mat.m[0] * this.m[0] + mat.m[2] * this.m[1];
  84. var m1 = mat.m[1] * this.m[0] + mat.m[3] * this.m[1];
  85. var m2 = mat.m[0] * this.m[2] + mat.m[2] * this.m[3];
  86. var m3 = mat.m[1] * this.m[2] + mat.m[3] * this.m[3];
  87. var m4 = mat.m[0] * this.m[4] + mat.m[2] * this.m[5] + mat.m[4];
  88. var m5 = mat.m[1] * this.m[4] + mat.m[3] * this.m[5] + mat.m[5];
  89. this.m = [m0, m1, m2, m3, m4, m5];
  90. };
  91. /**
  92. * Compose this transformation matrix with position scale and rotation and origin point.
  93. */
  94. Matrix.prototype.compose = function(px, py, sx, sy, ox, oy, a)
  95. {
  96. this.m = [1, 0, 0, 1, px, py];
  97. if(a !== 0)
  98. {
  99. var c = Math.cos(a);
  100. var s = Math.sin(a);
  101. this.multiply(new Matrix([c, s, -s, c, 0, 0]));
  102. }
  103. if(ox !== 0 || oy !== 0)
  104. {
  105. this.multiply(new Matrix([1, 0, 0, 1, -ox, -oy]));
  106. }
  107. if(sx !== 1 || sy !== 1)
  108. {
  109. this.scale(sx, sy);
  110. }
  111. };
  112. /**
  113. * Apply translation to this matrix.
  114. */
  115. Matrix.prototype.translate = function(x, y)
  116. {
  117. this.m[4] += this.m[0] * x + this.m[2] * y;
  118. this.m[5] += this.m[1] * x + this.m[3] * y;
  119. };
  120. /**
  121. * Apply rotation to this matrix.
  122. *
  123. * @param angle Angle in radians.
  124. */
  125. Matrix.prototype.rotate = function(rad)
  126. {
  127. var c = Math.cos(rad);
  128. var s = Math.sin(rad);
  129. var m11 = this.m[0] * c + this.m[2] * s;
  130. var m12 = this.m[1] * c + this.m[3] * s;
  131. var m21 = this.m[0] * -s + this.m[2] * c;
  132. var m22 = this.m[1] * -s + this.m[3] * c;
  133. this.m[0] = m11;
  134. this.m[1] = m12;
  135. this.m[2] = m21;
  136. this.m[3] = m22;
  137. };
  138. /**
  139. * Apply scale to this matrix.
  140. */
  141. Matrix.prototype.scale = function(sx, sy)
  142. {
  143. this.m[0] *= sx;
  144. this.m[1] *= sx;
  145. this.m[2] *= sy;
  146. this.m[3] *= sy;
  147. };
  148. /**
  149. * Set the position of the transformation matrix.
  150. */
  151. Matrix.prototype.setPosition = function(x, y)
  152. {
  153. this.m[4] = x;
  154. this.m[5] = y;
  155. };
  156. /**
  157. * Get the scale from the transformation matrix.
  158. */
  159. Matrix.prototype.getScale = function()
  160. {
  161. return new Vector2(this.m[0], this.m[3]);
  162. };
  163. /**
  164. * Get the position from the transformation matrix.
  165. */
  166. Matrix.prototype.getPosition = function()
  167. {
  168. return new Vector2(this.m[4], this.m[5]);
  169. };
  170. /**
  171. * Apply skew to this matrix.
  172. */
  173. Matrix.prototype.skew = function(radianX, radianY)
  174. {
  175. this.multiply(new Matrix([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]));
  176. };
  177. /**
  178. * Get the matrix determinant.
  179. */
  180. Matrix.prototype.determinant = function()
  181. {
  182. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  183. };
  184. /**
  185. * Get the inverse matrix.
  186. */
  187. Matrix.prototype.getInverse = function()
  188. {
  189. var d = this.determinant();
  190. 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])]);
  191. };
  192. /**
  193. * Transform a point using this matrix.
  194. */
  195. Matrix.prototype.transformPoint = function(p)
  196. {
  197. var px = p.x * this.m[0] + p.y * this.m[2] + this.m[4];
  198. var py = p.x * this.m[1] + p.y * this.m[3] + this.m[5];
  199. return new Vector2(px, py);
  200. };
  201. /**
  202. * Set a canvas context to use this transformation.
  203. */
  204. Matrix.prototype.setContextTransform = function(context)
  205. {
  206. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  207. };
  208. /**
  209. * Transform on top of the current context transformation.
  210. */
  211. Matrix.prototype.tranformContext = function(context)
  212. {
  213. context.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  214. };
  215. Matrix.prototype.cssTransform = function()
  216. {
  217. return "matrix(" + this.m[0] + "," + this.m[1] + "," + this.m[2] + "," + this.m[3] + "," + this.m[4] + "," + this.m[5] + ")";
  218. };
  219. export {Matrix};
  220. </code></pre>
  221. </article>
  222. </section>
  223. </div>
  224. <nav>
  225. <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><li><a href="ViewportControls.html">ViewportControls</a></li></ul>
  226. </nav>
  227. <br class="clear">
  228. <footer>
  229. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Thu Jun 13 2019 09:46:06 GMT+0100 (Western European Summer Time)
  230. </footer>
  231. <script> prettyPrint(); </script>
  232. <script src="scripts/linenumber.js"> </script>
  233. </body>
  234. </html>