Skip to content

jQuery plugin to pan and zoom SVG images either programatically or through mouse/touch events

License

Notifications You must be signed in to change notification settings

tinnut/SVGPanZoom

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SVG Pan & Zoom v1.0.0

jQuery plugin to pan and zoom SVG images either programatically or through mouse/touch events

Project forked

This project uses jquery-svg-pan-zoom project as base and improves on it.

Features

  • Programmatically manipulate the SVG viewBox
  • Mouse and touch events to pan the SVG viewBox
  • Mousewheel and pinch events for zooming the SVG viewBox
  • Animate pan and zoom actions
  • Zooming keeps the cursor over the same coordinates relative to the image (A.K.A. GoogleMaps-like zoom)

Dependencies

  • jQuery
  • SVG-enabled browser (does not work with SVG work-arounds that use Flash)

The viewBox

The viewBox is an attribute of SVG images that defines the area of the SVG that is visible, it is defined by 4 numbers: X, Y, Width, Height. These numbers together specify the visible area. This plugin works by manipulating these four numbers.

Examples Reducing X value pans the SVG to left. By reducing Width and Height we can zoom in to the SVG.

Usage

var SVGPanZoom= $("svg").SVGPanZoom(options)
  • If the selection has more than one element, the return value will be an array of SVGPanZoom instances for each element in the same order of the selection.
  • If only one element is selected then the return value is a single SVGPanZoom instance.
  • If no elements are selected the above call returns null

The returned SVGPanZoom instance contains all options as properties, these can be overwritten at any time directly, for example to disable mouseWheel events simply:

SVGPanZoom.events.mouseWheel= false

The SVGPanZoom instance also has methods for manipulating the viewBox programmatically. For example:

SVGPanZoom.zoomIn()

Options

Default values are inside ().

Options: {
    /* the initial viewBox,
     * if null or undefined will try to use the viewBox set in the svg tag.
     * Also accepts string in the format "X Y Width Height"
     ***/
    initialViewBox: Object {
        x: Number (0), // the top-left corner X coordinate
        y: Number (0), // the top-left corner Y coordinate
        width: Number (1000), // the width of the viewBox
        height: Number (1000) // the height of the viewBox
    } (null),
    /* ViewBox Limits
        * min X (left)
        * min Y (top)
        * max X (right)
        * max Y (bottom)
        ***/
    limits: Object {
        min: {
            x: Number (0 - initialViewBox.width * (15/100)),
            y: Number (0 - initialViewBox.height * (15/100))
        },
        max: {
            x: Number (initialViewBox.width  + initialViewBox.width * (15/100)),
            y: Number (initialViewBox.height  + initialViewBox.height * ( 15/100))
        }
    } (null),
    // Time(milliseconds) to use for animations. Set 0 to remove the animation
    animationTime: Number (300),
    // Zoom Params, set false to disable zoom
    zoom: {
        // Zoom Factor, viewBox values are multiplied or divided with this factor to zoom on each step
        factor: Number (0.25),
        // Zoom Limits, maxZoom:3 => zoom upto 3x
        minZoom: Number (1),
        maxZoom: Number (3),
        // Event related flags
        events: {
            // enable mouse wheel zooming events
            mouseWheel: Boolean (true),
            // enable double-click zooming events
            doubleClick: Boolean (true),
            // enable pinch zooming events
            pinch: Boolean (true)
        },
        // onZoom callback, multiplier = current multiplier
        callback: Function (function(multiplier) {})
    },
    // Pan Params, set false to disable pan
    pan: {
        // Pan Factor, this factor is added to or substracted from viewBox values to pan
        factor: Number (100),
        // Event related flags
        events: {
            // enable drag to PAN the SVG events
            drag: Boolean (true),
            // e.which value for the mouse button you want to use for dragging
            dragMouseButton : Number (1), 
            // cursor to use while dragging
            dragCursor: String ("move")
        },
        // onPan callback, coordinates = {x: x, y: y}
        callback: Function (function(coordinates) {})
    },
    /* Element(container) to which all the events are attached
     * If null events will be attached to SVG element itself
     * Useful when your own touch and mouse events are interfering with the events of plugin
     ***/
    eventMagnet: DOMElement or Selector (null)
}

Methods

Pan Methods

SVGPanZoom.panLeft([amount, animationTime])
SVGPanZoom.panRight([amount, animationTime])
SVGPanZoom.panUp([amount, animationTime])
SVGPanZoom.panDown([amount, animationTime])

Pans the SVG in the specified direction.

amount [Number] optional PAN distance, defaults to options.panFactor. animationTime [Number] optional Animation duration, defaults to options.animationTime.

Zoom Methods

SVGPanZoom.zoomIn([focalPoint, amount, animationTime])
SVGPanZoom.zoomOut([focalPoint, amount, animationTime])

Zooms the SVG in or out.

focalPoint [Object {x: Number,y: Number}] optional This point should be relative to SVG co-ordinate system, defaults to center of current viewBox. This point will remain at the same place after zooming. amount [Number] optional Zoom factor, defaults to options.zoomFactor. animationTime [Number] optional Animation duration, defaults to options.animationTime.

reset

SVGPanZoom.reset()

Resets the SVG to options.initialViewBox values.

getViewBox

SVGPanZoom.getViewBox()

Returns the viewbox in this format:

{
    x: Number
    y: Number
    width: Number
    height: Number
}

setViewBox

SVGPanZoom.setViewBox(x, y, width, height, animationTime)

Changes the viewBox to the specified coordinates. Will respect the options.limits adapting the viewBox if needed (moving or reducing it to fit into options.limits

x [Number] the new x coodinate of the top-left corner y [Number] the new y coodinate of the top-left corner width [Number] the new width of the viewBox height [Number] the new height of the viewBox animationTime [Number] optional Animation duration, defaults to options.animationTime.

setCenter

SVGPanZoom.setCenter(x, y, animationTime)

Sets the center of the SVG. Parameters: x [Number] the new x coodinate of the center y [Number] the new y coodinate of the center animationTime [Number] optional Animation duration, defaults to options.animationTime.

Notes:

  • Only works in SVGs inlined in the HTML. You can use $.load() to load the SVG image in the page using AJAX and call $().SVGPanZoom() in the callback
  • This plugin does not create any controls (like arrows to move the image) on top of the SVG. These controls are simple to create manually and they can programmatically call the methods to move the image.
  • Do not manipulate the SVG viewBox attribute manually, use SVGPanZoom.setViewBox() instead

About

jQuery plugin to pan and zoom SVG images either programatically or through mouse/touch events

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%