Skip to content

Commit

Permalink
fix: stop room background oscillation after a window resize
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterpaulkramer committed Feb 6, 2021
1 parent 30e371f commit 8f35864
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,36 @@ requestAnimationFrame(measureFps);

const $bg = document.querySelector('#background');
const pz = panzoom($bg, {
maxZoom: 5,
minZoom: Math.max(
(window.innerWidth / document.ROOM_CONFIG.width),
(window.innerHeight / document.ROOM_CONFIG.height)),
zoomSpeed: 0.25,
bounds: true,
boundsPadding: 1,
});

pz.on('panstart', (_) => {
const scale = pz.getTransform().scale;
pz.setMinZoom(scale);
pz.setMaxZoom(scale);
});
pz.on('panend', (_) => {
function setDefaultZoomParams() {
pz.setMinZoom(Math.max(
(window.innerWidth / document.ROOM_CONFIG.width),
(window.innerHeight / document.ROOM_CONFIG.height)));
pz.setMaxZoom(5);

if (pz.getTransform().scale < pz.getMinZoom()) {
// Zoom in towards the middle of what's currently visible. That doesn't
// guarantee that the room now fills the whole viewport, call moveBy to make
// panzoom fix that.
pz.zoomAbs(0.5, 0.5, pz.getMinZoom());
pz.moveBy(0, 0);
}
}

setDefaultZoomParams();
window.addEventListener('resize', setDefaultZoomParams);

// Disable zoom during pan.
pz.on('panstart', (_) => {
const scale = pz.getTransform().scale;
pz.setMinZoom(scale);
pz.setMaxZoom(scale);
});
pz.on('panend', setDefaultZoomParams);


class Player {
Expand Down

0 comments on commit 8f35864

Please sign in to comment.