Skip to content

Commit 69de268

Browse files
author
Weera Wu
committed
Use enums for touch types
1 parent fc06a4e commit 69de268

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

index.html

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@
130130
<script src="//unpkg.com/mithril@^1.1.6/mithril.js"></script>
131131
<script>
132132
// setup
133-
var Direction = {LEFT: 0, RIGHT: 1};
134133
var Theme = {BLACK: 0, WHITE: 1, GRADIENT: 2, SHOWROOM: 3};
135-
var Time = {TAP: 250, HOLD: 500};
134+
var TouchDirection = {LEFT: 0, CENTER: 1, RIGHT: 2};
135+
var TouchType = {TAP: 0, PRESS: 1, HOLD: 2};
136136

137137
var Fullscreen = {
138138
isEnabled: function() {
@@ -171,7 +171,7 @@
171171
index: 0,
172172
pointer: true,
173173
theme: Theme.GRADIENT,
174-
touch: {direction: null, time: null},
174+
touch: {direction: null, start: null},
175175

176176
setIndex: function(value) {
177177
if (value >= 0 && value < state.slides.length) {
@@ -189,9 +189,9 @@
189189
}
190190
},
191191
setTouch: function(value) {
192-
if (value in Object.values(Direction)) {
192+
if (value in Object.values(TouchDirection)) {
193193
state.touch.direction = value;
194-
state.touch.time = Date.now();
194+
state.touch.start = Date.now();
195195
}
196196
},
197197

@@ -200,7 +200,7 @@
200200
},
201201
clearTouch: function() {
202202
state.touch.direction = null;
203-
state.touch.time = null;
203+
state.touch.start = null;
204204
},
205205
currentSlide: function() {
206206
return state.slides.length > 0 ? state.slides[state.index] : {};
@@ -230,9 +230,18 @@
230230
nextTheme: function() {
231231
state.setTheme((state.theme + 1) % Object.keys(Theme).length);
232232
},
233-
touchTime: function() {
234-
var startTime = state.touch.time;
235-
return startTime ? Date.now() - startTime : 0;
233+
touchType: function() {
234+
if (!state.touch.start) {
235+
return null;
236+
}
237+
var elapsedTime = Date.now() - state.touch.start;
238+
if (elapsedTime > 500) {
239+
return TouchType.HOLD;
240+
} else if (elapsedTime > 250) {
241+
return TouchType.PRESS;
242+
} else {
243+
return TouchType.TAP;
244+
}
236245
},
237246
togglePointer: function() {
238247
state.pointer = !state.pointer;
@@ -280,7 +289,7 @@
280289
var touch = event.touches[0];
281290
var pageCenter = document.body.clientWidth / 2;
282291
state.setTouch(
283-
touch.pageX < pageCenter ? Direction.LEFT : Direction.RIGHT
292+
touch.pageX < pageCenter ? TouchDirection.LEFT : TouchDirection.RIGHT
284293
);
285294
},
286295
handleTouchmove: function(event) {
@@ -292,14 +301,14 @@
292301
if (event.touches.length > 0) {
293302
return;
294303
}
295-
var time = state.touchTime();
296-
if (time > Time.HOLD) {
304+
var type = state.touchType();
305+
var direction = state.touch.direction;
306+
if (type === TouchType.HOLD) {
297307
state.nextTheme();
298-
} else if (time > Time.TAP) {
299-
} else if (state.touch.direction === Direction.LEFT) {
308+
} else if (type === TouchType.TAP && direction === TouchDirection.LEFT) {
300309
this.play(state.backward);
301310
m.redraw();
302-
} else if (state.touch.direction === Direction.RIGHT) {
311+
} else if (type === TouchType.TAP && direction === TouchDirection.RIGHT) {
303312
this.play(state.forward);
304313
}
305314
state.clearTouch();

0 commit comments

Comments
 (0)