All Projects → luncheon → svg-pan-zoom-container

luncheon / svg-pan-zoom-container

Licence: WTFPL license
A vanilla-js module for adding zoom-on-wheel and pan-on-drag behavior to inline SVG elements.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to svg-pan-zoom-container

ZoomMarker
A jQuery plugin for scrolling and zooming in and out of the image
Stars: ✭ 81 (+161.29%)
Mutual labels:  pan, zoom
React Prismazoom
A pan and zoom component for React, using CSS transformations.
Stars: ✭ 29 (-6.45%)
Mutual labels:  pan, zoom
Flutter advanced networkimage
flutter advanced network image provider
Stars: ✭ 282 (+809.68%)
Mutual labels:  pan, zoom
Chartjs Plugin Zoom
Zoom and pan plugin for Chart.js
Stars: ✭ 404 (+1203.23%)
Mutual labels:  pan, zoom
Panzoom
A library for panning and zooming elements using CSS transforms 🔍
Stars: ✭ 1,593 (+5038.71%)
Mutual labels:  pan, zoom
core
Renderer for tldraw and maybe you, too.
Stars: ✭ 418 (+1248.39%)
Mutual labels:  pan, zoom
React Svg Pan Zoom
👀 A React component that adds pan and zoom features to SVG
Stars: ✭ 569 (+1735.48%)
Mutual labels:  pan, zoom
react-easy-panzoom
Wrapper to enable pan and zoom features for any React component
Stars: ✭ 69 (+122.58%)
Mutual labels:  pan, zoom
Tileview
TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images. Plugins are available for features like markers, hotspots, and path drawing.
Stars: ✭ 1,447 (+4567.74%)
Mutual labels:  pan, zoom
Panzoom
Universal pan and zoom library (DOM, SVG, Custom)
Stars: ✭ 1,003 (+3135.48%)
Mutual labels:  pan, zoom
React Pan Zoom
Generic html wrapper for pan and zoom feature for react canvas like components
Stars: ✭ 31 (+0%)
Mutual labels:  pan, zoom
Panandzoom
Pan and zoom control for Avalonia.
Stars: ✭ 159 (+412.9%)
Mutual labels:  pan, zoom
Easypz
Easy Pan and Zoom JS Library
Stars: ✭ 125 (+303.23%)
Mutual labels:  pan, zoom
ngx-ionic-image-viewer
An Ionic 4 Angular component to view & zoom on images and photos without any additional dependencies.
Stars: ✭ 129 (+316.13%)
Mutual labels:  directive, zoom
js-study-lv1
단국대학교 자바스크립트 스터디 레벨 1 과정을 위한 저장소
Stars: ✭ 18 (-41.94%)
Mutual labels:  vanilla-js
angular-ellipsis
A simple lightweight library for Angular which removes excess text and add ellipsis symbol to end of text before text overflows container
Stars: ✭ 16 (-48.39%)
Mutual labels:  directive
mattermost-plugin-zoom
Zoom plugin for Mattermost 🔌
Stars: ✭ 93 (+200%)
Mutual labels:  zoom
magnify-3d
Real time Javascript optic magnifying glass library.
Stars: ✭ 37 (+19.35%)
Mutual labels:  zoom
GithubSquare
Explore the most starred projects , trending developers , popular repositories of any date and face off Github Profiles of your choice.
Stars: ✭ 37 (+19.35%)
Mutual labels:  vanilla-js
angular-cron-gen
A basic way to for users to graphically build a cron expression using Angular.
Stars: ✭ 36 (+16.13%)
Mutual labels:  directive

svg-pan-zoom-container

BundlePhobia License: WTFPL npm jsDelivr

A vanilla-js module for adding zoom-on-wheel and pan-on-drag behavior to inline SVG elements.
No need to write scripts. Just markup.

Demo

Usage

  1. Load this module.
  2. Diddle the parent element of the inline SVG element:
    • Add data-zoom-on-wheel attribute to add zoom-on-wheel behavior.
    • Add data-pan-on-drag attribute to add pan-on-drag behavior.
    • Make sure that the container's height is not "auto". The container's height must not be calculated from its content.

That's it!

<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

<div
  data-zoom-on-wheel
  data-pan-on-drag
  style="height: 80vh;"
>
  <svg viewBox="0 0 100 100">
    <circle cx="50" cy="50" r="50" />
  </svg>
</div>

Run on CodePen

Installation

via npm (with a module bundler)

$ npm i svg-pan-zoom-container
import 'svg-pan-zoom-container'

via CDN (jsDelivr)

<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

Options

Some options can be specified as data-zoom-on-wheel and data-pan-on-drag attribute value.
Option name and value should be separated by colon (:).
Multiple options should be separated by semicolon (;).

Example

<div
  data-zoom-on-wheel="zoom-amount: 0.01; min-scale: 0.3; max-scale: 20;"
  data-pan-on-drag="button: right;"
  style="height: 80vh;"
>
  <svg viewBox="0 0 100 100">
    <circle cx="50" cy="50" r="50" />
  </svg>
</div>

Run on CodePen

Options for data-zoom-on-wheel

Name Type Default Description
zoom-amount number 0.002 Zoom amount per deltaY of wheel events.
min-scale number 1 Minimum scale.
max-scale number 10 Maximum scale.

Options for data-pan-on-drag

Name Type Default Description
button "left" | "right" "left" Mouse button to drag to pan.
modifier "" | "Alt" | "Control" | "Meta" | "Shift" "" Drag to pan only when this modifier key is pressed.

Observation

Observe the transform attribute of the SVG element using MutationObserver.

const container = document.getElementById('my-svg-container')

const observer = new MutationObserver(function (mutations) {
  mutations.forEach(function (mutation) {
    console.log('scale:', getScale(container));
  });
});

observer.observe(container.firstElementChild, {
  attributes: true,
  attributeFilter: ['transform'],
});

API

This module provides some functions for scripting to control pan and zoom behavior.

API usage

When installing via npm

import { pan, zoom, getScale, setScale, resetScale } from 'svg-pan-zoom-container';

When installing via CDN

<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script>
  const { pan, zoom, getScale, setScale, resetScale } = svgPanZoomContainer;
</script>

pan(container, deltaX, deltaY)

Pans.

getScale(container[, options])

Returns current scale.
The return value is a 1-based fraction, not a percentage.

setScale(container, value[, options])

Sets scale.
The value is considered as 1-based fraction, not as percentage.

The options can be specified as part of the following object (following values are the default):

const options = {
  origin: {
    clientX: 0,
    clientY: 0,
  },
  minScale: 1,
  maxScale: 10,
};

resetScale(container)

Resets scale and scroll position.

zoom(container, ratio[, options])

Equivalents to setScale(container, getScale(container) * ratio, options).

License

WTFPL

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].