Matrix.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(value !== 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. Matrix.prototype.multiply = function(mat)
  30. {
  31. var m0 = this.m[0] * mat[0] + this.m[2] * mat[1];
  32. var m1 = this.m[1] * mat[0] + this.m[3] * mat[1];
  33. var m2 = this.m[0] * mat[2] + this.m[2] * mat[3];
  34. var m3 = this.m[1] * mat[2] + this.m[3] * mat[3];
  35. var m4 = this.m[0] * mat[4] + this.m[2] * mat[5] + this.m[4];
  36. var m5 = this.m[1] * mat[4] + this.m[3] * mat[5] + this.m[5];
  37. this.m = [m0, m1, m2, m3, m4, m5];
  38. };
  39. /**
  40. * Set position of the transformation matrix.
  41. */
  42. Matrix.prototype.setPosition = function(x, y)
  43. {
  44. this.m[4] = x;
  45. this.m[5] = y;
  46. }
  47. /**
  48. * Apply translation to this matrix.
  49. */
  50. Matrix.prototype.translate = function(x, y)
  51. {
  52. this.multiply([1, 0, 0, 1, x, y]);
  53. };
  54. /**
  55. * Apply rotation to this matrix.
  56. */
  57. Matrix.prototype.rotate = function(rAngle)
  58. {
  59. var c = Math.cos(rAngle);
  60. var s = Math.sin(rAngle);
  61. var mat = [c, s, -s, c, 0, 0];
  62. this.multiply(mat);
  63. };
  64. /**
  65. * Apply scale to this matrix.
  66. */
  67. Matrix.prototype.scale = function(x, y)
  68. {
  69. this.multiply([x, 0, 0, y, 0, 0]);
  70. };
  71. /**
  72. * Apply skew to this matrix.
  73. */
  74. Matrix.prototype.skew = function(radianX, radianY)
  75. {
  76. this.multiply([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]);
  77. };
  78. /**
  79. * Get the matrix determinant.
  80. */
  81. Matrix.prototype.determinant = function()
  82. {
  83. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  84. };
  85. /**
  86. * Get the ivnerse matrix.
  87. */
  88. Matrix.prototype.getInverse = function()
  89. {
  90. var d = this.determinant();
  91. 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])];
  92. }
  93. /**
  94. * Set a canvas context to use this transformation.
  95. */
  96. Matrix.prototype.setContextTransform = function(context)
  97. {
  98. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  99. };
  100. /*
  101. var screenPoint=(transformedX, transformedY) =>
  102. {
  103. // invert
  104. var d =1/(this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  105. var im = [m[3] * d, -m[1] * d, -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])];
  106. // point
  107. return(
  108. {
  109. x: transformedX * im[0] + transformedY * im[2] + im[4],
  110. y: transformedX * im[1] + transformedY * im[3] + im[5]
  111. });
  112. };
  113. var transformedPoint = (screenX, screenY) => (
  114. {
  115. x: screenX * this.m[0] + screenY * this.m[2] + this.m[4],
  116. y: screenX * this.m[1] + screenY * this.m[3] + this.m[5]
  117. });
  118. */