Matrix.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import {Vector2} from "./Vector2.js";
  2. /**
  3. * 2D 3x2 transformation matrix, used to represent linear geometric transformations over objects.
  4. *
  5. * The values of the matrix are stored in a numeric array and can be applied to the canvas or DOM elements.
  6. *
  7. * @class
  8. * @param {number[]} values Array of matrix values by row, needs to have exactly 6 values.
  9. */
  10. function Matrix(values)
  11. {
  12. if(values !== undefined)
  13. {
  14. this.m = values;
  15. }
  16. else
  17. {
  18. this.identity();
  19. }
  20. }
  21. /**
  22. * Copy the content of another matrix and store in this one.
  23. *
  24. * @param {Matrix} mat Matrix to copy values from.
  25. */
  26. Matrix.prototype.copy = function(mat)
  27. {
  28. this.m = mat.m.slice(0);
  29. };
  30. /**
  31. * Create a new matrix object with a copy of the content of this one.
  32. *
  33. * @return {Matrix} Copy of this matrix.
  34. */
  35. Matrix.prototype.clone = function()
  36. {
  37. return new Matrix(this.m.slice(0))
  38. };
  39. /**
  40. * Reset this matrix to identity.
  41. */
  42. Matrix.prototype.identity = function()
  43. {
  44. this.m = [1, 0, 0, 1, 0, 0];
  45. };
  46. /**
  47. * Multiply another matrix by this one and store the result.
  48. *
  49. * @param {Matrix} mat
  50. */
  51. Matrix.prototype.multiply = function(mat)
  52. {
  53. var m0 = this.m[0] * mat.m[0] + this.m[2] * mat.m[1];
  54. var m1 = this.m[1] * mat.m[0] + this.m[3] * mat.m[1];
  55. var m2 = this.m[0] * mat.m[2] + this.m[2] * mat.m[3];
  56. var m3 = this.m[1] * mat.m[2] + this.m[3] * mat.m[3];
  57. var m4 = this.m[0] * mat.m[4] + this.m[2] * mat.m[5] + this.m[4];
  58. var m5 = this.m[1] * mat.m[4] + this.m[3] * mat.m[5] + this.m[5];
  59. this.m = [m0, m1, m2, m3, m4, m5];
  60. };
  61. /**
  62. * Premultiply another matrix by this one and store the result.
  63. *
  64. * @param {Matrix} mat
  65. */
  66. Matrix.prototype.premultiply = function(mat)
  67. {
  68. var m0 = mat.m[0] * this.m[0] + mat.m[2] * this.m[1];
  69. var m1 = mat.m[1] * this.m[0] + mat.m[3] * this.m[1];
  70. var m2 = mat.m[0] * this.m[2] + mat.m[2] * this.m[3];
  71. var m3 = mat.m[1] * this.m[2] + mat.m[3] * this.m[3];
  72. var m4 = mat.m[0] * this.m[4] + mat.m[2] * this.m[5] + mat.m[4];
  73. var m5 = mat.m[1] * this.m[4] + mat.m[3] * this.m[5] + mat.m[5];
  74. this.m = [m0, m1, m2, m3, m4, m5];
  75. };
  76. /**
  77. * Compose this transformation matrix with position scale and rotation and origin point.
  78. *
  79. * @param {number} px Position X
  80. * @param {number} py Position Y
  81. * @param {number} sx Scale X
  82. * @param {number} sy Scale Y
  83. * @param {number} ox Origin X (applied before scale and rotation)
  84. * @param {number} oy Origin Y (applied before scale and rotation)
  85. * @param {number} a Rotation angle (radians).
  86. */
  87. Matrix.prototype.compose = function(px, py, sx, sy, ox, oy, a)
  88. {
  89. this.m = [1, 0, 0, 1, px, py];
  90. if(a !== 0)
  91. {
  92. var c = Math.cos(a);
  93. var s = Math.sin(a);
  94. this.multiply(new Matrix([c, s, -s, c, 0, 0]));
  95. }
  96. if(sx !== 1 || sy !== 1)
  97. {
  98. this.scale(sx, sy);
  99. }
  100. if(ox !== 0 || oy !== 0)
  101. {
  102. this.multiply(new Matrix([1, 0, 0, 1, -ox, -oy]));
  103. }
  104. };
  105. /**
  106. * Apply translation to this matrix.
  107. *
  108. * Adds position over the transformation already stored in the matrix.
  109. *
  110. * @param {number} x
  111. * @param {number} y
  112. */
  113. Matrix.prototype.translate = function(x, y)
  114. {
  115. this.m[4] += this.m[0] * x + this.m[2] * y;
  116. this.m[5] += this.m[1] * x + this.m[3] * y;
  117. };
  118. /**
  119. * Apply rotation to this matrix.
  120. *
  121. * @param {number} rad Angle to rotate the matrix in radians.
  122. */
  123. Matrix.prototype.rotate = function(rad)
  124. {
  125. var c = Math.cos(rad);
  126. var s = Math.sin(rad);
  127. var m11 = this.m[0] * c + this.m[2] * s;
  128. var m12 = this.m[1] * c + this.m[3] * s;
  129. var m21 = this.m[0] * -s + this.m[2] * c;
  130. var m22 = this.m[1] * -s + this.m[3] * c;
  131. this.m[0] = m11;
  132. this.m[1] = m12;
  133. this.m[2] = m21;
  134. this.m[3] = m22;
  135. };
  136. /**
  137. * Apply scale to this matrix.
  138. *
  139. * @param {number} sx
  140. * @param {number} sy
  141. */
  142. Matrix.prototype.scale = function(sx, sy)
  143. {
  144. this.m[0] *= sx;
  145. this.m[1] *= sx;
  146. this.m[2] *= sy;
  147. this.m[3] *= sy;
  148. };
  149. /**
  150. * Set the position of the transformation matrix.
  151. *
  152. * @param {number} x
  153. * @param {number} y
  154. */
  155. Matrix.prototype.setPosition = function(x, y)
  156. {
  157. this.m[4] = x;
  158. this.m[5] = y;
  159. };
  160. /**
  161. * Extract the scale from the transformation matrix.
  162. *
  163. * @return {Vector2} Scale of the matrix transformation.
  164. */
  165. Matrix.prototype.getScale = function()
  166. {
  167. return new Vector2(this.m[0], this.m[3]);
  168. };
  169. /**
  170. * Extract the position from the transformation matrix.
  171. *
  172. * @return {Vector2} Position of the matrix transformation.
  173. */
  174. Matrix.prototype.getPosition = function()
  175. {
  176. return new Vector2(this.m[4], this.m[5]);
  177. };
  178. /**
  179. * Apply skew to this matrix.
  180. */
  181. Matrix.prototype.skew = function(radianX, radianY)
  182. {
  183. this.multiply(new Matrix([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]));
  184. };
  185. /**
  186. * Get the matrix determinant.
  187. *
  188. * @return {number} Determinant of this matrix.
  189. */
  190. Matrix.prototype.determinant = function()
  191. {
  192. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  193. };
  194. /**
  195. * Get the inverse matrix.
  196. *
  197. * @return {Matrix} New matrix instance containing the inverse matrix.
  198. */
  199. Matrix.prototype.getInverse = function()
  200. {
  201. var d = this.determinant();
  202. 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])]);
  203. };
  204. /**
  205. * Transform a point using this matrix.
  206. *
  207. * @param {Vector2} p Point to be transformed.
  208. * @return {Vector2} Transformed point.
  209. */
  210. Matrix.prototype.transformPoint = function(p)
  211. {
  212. var px = p.x * this.m[0] + p.y * this.m[2] + this.m[4];
  213. var py = p.x * this.m[1] + p.y * this.m[3] + this.m[5];
  214. return new Vector2(px, py);
  215. };
  216. /**
  217. * Set a canvas context to use this transformation.
  218. *
  219. * @param {CanvasRenderingContext2D} context Canvas context to apply this matrix transform.
  220. */
  221. Matrix.prototype.setContextTransform = function(context)
  222. {
  223. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  224. };
  225. /**
  226. * Transform on top of the current context transformation.
  227. *
  228. * @param {CanvasRenderingContext2D} context Canvas context to apply this matrix transform.
  229. */
  230. Matrix.prototype.tranformContext = function(context)
  231. {
  232. context.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  233. };
  234. /**
  235. * Generate a CSS transform string that can be applied to the transform style of any DOM element.
  236. *
  237. * @returns {string} CSS transform matrix.
  238. */
  239. Matrix.prototype.cssTransform = function()
  240. {
  241. return "matrix(" + this.m[0] + "," + this.m[1] + "," + this.m[2] + "," + this.m[3] + "," + this.m[4] + "," + this.m[5] + ")";
  242. };
  243. export {Matrix};