Matrix.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import {Vector2} from "./Vector2.js";
  2. /**
  3. * 2D 3x2 transformation matrix, applied to the canvas elements.
  4. *
  5. * The values of the matrix are stored in a numeric array.
  6. *
  7. * @class
  8. * @param {array} [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
  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. Matrix.prototype.clone = function()
  34. {
  35. return new Matrix(this.m.slice(0))
  36. };
  37. /**
  38. * Reset this matrix to indentity.
  39. */
  40. Matrix.prototype.identity = function()
  41. {
  42. this.m = [1, 0, 0, 1, 0, 0];
  43. };
  44. /**
  45. * Multiply another matrix by this one and store the result.
  46. *
  47. * @param {Matrix} mat
  48. */
  49. Matrix.prototype.multiply = function(mat)
  50. {
  51. var m0 = this.m[0] * mat.m[0] + this.m[2] * mat.m[1];
  52. var m1 = this.m[1] * mat.m[0] + this.m[3] * mat.m[1];
  53. var m2 = this.m[0] * mat.m[2] + this.m[2] * mat.m[3];
  54. var m3 = this.m[1] * mat.m[2] + this.m[3] * mat.m[3];
  55. var m4 = this.m[0] * mat.m[4] + this.m[2] * mat.m[5] + this.m[4];
  56. var m5 = this.m[1] * mat.m[4] + this.m[3] * mat.m[5] + this.m[5];
  57. this.m = [m0, m1, m2, m3, m4, m5];
  58. };
  59. /**
  60. * Premultiply another matrix by this one and store the result.
  61. *
  62. * @param {Matrix} mat
  63. */
  64. Matrix.prototype.premultiply = function(mat)
  65. {
  66. var m0 = mat.m[0] * this.m[0] + mat.m[2] * this.m[1];
  67. var m1 = mat.m[1] * this.m[0] + mat.m[3] * this.m[1];
  68. var m2 = mat.m[0] * this.m[2] + mat.m[2] * this.m[3];
  69. var m3 = mat.m[1] * this.m[2] + mat.m[3] * this.m[3];
  70. var m4 = mat.m[0] * this.m[4] + mat.m[2] * this.m[5] + mat.m[4];
  71. var m5 = mat.m[1] * this.m[4] + mat.m[3] * this.m[5] + mat.m[5];
  72. this.m = [m0, m1, m2, m3, m4, m5];
  73. };
  74. /**
  75. * Compose this transformation matrix with position scale and rotation and origin point.
  76. *
  77. * @param {number} px Position X
  78. * @param {number} py Position Y
  79. * @param {number} sx Scale X
  80. * @param {number} sy Scale Y
  81. * @param {number} ox Origin X (applied before scale and rotation)
  82. * @param {number} oy Origin Y (applied before scale and rotation)
  83. * @param {number} a Rotation angle (radians).
  84. */
  85. Matrix.prototype.compose = function(px, py, sx, sy, ox, oy, a)
  86. {
  87. this.m = [1, 0, 0, 1, px, py];
  88. if(a !== 0)
  89. {
  90. var c = Math.cos(a);
  91. var s = Math.sin(a);
  92. this.multiply(new Matrix([c, s, -s, c, 0, 0]));
  93. }
  94. if(sx !== 1 || sy !== 1)
  95. {
  96. this.scale(sx, sy);
  97. }
  98. if(ox !== 0 || oy !== 0)
  99. {
  100. this.multiply(new Matrix([1, 0, 0, 1, -ox, -oy]));
  101. }
  102. };
  103. /**
  104. * Apply translation to this matrix.
  105. *
  106. * @param {number} x
  107. * @param {number} y
  108. */
  109. Matrix.prototype.translate = function(x, y)
  110. {
  111. this.m[4] += this.m[0] * x + this.m[2] * y;
  112. this.m[5] += this.m[1] * x + this.m[3] * y;
  113. };
  114. /**
  115. * Apply rotation to this matrix.
  116. *
  117. * @param {number} angle Angle in radians.
  118. */
  119. Matrix.prototype.rotate = function(rad)
  120. {
  121. var c = Math.cos(rad);
  122. var s = Math.sin(rad);
  123. var m11 = this.m[0] * c + this.m[2] * s;
  124. var m12 = this.m[1] * c + this.m[3] * s;
  125. var m21 = this.m[0] * -s + this.m[2] * c;
  126. var m22 = this.m[1] * -s + this.m[3] * c;
  127. this.m[0] = m11;
  128. this.m[1] = m12;
  129. this.m[2] = m21;
  130. this.m[3] = m22;
  131. };
  132. /**
  133. * Apply scale to this matrix.
  134. *
  135. * @param {number} sx
  136. * @param {number} sy
  137. */
  138. Matrix.prototype.scale = function(sx, sy)
  139. {
  140. this.m[0] *= sx;
  141. this.m[1] *= sx;
  142. this.m[2] *= sy;
  143. this.m[3] *= sy;
  144. };
  145. /**
  146. * Set the position of the transformation matrix.
  147. *
  148. * @param {number} x
  149. * @param {number} y
  150. */
  151. Matrix.prototype.setPosition = function(x, y)
  152. {
  153. this.m[4] = x;
  154. this.m[5] = y;
  155. };
  156. /**
  157. * Get the scale from the transformation matrix.
  158. *
  159. * @return {Vector2} Scale of the matrix transformation.
  160. */
  161. Matrix.prototype.getScale = function()
  162. {
  163. return new Vector2(this.m[0], this.m[3]);
  164. };
  165. /**
  166. * Get the position from the transformation matrix.
  167. *
  168. * @return {Vector2} Position of the matrix transformation.
  169. */
  170. Matrix.prototype.getPosition = function()
  171. {
  172. return new Vector2(this.m[4], this.m[5]);
  173. };
  174. /**
  175. * Apply skew to this matrix.
  176. */
  177. Matrix.prototype.skew = function(radianX, radianY)
  178. {
  179. this.multiply(new Matrix([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]));
  180. };
  181. /**
  182. * Get the matrix determinant.
  183. */
  184. Matrix.prototype.determinant = function()
  185. {
  186. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  187. };
  188. /**
  189. * Get the inverse matrix.
  190. */
  191. Matrix.prototype.getInverse = function()
  192. {
  193. var d = this.determinant();
  194. 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])]);
  195. };
  196. /**
  197. * Transform a point using this matrix.
  198. */
  199. Matrix.prototype.transformPoint = function(p)
  200. {
  201. var px = p.x * this.m[0] + p.y * this.m[2] + this.m[4];
  202. var py = p.x * this.m[1] + p.y * this.m[3] + this.m[5];
  203. return new Vector2(px, py);
  204. };
  205. /**
  206. * Set a canvas context to use this transformation.
  207. */
  208. Matrix.prototype.setContextTransform = function(context)
  209. {
  210. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  211. };
  212. /**
  213. * Transform on top of the current context transformation.
  214. */
  215. Matrix.prototype.tranformContext = function(context)
  216. {
  217. context.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  218. };
  219. Matrix.prototype.cssTransform = function()
  220. {
  221. return "matrix(" + this.m[0] + "," + this.m[1] + "," + this.m[2] + "," + this.m[3] + "," + this.m[4] + "," + this.m[5] + ")";
  222. };
  223. export {Matrix};