Viewport.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. import {Vector2} from "./math/Vector2.js";
  3. import {Matrix} from "./math/Matrix.js";
  4. import {UUID} from "./math/UUID.js";
  5. import {Pointer} from "./input/Pointer.js";
  6. /**
  7. * Used to indicate how the user views the content inside of the canvas.
  8. *
  9. * @class
  10. * @param {DOM} 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 scalling.
  48. *
  49. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  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. /*var ox = (this.canvas.width / 2.0);
  80. var oy = (this.canvas.height / 2.0);
  81. if(ox !== 0 || oy !== 0)
  82. {
  83. this.matrix.multiply(new Matrix([1, 0, 0, 1, -ox, -oy]));
  84. }*/
  85. this.inverseMatrix = this.matrix.getInverse();
  86. this.matrixNeedsUpdate = false;
  87. }
  88. };
  89. /**
  90. * Center the viewport relative to a object.
  91. *
  92. * The position of the object is used a central point, this method does not consider "box" attributes or other strucures in the object.
  93. *
  94. * @param {Object2D} object Object to be centered on the viewport.
  95. * @param {DOM} canvas Canvas element where the image is drawn.
  96. */
  97. Viewport.prototype.centerObject = function(object, canvas)
  98. {
  99. var position = object.globalMatrix.transformPoint(new Vector2());
  100. position.multiplyScalar(-this.scale);
  101. position.x += canvas.width / 2;
  102. position.y += canvas.height / 2;
  103. this.position.copy(position);
  104. this.matrixNeedsUpdate = true;
  105. };
  106. export {Viewport};