math_Matrix.js.html 9.2 KB

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