Matrix.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. "use strict";
  2. import {Vector2} from "./Vector2.js";
  3. /**
  4. * 2D 3x2 transformation matrix, applied to the canvas elements.
  5. *
  6. * @class
  7. */
  8. function Matrix(values)
  9. {
  10. if(values !== undefined)
  11. {
  12. this.m = values;
  13. }
  14. else
  15. {
  16. this.identity();
  17. }
  18. }
  19. /**
  20. * Copy the content of another matrix and store in this one.
  21. */
  22. Matrix.prototype.copy = function(mat)
  23. {
  24. this.m = mat.m.slice(0);
  25. };
  26. /**
  27. * Create a new matrix object with a copy of the content of this one.
  28. */
  29. Matrix.prototype.clone = function()
  30. {
  31. return new Matrix(this.m.slice(0))
  32. };
  33. /**
  34. * Reset this matrix to indentity.
  35. */
  36. Matrix.prototype.identity = function()
  37. {
  38. this.m = [1, 0, 0, 1, 0, 0];
  39. };
  40. /**
  41. * Multiply another matrix by this one and store the result.
  42. *
  43. * @param mat Matrix array.
  44. */
  45. Matrix.prototype.multiply = function(mat)
  46. {
  47. var m0 = this.m[0] * mat.m[0] + this.m[2] * mat.m[1];
  48. var m1 = this.m[1] * mat.m[0] + this.m[3] * mat.m[1];
  49. var m2 = this.m[0] * mat.m[2] + this.m[2] * mat.m[3];
  50. var m3 = this.m[1] * mat.m[2] + this.m[3] * mat.m[3];
  51. var m4 = this.m[0] * mat.m[4] + this.m[2] * mat.m[5] + this.m[4];
  52. var m5 = this.m[1] * mat.m[4] + this.m[3] * mat.m[5] + this.m[5];
  53. this.m = [m0, m1, m2, m3, m4, m5];
  54. };
  55. /**
  56. * Premultiply another matrix by this one and store the result.
  57. *
  58. * @param mat Matrix array to multiply.
  59. */
  60. Matrix.prototype.premultiply = function(mat)
  61. {
  62. var m0 = mat.m[0] * this.m[0] + mat.m[2] * this.m[1];
  63. var m1 = mat.m[1] * this.m[0] + mat.m[3] * this.m[1];
  64. var m2 = mat.m[0] * this.m[2] + mat.m[2] * this.m[3];
  65. var m3 = mat.m[1] * this.m[2] + mat.m[3] * this.m[3];
  66. var m4 = mat.m[0] * this.m[4] + mat.m[2] * this.m[5] + mat.m[4];
  67. var m5 = mat.m[1] * this.m[4] + mat.m[3] * this.m[5] + mat.m[5];
  68. this.m = [m0, m1, m2, m3, m4, m5];
  69. };
  70. /**
  71. * Compose this transformation matrix with position scale and rotation.
  72. */
  73. Matrix.prototype.compose = function(px, py, sx, sy, a)
  74. {
  75. this.m = [1, 0, 0, 1, px, py];
  76. var c = Math.cos(a);
  77. var s = Math.sin(a);
  78. this.multiply(new Matrix([c, s, -s, c, 0, 0]));
  79. this.scale(sx, sy);
  80. };
  81. /**
  82. * Apply translation to this matrix.
  83. */
  84. Matrix.prototype.translate = function(x, y)
  85. {
  86. this.m[4] += this.m[0] * x + this.m[2] * y;
  87. this.m[5] += this.m[1] * x + this.m[3] * y;
  88. };
  89. /**
  90. * Apply rotation to this matrix.
  91. *
  92. * @param angle Angle in radians.
  93. */
  94. Matrix.prototype.rotate = function(rad)
  95. {
  96. var c = Math.cos(rad);
  97. var s = Math.sin(rad);
  98. var m11 = this.m[0] * c + this.m[2] * s;
  99. var m12 = this.m[1] * c + this.m[3] * s;
  100. var m21 = this.m[0] * -s + this.m[2] * c;
  101. var m22 = this.m[1] * -s + this.m[3] * c;
  102. this.m[0] = m11;
  103. this.m[1] = m12;
  104. this.m[2] = m21;
  105. this.m[3] = m22;
  106. };
  107. /**
  108. * Apply scale to this matrix.
  109. */
  110. Matrix.prototype.scale = function(sx, sy)
  111. {
  112. this.m[0] *= sx;
  113. this.m[1] *= sx;
  114. this.m[2] *= sy;
  115. this.m[3] *= sy;
  116. };
  117. /**
  118. * Set the position of the transformation matrix.
  119. */
  120. Matrix.prototype.setPosition = function(x, y)
  121. {
  122. this.m[4] = x;
  123. this.m[5] = y;
  124. };
  125. /**
  126. * Get the scale from the transformation matrix.
  127. */
  128. Matrix.prototype.getScale = function()
  129. {
  130. return new Vector2(this.m[0], this.m[3]);
  131. };
  132. /**
  133. * Get the position from the transformation matrix.
  134. */
  135. Matrix.prototype.getPosition = function()
  136. {
  137. return new Vector2(this.m[4], this.m[5]);
  138. };
  139. /**
  140. * Apply skew to this matrix.
  141. */
  142. Matrix.prototype.skew = function(radianX, radianY)
  143. {
  144. this.multiply(new Matrix([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]));
  145. };
  146. /**
  147. * Get the matrix determinant.
  148. */
  149. Matrix.prototype.determinant = function()
  150. {
  151. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  152. };
  153. /**
  154. * Get the inverse matrix.
  155. */
  156. Matrix.prototype.getInverse = function()
  157. {
  158. var d = this.determinant();
  159. 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])]);
  160. };
  161. /**
  162. * Transform a point using this matrix.
  163. */
  164. Matrix.prototype.transformPoint = function(p)
  165. {
  166. var px = p.x * this.m[0] + p.y * this.m[2] + this.m[4];
  167. var py = p.x * this.m[1] + p.y * this.m[3] + this.m[5];
  168. return new Vector2(px, py);
  169. };
  170. /**
  171. * Set a canvas context to use this transformation.
  172. */
  173. Matrix.prototype.setContextTransform = function(context)
  174. {
  175. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  176. };
  177. /**
  178. * Transform on top of the current context transformation.
  179. */
  180. Matrix.prototype.tranformContext = function(context)
  181. {
  182. context.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  183. };
  184. Matrix.prototype.cssTransform = function()
  185. {
  186. return "matrix3d(" + this.m[0] + "," + this.m[2] + "," + this.m[4] + ",0," + this.m[1] + "," + this.m[3] + "," + this.m[5] + ",0,0,0,1,0,0,0,0,1)";
  187. };
  188. export {Matrix};