11export class Drag {
22
33 constructor ( el , onTranslate = ( ) => { } , onStart = ( ) => { } , onDrag = ( ) => { } ) {
4- this . mouseStart = null ;
4+ this . pointerStart = null ;
55
66 this . el = el ;
77 this . onTranslate = onTranslate ;
@@ -12,46 +12,35 @@ export class Drag {
1212 }
1313
1414 initEvents ( el ) {
15- el . addEventListener ( 'mousedown' , this . down . bind ( this ) ) ;
16- window . addEventListener ( 'mousemove' , this . move . bind ( this ) ) ;
17- window . addEventListener ( 'mouseup' , this . up . bind ( this ) ) ;
18-
19- el . addEventListener ( 'touchstart' , this . down . bind ( this ) ) ;
20- window . addEventListener ( 'touchmove' , this . move . bind ( this ) , {
21- passive : false
22- } ) ;
23- window . addEventListener ( 'touchend' , this . up . bind ( this ) ) ;
24- }
25-
26- getCoords ( e ) {
27- const props = e . touches ? e . touches [ 0 ] : e ;
15+ el . style . touchAction = 'none' ;
2816
29- return [ props . pageX , props . pageY ] ;
17+ el . addEventListener ( 'pointerdown' , this . down . bind ( this ) ) ;
18+ window . addEventListener ( 'pointermove' , this . move . bind ( this ) ) ;
19+ window . addEventListener ( 'pointerup' , this . up . bind ( this ) ) ;
3020 }
3121
3222 down ( e ) {
3323 e . stopPropagation ( ) ;
34- this . mouseStart = this . getCoords ( e ) ;
24+ this . pointerStart = [ e . pageX , e . pageY ]
3525
3626 this . onStart ( e ) ;
3727 }
3828
3929 move ( e ) {
40- if ( ! this . mouseStart ) return ;
30+ if ( ! this . pointerStart ) return ;
4131 e . preventDefault ( ) ;
42- e . stopPropagation ( ) ;
4332
44- let [ x , y ] = this . getCoords ( e ) ;
45- let delta = [ x - this . mouseStart [ 0 ] , y - this . mouseStart [ 1 ] ] ;
33+ let [ x , y ] = [ e . pageX , e . pageY ]
34+ let delta = [ x - this . pointerStart [ 0 ] , y - this . pointerStart [ 1 ] ] ;
4635 let zoom = this . el . getBoundingClientRect ( ) . width / this . el . offsetWidth ;
4736
4837 this . onTranslate ( delta [ 0 ] / zoom , delta [ 1 ] / zoom , e ) ;
4938 }
5039
5140 up ( e ) {
52- if ( ! this . mouseStart ) return ;
41+ if ( ! this . pointerStart ) return ;
5342
54- this . mouseStart = null ;
43+ this . pointerStart = null ;
5544 this . onDrag ( e ) ;
5645 }
5746}
0 commit comments