Skip to content

Commit

Permalink
fix for dynamic vertical centering on mobile (#2266)
Browse files Browse the repository at this point in the history
  • Loading branch information
whitneywind authored Nov 1, 2024
1 parent 0ad987d commit 172f412
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions client/src/hooks/useMapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ export const useMapbox = () => {
const baseLongOffset = 0.08;
const longitudeOffset = baseLongOffset * Math.pow(2, 11 - currentZoom);

// calculates latitude offset for mobile according to zoom level and takes screen height into account
const baseLatOffset = 0.05;
// calculates latitude offset for mobile according to zoom level and screen height
const baseLatOffset = 0.065;
const screenHeight = window.innerHeight;
const referenceHeight = 800;
const screenHeightFactor = screenHeight / referenceHeight;
const latitudeOffset =
baseLatOffset * Math.pow(2, 11 - currentZoom) * screenHeightFactor;

function calculateLatOffset(screenHeight) {
const baseHeight = 550;
const rate = 0.006 / 50; // approximately 0.006 deg per 50 pixels
return 0.034 + (screenHeight - baseHeight) * rate;
}

const heightOffsetFactor = !screenHeight
? baseLatOffset
: calculateLatOffset(screenHeight);

const latitudeOffset = heightOffsetFactor * Math.pow(2, 11 - currentZoom);

mapbox.default.flyTo({
center: [
Expand Down

0 comments on commit 172f412

Please sign in to comment.