• Stars
    star
    449
  • Rank 97,328 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A zooming and panning plugin inspired by google photos for your web images.

iv-viewer

All Contributors A zooming and panning plugin inspired by google photos for your web images.

Features

  • Smooth dragging and panning of images.
  • Support touch devices.
  • Double tap to zoom in/zoom out.
  • Pinch in/out to control zoom.
  • Snap view for better panning and zooming experience.
  • Allow quick display of loaded image then loading of high quality image progressively.
  • Exposed API to control zoom programmatically.
  • Custom Events to listen for the state changes.

alt tag

Install

Through npm

npm install iv-viewer --save

Or get compiled development and production version (css and js) from ./dist

Usage

Import ImageViewer and it's style.

import ImageViewer from 'iv-viewer';

// or
const ImageViewer = require('iv-viewer').default; 

// Import css
import 'iv-viewer/dist/iv-viewer.css';

You can choose to import css file inside your scss/less files.

You can also use the standalone UMD build by including dist/iv-viewer.js and dist/iv-viewer.css in your page.

<script src="https://unpkg.com/iv-viewer/dist/iv-viewer.js"></script>

<link rel="stylesheet" href="https://unpkg.com/iv-viewer/dist/iv-viewer.css">

Three different modes

Full-Screen Mode

If you want to show images in full screen, with predefined styles. You can use FullScreenViewer.

import { FullScreenViewer } from 'iv-viewer';

const viewer = new FullScreenViewer(options); // check options section for supported options

viewer.show('images/low-res-img', 'images/hi-res-img'); //second option is optional. Check better image loading section

Container Mode

If you have your own container to show images (you might have custom layout or gallery), you can use image-viewer in a container mode.

<div id="image-container"></div>
import ImageViewer from 'iv-viewer';

const container = document.querySelector('#image-container');
const viewer = new ImageViewer(container, options); //check options section for supported options

viewer.load('images/low-res-img', 'images/hi-res-img'); //second option is optional. Check better image loading section

Image Mode

If you just want to add zoom and pan gesture to your images in a image-viewer style, you can use image-viewer in image mode.

<img id="image" src="image.png" data-high-res-src="hi-res-image.png" />
import ImageViewer from 'iv-viewer';

const image = document.querySelector('#image');
const viewer = new ImageViewer(image, options); // check options section for supported options

Options

Option Allowed Value Default Description
zoomValue number in percentage 100 It defines the initial zoom value of the image.
maxZoom number in percentage 500 It defines the max limit for the zoom value of the image.
snapView boolean true If true will show snap view.
refreshOnResize boolean true Defines whether to refresh the viewer on resize of window. This is available only for Container and Image mode. On FullScreen mode it will refresh on window resize by default.
zoomOnMouseWheel boolean true Defines weather to allow zoom with mouse scroll or not.
hasZoomButtons boolean true Defines weather to add zoom buttons or not
zoomStep number 50 The number of which the zoom should increase/decrease when the buttons are clicked
listeners object null multiple useful callbacks that could use in-order to get the current state of the viewer

The Listeners

There are multiple listeners you can register with each viewer instance

import ImageViewer from 'iv-viewer';

const viewer = new ImageViewer(element, { 
  listeners: { 
    onInit: callback(data), // called when the instance is initiated 
    onDestroy: callback(), // called when the instance is destroyed
    onImageLoaded: callback(data), // called on image load
    onZoomChange: callback(data), // called on zoom in/out
  } 
});

Callback Data

The data passed to each callback is very useful, it contains the current instance with more info that you can use to react to the instance state

Option dataType Description
container HTMLElement The current container of the viewer
snapView HTMLElement The snap view element in the viewer
zoomValue Number The current zoom value
reachedMin boolean A boolean value that determine if the zoom value reached the initial zoom.
reachedMax boolean A boolean value that determine if the zoom value reached the maximum zoom.
instance ImageViewer The current instance which contains all other info if needed

API (ImageViewer)

Creating an instance

import ImageViewer from 'iv-viewer';

const viewer = new ImageViewer(element, options);

Here the first argument is the element, which can be container where viewer will be loaded, or it can be a image in which case viewer will be initialized in a image mode.

You can also pass a selector directly instead of a DOM element.

const viewer = new ImageViewer('#element', options);

Second argument is to provide configuration options for the ImageViewer. This argument is optional.

Instance methods

load(imgSrc, highResImg)

Load an image to the viewer. You can pass two different resolution of the image as first and second argument (optional). See why do you need it at better image loading section.

viewer.load('image.png', 'hi-res-image.png');

Note that this is just required for the container mode, as in image mode you can just use src and data-high-res-src attribute and the image will load by itself. See image mode example

zoom(zoomValue, point)

zoomValue: A percentage value to which you want to zoom the image.

point(optional): Point {x, y} is the coordinate of the container which would act as the center for the zoom. If not defined, it will take the center of the container as the zooming point.

viewer.zoom(300, { x: 500, y: 500 });

resetZoom()

Reset zoom to the default or provided initial zoomValue.

viewer.resetZoom();

refresh()

Method to manually refresh the viewer. It will reset the zoom and pan. It will also recalculate the dimension of container, window and image in case if that is changed.

viewer.refresh();

destroy()

Destroys the plugin instance and unbind all events. It will also reset the container or the image(if ImageViewer is used in image mode). It returns null which you should assign to viewer variable to completely free up memory.

viewer = viewer.destroy();

API (FullScreenViewer)

