Viewport.js 2.8 KB

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