February 2019
Beginner to intermediate
180 pages
4h 4m
English
To get the velocity of a user's swipe, we'll need to also store the current time along with the touch event's clientX. Let's bind to the browser's performance.now() method:
[@bs.val] [@bs.scope "performance"] external now: unit => float = "";
And we'll make some room for the touch's current time in the touches type:
type touches = { first: option((float, float)), last: option((float, float)),};
In the reducer, we then change Some(clientX) to Some((clientX, now())).
Now, we can calculate the velocity of a user's swipe in the TouchEnd case:
| TouchEnd => if (state.isOpen) { let (x, t) = Belt.Option.getWithDefault(state.touches.first, (0.0, 0.0)); let (x', t') = Belt.Option.getWithDefault(state.touches.last, (0.0, 0.0)); let velocity ...Read now
Unlock full access