FullScreenViewer is extended from ImageViewer. So it shares the same ImageViewer api along with some FullScreenViewer API.

Creating an instance

import { FullScreenViewer } from 'iv-viewer';

const viewer = new FullScreenViewer(options);

Unlike ImageViewer you don't have to pass container for the viewer as it will be initialized in pre-defined full screen container.

First argument is to provide configuration options for the FullScreenViewer. This argument is optional.

Instance methods (FullScreenViewer)

FullScreenViewer inherits all the instance method of ImageViewer. In additional to that it has following methods.

show(imgSrc, highResImg)

Show the full screen viewer with passed image on the show method. You can pass two different resolution of the image as first and second argument (optional). See why do you need it at better image loading section.

viewer.show('image.png', 'hi-res-image.png');

hide()

Hide/Close the fullScreen viewer.

viewer.hide();

Better image loading

To improve the perceived experience, it is always recommended to show the already loaded image or the low quality image on the viewer and let the ImageViewer load high quality image in parallel.

It will also try to preview the high quality image while it's loading so it will give a progressive loading effect.

ImageViewer provides api to do this. Container mode

viewer.load('image.png', 'hi-res-image.png');

FullScreen mode

viewer.show('image.png', 'hi-res-image.png');

Image Mode

<img id="image" src="image.png" data-high-res-src="hi-res-image.png" />

In all of the above example it will first try to display the first image and then load the second image (if passed) in parallel.

The second image is optional, which you should pass when you feel you can improve the image loading experience by first showing low quality image and then progressively update it with high quality image.

Demo

codesandbox preview link: https://8ypwzryom0.codesandbox.io/

codesandbox link: https://codesandbox.io/s/8ypwzryom0

Like this

⭐ this repo

Major updates

v2.1.0

  • It's a complete rewrite of ImageViewer with no jQuery requirement in ES6.
  • While the options and instance method are same the way you use a ImageViewer and FullScreenView is changed. The v1 API will no longer be supported.
  • Published on the npm. V1 was not available on npm.
  • Changed the package name to iv-viewer. image-viewer name was not available on npm.
  • Added some listeners
  • Added zoom in/out buttons
  • Some SCSS variable for easier overrides.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Ruchika
Ruchika

πŸ“–
Sudhanshu Yadav
Sudhanshu Yadav

πŸ“–
Amrit Kahlon
Amrit Kahlon

πŸ“–
10000
10000

πŸ“–

This project follows the all-contributors specification. Contributions of any kind welcome!

More Repositories

1

react-number-format

React component to format numbers in an input or as a text.
JavaScript
3,858
star
2

patternLock

A light weight plugin to simulate android like pattern lock interface for your hybrid app or website.
JavaScript
378
star
3

angulargrid

Pinterest like responsive masonry grid system for angular
JavaScript
277
star
4

radialIndicator

A simple and light weight circular indicator / progressbar plugin.
JavaScript
232
star
5

react-meta-tags

Handle document meta/head tags in isomorphic react with ease.
JavaScript
226
star
6

contextMenu.js

contextMenu.js is a plugin to create windows like context menu with keyboard interaction, different type of inputs ,trigger events and much more.
JavaScript
208
star
7

ScrollMenu

A new interface to replace your old boring scrollbar
JavaScript
201
star
8

jsonQ

A JavaScript library to make manipulation and extraction of data from a JSON very easy and fast.
JavaScript
201
star
9

relative-to-alias

πŸ› οΈ A codemod to do large-scale refactor of your relative path imports to alias.
JavaScript
132
star
10

FlakeId

Twittter Snowflake like unique id generator plugin for nodejs and browser
JavaScript
76
star
11

modalBox.js

A very light weight and minimal plugin to display modal window.
JavaScript
43
star
12

patternCaptcha

Android like pattern matching captcha system for your node webapps.
JavaScript
42
star
13

github-turbo-pr

Chrome extension to optimize github for handling big pull request. πŸš€
JavaScript
25
star
14

eventPause.js

eventPause is a tiny plugin with lots of methods to control events. So whenever you want you can pause and activate any event.
JavaScript
21
star
15

create-effect

A small utility to create custom hooks using useEffect
JavaScript
9
star
16

fqueue

A micro-plugin to queue function execution to handle asynchronous flow and stepping through functions.
JavaScript
7
star
17

coequal.js

coequal is a small utility function to check equality of all data types and objects in JavaScript.
JavaScript
6
star
18

node-website-build-script

Boilerplate to use node.js as a build script for web projects.
CSS
5
star
19

LongListScroller

A small plugin to handle long list scroll using IScroll
JavaScript
3
star
20

findR.js

A find and replace JavaScript plugin for webpages.
JavaScript
3
star
21

deferred

A mini deferred plugin, having just what you need
JavaScript
2
star
22

ask-me-anything

A place where you can ask me anything about frontend, architecture, performance or general career stuff
2
star
23

number-format-app

JavaScript
1
star
24

class-to-function-with-react-hooks

Examples to map lifecycle method of a class component to a functional component with react hooks
JavaScript
1
star
25

isomorphic_react_sample

An isomorphic react sample app.
JavaScript
1
star
26

babel-plugin-react-create-element-alias

A babel plugin to replace React.createElement to an alias
JavaScript
1
star
27

patch-commits

Patch all commits to have desired changes in a branch
JavaScript
1
star
28

s-yadav.github.com

HTML
1
star
29

useful-array-prototypes

This repository contains some useful prototypes which native javascript dont provide or have some perfomance gain over native javascript functionality.
JavaScript
1
star