RotationEvent.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. export default 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. const pageX = t.pageX
  59. const pageY = t.pageY
  60. const offsetX = pageX - this.touchStartX
  61. const offsetY = pageY - this.touchStartY
  62. const offsetHeight = this._room.options.canvas.offsetHeight
  63. const offsetWidth = this._room.options.canvas.offsetWidth
  64. let pitch = 2 * offsetY / offsetHeight;
  65. let yaw = 2 * offsetX / offsetWidth;
  66. pitch > 1 && (pitch = 1);
  67. yaw > 1 && (yaw = 1);
  68. this._room.actionsHandler.rotate({
  69. pitch: pitch,
  70. yaw: yaw
  71. });
  72. /***********************************************************************xst****************************************************************/
  73. // let angle = offsetX / offsetWidth * 2 * Math.PI
  74. // this._room.actionsHandler.rotate({
  75. // type: 'rotate',
  76. // angle: angle
  77. // });
  78. /********************************************************************************************************************************************/
  79. this.touchStartX = pageX;
  80. this.touchStartY = pageY;
  81. /***********************************************************************xst****************************************************************/
  82. // this._room.emit("touchMove", {
  83. // pitch: pitch,
  84. // yaw: yaw,
  85. // event: e
  86. // })
  87. /********************************************************************************************************************************************/
  88. }
  89. );
  90. E(this, "handleTouchEnd", e=>{
  91. this._room.emit("touchEnd", {
  92. event: e
  93. })
  94. }
  95. );
  96. this._room = e,
  97. this._canvas = e.canvas,
  98. this.handelResize = this.reiszeChange()
  99. }
  100. init() {
  101. this._canvas.addEventListener("touchstart", this.handleTouchStart),
  102. this._canvas.addEventListener("touchmove", this.handleTouchMove),
  103. this._canvas.addEventListener("touchend", this.handleTouchEnd),
  104. this._room.scene.preventDefaultOnPointerDown = !1,
  105. this._room.scene.preventDefaultOnPointerUp = !1,
  106. this._canvas.addEventListener("mousedown", this.handleMouseDown),
  107. this._canvas.addEventListener("mousemove", this.handleMouseMove),
  108. this._canvas.addEventListener("mouseup", this.handleMouseUp)
  109. }
  110. clear() {
  111. this._canvas.removeEventListener("touchstart", this.handleTouchStart),
  112. this._canvas.removeEventListener("touchmove", this.handleTouchMove),
  113. this._canvas.removeEventListener("touchend", this.handleTouchEnd),
  114. this._canvas.removeEventListener("mousedown", this.handleMouseDown),
  115. this._canvas.removeEventListener("mousemove", this.handleMouseMove),
  116. this._canvas.removeEventListener("mouseup", this.handleMouseUp)
  117. }
  118. reiszeChange() {
  119. window.addEventListener("resize", ()=>{}
  120. )
  121. }
  122. }