likashefqet / react-native-image-zoom

A performant and customizable image zoom component built with Reanimated v2+ and TypeScript. 🌃 🚀
https://www.npmjs.com/package/@likashefqet/react-native-image-zoom
MIT License
262 stars 44 forks source link
gesture gesture-handler image image-zoom pan photo picture pinch react react-native react-native-image-zoom reanimated zoom zoomable zoomable-image

REACT NATIVE IMAGE ZOOM

npm NPM npm peer dependency version npm peer dependency version npm bundle size npm npms.io (final) GitHub issues

A performant and customizable image zoom component
built with Reanimated v2+ and TypeScript. 🌃 🚀

Demo:

React Native Image Zoom

Photo by Walling on Unsplash

What's new

Features

Getting Started

To use the ImageZoom component, you first need to install the package via npm or yarn. Run either of the following commands:

npm install @likashefqet/react-native-image-zoom
yarn add @likashefqet/react-native-image-zoom

[!CAUTION]

🚨

Please note that this library is compatible with Reanimated v2 & Reanimated v3 and uses GestureHandler v2.

If you haven't installed Reanimated and Gesture Handler yet, please follow the installation instructions for Reanimated and Gesture Handler.

[!NOTE]

Usage with modals on Android

On Android RNGH does not work by default because modals are not located under React Native Root view in native hierarchy. To fix that, components need to be wrapped with gestureHandlerRootHOC (it's no-op on iOS and web).

Usage

First, import the ImageZoom component from the @likashefqet/react-native-image-zoom library:

import { ImageZoom } from '@likashefqet/react-native-image-zoom';

To use the ImageZoom component, simply pass the uri prop with the URL of the image you want to zoom:

Basic Example

<ImageZoom uri={imageUri} />

Customized Example

<ImageZoom
  ref={ref}
  uri={uri}
  minScale={minScale}
  maxScale={maxScale}
  scale={scale}
  doubleTapScale={3}
  minPanPointers={1}
  isSingleTapEnabled
  isDoubleTapEnabled
  onInteractionStart={() => {
    console.log('onInteractionStart');
    onZoom();
  }}
  onInteractionEnd={() => console.log('onInteractionEnd')}
  onPanStart={() => console.log('onPanStart')}
  onPanEnd={() => console.log('onPanEnd')}
  onPinchStart={() => console.log('onPinchStart')}
  onPinchEnd={() => console.log('onPinchEnd')}
  onSingleTap={() => console.log('onSingleTap')}
  onDoubleTap={(zoomType) => {
    console.log('onDoubleTap', zoomType);
    onZoom(zoomType);
  }}
  onProgrammaticZoom={(zoomType) => {
    console.log('onZoom', zoomType);
    onZoom(zoomType);
  }}
  style={styles.image}
  onResetAnimationEnd={(finished, values) => {
    console.log('onResetAnimationEnd', finished);
    console.log('lastScaleValue:', values?.SCALE.lastValue);
    onAnimationEnd(finished);
  }}
  resizeMode="cover"
/>

Zoomable with Expo Image Example

<Zoomable
  ref={ref}
  minScale={minScale}
  maxScale={maxScale}
  scale={scale}
  doubleTapScale={3}
  minPanPointers={1}
  isSingleTapEnabled
  isDoubleTapEnabled
  onInteractionStart={() => {
    console.log('onInteractionStart');
    onZoom();
  }}
  onInteractionEnd={() => console.log('onInteractionEnd')}
  onPanStart={() => console.log('onPanStart')}
  onPanEnd={() => console.log('onPanEnd')}
  onPinchStart={() => console.log('onPinchStart')}
  onPinchEnd={() => console.log('onPinchEnd')}
  onSingleTap={() => console.log('onSingleTap')}
  onDoubleTap={(zoomType) => {
    console.log('onDoubleTap', zoomType);
    onZoom(zoomType);
  }}
  onProgrammaticZoom={(zoomType) => {
    console.log('onZoom', zoomType);
    onZoom(zoomType);
  }}
  style={styles.image}
  onResetAnimationEnd={(finished, values) => {
    console.log('onResetAnimationEnd', finished);
    console.log('lastScaleValue:', values?.SCALE.lastValue);
    onAnimationEnd(finished);
  }}
>
  <Image style={styles.image} source={{ uri }} contentFit="cover" />
</Zoomable>

Properties

ImageZoom Props

All React Native Image Props &

Property Type Default Description
uri String '' (empty string) The image's URI, which can be overridden by the source prop.
minScale Number 1 The minimum scale allowed for zooming.
maxScale Number 5 The maximum scale allowed for zooming.
doubleTapScale Number 3 The value of the image scale when a double-tap gesture is detected.
minPanPointers Number 2 The minimum number of pointers required to enable panning.
maxPanPointers Number 2 The maximum number of pointers required to enable panning.
isPanEnabled Boolean true Determines whether panning is enabled within the range of the minimum and maximum pan pointers.
isPinchEnabled Boolean true Determines whether pinching is enabled.
isSingleTapEnabled Boolean false Enables or disables the single tap feature.
isDoubleTapEnabled Boolean false Enables or disables the double tap feature. When enabled, this feature prevents automatic reset of the image zoom to its initial position, allowing continuous zooming. To return to the initial position, double tap again or zoom out to a scale level less than 1.
onInteractionStart Function undefined A callback triggered when the image interaction starts.
onInteractionEnd Function undefined A callback triggered when the image interaction ends.
onPinchStart Function undefined A callback triggered when the image pinching starts.
onPinchEnd Function undefined A callback triggered when the image pinching ends.
onPanStart Function undefined A callback triggered when the image panning starts.
onPanEnd Function undefined A callback triggered when the image panning ends.
onSingleTap Function undefined A callback triggered when a single tap is detected.
onDoubleTap Function undefined A callback triggered when a double tap gesture is detected.
onProgrammaticZoom Function undefined A callback function that is invoked when a programmatic zoom event occurs.
onResetAnimationEnd Function undefined A callback triggered upon the completion of the reset animation. It accepts two parameters: finished and values. The finished parameter evaluates to true if all animation values have successfully completed the reset animation; otherwise, it is false, indicating interruption by another gesture or unforeseen circumstances. The values parameter provides additional detailed information for each animation value.

ImageZoom Ref

Property Type Description
reset Function Resets the image zoom, restoring it to its initial position and scale level.
zoom Function Zoom in the image to a given point (x, y) at a given scale level. Calls the reset method if the given scale level is less or equal to 1.

Changelog

Please refer to the Releases section on the GitHub repository. Each release includes a detailed list of changes made to the library, including bug fixes, new features, and any breaking changes. We recommend reviewing these changes before updating to a new version of the library to ensure a smooth transition.

Troubleshooting

Not working on android?

Usage with modals on Android

Author



Shefqet Lika
💻 commits

Support

For ongoing maintenance and updates, your support is greatly appreciated

Buy Me A Coffee

If you need further assistance, feel free to reach out to me by email at @likashefi.

License

The library is licensed under the MIT License.