math_Matrix.js.html 7.5 KB

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