Skip to content

v-pinch

Two-finger pinch/zoom gesture.

Since: 1.5.0

Usage

Basic

vue
<template>
  <div v-pinch="handlePinch">Pinch to zoom</div>
</template>

<script setup>
function handlePinch(e) {
  console.log('Scale:', e.scale)
  console.log('Center:', e.centerX, e.centerY)
}
</script>

With Options

vue
<template>
  <div v-pinch="{
    onPinch: handlePinch,
    enableTransform: true,
    minScale: 0.5,
    maxScale: 3
  }">
    Pinch to scale
  </div>
</template>

API

Options

OptionTypeDefaultDescription
onPinch(event: PinchEvent) => void-Pinch event callback
enableTransformbooleanfalseAuto-apply CSS transform
minScalenumber0.5Minimum scale factor
maxScalenumber3Maximum scale factor
disabledbooleanfalseDisable pinch detection

Pinch Event

ts
interface PinchEvent {
  /** Current scale factor */
  scale: number
  /** Center point X */
  centerX: number
  /** Center point Y */
  centerY: number
  /** Delta scale from last event */
  deltaScale: number
  /** Whether pinch is in progress */
  isPinching: boolean
  /** Original event */
  event: TouchEvent
}

Examples

Image Zoom

vue
<template>
  <div v-pinch="{
    onPinch: handlePinch,
    enableTransform: true,
    minScale: 1,
    maxScale: 4
  }" class="image-container">
    <img src="image.jpg" />
  </div>
</template>

<style>
.image-container {
  overflow: hidden;
  touch-action: none;
}
</style>

Map Zoom

vue
<template>
  <div v-pinch="{ onPinch: handlePinch }" class="map">
    <div :style="{ transform: `scale(${scale})` }">
      <!-- Map content -->
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const scale = ref(1)

function handlePinch(e) {
  scale.value = Math.max(0.5, Math.min(3, e.scale))
}
</script>

Composable API

For programmatic use, you can use the usePinch composable:

typescript
import { usePinch } from 'directix'

const { scale, isPinching, bind } = usePinch({
  minScale: 0.5,
  maxScale: 3,
  onPinch: handlePinch
})

// Bind to element
onMounted(() => bind(elementRef.value))

UsePinchReturn

PropertyTypeDescription
scaleRef<number>Current scale factor
isPinchingRef<boolean>Whether pinch is in progress
bind(element: HTMLElement) => () => voidBind to element

Code Generator

Quick Code Generator
<template>
  <div v-pinch>
    <!-- Two-finger pinch/zoom gesture. directive -->
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

// Configure your options here
const options = {}
</script>

Released under the MIT License.