9.0.2 • Published 1 month ago

vanilla-js-wheel-zoom v9.0.2

Weekly downloads
658
License
MIT
Repository
github
Last release
1 month ago

vanilla-js-wheel-zoom

Image resizing using mouse wheel (pinch to zoom) + drag scrollable image (as well as any HTML content)

GitHub tag (latest by date) GitHub stars GitHub issues GitHub forks npm.io

Advantages:

  • the ability to fit the image into a container of any proportion
  • the ability to scale any HTML content
  • touch screen devices support

Starting with version 5, the plugin switched to using style transform. To use the plugin in older browsers, switch to earlier versions.

You need to center the image (or any HTML content) in the "viewport" in which scaling will take place. The "viewport" is taken automatically as the parent of the image in DOM.

HTML content can be of any structure, but the topmost child element in the “viewport” must be one. In the example with "badge" below, it will be more clear what is meant.

🖐 If you find my plugin helpful, please donate me 🤝

Demo (only one image)

Demo (multi images)

Demo (html)

Install

npm i vanilla-js-wheel-zoom

or

yarn add vanilla-js-wheel-zoom

Get started

#myViewport {
    display: flex;
    align-items: center;
    justify-content: center;
}
<div id="myViewport" style="width:600px;height:600px;">
    <img id="myContent" src="https://via.placeholder.com/2400x1400" alt="image" />
</div>
WZoom.create('#myContent');

Syntax & Parameters

/**
 * Create WZoom instance
 * @param {string|HTMLElement} selectorOrHTMLElement
 * @param {Object} [options]
 * @returns {WZoom}
 */
const wzoom = WZoom.create(selectorOrHTMLElement[, options]);

Badge on the image

#myViewport {
    display: flex;
    align-items: center;
    justify-content: center;
}

#myBadge {
    position: absolute;
    border: solid 2px red;
    font-size: 80px;
}

#myImage {
    width: auto;
    height: auto;
    margin: auto;
}
<div id="myViewport" style="width:600px;height:600px;">
    <div id="myContent">
        <div id="myBadge" style="left:900px;top:500px;">Badge</div>
        <img id="myImage" src="https://via.placeholder.com/2500x1500" alt="image"/>
    </div>
</div>
WZoom.create('#myContent', {
    type: 'html',
    width: 2500,
    height: 1500,
});

Control buttons

<button data-zoom-up>Zoom Up</button>
<button data-zoom-down>Zoom Down</button>
const wzoom = WZoom.create('img');

document.querySelector('[data-zoom-up]').addEventListener('click', () => {
    wzoom.zoomUp();
});

document.querySelector('[data-zoom-down]').addEventListener('click', () => {
    wzoom.zoomDown();
});

On window resize

const wzoom = WZoom.create('img');

window.addEventListener('resize', () => {
    wzoom.prepare();
});

How to rotate the image?

Try this 😉 (and see demo)

Callbacks onMaxScaleReached() / onMinScaleReached()

There are no such, but you can get the desired behavior (and see demo)

Saving image state on page reload

See demo

Playground...

Have some fun 🤸‍♂️

Options

