source: imaps-frontend/node_modules/react-use/esm/usePinchZoom.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[d565449]1import { useEffect, useMemo, useState } from 'react';
2export var ZoomState;
3(function (ZoomState) {
4 ZoomState["ZOOMING_IN"] = "ZOOMING_IN";
5 ZoomState["ZOOMING_OUT"] = "ZOOMING_OUT";
6})(ZoomState || (ZoomState = {}));
7var usePinchZoom = function (ref) {
8 var cacheRef = useMemo(function () { return ({
9 evCache: [],
10 prevDiff: -1,
11 }); }, [ref.current]);
12 var _a = useState(), zoomingState = _a[0], setZoomingState = _a[1];
13 var pointermove_handler = function (ev) {
14 // This function implements a 2-pointer horizontal pinch/zoom gesture.
15 //
16 // If the distance between the two pointers has increased (zoom in),
17 // the target element's background is changed to 'pink' and if the
18 // distance is decreasing (zoom out), the color is changed to 'lightblue'.
19 //
20 // This function sets the target element's border to 'dashed' to visually
21 // indicate the pointer's target received a move event.
22 // Find this event in the cache and update its record with this event
23 for (var i = 0; i < cacheRef.evCache.length; i++) {
24 if (ev.pointerId == cacheRef.evCache[i].pointerId) {
25 cacheRef.evCache[i] = ev;
26 break;
27 }
28 }
29 // If two pointers are down, check for pinch gestures
30 if (cacheRef.evCache.length == 2) {
31 // console.log(prevDiff)
32 // Calculate the distance between the two pointers
33 var curDiff = Math.abs(cacheRef.evCache[0].clientX - cacheRef.evCache[1].clientX);
34 if (cacheRef.prevDiff > 0) {
35 if (curDiff > cacheRef.prevDiff) {
36 // The distance between the two pointers has increased
37 setZoomingState([ZoomState.ZOOMING_IN, curDiff]);
38 }
39 if (curDiff < cacheRef.prevDiff) {
40 // The distance between the two pointers has decreased
41 setZoomingState([ZoomState.ZOOMING_OUT, curDiff]);
42 }
43 }
44 // Cache the distance for the next move event
45 cacheRef.prevDiff = curDiff;
46 }
47 };
48 var pointerdown_handler = function (ev) {
49 // The pointerdown event signals the start of a touch interaction.
50 // This event is cached to support 2-finger gestures
51 cacheRef.evCache.push(ev);
52 // console.log('pointerDown', ev);
53 };
54 var pointerup_handler = function (ev) {
55 // Remove this pointer from the cache and reset the target's
56 // background and border
57 remove_event(ev);
58 // If the number of pointers down is less than two then reset diff tracker
59 if (cacheRef.evCache.length < 2) {
60 cacheRef.prevDiff = -1;
61 }
62 };
63 var remove_event = function (ev) {
64 // Remove this event from the target's cache
65 for (var i = 0; i < cacheRef.evCache.length; i++) {
66 if (cacheRef.evCache[i].pointerId == ev.pointerId) {
67 cacheRef.evCache.splice(i, 1);
68 break;
69 }
70 }
71 };
72 useEffect(function () {
73 if (ref === null || ref === void 0 ? void 0 : ref.current) {
74 ref.current.onpointerdown = pointerdown_handler;
75 ref.current.onpointermove = pointermove_handler;
76 ref.current.onpointerup = pointerup_handler;
77 ref.current.onpointercancel = pointerup_handler;
78 ref.current.onpointerout = pointerup_handler;
79 ref.current.onpointerleave = pointerup_handler;
80 }
81 }, [ref === null || ref === void 0 ? void 0 : ref.current]);
82 return zoomingState
83 ? { zoomingState: zoomingState[0], pinchState: zoomingState[1] }
84 : { zoomingState: null, pinchState: 0 };
85};
86export default usePinchZoom;
Note: See TracBrowser for help on using the repository browser.