Fit the harbour to the viewport on laptop and phone so Prod, Staging and the shore never need a page scroll.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-09 15:43:12 +07:00
parent 358a6ab304
commit 710782b786
2 changed files with 162 additions and 62 deletions

View File

@@ -235,16 +235,85 @@
const beresWidth = (boatW) => BERES_PER_ROW * boatW + (BERES_PER_ROW - 1) * SLOT_GAP;
// Shrink slot and boat sizes so every lane / phone scene fits inside the viewport.
const SLOT_MAX = 176;
const SLOT_MIN = 100;
const BOAT_NATURAL = 158; // hull + chip + title + marks at full size (desktop)
const PHONE_BOAT = 112; // hull + chip + marks at phone defaults
function clearFitVars(root) {
root.style.removeProperty('--slot-h');
root.style.removeProperty('--boat-w');
root.style.removeProperty('--hull-w');
root.style.removeProperty('--hull-h');
delete root.dataset.boatCompact;
}
function applyBoatScale(root, scale, baseBoat, baseHullW, baseHullH) {
if (scale >= 0.995) return;
root.style.setProperty('--boat-w', `${Math.max(64, Math.round(baseBoat * scale))}px`);
root.style.setProperty('--hull-w', `${Math.max(48, Math.round(baseHullW * scale))}px`);
root.style.setProperty('--hull-h', `${Math.max(36, Math.round(baseHullH * scale))}px`);
}
function layoutPhoneFit(root) {
const scene = document.querySelector('.harbour-scene');
if (!scene) {
clearFitVars(root);
return;
}
clearFitVars(root);
const baseBoat = parseFloat(getComputedStyle(root).getPropertyValue('--boat-w')) || 108;
const baseHullW = parseFloat(getComputedStyle(root).getPropertyValue('--hull-w')) || 88;
const baseHullH = parseFloat(getComputedStyle(root).getPropertyValue('--hull-h')) || 66;
const beach = scene.querySelector('.beach');
const avail = scene.clientHeight - (beach ? beach.offsetHeight : 22);
if (avail < 60) return;
// Grid rows are 1fr / 1.15fr / 1fr — size boats to the mid band.
const midBand = avail * (1.15 / 3.15);
const scale = Math.min(1, Math.max(0.55, (midBand - 6) / PHONE_BOAT));
applyBoatScale(root, scale, baseBoat, baseHullW, baseHullH);
}
function layoutFit() {
const root = document.documentElement;
if (phone.matches) {
layoutPhoneFit(root);
return;
}
const main = $('lanes');
const n = main.querySelectorAll('.lane').length;
if (!n) {
clearFitVars(root);
return;
}
// Read the stylesheet defaults (after clearing any previous inline shrink) so a roomy
// window can grow back to full size, and narrow-laptop media rules still apply.
clearFitVars(root);
const baseBoat = parseFloat(getComputedStyle(root).getPropertyValue('--boat-w')) || 120;
const baseHullW = parseFloat(getComputedStyle(root).getPropertyValue('--hull-w')) || 96;
const baseHullH = parseFloat(getComputedStyle(root).getPropertyValue('--hull-h')) || 72;
const avail = main.clientHeight;
if (avail < 80) return;
// Per lane: h2 + paddings + zone-label row + boat-row gap, then two slot rows.
const laneChrome = 28 + 26 + 36 + 4;
const slot = Math.max(SLOT_MIN, Math.min(SLOT_MAX, Math.floor((avail / n - laneChrome) / ROWS)));
root.style.setProperty('--slot-h', `${slot}px`);
// When the slot is shorter than a full boat, scale the boat and drop the title.
const boatH = Math.min(BOAT_NATURAL, slot - 4);
const scale = Math.min(1, boatH / BOAT_NATURAL);
root.dataset.boatCompact = scale < 0.92 ? '1' : '0';
applyBoatScale(root, scale, baseBoat, baseHullW, baseHullH);
}
// Places the boats already in `rows` (one grid per lane) and sizes the columns.
function layoutColumns() {
const rows = [...$('lanes').querySelectorAll('.zones')];
if (!rows.length) return;
if (phone.matches) {
layoutFit();
for (const r of rows) r.style.gridTemplateColumns = '';
for (const b of $('lanes').querySelectorAll('.boats')) b.style.gridTemplateColumns = '';
state.cols = null;
return;
}
if (!rows.length) return;
layoutFit();
const { total, boatW } = measure(rows[0]);
const count = (z) => Math.max(1, ...rows.map((r) => r.children[z].querySelector('.boats').childElementCount));
const beres = beresWidth(boatW);
@@ -360,6 +429,7 @@
keep.style.transform = '';
main.classList.remove('lane-panning');
main.style.height = '';
layoutFit();
}
function panPhoneHarbour(main, dir) {
const target = state.lane;
@@ -371,6 +441,7 @@
if (calm.matches) {
main.classList.remove('lane-panning');
main.replaceChildren(buildPhoneScene(target));
layoutFit();
return;
}
@@ -495,9 +566,11 @@
main.classList.remove('lane-panning');
if (!state.lane) {
main.replaceChildren();
clearFitVars(document.documentElement);
return;
}
main.replaceChildren(buildPhoneScene(state.lane));
layoutFit();
}
let drawnLanes = '';
@@ -571,7 +644,8 @@
}
for (const b of main.querySelectorAll('.boat[data-id]')) shown.add(b.dataset.id);
// Boats take their new slots at once; the columns then widen or narrow through their
// transition, and every boat glides from where it was.
// transition, and every boat glides from where it was. layoutColumns fits height first
// so the shrunk boat width is what the columns measure.
layoutColumns();
sail(new Map([...before].map(([id, b]) => [id, b.rect])));
depart(before, shown);