zoomViewport
A 2D viewport zoomed by a factor along x, y or both, never past the fitted view.
JavaScript only.
<script>
import { Plot, Points, zoomViewport } from "hmplot";
let viewport = $state({ x: 0.5, y: 0.5, zoom: 1 });
</script>
<button onclick={() => (viewport = zoomViewport(viewport, 1.5))}>Zoom in</button>
<button onclick={() => (viewport = zoomViewport(viewport, 1 / 1.5, "x"))}>Zoom x out</button>
<Plot {viewport} onviewportchange={(v) => (viewport = v)} axes2d>…</Plot>Usage
A 2D Plot’s viewport is { x, y, zoom }: the center in the plot’s unit square (0.5 is the middle) and the zoom, 1 being the fitted view; zoomX and zoomY zoom the axes apart, and each defaults to zoom. Pass a viewport to Plot’s viewport prop and follow the viewer’s changes with onviewportchange, which reports the same fields. zoomViewport gives the next state for a button or a key: it multiplies the zoom along axis, never below the fitted view, and keeps the center. Changing zoom alone would leave an axis that has its own factor where it was.
The same object is what getViewport() and setViewport() on the Plot handle read and write; Python sets it with await plot.set_viewport(...). An orthographic globe’s viewport also has center, its longitude and latitude, which zoomViewport keeps. See docs/NAVIGATION.md for zooming one axis from its margin.