Comments (4)
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.
Yeah, this is a good idea.
from pinch-zoom.
It's not only a good idea, it's an excellent idea. I miss this function very much.
from pinch-zoom.
@kenchris did you ever find a fix for this? Or should one search for alternatives?
from pinch-zoom.
Related Issues (20)
- Feature request: ability to disable pinch-zooming HOT 2
- Configurable zoom factor using mouse wheel
- Affects whole screen on Chrome on iOS
- customElements is not defined HOT 2
- Prevent translation out of pinch-zoom HOT 4
- Text inputs inside pinch-zoom are not interactable HOT 4
- Feature request: Additional event in case of reaches of the minScale case
- Makes zoom by clicks and taps actions HOT 5
- Feature request: zoom entire view port
- support rotation
- Bugs out in safari on iPhone HOT 3
- Vue TSLint Error
- Draggable element inside pinch-zoom component HOT 1
- dead demo link
- It doesn't work on IE HOT 1
- Pro installation instructions are unclear HOT 1
- Firefox not working properly? HOT 2
- pointer-tracker not found HOT 3
- pinch-zoom injects styles into <head>, won't work in ShadowRoots.
- Add min-scale HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pinch-zoom.