Giter Site home page Giter Site logo

luncheon / svg-pan-zoom-container Goto Github PK

View Code? Open in Web Editor NEW
38.0 3.0 1.0 2.61 MB

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

Home Page: https://luncheon.github.io/svg-pan-zoom-container/index.html

License: Do What The F*ck You Want To Public License

JavaScript 29.26% TypeScript 70.74%
svg pan zoom directive vanilla-js

svg-pan-zoom-container's Introduction

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

svg-pan-zoom-container's People

Contributors

luncheon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

minsomai

svg-pan-zoom-container's Issues

Support for different keys for data-pan-on-drag

FIrst of all Thanks so much for that lovely tool.

I was wondering if somehow Support for different keys for data-pan-on-drag can be added ?!

In my project i kind of need usage of left click , so it will be easier for me if for example the function works when i push for example CTRL + L-CLICK . Or maybe another combinations !!

Can something like that be added ?

Performance issue on Chromium-based browsers

Zooming is very slow on Chrome. Firefox and Safari are fine.
I know that the setting scrollLeft takes too much time. But I don't know the solution.


I don't know if this is related to the following issue.

To address this issue, give the scrolling container an opaque background color and set "contain: paint" on it.

The above solution does not help.


See also

Listenning to event "keydown"

Why is it not possible to listen to event "onkeydown" when using this library?

// this is never called

document.getElementById('svg-wrapper').addEventListener("keydown", function(event) {
  console.debug(event.keyCode);
});

If I remove the data attribute it works again:

<div id="svg-wrapper" tabindex="1" data-pan-on-drag data-zoom-on-wheel="min-scale: 0.3; max-scale: 1000;">
 <svg>...</svg>
</div>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.