Matrix.js 4.3 KB

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