rs-react-native-image-gallery
Version:
React Native Image Gallery with Thumbnails
37 lines (29 loc) • 1.08 kB
text/typescript
import { NativeTouchEvent } from 'react-native';
/**
* Calculate squared distance between two points
* @param a First coordinate
* @param b Second coordinate
*/
export const pow2abs = (a: number, b: number): number => (a - b) ** 2;
/**
* Get distance between two touch points
* @param touches Array of touch events
*/
export const getDistance = (touches: Array<NativeTouchEvent>): number => {
// Early validation
if (!touches || touches.length < 2) return 0;
const [a, b] = touches;
// Null check with optional chaining
if (!a?.pageX || !b?.pageX || !a?.pageY || !b?.pageY) return 0;
return Math.sqrt(pow2abs(a.pageX, b.pageX) + pow2abs(a.pageY, b.pageY));
};
/**
* Calculate scale based on distance ratio with a multiplier
* @param currentDistance Current distance between touch points
* @param initialDistance Initial distance between touch points
*/
export const getScale = (currentDistance: number, initialDistance: number): number => {
// Prevent division by zero
if (!initialDistance) return 1;
return (currentDistance / initialDistance) * 1.2;
};