11'use strict' ;
22
33export function newSwiper ( el , callback ) {
4+ const debug = 0 ? console . log . bind ( console , '[swiper]' ) : function ( ) { } ;
5+
6+ const simulateTwoFingers = false ;
7+
48 // Number of pixels between touch start/end for us to consider it a swipe.
59 const moveThreshold = 50 ;
610
7- var touches = {
8- touchstart : { x : - 1 , y : - 1 } ,
9- touchmove : { x : - 1 , y : - 1 } ,
11+ const fingerDistance = ( touches ) => {
12+ return Math . hypot ( touches [ 0 ] . pageX - touches [ 1 ] . pageX , touches [ 0 ] . pageY - touches [ 1 ] . pageY ) ;
13+ } ;
14+
15+ var touch = {
16+ touchstart : { x : - 1 , y : - 1 , d : - 1 } ,
17+ touchmove : { x : - 1 , y : - 1 , d : - 1 } ,
18+ multitouch : false ,
1019 } ;
1120
1221 // direction returns right, left, up or down or empty if below moveThreshold.
13- touches . direction = function ( ) {
22+ touch . direction = function ( ) {
1423 if ( this . touchmove . x == - 1 ) {
1524 // A regular click.
1625 return '' ;
@@ -27,32 +36,78 @@ export function newSwiper(el, callback) {
2736 return distancex > 0 ? 'right' : 'left' ;
2837 } ;
2938
30- touches . reset = function ( ) {
39+ touch . reset = function ( ) {
3140 ( this . touchstart . x = - 1 ) , ( this . touchstart . y = - 1 ) ;
3241 ( this . touchmove . x = - 1 ) , ( this . touchmove . y = - 1 ) ;
42+ ( this . touchstart . d = - 1 ) , ( this . touchmove . d = - 1 ) ;
43+ this . multitouch = false ;
44+ } ;
45+
46+ touch . update = function ( event , touches ) {
47+ this . multitouch = this . multitouch || touches . length > 1 ;
48+ if ( touches . length > 1 ) {
49+ this [ event . type ] . d = fingerDistance ( touches ) ;
50+ }
51+ this [ event . type ] . x = touches [ 0 ] . pageX ;
52+ this [ event . type ] . y = touches [ 0 ] . pageY ;
3353 } ;
3454
35- touches . update = function ( event , touch ) {
36- this [ event . type ] . x = touch . pageX ;
37- this [ event . type ] . y = touch . pageY ;
55+ const pinch = function ( event , touches ) {
56+ let scale = 1 ;
57+ if ( touches . length === 2 ) {
58+ // Two fingers on screen.
59+ if ( event . scale ) {
60+ scale = event . scale ;
61+ } else {
62+ scale = touch . touchmove . d / touch . touchstart . d ;
63+ scale = Math . round ( scale * 100 ) / 100 ;
64+ }
65+ if ( scale < 1 ) {
66+ scale = 1 ;
67+ }
68+ let distancex = touch . touchmove . x - touch . touchstart . x ;
69+ let distancey = touch . touchmove . y - touch . touchstart . y ;
70+
71+ el . style . transform = `translate3d(${ distancex } px, ${ distancey } px, 0) scale(${ scale } )` ;
72+ el . style . zIndex = 1000 ;
73+ } else {
74+ // One finger on screen.
75+ el . style . transform = '' ;
76+ el . style . zIndex = '' ;
77+ }
3878 } ;
3979
4080 var handleTouch = function ( event ) {
81+ debug ( 'event' , event . type ) ;
4182 if ( typeof event !== 'undefined' && typeof event . touches !== 'undefined' ) {
42- var touch = event . touches [ 0 ] ;
83+ let touches = event . touches ;
84+
85+ if ( simulateTwoFingers && touches . length === 1 ) {
86+ touches = [
87+ { pageX : touches [ 0 ] . pageX , pageY : touches [ 0 ] . pageY } ,
88+ { pageX : 0 , pageY : 0 } ,
89+ ] ;
90+ }
4391 switch ( event . type ) {
4492 case 'touchstart' :
45- touches . reset ( ) ;
46- touches . update ( event , touch ) ;
93+ touch . reset ( ) ;
94+ touch . update ( event , touches ) ;
4795 break ;
4896 case 'touchmove' :
49- touches . update ( event , touch ) ;
97+ touch . update ( event , touches ) ;
98+ pinch ( event , touches ) ;
5099 break ;
51100 case 'touchend' :
52- let direction = touches . direction ( ) ;
53- if ( direction ) {
54- callback ( direction ) ;
101+ el . style . transform = '' ;
102+ if ( ! touch . multitouch ) {
103+ // Only consider a swipe if we have one finger on screen.
104+ let direction = touch . direction ( ) ;
105+ debug ( 'direction' , direction ) ;
106+ if ( direction ) {
107+ callback ( direction ) ;
108+ }
55109 }
110+
56111 break ;
57112 default :
58113 break ;
0 commit comments