|
1 |
| -import { useRef, useEffect, useCallback, useLayoutEffect } from 'react'; |
| 1 | +import { useRef, useEffect, useCallback } from 'react'; |
2 | 2 |
|
3 | 3 | // Saves incoming handler to the ref in order to avoid "useCallback hell"
|
4 | 4 | export function useEventCallback<T, K>(handler?: (value: T, event: K) => void): (value: T, event: K) => void {
|
@@ -36,19 +36,37 @@ export interface Interaction {
|
36 | 36 | y: number;
|
37 | 37 | }
|
38 | 38 |
|
| 39 | +// // Returns a relative position of the pointer inside the node's bounding box |
| 40 | +// export const getRelativePosition = (node: HTMLDivElement, event: MouseEvent | TouchEvent): Interaction => { |
| 41 | +// const rect = node.getBoundingClientRect(); |
| 42 | + |
| 43 | +// // Get user's pointer position from `touches` array if it's a `TouchEvent` |
| 44 | +// const pointer = isTouch(event) ? event.touches[0] : (event as MouseEvent); |
| 45 | + |
| 46 | +// return { |
| 47 | +// left: clamp((pointer.pageX - (rect.left + window.pageXOffset)) / rect.width), |
| 48 | +// top: clamp((pointer.pageY - (rect.top + window.pageYOffset)) / rect.height), |
| 49 | +// width: rect.width, |
| 50 | +// height: rect.height, |
| 51 | +// x: pointer.pageX - (rect.left + window.pageXOffset), |
| 52 | +// y: pointer.pageY - (rect.top + window.pageYOffset), |
| 53 | +// }; |
| 54 | +// }; |
| 55 | + |
39 | 56 | // Returns a relative position of the pointer inside the node's bounding box
|
40 | 57 | export const getRelativePosition = (node: HTMLDivElement, event: MouseEvent | TouchEvent): Interaction => {
|
41 | 58 | const rect = node.getBoundingClientRect();
|
42 |
| - |
43 |
| - // Get user's pointer position from `touches` array if it's a `TouchEvent` |
44 | 59 | const pointer = isTouch(event) ? event.touches[0] : (event as MouseEvent);
|
45 | 60 |
|
| 61 | + const x = clamp(pointer.pageX - (rect.left + window.pageXOffset), 0, rect.width); |
| 62 | + const y = clamp(pointer.pageY - (rect.top + window.pageYOffset), 0, rect.height); |
| 63 | + |
46 | 64 | return {
|
47 |
| - left: clamp((pointer.pageX - (rect.left + window.pageXOffset)) / rect.width), |
48 |
| - top: clamp((pointer.pageY - (rect.top + window.pageYOffset)) / rect.height), |
| 65 | + left: x / rect.width, |
| 66 | + top: y / rect.height, |
49 | 67 | width: rect.width,
|
50 | 68 | height: rect.height,
|
51 |
| - x: pointer.pageX - (rect.left + window.pageXOffset), |
52 |
| - y: pointer.pageY - (rect.top + window.pageYOffset), |
| 69 | + x, |
| 70 | + y, |
53 | 71 | };
|
54 | 72 | };
|
0 commit comments