Code Monkey home page Code Monkey logo

svg.panzoom.js's Introduction

svg.panzoom.js

A plugin for svg.js that enables panzoom for svg elements

Getting started

npm install @svgdotjs/svg.panzoom.js

Include this plugin after including the svg.js library in your html document.

<script src="svg.js"></script>
<script src="svg.panzoom.js"></script>

Or for esm just import it:

import '@svgdotjs/svg.panzoom.js'

To enable pan/zoom on an svg:

// enables panZoom
var canvas = SVG().addTo('#id')
  .size(1000,1000).panZoom()

// zoom programatically
canvas.zoom(lvl, point)

You can configure panZoom by passing options to it.

  • zoomMin: Minimal zoom level
  • zoomMax: Maximal zoom level
  • zoomFactor: How much is zoomed by one mouse wheel step

This could look like this:

var canvas = SVG().addTo('#id')
  .size(1000,1000)
  .panZoom({zoomMin: 0.5, zoomMax: 20})

Setting the min and max value will automatically restrict the zoom to the provided level.
However you are still able to change the zoom out of that bonds by calling zoom(lvl) programatically.

On touchable devices a pinchZoom gesture is supported. Min and max values also apply here.

Zooming is animatable, too:

canvas.zoom(1) // uses center of viewport by default
    .animate()
    .zoom(2, {x:100, y:100}) // zoom into specified point

To disable panZoom or change its options just call it again with false or the new options.

API

svg.panzoom.js adds the .zoom() method to <svg>

  • zoom() - returns current zoom level
  • zoom(Number) - will zoom in or out depending if the Number is greater or less than the current zoom level
  • zoom(Number, {x,y}) - will zoom with the x/y coordinate as center point
  • zoom(Number, new SVG.Point(x,y)) - will zoom with the x/y coordinate as center point
Method Return Value
zoom() Number
zoom(Number) element
zoom(Number, {x,y}) element
zoom(Number, new SVG.Point(x,y)) element

Events

Multiple events are fired doing different actions. This allow you to respond to actions and in some cases stop an action via preventDefault().

zoom is fired when a mouse wheel event or programmable zoom() triggers a zoom. This usually doesn't happen on mobile devices, in which case pinchZoomStart is fired when a zoom happens.

Events fired from SVG.js are CustomEvents, so the arguments passed from svg.panzoom.js are in in the .detail property.

Event Name Argument Value preventDefault support
zoom { lvl, focus } YES
panStart { event } NO
panEnd { event } NO
pinchZoomStart { event } YES
pinchZoomEnd { event } NO

Where lvl is the new zoom level, focus is point of zoom and event is the event that triggered the action.

An example of stopping a pan-zoom action:

var canvas = SVG().addTo('#id')
  .size(1000,1000).panZoom()

canvas.on('pinchZoomStart', function(ev) {
    ev.preventDefault()
    // ...
})

svg.panzoom.js's People

Contributors

fuzzyma avatar dotnetcarpenter avatar ltouati avatar dependabot[bot] avatar inanisvitae avatar andrewcourtice avatar frederic-schwarz avatar lexelby avatar kaalleen avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Forkers

vb6hobbyst7

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.