nametypedefaultnote
typeStringimageimage - if you need to scale only one image. In this case, there is no need to pass the parameters width and height. html - if you need to scale the HTML code. It is advisable to specify the parameters width and height that correspond to the original full size of the HTML content.
widthNumbernullFor type image computed auto (if width set null), for type html need set real html content width, else computed auto.
heightNumbernullFor type image computed auto (if height set null), for type html need set real html content height, else computed auto.
minScaleNumbernullThe minimum scale to which the image can be zoomed.If falsy or greater than maxScale then computed auto.
maxScaleNumber1The maximum scale to which the image can be zoomed.1 means that the image can be maximized to 100%, 2 - 200%, etc.
speedNumber1.1Factor with which the image will be scaled.The larger the value, the larger the step.Can tend to 1, but should not be equal to it (ex. 1.05, 1.005) or can be greater (ex. 1.5, 2, 5, 10)
zoomOnClickBooleantrueZoom to maximum (minimum) size on click.
zoomOnDblClickBooleanfalseZoom to maximum (minimum) size on double click. If true then zoomOnClick = false
prepareFunctionundefinedCalled after the script is initialized when the image is scaled and fit into the container. Gets WZoom instance as the first argument.
rescaleFunctionundefinedCalled on every change of scale. Gets WZoom instance as the first argument.
alignContentStringcenterAlign content center, left, top, right, bottom
smoothTimeNumber.25Time of smooth extinction. if 0 then no smooth extinction. Disabled for touch devices. (value in seconds)
disableWheelZoomBooleanfalse
reverseWheelDirectionBooleanfalseReverse wheel zoom direction
dragScrollableBooleantrueIf true - scaled image can be dragged with the mouse to see parts of the image that are out of scale.
smoothTimeDragNumbersmoothTimeOptional override to smoothTime for mouse drag/pan actions.Setting low (or 0) allows fluid drag actions, while maintaining zoom-smoothness from higher smoothTime.If not provided, matches whatever smoothTime resolves to: smoothTime's provided value or its default.
onGrabFunctionundefinedCalled after grabbing an element. Gets the event and WZoom instance as the arguments.
onMoveFunctionundefinedCalled on every tick when moving element. Gets the event and WZoom instance as the arguments.
onDropFunctionundefinedCalled after dropping an element. Gets the event and WZoom instance as the arguments.

API

namenote
.prepare()Reinitialize script
.transform(top, left, scale)Rebuild content state with passed params
.zoomUp()Zoom on one step (see option speed)
.maxZoomUp()Zoom to max scale
.zoomDown()Zoom out on one step (see option speed)
.maxZoomDown()Zoom to min scale
.zoomUpToPoint({x, y})Zoom on one step to point (see option speed)
.zoomDownToPoint({x, y})Zoom out on one step to point (see option speed)
.maxZoomUpToPoint({x, y})Zoom to max scale to point
.destroy()Destroy object

License

MIT

9.0.2

1 month ago

9.0.1

3 months ago

8.2.0

5 months ago

9.0.0

5 months ago

8.1.0

7 months ago

7.0.2

11 months ago

7.0.1

11 months ago

8.0.1

7 months ago

8.0.0

11 months ago

8.0.2

7 months ago

6.22.0

1 year ago

6.24.0

1 year ago

6.23.0

1 year ago

6.21.0

1 year ago

7.0.0

12 months ago

6.20.0

1 year ago

6.19.0

1 year ago

6.17.2

2 years ago

6.17.3

2 years ago

6.17.0

2 years ago

6.17.1

2 years ago

6.17.4

2 years ago

6.18.1

2 years ago

6.18.2

1 year ago

6.18.0

2 years ago

6.18.5

1 year ago

6.18.3

1 year ago

6.18.4

1 year ago

6.15.0

2 years ago

6.16.0

2 years ago

6.14.0

2 years ago

6.13.0

2 years ago

6.11.0

2 years ago

6.12.0

2 years ago

6.10.1

3 years ago

6.10.0

3 years ago

6.9.1

3 years ago

6.8.2

3 years ago

6.9.0

3 years ago

6.8.1

3 years ago

6.8.0

3 years ago

6.7.2

3 years ago

6.7.3

3 years ago

6.7.0

3 years ago

6.7.1

3 years ago

6.6.2

3 years ago

6.6.1

3 years ago

6.6.0

3 years ago

6.5.1

3 years ago

6.5.0

3 years ago

6.4.1

3 years ago

6.4.0

3 years ago

6.3.2

3 years ago

6.3.1

3 years ago

6.3.0

3 years ago

6.2.2

3 years ago

6.2.1

4 years ago

6.2.0

4 years ago

6.1.1

4 years ago

6.1.0

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

5.1.1

4 years ago

5.1.0

4 years ago

5.0.9

4 years ago

5.0.8

4 years ago

5.0.6

4 years ago

5.0.7

4 years ago

5.0.5

4 years ago

4.3.4

4 years ago

4.3.3

4 years ago

4.3.2

4 years ago

4.3.1

4 years ago

4.3.0

4 years ago

4.2.1

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.0

4 years ago

3.2.3

4 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

2.2.2

5 years ago