Viewport.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import {Vector2} from "./math/Vector2.js";
  2. import {Matrix} from "./math/Matrix.js";
  3. import {UUID} from "./math/UUID.js";
  4. /**
  5. * Used to indicate how the user views the content inside of the canvas.
  6. *
  7. * @class
  8. * @param {Element} canvas Canvas DOM element where the viewport is being rendered.
  9. */
  10. function Viewport(canvas)
  11. {
  12. /**
  13. * UUID of the object.
  14. */
  15. this.uuid = UUID.generate();
  16. /**
  17. * Canvas DOM element where the viewport is being rendered.
  18. */
  19. this.canvas = canvas;
  20. /**
  21. * Position of the object.
  22. */
  23. this.position = new Vector2(0, 0);
  24. /**
  25. * Scale of the object.
  26. */
  27. this.scale = 1.0
  28. /**
  29. * Rotation of the object relative to its center.
  30. */
  31. this.rotation = 0.0;
  32. /**
  33. * Local transformation matrix applied to the object.
  34. */
  35. this.matrix = new Matrix();
  36. /**
  37. * Inverse of the local transformation matrix.
  38. */
  39. this.inverseMatrix = new Matrix();
  40. /**
  41. * If true the matrix is updated before rendering the object.
  42. */
  43. this.matrixNeedsUpdate = true;
  44. /**
  45. * Flag to indicate if the viewport should move when scalling.
  46. *
  47. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  48. */
  49. this.moveOnScale = false;
  50. /**
  51. * Value of the initial point of rotation if the viewport is being rotated.
  52. *
  53. * Is set to null when the viewport is not being rotated.
  54. */
  55. this.rotationPoint = null;
  56. }
  57. /**
  58. * Calculate and update the viewport transformation matrix.
  59. *
  60. * Also updates the inverse matrix of the viewport.
  61. */
  62. Viewport.prototype.updateMatrix = function()
  63. {
  64. if(this.matrixNeedsUpdate)
  65. {
  66. this.matrix.m = [1, 0, 0, 1, this.position.x, this.position.y];
  67. if(this.rotation !== 0)
  68. {
  69. var c = Math.cos(this.rotation);
  70. var s = Math.sin(this.rotation);
  71. this.matrix.multiply(new Matrix([c, s, -s, c, 0, 0]));
  72. }
  73. if(this.scale !== 1)
  74. {
  75. this.matrix.scale(this.scale, this.scale);
  76. }
  77. this.inverseMatrix = this.matrix.getInverse();
  78. this.matrixNeedsUpdate = false;
  79. }
  80. };
  81. /**
  82. * Center the viewport relative to a object.
  83. *
  84. * The position of the object is used a central point, this method does not consider "box" attributes or other strucures in the object.
  85. *
  86. * @param {Object2D} object Object to be centered on the viewport.
  87. * @param {Element} canvas Canvas element where the image is drawn.
  88. */
  89. Viewport.prototype.centerObject = function(object, canvas)
  90. {
  91. var position = object.globalMatrix.transformPoint(new Vector2());
  92. position.multiplyScalar(-this.scale);
  93. position.x += canvas.width / 2;
  94. position.y += canvas.height / 2;
  95. this.position.copy(position);
  96. this.matrixNeedsUpdate = true;
  97. };
  98. export {Viewport};