math_Matrix.js.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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>import {Vector2} from "./Vector2.js";
  20. /**
  21. * 2D 3x2 transformation matrix, used to represent linear geometric transformations over objects.
  22. *
  23. * The values of the matrix are stored as numeric array. The matrix can be applied to the canvas or DOM elements using CSS transforms.
  24. *
  25. * @class
  26. * @param {number[]} values Array of matrix values by row, needs to have exactly 6 values.
  27. */
  28. function Matrix(values)
  29. {
  30. if(values !== undefined)
  31. {
  32. /**
  33. * Array that contains the matrix data by row. This matrix should have 6 values.
  34. *
  35. * Matrix can be directly edited by accessing this attribute.
  36. *
  37. * @type {number[]}
  38. */
  39. this.m = values;
  40. }
  41. else
  42. {
  43. this.identity();
  44. }
  45. }
  46. /**
  47. * Copy the content of another matrix and store in this one.
  48. *
  49. * @param {Matrix} mat Matrix to copy values from.
  50. */
  51. Matrix.prototype.copy = function(mat)
  52. {
  53. this.m = mat.m.slice(0);
  54. };
  55. /**
  56. * Create a new matrix object with a copy of the content of this one.
  57. *
  58. * @return {Matrix} Copy of this matrix.
  59. */
  60. Matrix.prototype.clone = function()
  61. {
  62. return new Matrix(this.m.slice(0))
  63. };
  64. /**
  65. * Reset this matrix to identity.
  66. */
  67. Matrix.prototype.identity = function()
  68. {
  69. this.m = [1, 0, 0, 1, 0, 0];
  70. };
  71. /**
  72. * Multiply another matrix by this one and store the result.
  73. *
  74. * @param {Matrix} mat
  75. */
  76. Matrix.prototype.multiply = function(mat)
  77. {
  78. var m0 = this.m[0] * mat.m[0] + this.m[2] * mat.m[1];
  79. var m1 = this.m[1] * mat.m[0] + this.m[3] * mat.m[1];
  80. var m2 = this.m[0] * mat.m[2] + this.m[2] * mat.m[3];
  81. var m3 = this.m[1] * mat.m[2] + this.m[3] * mat.m[3];
  82. var m4 = this.m[0] * mat.m[4] + this.m[2] * mat.m[5] + this.m[4];
  83. var m5 = this.m[1] * mat.m[4] + this.m[3] * mat.m[5] + this.m[5];
  84. this.m = [m0, m1, m2, m3, m4, m5];
  85. };
  86. /**
  87. * Premultiply another matrix by this one and store the result.
  88. *
  89. * @param {Matrix} mat
  90. */
  91. Matrix.prototype.premultiply = function(mat)
  92. {
  93. var m0 = mat.m[0] * this.m[0] + mat.m[2] * this.m[1];
  94. var m1 = mat.m[1] * this.m[0] + mat.m[3] * this.m[1];
  95. var m2 = mat.m[0] * this.m[2] + mat.m[2] * this.m[3];
  96. var m3 = mat.m[1] * this.m[2] + mat.m[3] * this.m[3];
  97. var m4 = mat.m[0] * this.m[4] + mat.m[2] * this.m[5] + mat.m[4];
  98. var m5 = mat.m[1] * this.m[4] + mat.m[3] * this.m[5] + mat.m[5];
  99. this.m = [m0, m1, m2, m3, m4, m5];
  100. };
  101. /**
  102. * Compose this transformation matrix with position scale and rotation and origin point.
  103. *
  104. * @param {number} px Position X
  105. * @param {number} py Position Y
  106. * @param {number} sx Scale X
  107. * @param {number} sy Scale Y
  108. * @param {number} ox Origin X (applied before scale and rotation)
  109. * @param {number} oy Origin Y (applied before scale and rotation)
  110. * @param {number} a Rotation angle (radians).
  111. */
  112. Matrix.prototype.compose = function(px, py, sx, sy, ox, oy, a)
  113. {
  114. this.m = [1, 0, 0, 1, px, py];
  115. if(a !== 0)
  116. {
  117. var c = Math.cos(a);
  118. var s = Math.sin(a);
  119. this.multiply(new Matrix([c, s, -s, c, 0, 0]));
  120. }
  121. if(sx !== 1 || sy !== 1)
  122. {
  123. this.scale(sx, sy);
  124. }
  125. if(ox !== 0 || oy !== 0)
  126. {
  127. this.multiply(new Matrix([1, 0, 0, 1, -ox, -oy]));
  128. }
  129. };
  130. /**
  131. * Apply translation to this matrix.
  132. *
  133. * Adds position over the transformation already stored in the matrix.
  134. *
  135. * @param {number} x
  136. * @param {number} y
  137. */
  138. Matrix.prototype.translate = function(x, y)
  139. {
  140. this.m[4] += this.m[0] * x + this.m[2] * y;
  141. this.m[5] += this.m[1] * x + this.m[3] * y;
  142. };
  143. /**
  144. * Apply rotation to this matrix.
  145. *
  146. * @param {number} rad Angle to rotate the matrix in radians.
  147. */
  148. Matrix.prototype.rotate = function(rad)
  149. {
  150. var c = Math.cos(rad);
  151. var s = Math.sin(rad);
  152. var m11 = this.m[0] * c + this.m[2] * s;
  153. var m12 = this.m[1] * c + this.m[3] * s;
  154. var m21 = this.m[0] * -s + this.m[2] * c;
  155. var m22 = this.m[1] * -s + this.m[3] * c;
  156. this.m[0] = m11;
  157. this.m[1] = m12;
  158. this.m[2] = m21;
  159. this.m[3] = m22;
  160. };
  161. /**
  162. * Apply scale to this matrix.
  163. *
  164. * @param {number} sx
  165. * @param {number} sy
  166. */
  167. Matrix.prototype.scale = function(sx, sy)
  168. {
  169. this.m[0] *= sx;
  170. this.m[1] *= sx;
  171. this.m[2] *= sy;
  172. this.m[3] *= sy;
  173. };
  174. /**
  175. * Set the position of the transformation matrix.
  176. *
  177. * @param {number} x
  178. * @param {number} y
  179. */
  180. Matrix.prototype.setPosition = function(x, y)
  181. {
  182. this.m[4] = x;
  183. this.m[5] = y;
  184. };
  185. /**
  186. * Extract the scale from the transformation matrix.
  187. *
  188. * @return {Vector2} Scale of the matrix transformation.
  189. */
  190. Matrix.prototype.getScale = function()
  191. {
  192. return new Vector2(this.m[0], this.m[3]);
  193. };
  194. /**
  195. * Extract the position from the transformation matrix.
  196. *
  197. * @return {Vector2} Position of the matrix transformation.
  198. */
  199. Matrix.prototype.getPosition = function()
  200. {
  201. return new Vector2(this.m[4], this.m[5]);
  202. };
  203. /**
  204. * Apply skew to this matrix.
  205. */
  206. Matrix.prototype.skew = function(radianX, radianY)
  207. {
  208. this.multiply(new Matrix([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]));
  209. };
  210. /**
  211. * Get the matrix determinant.
  212. *
  213. * @return {number} Determinant of this matrix.
  214. */
  215. Matrix.prototype.determinant = function()
  216. {
  217. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  218. };
  219. /**
  220. * Get the inverse matrix.
  221. *
  222. * @return {Matrix} New matrix instance containing the inverse matrix.
  223. */
  224. Matrix.prototype.getInverse = function()
  225. {
  226. var d = this.determinant();
  227. 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])]);
  228. };
  229. /**
  230. * Transform a point using this matrix.
  231. *
  232. * @param {Vector2} p Point to be transformed.
  233. * @return {Vector2} Transformed point.
  234. */
  235. Matrix.prototype.transformPoint = function(p)
  236. {
  237. var px = p.x * this.m[0] + p.y * this.m[2] + this.m[4];
  238. var py = p.x * this.m[1] + p.y * this.m[3] + this.m[5];
  239. return new Vector2(px, py);
  240. };
  241. /**
  242. * Set a canvas context to use this transformation.
  243. *
  244. * @param {CanvasRenderingContext2D} context Canvas context to apply this matrix transform.
  245. */
  246. Matrix.prototype.setContextTransform = function(context)
  247. {
  248. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  249. };
  250. /**
  251. * Transform on top of the current context transformation.
  252. *
  253. * @param {CanvasRenderingContext2D} context Canvas context to apply this matrix transform.
  254. */
  255. Matrix.prototype.tranformContext = function(context)
  256. {
  257. context.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  258. };
  259. /**
  260. * Generate a CSS transform string that can be applied to the transform style of any DOM element.
  261. *
  262. * @returns {string} CSS transform matrix.
  263. */
  264. Matrix.prototype.cssTransform = function()
  265. {
  266. return "matrix(" + this.m[0] + "," + this.m[1] + "," + this.m[2] + "," + this.m[3] + "," + this.m[4] + "," + this.m[5] + ")";
  267. };
  268. export {Matrix};
  269. </code></pre>
  270. </article>
  271. </section>
  272. </div>
  273. <nav>
  274. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AnimationTimer.html">AnimationTimer</a></li><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="FileUtils.html">FileUtils</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="MultiLineText.html">MultiLineText</a></li><li><a href="Node.html">Node</a></li><li><a href="NodeConnector.html">NodeConnector</a></li><li><a href="NodeGraph.html">NodeGraph</a></li><li><a href="NodeSocket.html">NodeSocket</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="QuadraticCurve.html">QuadraticCurve</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="RoundedBox.html">RoundedBox</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><h3>Global</h3><ul><li><a href="global.html#writeFile">writeFile</a></li></ul>
  275. </nav>
  276. <br class="clear">
  277. <footer>
  278. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Tue May 19 2020 22:29:24 GMT+0100 (Western European Summer Time)
  279. </footer>
  280. <script> prettyPrint(); </script>
  281. <script src="scripts/linenumber.js"> </script>
  282. </body>
  283. </html>