davidfig / pixi-viewport

A highly configurable viewport/2D camera designed to work with pixi.js
https://davidfig.github.io/pixi-viewport/
MIT License
1.06k stars 177 forks source link

Ability to pan and zoom via touchpad simultaneously #337

Open FrankiePo opened 3 years ago

FrankiePo commented 3 years ago

How can we achieve simultaneously pan and zoom via touchpad using API?

Currently I have this settings

this.viewport
  .drag()
  .wheel({
    trackpadPinch: true,
    wheelZoom: false,
  })
  .clampZoom({
    minScale: MIN_SCALE,
    maxScale: MAX_SCALE,
  });
boy51 commented 2 years ago

Did you find a way?

SimonBlok commented 2 years ago

I got this working in a two-step solution.

  1. Set default configuration so tracking and mouse are able to drag
  2. Because there is no 'keyToPress' property on the wheel (zoom) configuration, I set a separate event handler on a keypress to disable dragging and enabling zooming

1 - Default configuration

viewport.drag({
  ignoreKeyToPressOnTouch: true,
})
.wheel({
  percent: 2,
  trackpadPinch: true,
  wheelZoom: false,
})
.decelerate()
.pinch()
.clampZoom({
  minScale: 0.05,
  maxScale: 3.0
});

2 - Setting separate keypress handler

window.onkeydown = (e) => {
            if (e.key === 'Meta') {
                viewport.wheel({
                    percent: 2,
                    trackpadPinch: true,
                    wheelZoom: true,
                })
            }
        }

        window.onkeyup = (e) => {
            if (e.key === 'Meta') {
                viewport.wheel({
                    percent: 2,
                    trackpadPinch: true,
                    wheelZoom: false,
                })
            }
        }