Vue Pan and Zoom Component

1.1.6 · active · verified Sun Apr 19

The `vue-panzoom` library is a Vue.js plugin that provides intuitive pan and zoom capabilities for various DOM elements, including images, regular HTML content, and SVG. It serves as a Vue.js wrapper around the well-regarded `anvaka/panzoom` JavaScript library, inheriting its robust features and mobile-friendly design. The current stable version, 1.1.6, offers flexibility through attributes like `selector` to target specific elements within the component's scope and supports all options provided by the underlying `panzoom` library. Developers can also customize the component's name and listen to a comprehensive set of events, including its own `init` event and all `panzoom` lifecycle events, allowing for fine-grained control over the pan and zoom experience. While a strict release cadence isn't specified, the recent updates addressing Vue 3 compatibility (starting from v1.1.4) indicate active maintenance and adaptation to the Vue ecosystem's evolution. Its primary differentiator is its seamless integration into Vue applications, abstracting the complexities of direct `panzoom` library usage while exposing its full power.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates global plugin installation with a custom component name, and basic usage within a Vue template showcasing pan/zoom on both DOM images and SVG elements, including custom options and event handling.

import Vue from 'vue'
import App from './App.vue'
import panZoom from 'vue-panzoom'

// Install plugin, optionally change component name
Vue.use(panZoom, { componentName: 'MyPanZoomComponent' });

new Vue({
  render: h => h(App),
}).$mount('#app');

// Example App.vue content:
/*
<template>
    <div id="app">
        <!-- Apply to an image with custom options and event listener -->
        <MyPanZoomComponent :options="{minZoom: 0.5, maxZoom: 5}" @panstart="onPanStart">
            <img src="https://picsum.photos/300">
        </MyPanZoomComponent>

        <!-- Apply to SVG with a specific selector -->
        <MyPanZoomComponent selector="#svg-target" @init="onInit">
            <svg height="210" width="400">
                <g id="svg-target">
                    <path d="M150 0 L75 200 L225 200 Z" />
                </g>
            </svg>
        </MyPanZoomComponent>
    </div>
</template>

<script>
export default {
  methods: {
    onPanStart(e) {
      console.log('Pan started:', e);
    },
    onInit(panzoomInstance, id) {
      console.log('Panzoom initialized for ID:', id);
      panzoomInstance.on('zoom', (e) => {
        console.log('Zoom event via instance:', e);
      });
    }
  }
}
</script>
*/

view raw JSON →