RotationEvent.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. class RotationEvent {
  2. constructor(e) {
  3. E(this, "touchStartX");
  4. E(this, "touchStartY");
  5. E(this, "handelResize");
  6. E(this, "_room");
  7. E(this, "_canvas");
  8. E(this, "handleTouchStart", e=>{
  9. const t = e.touches[0];
  10. this.touchStartX = t.pageX,
  11. this.touchStartY = t.pageY,
  12. this._room.emit("touchStart", {
  13. event: e
  14. })
  15. }
  16. );
  17. E(this, "handleMouseDown", e=>{
  18. this.touchStartX = e.pageX,
  19. this.touchStartY = e.pageY
  20. }
  21. );
  22. E(this, "handleMouseMove", e=>{
  23. if (!this.touchStartX || !this.touchStartY)
  24. return;
  25. const t = e.pageX
  26. , r = e.pageY
  27. , n = t - this.touchStartX
  28. , o = r - this.touchStartY
  29. , a = this._room.options.canvas.offsetHeight
  30. , s = this._room.options.canvas.offsetWidth;
  31. let l = 2 * o / a
  32. , u = 2 * n / s;
  33. l > 1 && (l = 1),
  34. u > 1 && (u = 1),
  35. this._room.actionsHandler.rotate({
  36. pitch: l,
  37. yaw: u
  38. }),
  39. this.touchStartX = t,
  40. this.touchStartY = r
  41. }
  42. );
  43. E(this, "handleMouseUp", ()=>{
  44. this.touchStartX = void 0,
  45. this.touchStartY = void 0
  46. }
  47. );
  48. E(this, "handleTouchMove", e=>{
  49. if (!this.touchStartX || !this.touchStartY)
  50. return;
  51. const t = e.touches[0]
  52. , r = t.pageX
  53. , n = t.pageY
  54. , o = r - this.touchStartX
  55. , a = n - this.touchStartY
  56. , s = this._room.options.canvas.offsetHeight
  57. , l = this._room.options.canvas.offsetWidth;
  58. let u = 2 * a / s
  59. , c = 2 * o / l;
  60. u > 1 && (u = 1),
  61. c > 1 && (c = 1),
  62. this._room.actionsHandler.rotate({
  63. pitch: u,
  64. yaw: c
  65. }),
  66. this.touchStartX = r,
  67. this.touchStartY = n,
  68. this._room.emit("touchMove", {
  69. pitch: u,
  70. yaw: c,
  71. event: e
  72. })
  73. }
  74. );
  75. E(this, "handleTouchEnd", e=>{
  76. this._room.emit("touchEnd", {
  77. event: e
  78. })
  79. }
  80. );
  81. this._room = e,
  82. this._canvas = e.canvas,
  83. this.handelResize = this.reiszeChange()
  84. }
  85. init() {
  86. this._canvas.addEventListener("touchstart", this.handleTouchStart),
  87. this._canvas.addEventListener("touchmove", this.handleTouchMove),
  88. this._canvas.addEventListener("touchend", this.handleTouchEnd),
  89. this._room.scene.preventDefaultOnPointerDown = !1,
  90. this._room.scene.preventDefaultOnPointerUp = !1,
  91. this._canvas.addEventListener("mousedown", this.handleMouseDown),
  92. this._canvas.addEventListener("mousemove", this.handleMouseMove),
  93. this._canvas.addEventListener("mouseup", this.handleMouseUp)
  94. }
  95. clear() {
  96. this._canvas.removeEventListener("touchstart", this.handleTouchStart),
  97. this._canvas.removeEventListener("touchmove", this.handleTouchMove),
  98. this._canvas.removeEventListener("touchend", this.handleTouchEnd),
  99. this._canvas.removeEventListener("mousedown", this.handleMouseDown),
  100. this._canvas.removeEventListener("mousemove", this.handleMouseMove),
  101. this._canvas.removeEventListener("mouseup", this.handleMouseUp)
  102. }
  103. reiszeChange() {
  104. window.addEventListener("resize", ()=>{}
  105. )
  106. }
  107. }