RotationEvent.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // 后端定,鼠标移动一个canvas width转动90度。*2移动距离加倍,所以前端实际会转180度
  32. let l = 2 * o / a
  33. , u = 2 * n / s;
  34. // 瞬时最大不超过90度
  35. l > 1 && (l = 1),
  36. u > 1 && (u = 1),
  37. this._room.actionsHandler.rotate({
  38. pitch: l,
  39. yaw: u
  40. }),
  41. this.touchStartX = t,
  42. this.touchStartY = r
  43. }
  44. );
  45. E(this, "handleMouseUp", ()=>{
  46. this.touchStartX = void 0,
  47. this.touchStartY = void 0
  48. }
  49. );
  50. E(this, "handleTouchMove", e=>{
  51. if (!this.touchStartX || !this.touchStartY)
  52. return;
  53. const t = e.touches[0]
  54. , r = t.pageX
  55. , n = t.pageY
  56. , o = r - this.touchStartX
  57. , a = n - this.touchStartY
  58. , s = this._room.options.canvas.offsetHeight
  59. , l = this._room.options.canvas.offsetWidth;
  60. let u = 2 * a / s
  61. , c = 2 * o / l;
  62. u > 1 && (u = 1),
  63. c > 1 && (c = 1),
  64. this._room.actionsHandler.rotate({
  65. pitch: u,
  66. yaw: c
  67. }),
  68. this.touchStartX = r,
  69. this.touchStartY = n,
  70. this._room.emit("touchMove", {
  71. pitch: u,
  72. yaw: c,
  73. event: e
  74. })
  75. }
  76. );
  77. E(this, "handleTouchEnd", e=>{
  78. this._room.emit("touchEnd", {
  79. event: e
  80. })
  81. }
  82. );
  83. this._room = e,
  84. this._canvas = e.canvas,
  85. this.handelResize = this.reiszeChange()
  86. }
  87. init() {
  88. this._canvas.addEventListener("touchstart", this.handleTouchStart),
  89. this._canvas.addEventListener("touchmove", this.handleTouchMove),
  90. this._canvas.addEventListener("touchend", this.handleTouchEnd),
  91. this._room.scene.preventDefaultOnPointerDown = !1,
  92. this._room.scene.preventDefaultOnPointerUp = !1,
  93. this._canvas.addEventListener("mousedown", this.handleMouseDown),
  94. this._canvas.addEventListener("mousemove", this.handleMouseMove),
  95. this._canvas.addEventListener("mouseup", this.handleMouseUp)
  96. }
  97. clear() {
  98. this._canvas.removeEventListener("touchstart", this.handleTouchStart),
  99. this._canvas.removeEventListener("touchmove", this.handleTouchMove),
  100. this._canvas.removeEventListener("touchend", this.handleTouchEnd),
  101. this._canvas.removeEventListener("mousedown", this.handleMouseDown),
  102. this._canvas.removeEventListener("mousemove", this.handleMouseMove),
  103. this._canvas.removeEventListener("mouseup", this.handleMouseUp)
  104. }
  105. reiszeChange() {
  106. window.addEventListener("resize", ()=>{}
  107. )
  108. }
  109. }