1
+ window . onload = function ( ) {
2
+ var navBar = document . querySelector ( '#side-bar' ) ;
3
+ var pageWrapper = document . querySelector ( '.page-wrapper' ) ;
4
+ var pages = document . querySelectorAll ( '.page' ) ;
5
+
6
+ var currentPageIndex = 0 ;
7
+ var previousPageIndex ;
8
+
9
+ var debounceUpDownTime = 700 ;
10
+ var debounceLeftRightTime = 300 ;
11
+
12
+ var acceptUpDownGesture = true ;
13
+ var acceptLeftRightGesture = true ;
14
+
15
+ Leap . loop ( {
16
+ hand : function ( hand ) {
17
+ var handMesh = hand . data ( 'riggedHand.mesh' ) ;
18
+
19
+ var screenPosition = handMesh . screenPosition (
20
+ hand . palmPosition ,
21
+ riggedHandPlugin . camera
22
+ ) ;
23
+ }
24
+ } ,
25
+ function ( frame ) {
26
+ if ( frame . gestures . length > 0 ) {
27
+ for ( var index = 0 ; index < frame . gestures . length ; index ++ ) {
28
+ var gesture = frame . gestures [ index ] ;
29
+
30
+ switch ( gesture . type ) {
31
+ case "swipe" :
32
+ // Horizontal or Vertical?
33
+ var isHorizontal = Math . abs ( gesture . direction [ 0 ] ) > Math . abs ( gesture . direction [ 1 ] ) ;
34
+ var swipeDirection = "" ;
35
+
36
+ if ( isHorizontal ) {
37
+ if ( ! acceptLeftRightGesture ) {
38
+ break ;
39
+ }
40
+
41
+ // Debounce me bby
42
+ acceptLeftRightGesture = false ;
43
+ setTimeout ( function ( ) {
44
+ acceptLeftRightGesture = true ;
45
+ } ,
46
+ debounceLeftRightTime ) ;
47
+
48
+ // Direction
49
+ if ( gesture . direction [ 0 ] > 0 ) {
50
+ swipeDirection = "right" ;
51
+ } else {
52
+ swipeDirection = "left" ;
53
+ }
54
+ scrollPage ( swipeDirection , gesture . speed ) ;
55
+ } else {
56
+ if ( ! acceptUpDownGesture ) {
57
+ break ;
58
+ }
59
+
60
+ // Debounce me bby
61
+ acceptUpDownGesture = false ;
62
+ setTimeout ( function ( ) {
63
+ acceptUpDownGesture = true ;
64
+ } ,
65
+ debounceUpDownTime ) ;
66
+
67
+ // Direction
68
+ if ( gesture . direction [ 1 ] > 0 ) {
69
+ swipeDirection = "up" ;
70
+ } else {
71
+ swipeDirection = "down" ;
72
+ }
73
+ changePage ( swipeDirection ) ;
74
+ }
75
+ break ;
76
+ }
77
+ }
78
+ }
79
+ } )
80
+ . use ( 'riggedHand' )
81
+ . use ( 'handEntry' ) ;
82
+
83
+ riggedHandPlugin = Leap . loopController . plugins . riggedHand ;
84
+
85
+ function scrollPage ( direction , speed ) {
86
+ var currentPage = pages [ currentPageIndex ] ;
87
+ var maximum = currentPage . querySelector ( 'img' ) . clientWidth ;
88
+ if ( direction === "left" ) {
89
+ var scrollTo = pageWrapper . scrollLeft + currentPage . clientWidth ;
90
+ if ( scrollTo < maximum ) {
91
+ $ ( pageWrapper ) . stop ( ) . animate ( { scrollLeft : scrollTo + 'px' } , 500 , 'easeInOutQuart' ) ;
92
+ }
93
+ } else {
94
+ var scrollTo = pageWrapper . scrollLeft - currentPage . clientWidth ;
95
+ if ( scrollTo < 0 ) {
96
+ scrollTo = 0 ;
97
+ }
98
+ $ ( pageWrapper ) . stop ( ) . animate ( { scrollLeft : scrollTo + 'px' } , 500 , 'easeInOutQuart' ) ;
99
+ }
100
+ }
101
+
102
+ function changePage ( direction ) {
103
+ if ( direction === "up" ) {
104
+ // Go to the next page if available
105
+ if ( pages && pages [ currentPageIndex + 1 ] !== undefined ) {
106
+ // Store index for page
107
+ previousPageIndex = currentPageIndex ;
108
+ currentPageIndex = currentPageIndex + 1 ;
109
+ }
110
+ } else {
111
+ // Go to the previous page if available
112
+ if ( pages && pages [ currentPageIndex - 1 ] !== undefined ) {
113
+ // Store index for page
114
+ previousPageIndex = currentPageIndex ;
115
+ currentPageIndex = currentPageIndex - 1 ;
116
+ }
117
+ }
118
+
119
+ // Get pages
120
+ var previousPage = pages [ previousPageIndex ] ;
121
+ var currentPage = pages [ currentPageIndex ] ;
122
+
123
+ // Hide previous page
124
+ if ( previousPage !== undefined && previousPage . classList . contains ( 'active' ) ) {
125
+ previousPage . classList . remove ( 'active' ) ;
126
+ }
127
+
128
+ // Show current page
129
+ if ( ! currentPage . classList . contains ( 'active' ) ) {
130
+ currentPage . classList . add ( 'active' ) ;
131
+ }
132
+
133
+ // Update nav bar
134
+ navBar . className = 'nav-' + currentPageIndex ;
135
+
136
+ // Back to 0
137
+ pageWrapper . scrollLeft = 0 ;
138
+ }
139
+ }
0 commit comments