Hi, I’m building a frontend web application to annotate blueprints.
Users of this application are used to CAD software where zoom is managed with mousewheel (only !, not pressing the CTRL key).
Current situation : when the user scrolls (with mousewheel), the pdf document is scrolling.
Desired situation : when the user scrolls (with mousewheel), the pdf document is zooming.
I tried to add a wheel event listener to the document viewer container but I can’t catch any event…I guess it is just because all the app is sandboxed in an iframe.
You can add an event listener to override the default behaviour like the following
Webviewer({}, document.getElementById('viewer')).then(instance => {
const { docViewer } = instance;
let throttleTime;
// wait till after the "pageComplete" event so the element will be on the DOM
docViewer.on('pageComplete', function() {
document.querySelector('#viewer > iframe').contentDocument.querySelector('.document').addEventListener('wheel', (e) => {
// the "wheel" event actually trigger on the "documentContainer" element
// however add event to the document element to use "stopPropagation" to disable default behaviour
e.stopPropagation();
e.preventDefault();
if (throttleTime) {
return;
} else {
throttleTime = setTimeout(() => {
// change zoom level base on the direction of the mouse wheel
if (e.deltaY < 0) {
instance.setZoomLevel(instance.getZoomLevel() * 1.2)
} else {
instance.setZoomLevel(instance.getZoomLevel() * 0.9)
}
clearTimeout(throttleTime);
throttleTime = null;
}, 100);
}
});
});
});
The main idea is to prevent the “wheel” even on the “documentContainer” event from scrolling and use “setZoomLevel” to change the zoom. Please let me know if the above works for you or if you want me to clarify something
Best Regards,
Andrew Yip
Software Developer
PDFTron Systems, Inc. www.pdftron.com
thanks for your answer !! I was not listening on the right element. Good to know that I needed to reach the documentContainer inside the iframe !
It works perfectly … well, almost perfectly
Now, the challenge is to center the zoom on the mouse position. So instead of using the instance.setZoomLevel method, I’m using the documentViewer.zoom(zoom, x, y)To method.
My problem : I’m struggling to compute the appropriate (x,y)…
Below the getMouseLocation I’m using to get the x,y at the moment.
Question => How should I offset the resulting position to keep the zoom centered on the mouse position ?
One thing to note is that the mouse event X and Y are based on the mouse cursor location relative to the browser. So you’ll need to provide offset for where the viewer is on the page.
Best Regards,
Andrew Yip
Software Developer
PDFTron Systems, Inc. www.pdftron.com