math_Matrix.js.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. Default is the identity matrix.
  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 Matrix to multiply by.
  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 Matrix to premultiply by.
  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} rot Rotation angle (radians).
  111. */
  112. Matrix.prototype.compose = function(px, py, sx, sy, ox, oy, rot)
  113. {
  114. // Position
  115. this.m = [1, 0, 0, 1, px, py];
  116. // Rotation
  117. if(rot !== 0)
  118. {
  119. var c = Math.cos(rot);
  120. var s = Math.sin(rot);
  121. this.multiply(new Matrix([c, s, -s, c, 0, 0]));
  122. }
  123. // Scale
  124. if(sx !== 1 || sy !== 1)
  125. {
  126. this.scale(sx, sy);
  127. }
  128. // Origin
  129. if(ox !== 0 || oy !== 0)
  130. {
  131. this.multiply(new Matrix([1, 0, 0, 1, -ox, -oy]));
  132. }
  133. };
  134. /**
  135. * Apply translation to this matrix.
  136. *
  137. * Adds position over the transformation already stored in the matrix.
  138. *
  139. * @param {number} x
  140. * @param {number} y
  141. */
  142. Matrix.prototype.translate = function(x, y)
  143. {
  144. this.m[4] += this.m[0] * x + this.m[2] * y;
  145. this.m[5] += this.m[1] * x + this.m[3] * y;
  146. };
  147. /**
  148. * Apply rotation to this matrix.
  149. *
  150. * @param {number} rad Angle to rotate the matrix in radians.
  151. */
  152. Matrix.prototype.rotate = function(rad)
  153. {
  154. var c = Math.cos(rad);
  155. var s = Math.sin(rad);
  156. var m11 = this.m[0] * c + this.m[2] * s;
  157. var m12 = this.m[1] * c + this.m[3] * s;
  158. var m21 = this.m[0] * -s + this.m[2] * c;
  159. var m22 = this.m[1] * -s + this.m[3] * c;
  160. this.m[0] = m11;
  161. this.m[1] = m12;
  162. this.m[2] = m21;
  163. this.m[3] = m22;
  164. };
  165. /**
  166. * Apply scale to this matrix.
  167. *
  168. * @param {number} sx
  169. * @param {number} sy
  170. */
  171. Matrix.prototype.scale = function(sx, sy)
  172. {
  173. this.m[0] *= sx;
  174. this.m[1] *= sx;
  175. this.m[2] *= sy;
  176. this.m[3] *= sy;
  177. };
  178. /**
  179. * Set the position of the transformation matrix.
  180. *
  181. * @param {number} x
  182. * @param {number} y
  183. */
  184. Matrix.prototype.setPosition = function(x, y)
  185. {
  186. this.m[4] = x;
  187. this.m[5] = y;
  188. };
  189. /**
  190. * Extract the scale from the transformation matrix.
  191. *
  192. * @return {Vector2} Scale of the matrix transformation.
  193. */
  194. Matrix.prototype.getScale = function()
  195. {
  196. return new Vector2(this.m[0], this.m[3]);
  197. };
  198. /**
  199. * Extract the position from the transformation matrix.
  200. *
  201. * @return {Vector2} Position of the matrix transformation.
  202. */
  203. Matrix.prototype.getPosition = function()
  204. {
  205. return new Vector2(this.m[4], this.m[5]);
  206. };
  207. /**
  208. * Apply skew to this matrix.
  209. *
  210. * @param {number} radianX
  211. * @param {number} radianY
  212. */
  213. Matrix.prototype.skew = function(radianX, radianY)
  214. {
  215. this.multiply(new Matrix([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]));
  216. };
  217. /**
  218. * Get the matrix determinant.
  219. *
  220. * @return {number} Determinant of this matrix.
  221. */
  222. Matrix.prototype.determinant = function()
  223. {
  224. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  225. };
  226. /**
  227. * Get the inverse matrix.
  228. *
  229. * @return {Matrix} New matrix instance containing the inverse matrix.
  230. */
  231. Matrix.prototype.getInverse = function()
  232. {
  233. var d = this.determinant();
  234. 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])]);
  235. };
  236. /**
  237. * Transform a point using this matrix.
  238. *
  239. * @param {Vector2} p Point to be transformed.
  240. * @return {Vector2} Transformed point.
  241. */
  242. Matrix.prototype.transformPoint = function(p)
  243. {
  244. var px = p.x * this.m[0] + p.y * this.m[2] + this.m[4];
  245. var py = p.x * this.m[1] + p.y * this.m[3] + this.m[5];
  246. return new Vector2(px, py);
  247. };
  248. /**
  249. * Set a canvas context to use this transformation.
  250. *
  251. * @param {CanvasRenderingContext2D} context Canvas context to apply this matrix transform.
  252. */
  253. Matrix.prototype.setContextTransform = function(context)
  254. {
  255. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  256. };
  257. /**
  258. * Transform on top of the current context transformation.
  259. *
  260. * @param {CanvasRenderingContext2D} context Canvas context to apply this matrix transform.
  261. */
  262. Matrix.prototype.tranformContext = function(context)
  263. {
  264. context.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  265. };
  266. /**
  267. * Generate a CSS transform string that can be applied to the transform style of any DOM element.
  268. *
  269. * @returns {string} CSS transform matrix.
  270. */
  271. Matrix.prototype.cssTransform = function()
  272. {
  273. return "matrix(" + this.m[0] + "," + this.m[1] + "," + this.m[2] + "," + this.m[3] + "," + this.m[4] + "," + this.m[5] + ")";
  274. };
  275. export {Matrix};
  276. </code></pre>
  277. </article>
  278. </section>
  279. </div>
  280. <nav>
  281. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AnimationTimer.html">AnimationTimer</a></li><li><a href="BarGraph.html">BarGraph</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="ColorStyle.html">ColorStyle</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="Gauge.html">Gauge</a></li><li><a href="GradientColorStop.html">GradientColorStop</a></li><li><a href="GradientStyle.html">GradientStyle</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="LinearGradientStyle.html">LinearGradientStyle</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="Path.html">Path</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="PatternStyle.html">PatternStyle</a></li><li><a href="PieChart.html">PieChart</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="QuadraticCurve.html">QuadraticCurve</a></li><li><a href="RadialGradientStyle.html">RadialGradientStyle</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="RoundedBox.html">RoundedBox</a></li><li><a href="ScatterGraph.html">ScatterGraph</a></li><li><a href="Style.html">Style</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>
  282. </nav>
  283. <br class="clear">
  284. <footer>
  285. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Sat Sep 17 2022 14:24:36 GMT+0100 (Hora de verão da Europa Ocidental)
  286. </footer>
  287. <script> prettyPrint(); </script>
  288. <script src="scripts/linenumber.js"> </script>
  289. </body>
  290. </html>