Matrix.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.m = null;
  16. this.reset();
  17. }
  18. }
  19. /**
  20. * Reset this matrix to indentity.
  21. */
  22. Matrix.prototype.reset = function()
  23. {
  24. this.m = [1, 0, 0, 1, 0, 0];
  25. };
  26. /**
  27. * Multiply another matrix by this one and store the result.
  28. *
  29. * @param mat Matrix array.
  30. */
  31. Matrix.prototype.multiply = function(mat)
  32. {
  33. var m0 = this.m[0] * mat.m[0] + this.m[2] * mat.m[1];
  34. var m1 = this.m[1] * mat.m[0] + this.m[3] * mat.m[1];
  35. var m2 = this.m[0] * mat.m[2] + this.m[2] * mat.m[3];
  36. var m3 = this.m[1] * mat.m[2] + this.m[3] * mat.m[3];
  37. var m4 = this.m[0] * mat.m[4] + this.m[2] * mat.m[5] + this.m[4];
  38. var m5 = this.m[1] * mat.m[4] + this.m[3] * mat.m[5] + this.m[5];
  39. this.m = [m0, m1, m2, m3, m4, m5];
  40. };
  41. /**
  42. * Premultiply another matrix by this one and store the result.
  43. *
  44. * @param mat Matrix array to multiply.
  45. */
  46. Matrix.prototype.premultiply = function(mat)
  47. {
  48. var m0 = mat.m[0] * this.m[0] + mat.m[2] * this.m[1];
  49. var m1 = mat.m[1] * this.m[0] + mat.m[3] * this.m[1];
  50. var m2 = mat.m[0] * this.m[2] + mat.m[2] * this.m[3];
  51. var m3 = mat.m[1] * this.m[2] + mat.m[3] * this.m[3];
  52. var m4 = mat.m[0] * this.m[4] + mat.m[2] * this.m[5] + mat.m[4];
  53. var m5 = mat.m[1] * this.m[4] + mat.m[3] * this.m[5] + mat.m[5];
  54. this.m = [m0, m1, m2, m3, m4, m5];
  55. };
  56. /**
  57. * Compose this transformation matrix with position scale and rotation.
  58. */
  59. Matrix.prototype.compose = function(px, py, sx, sy, a)
  60. {
  61. this.m = [1, 0, 0, 1, px, py];
  62. var c = Math.cos(a);
  63. var s = Math.sin(a);
  64. this.multiply(new Matrix([c, s, -s, c, 0, 0]));
  65. this.scale(sx, sy);
  66. };
  67. /**
  68. * Apply translation to this matrix.
  69. */
  70. Matrix.prototype.translate = function(x, y)
  71. {
  72. this.m[4] += this.m[0] * x + this.m[2] * y;
  73. this.m[5] += this.m[1] * x + this.m[3] * y;
  74. };
  75. /**
  76. * Apply rotation to this matrix.
  77. *
  78. * @param angle Angle in radians.
  79. */
  80. Matrix.prototype.rotate = function(rad)
  81. {
  82. var c = Math.cos(rad);
  83. var s = Math.sin(rad);
  84. var m11 = this.m[0] * c + this.m[2] * s;
  85. var m12 = this.m[1] * c + this.m[3] * s;
  86. var m21 = this.m[0] * -s + this.m[2] * c;
  87. var m22 = this.m[1] * -s + this.m[3] * c;
  88. this.m[0] = m11;
  89. this.m[1] = m12;
  90. this.m[2] = m21;
  91. this.m[3] = m22;
  92. };
  93. /**
  94. * Apply scale to this matrix.
  95. */
  96. Matrix.prototype.scale = function(sx, sy)
  97. {
  98. this.m[0] *= sx;
  99. this.m[1] *= sx;
  100. this.m[2] *= sy;
  101. this.m[3] *= sy;
  102. };
  103. /**
  104. * Apply skew to this matrix.
  105. */
  106. Matrix.prototype.skew = function(radianX, radianY)
  107. {
  108. this.multiply(new Matrix([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]));
  109. };
  110. /**
  111. * Get the matrix determinant.
  112. */
  113. Matrix.prototype.determinant = function()
  114. {
  115. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  116. };
  117. /**
  118. * Get the ivnerse matrix.
  119. */
  120. Matrix.prototype.getInverse = function()
  121. {
  122. var d = this.determinant();
  123. return [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])];
  124. }
  125. /**
  126. * Set a canvas context to use this transformation.
  127. */
  128. Matrix.prototype.setContextTransform = function(context)
  129. {
  130. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  131. };
  132. /**
  133. * Transform on top of the current context transformation.
  134. */
  135. Matrix.prototype.tranformContext = function(context)
  136. {
  137. context.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  138. };
  139. /**
  140. * Transform a point using this matrix.
  141. */
  142. Matrix.prototype.transformPoint = function(px, py)
  143. {
  144. var x = px;
  145. var y = py;
  146. px = x * this.m[0] + y * this.m[2] + this.m[4];
  147. py = x * this.m[1] + y * this.m[3] + this.m[5];
  148. return new Vector2(px, py);
  149. };