Matrix.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.
  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
  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
  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} a Rotation angle (radians).
  93. */
  94. Matrix.prototype.compose = function(px, py, sx, sy, ox, oy, a)
  95. {
  96. this.m = [1, 0, 0, 1, px, py];
  97. if(a !== 0)
  98. {
  99. var c = Math.cos(a);
  100. var s = Math.sin(a);
  101. this.multiply(new Matrix([c, s, -s, c, 0, 0]));
  102. }
  103. if(sx !== 1 || sy !== 1)
  104. {
  105. this.scale(sx, sy);
  106. }
  107. if(ox !== 0 || oy !== 0)
  108. {
  109. this.multiply(new Matrix([1, 0, 0, 1, -ox, -oy]));
  110. }
  111. };
  112. /**
  113. * Apply translation to this matrix.
  114. *
  115. * Adds position over the transformation already stored in the matrix.
  116. *
  117. * @param {number} x
  118. * @param {number} y
  119. */
  120. Matrix.prototype.translate = function(x, y)
  121. {
  122. this.m[4] += this.m[0] * x + this.m[2] * y;
  123. this.m[5] += this.m[1] * x + this.m[3] * y;
  124. };
  125. /**
  126. * Apply rotation to this matrix.
  127. *
  128. * @param {number} rad Angle to rotate the matrix in radians.
  129. */
  130. Matrix.prototype.rotate = function(rad)
  131. {
  132. var c = Math.cos(rad);
  133. var s = Math.sin(rad);
  134. var m11 = this.m[0] * c + this.m[2] * s;
  135. var m12 = this.m[1] * c + this.m[3] * s;
  136. var m21 = this.m[0] * -s + this.m[2] * c;
  137. var m22 = this.m[1] * -s + this.m[3] * c;
  138. this.m[0] = m11;
  139. this.m[1] = m12;
  140. this.m[2] = m21;
  141. this.m[3] = m22;
  142. };
  143. /**
  144. * Apply scale to this matrix.
  145. *
  146. * @param {number} sx
  147. * @param {number} sy
  148. */
  149. Matrix.prototype.scale = function(sx, sy)
  150. {
  151. this.m[0] *= sx;
  152. this.m[1] *= sx;
  153. this.m[2] *= sy;
  154. this.m[3] *= sy;
  155. };
  156. /**
  157. * Set the position of the transformation matrix.
  158. *
  159. * @param {number} x
  160. * @param {number} y
  161. */
  162. Matrix.prototype.setPosition = function(x, y)
  163. {
  164. this.m[4] = x;
  165. this.m[5] = y;
  166. };
  167. /**
  168. * Extract the scale from the transformation matrix.
  169. *
  170. * @return {Vector2} Scale of the matrix transformation.
  171. */
  172. Matrix.prototype.getScale = function()
  173. {
  174. return new Vector2(this.m[0], this.m[3]);
  175. };
  176. /**
  177. * Extract the position from the transformation matrix.
  178. *
  179. * @return {Vector2} Position of the matrix transformation.
  180. */
  181. Matrix.prototype.getPosition = function()
  182. {
  183. return new Vector2(this.m[4], this.m[5]);
  184. };
  185. /**
  186. * Apply skew to this matrix.
  187. */
  188. Matrix.prototype.skew = function(radianX, radianY)
  189. {
  190. this.multiply(new Matrix([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]));
  191. };
  192. /**
  193. * Get the matrix determinant.
  194. *
  195. * @return {number} Determinant of this matrix.
  196. */
  197. Matrix.prototype.determinant = function()
  198. {
  199. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  200. };
  201. /**
  202. * Get the inverse matrix.
  203. *
  204. * @return {Matrix} New matrix instance containing the inverse matrix.
  205. */
  206. Matrix.prototype.getInverse = function()
  207. {
  208. var d = this.determinant();
  209. 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])]);
  210. };
  211. /**
  212. * Transform a point using this matrix.
  213. *
  214. * @param {Vector2} p Point to be transformed.
  215. * @return {Vector2} Transformed point.
  216. */
  217. Matrix.prototype.transformPoint = function(p)
  218. {
  219. var px = p.x * this.m[0] + p.y * this.m[2] + this.m[4];
  220. var py = p.x * this.m[1] + p.y * this.m[3] + this.m[5];
  221. return new Vector2(px, py);
  222. };
  223. /**
  224. * Set a canvas context to use this transformation.
  225. *
  226. * @param {CanvasRenderingContext2D} context Canvas context to apply this matrix transform.
  227. */
  228. Matrix.prototype.setContextTransform = function(context)
  229. {
  230. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  231. };
  232. /**
  233. * Transform on top of the current context transformation.
  234. *
  235. * @param {CanvasRenderingContext2D} context Canvas context to apply this matrix transform.
  236. */
  237. Matrix.prototype.tranformContext = function(context)
  238. {
  239. context.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  240. };
  241. /**
  242. * Generate a CSS transform string that can be applied to the transform style of any DOM element.
  243. *
  244. * @returns {string} CSS transform matrix.
  245. */
  246. Matrix.prototype.cssTransform = function()
  247. {
  248. return "matrix(" + this.m[0] + "," + this.m[1] + "," + this.m[2] + "," + this.m[3] + "," + this.m[4] + "," + this.m[5] + ")";
  249. };
  250. export {Matrix};