@xviewer.js/core 1.0.4-alpha.6 → 1.0.4-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.cjs CHANGED
@@ -2586,6 +2586,60 @@ class Pressability {
2586
2586
  }
2587
2587
  }
2588
2588
 
2589
+ const PIXEL_STEP = 10;
2590
+ const LINE_HEIGHT = 40;
2591
+ const PAGE_HEIGHT = 800;
2592
+ function normalizeWheel(/*object*/ event) /*object*/ {
2593
+ let sX = 0, sY = 0, pX = 0, pY = 0; // pixelX, pixelY
2594
+ // Legacy
2595
+ if ('detail' in event) {
2596
+ sY = event.detail;
2597
+ }
2598
+ if ('wheelDelta' in event) {
2599
+ sY = -event.wheelDelta / 120;
2600
+ }
2601
+ if ('wheelDeltaY' in event) {
2602
+ sY = -event.wheelDeltaY / 120;
2603
+ }
2604
+ if ('wheelDeltaX' in event) {
2605
+ sX = -event.wheelDeltaX / 120;
2606
+ }
2607
+ // side scrolling on FF with DOMMouseScroll
2608
+ if ('axis' in event && event.axis === event.HORIZONTAL_AXIS) {
2609
+ sX = sY;
2610
+ sY = 0;
2611
+ }
2612
+ pX = sX * PIXEL_STEP;
2613
+ pY = sY * PIXEL_STEP;
2614
+ if ('deltaY' in event) {
2615
+ pY = event.deltaY;
2616
+ }
2617
+ if ('deltaX' in event) {
2618
+ pX = event.deltaX;
2619
+ }
2620
+ if ((pX || pY) && event.deltaMode) {
2621
+ if (event.deltaMode == 1) {
2622
+ pX *= LINE_HEIGHT;
2623
+ pY *= LINE_HEIGHT;
2624
+ } else {
2625
+ pX *= PAGE_HEIGHT;
2626
+ pY *= PAGE_HEIGHT;
2627
+ }
2628
+ }
2629
+ // Fall-back if spin cannot be determined
2630
+ if (pX && !sX) {
2631
+ sX = pX < 1 ? -1 : 1;
2632
+ }
2633
+ if (pY && !sY) {
2634
+ sY = pY < 1 ? -1 : 1;
2635
+ }
2636
+ return {
2637
+ spinX: sX,
2638
+ spinY: sY,
2639
+ pixelX: pX,
2640
+ pixelY: pY
2641
+ };
2642
+ }
2589
2643
  class DeviceInput extends Component {
2590
2644
  get pointer() {
2591
2645
  return this._pointer;
@@ -2602,9 +2656,18 @@ class DeviceInput extends Component {
2602
2656
  get prePointerPixel() {
2603
2657
  return this._prePointerPixel;
2604
2658
  }
2659
+ get preTouches() {
2660
+ return this._preTouches;
2661
+ }
2605
2662
  get mouseWheel() {
2606
2663
  return this._mouseWheel;
2607
2664
  }
2665
+ get touchStart() {
2666
+ return this._touchStart;
2667
+ }
2668
+ get touchMoving() {
2669
+ return this._touchMoving;
2670
+ }
2608
2671
  get touchCount() {
2609
2672
  return this._touchCount;
2610
2673
  }
@@ -2668,7 +2731,12 @@ class DeviceInput extends Component {
2668
2731
  lastUpdate(dt) {
2669
2732
  this._prePointer.copy(this._pointer);
2670
2733
  this._prePointerPixel.copy(this._pointerPixel);
2734
+ for(let i = this._touchCount; i--;){
2735
+ this._preTouches[i].id = this._touches[i].id;
2736
+ this._preTouches[i].position.copy(this._touches[i].position);
2737
+ }
2671
2738
  this._mouseWheel = 0;
2739
+ this._touchStart = false;
2672
2740
  }
2673
2741
  connect(target, event) {
2674
2742
  switch(event){
@@ -2739,14 +2807,16 @@ class DeviceInput extends Component {
2739
2807
  const isDocument = target instanceof Document;
2740
2808
  const width = isDocument ? window.innerWidth : target.offsetWidth;
2741
2809
  const height = isDocument ? window.innerHeight : target.offsetHeight;
2742
- this._pointer.x = e.clientX / width * 2 - 1;
2743
- this._pointer.y = 1 - e.clientY / height * 2;
2744
- this._pointerPixel.set(e.clientX, e.clientY);
2810
+ this._pointer.x = e.pageX / width * 2 - 1;
2811
+ this._pointer.y = 1 - e.pageY / height * 2;
2812
+ this._pointerPixel.set(e.pageX, e.pageY);
2745
2813
  }
2746
2814
  _onPointerDown(e) {
2747
2815
  e = this._remapPointer(e);
2748
2816
  this._pointerButton = e.button;
2749
2817
  this._computePointer(e);
2818
+ this._prePointer.copy(this._pointer);
2819
+ this._prePointerPixel.copy(this._pointerPixel);
2750
2820
  this._pressability.pointerDown(this._pointer, this.viewer.camera);
2751
2821
  this.viewer.emit(DeviceInput.POINTER_DOWN, e);
2752
2822
  }
@@ -2764,33 +2834,54 @@ class DeviceInput extends Component {
2764
2834
  this.viewer.emit(DeviceInput.POINTER_MOVE, e);
2765
2835
  }
2766
2836
  _onMouseWheel(e) {
2767
- this._mouseWheel = e.deltaY || e.wheelDelta;
2837
+ this._mouseWheel = this._normalizeWheel(e).pixelY;
2768
2838
  this.viewer.emit(DeviceInput.MOUSE_WHEEL, e);
2769
2839
  }
2770
2840
  _onTouchStart(e) {
2841
+ this._touchStart = true;
2771
2842
  e = this._remapTouch(e);
2843
+ const curr = e.touches;
2844
+ const touches = this._touches;
2845
+ const preTouches = this._preTouches;
2846
+ for(let i = curr.length; i--;){
2847
+ if (touches[i] === undefined) {
2848
+ touches[i] = {
2849
+ id: -1,
2850
+ position: new THREE.Vector2()
2851
+ };
2852
+ }
2853
+ const touch = curr[i];
2854
+ touches[i].id = touch.identifier;
2855
+ touches[i].position.set(touch.pageX, touch.pageY);
2856
+ if (preTouches[i] === undefined) {
2857
+ preTouches[i] = {
2858
+ id: touches[i].id,
2859
+ position: touches[i].position.clone()
2860
+ };
2861
+ } else {
2862
+ preTouches[i].id = touches[i].id;
2863
+ preTouches[i].position.copy(touches[i].position);
2864
+ }
2865
+ }
2866
+ this._touchCount = curr.length;
2772
2867
  this.viewer.emit(DeviceInput.TOUCH_START, e);
2773
2868
  }
2774
2869
  _onTouchEnd(e) {
2870
+ this._touchMoving = false;
2775
2871
  e = this._remapTouch(e);
2776
2872
  this.viewer.emit(DeviceInput.TOUCH_END, e);
2777
2873
  }
2778
2874
  _onTouchMove(e) {
2875
+ this._touchMoving = true;
2779
2876
  e = this._remapTouch(e);
2780
- const touches = e.touches;
2781
- const touchesTo = this._touches;
2782
- for(let i = touches.length; i--;){
2783
- if (touchesTo[i] == undefined) {
2784
- touchesTo[i] = {
2785
- id: -1,
2786
- position: new THREE.Vector2()
2787
- };
2788
- }
2789
- const touch = touches[i];
2790
- touchesTo[i].id = touch.identifier;
2791
- touchesTo[i].position.set(touch.pageX, touch.pageY);
2792
- }
2793
- this._touchCount = touches.length;
2877
+ const curr = e.touches;
2878
+ const touches = this._touches;
2879
+ for(let i = curr.length; i--;){
2880
+ const touch = curr[i];
2881
+ touches[i].id = touch.identifier;
2882
+ touches[i].position.set(touch.pageX, touch.pageY);
2883
+ }
2884
+ this._touchCount = curr.length;
2794
2885
  this.viewer.emit(DeviceInput.TOUCH_MOVE, e);
2795
2886
  }
2796
2887
  _onKeyDown(e) {
@@ -2805,9 +2896,10 @@ class DeviceInput extends Component {
2805
2896
  this._keys[e.key] = false;
2806
2897
  this.viewer.emit(DeviceInput.KEYUP, e);
2807
2898
  }
2808
- constructor(target){
2809
- super(), this._listeners = [], this._touches = [], this._touchCount = 0, this._pointer = new THREE.Vector2(), this._pointerPixel = new THREE.Vector2(), this._pointerButton = -1, this._prePointer = new THREE.Vector2(), this._prePointerPixel = new THREE.Vector2(), this._mouseWheel = 0, this._keys = {}, this._pressability = new Pressability();
2810
- this._target = target;
2899
+ constructor(option){
2900
+ super(), this._listeners = [], this._touches = [], this._touchCount = 0, this._touchStart = false, this._touchMoving = false, this._pointer = new THREE.Vector2(), this._pointerPixel = new THREE.Vector2(), this._pointerButton = -1, this._prePointer = new THREE.Vector2(), this._prePointerPixel = new THREE.Vector2(), this._preTouches = [], this._mouseWheel = 0, this._keys = {}, this._pressability = new Pressability();
2901
+ this._target = option.source;
2902
+ this._normalizeWheel = option.normalizeWheel || normalizeWheel;
2811
2903
  }
2812
2904
  }
2813
2905
  DeviceInput.CLICK = "click";
@@ -5059,7 +5151,9 @@ class Viewer extends EventEmitter {
5059
5151
  this._mount = applyProps(new THREE.Object3D(), {
5060
5152
  name: "Mount"
5061
5153
  });
5062
- this._input = this.add(new DeviceInput(input || this._canvas));
5154
+ this._input = this.add(new DeviceInput(input || {
5155
+ source: this._canvas
5156
+ }));
5063
5157
  this.add(Renderer);
5064
5158
  this.addLoader(GLTFLoader);
5065
5159
  this.addLoader(HDRLoader);