Matrix.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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[0] + this.m[2] * mat[1];
  34. var m1 = this.m[1] * mat[0] + this.m[3] * mat[1];
  35. var m2 = this.m[0] * mat[2] + this.m[2] * mat[3];
  36. var m3 = this.m[1] * mat[2] + this.m[3] * mat[3];
  37. var m4 = this.m[0] * mat[4] + this.m[2] * mat[5] + this.m[4];
  38. var m5 = this.m[1] * mat[4] + this.m[3] * mat[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[0] * this.m[0] + mat[2] * this.m[1];
  49. var m1 = mat[1] * this.m[0] + mat[3] * this.m[1];
  50. var m2 = mat[0] * this.m[2] + mat[2] * this.m[3];
  51. var m3 = mat[1] * this.m[2] + mat[3] * this.m[3];
  52. var m4 = mat[0] * this.m[4] + mat[2] * this.m[5] + mat[4];
  53. var m5 = mat[1] * this.m[4] + mat[3] * this.m[5] + mat[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([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.multiply([1, 0, 0, 1, x, y]);
  73. };
  74. /**
  75. * Apply rotation to this matrix.
  76. *
  77. * @param angle Angle in radians.
  78. */
  79. Matrix.prototype.rotate = function(angle)
  80. {
  81. var c = Math.cos(angle);
  82. var s = Math.sin(angle);
  83. var mat = [c, s, -s, c, 0, 0];
  84. this.multiply(mat);
  85. };
  86. /**
  87. * Apply scale to this matrix.
  88. */
  89. Matrix.prototype.scale = function(x, y)
  90. {
  91. this.multiply([x, 0, 0, y, 0, 0]);
  92. };
  93. /**
  94. * Apply skew to this matrix.
  95. */
  96. Matrix.prototype.skew = function(radianX, radianY)
  97. {
  98. this.multiply([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]);
  99. };
  100. /**
  101. * Get the matrix determinant.
  102. */
  103. Matrix.prototype.determinant = function()
  104. {
  105. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  106. };
  107. /**
  108. * Get the ivnerse matrix.
  109. */
  110. Matrix.prototype.getInverse = function()
  111. {
  112. var d = this.determinant();
  113. 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])];
  114. }
  115. /**
  116. * Set a canvas context to use this transformation.
  117. */
  118. Matrix.prototype.setContextTransform = function(context)
  119. {
  120. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  121. };
  122. /**
  123. * Transform on top of the current context transformation.
  124. */
  125. Matrix.prototype.tranformContext = function(context)
  126. {
  127. context.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  128. };