source: imaps-frontend/node_modules/react-use/lib/usePinchZoom.js

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

Update repo after prototype presentation

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