Matrix.js 6.7 KB

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