Viewport.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. /**
  3. * Used to indicate how the user views the content inside of the canvas.
  4. *
  5. * @class
  6. */
  7. function Viewport()
  8. {
  9. /**
  10. * UUID of the object.
  11. */
  12. this.uuid = UUID.generate();
  13. /**
  14. * Position of the object.
  15. */
  16. this.position = new Vector2(0, 0);
  17. /**
  18. * Scale of the object.
  19. */
  20. this.scale = 1.0
  21. /**
  22. * Rotation of the object relative to its center.
  23. */
  24. this.rotation = 0.0;
  25. /**
  26. * Local transformation matrix applied to the object.
  27. */
  28. this.matrix = new Matrix();
  29. /**
  30. * Inverse of the local transformation matrix.
  31. */
  32. this.inverseMatrix = new Matrix();
  33. /**
  34. * If true the matrix is updated before rendering the object.
  35. */
  36. this.matrixNeedsUpdate = true;
  37. }
  38. /**
  39. * Update the viewport controls using the mouse object.
  40. */
  41. Viewport.prototype.updateControls = function(mouse)
  42. {
  43. this.scale -= mouse.wheel * 1e-3 * this.scale;
  44. if(mouse.buttonPressed(Mouse.RIGHT))
  45. {
  46. this.position.x += mouse.delta.x;
  47. this.position.y += mouse.delta.y;
  48. }
  49. };
  50. /**
  51. * Calculate and update the viewport transformation matrix.
  52. */
  53. Viewport.prototype.updateMatrix = function()
  54. {
  55. if(this.matrixNeedsUpdate)
  56. {
  57. this.matrix.compose(this.position.x, this.position.y, this.scale, this.scale, this.rotation);
  58. this.inverseMatrix = this.matrix.getInverse();
  59. //this.matrixNeedsUpdate = false;
  60. }
  61. };