GithubHelp home page GithubHelp logo

Comments (4)

streamtw avatar streamtw commented on June 20, 2024 3

Bounds can be achieved by adding custom event listeners:

HTML:

<pinch-zoom class="my-pinch-zoom">
  <img>
</pinch-zoom>

Javascript:

const pinchZoom = document.querySelector('.my-pinch-zoom')
pinchZoom.addEventListener('touchend', moveToSafeArea)
pinchZoom.addEventListener('wheel', moveToSafeArea)

Event listener moveToSafeArea will calculate proper position for content, according to width and height of both wrapper and content:

const moveToSafeArea = function () {
  const wrapper = document.querySelector('.my-pinch-zoom')
  const wrapperWidth = wrapper.clientWidth
  const wrapperHeight = wrapper.clientHeight

  const content = wrapper.children[0]
  const contentWidth = content.clientWidth * wrapper.scale
  const contentHeight = content.clientHeight * wrapper.scale

  content.classList.add('moving')

  let safeX
  let safeY

  const safeRangeX = wrapperWidth - contentWidth
  if (safeRangeX > 0) {
    safeX = Math.min(Math.max(wrapper.x, 0), safeRangeX)
  } else {
    safeX = Math.max(Math.min(wrapper.x, 0), safeRangeX)
  }

  const safeRangeY = wrapperHeight - contentHeight
  if (safeRangeY > 0) {
    safeY = Math.min(Math.max(wrapper.y, 0), safeRangeY)
  } else {
    safeY = Math.max(Math.min(wrapper.y, 0), safeRangeY)
  }

  wrapper.setTransform({
    scale: wrapper.scale,
    x: safeX,
    y: safeY
  })

  setTimeout(() => {
    content.classList.remove('moving')
  }, 250)
}

And remember to add a nice bounce animation:

.my-pinch-zoom > .moving {
  transition: all .25s;
}

from pinch-zoom.

jakearchibald avatar jakearchibald commented on June 20, 2024

Yeah, this is a good idea.

from pinch-zoom.

valpet avatar valpet commented on June 20, 2024

It's not only a good idea, it's an excellent idea. I miss this function very much.

from pinch-zoom.

WernerRaath avatar WernerRaath commented on June 20, 2024

@kenchris did you ever find a fix for this? Or should one search for alternatives?

from pinch-zoom.

Related Issues (20)

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.