ViewportControls.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. function ViewportControls(viewport)
  3. {
  4. this.viewport = viewport;
  5. var pressed = -1;
  6. var x = 0, y = 0;
  7. var dx = 0, dy = 0;
  8. var self = this;
  9. this.manager = new EventManager();
  10. this.manager.add(canvas, "contextmenu", function(event)
  11. {
  12. event.preventDefault();
  13. return false;
  14. });
  15. this.manager.add(canvas, "mousedown", function(event)
  16. {
  17. pressed = event.which;
  18. });
  19. this.manager.add(canvas, "mouseup", function(event)
  20. {
  21. pressed = -1;
  22. });
  23. this.manager.add(canvas, "mousemove", function(event)
  24. {
  25. dx = event.clientX - x;
  26. dy = event.clientY - y;
  27. x = event.clientX;
  28. y = event.clientY;
  29. // Mouse
  30. if(pressed === 3)
  31. {
  32. self.viewport.position.x += dx;
  33. self.viewport.position.y += dy;
  34. }
  35. });
  36. this.manager.add(canvas, "wheel", function(event)
  37. {
  38. self.viewport.scale -= (event.deltaY * 0.001) * self.viewport.scale;
  39. });
  40. }
  41. ViewportControls.prototype.create = function()
  42. {
  43. this.manager.create();
  44. };
  45. ViewportControls.prototype.destroy = function()
  46. {
  47. this.manager.destroy();
  48. };