Foldable paper OpenStreetMap pocket map
I wanted to see if real OpenStreetMap tiles and live interactive HTML place pins could fold together along vertical and horizontal paper-map creases on one 2D canvas. Drag the edges of the map or use the fold presets to crease it into a pocket map, then unfold it flat to click stops, filter the legend, and pan the streets.
Show the code OSM tile atlas, 4x2 paper fold with 9-argument slices, hit boxes, and pointer input
- The markup. One
<canvas content="drawable" layoutsubtree>holding fourdrawableplace pins (#pin-0to#pin-3) and onedrawablelegend card (#map-legend). - OpenStreetMap tile atlas and fallback. Web Mercator projection into a plain offscreen 2D canvas, fetching
tile.openstreetmap.orgPNGs withcrossOrigin = 'anonymous'and drawing procedural paper-map fallback tiles until each image arrives. - Two-axis pocket fold. Four vertical accordion columns crossed by a horizontal midline hinge, slicing both the offscreen tile atlas and every overlapping HTML pin with nine-argument
drawElementImage(), plus a back-cover pocket flap. - Flat draws and hit boxes. When flat, each pin and the legend draw once with five-argument
drawElementImage(). When lightly folded, empty-clip five-argument draws restore hit boxes; when covered by the back flap,park()moves hidden controls out of hit testing. - Panning vs folding. Clicks on buttons, inputs, and checkboxes go straight to the live DOM. Dragging inside an unfolded map pans the tiles; dragging the canvas margins or any folded packet creases both axes.
- Card and legend layout. Fixed paper colours for the pins and legend so they read against both real OpenStreetMap tiles and dark-mode page chrome.
How it works
I built this around a single <canvas content="drawable" layoutsubtree> and a plain offscreen 2D canvas that holds the map sheet. The offscreen canvas composites live raster tiles from OpenStreetMap (https://tile.openstreetmap.org/{z}/{x}/{y}.png with crossOrigin = 'anonymous'), cached in memory by z/x/y. While tiles are loading or if the device is offline, each 256 by 256 tile slot draws a deterministic procedural street, river, park, and grid tile in the colour palette of Ordnance Survey pocket paper maps. The dashed walking route, neatline ticks, scale bar, and pin anchor dots draw onto that same offscreen sheet.
The four place pins (#pin-0 to #pin-3) and the pocket legend card (#map-legend) sit inside the canvas as sibling drawable HTML elements. When the map is flat (foldX <= 0.01 and foldY <= 0.01), I draw the offscreen sheet once and then draw each HTML element with a single unclipped five-argument drawElementImage(el, x, y, w, h). That leaves every button, search input, and checkbox with an exact native hit box while you pan the map underneath.
Once either fold slider moves off zero, the renderer switches to a 4 by 2 grid of hinged paper panels, extending the sheared-strip folding pattern from /accordion/ and /origami-ticket/ into two axes. Each of the four vertical columns applies cosine foreshortening and alternating vertical affine shear (ctx.transform(cosX, shearY, 0, scaleY, dx, dy)) so adjacent vertical creases meet without gaps. Inside every panel clip, I copy the matching strip of the offscreen tile atlas and slice every overlapping HTML pin or legend card with nine-argument drawElementImage(el, sx, sy, sw, sh, dx, dy, dw, dh) so tiles, routes, and live DOM bend together along the exact same creases.
The horizontal midline crease folds the top half down over the bottom half. Below 50% vertical fold, the top row foreshortens upward toward the midline. Past 50%, it flips downward over the bottom row and draws the cream paper back with an Ordnance-Survey-style pocket cover. Tight horizontal folding (past 72%) also swings a hinged back-cover pocket flap over the edge of the concertina stack. When elements are hidden under the folded cover, I call park() from /lib/stage-2d.js so covered buttons don't intercept taps; when motion settles back to flat, toggling a data-hit attribute clears Chromium's hit-test cache.
Live checks
These six checks run on isolated fixtures in the corner of the window and leave the map above untouched. Every pixel read copies the fixture canvas into a plain non-drawable 2D canvas with drawImage() before calling getImageData().
Findings
- Credits and inspirations: map tiles by OpenStreetMap contributors, physical accordion-and-midline folding from Ordnance Survey and pocket tourist paper maps, and the sheared-strip 2D canvas folding pattern from /accordion/ and /origami-ticket/ on this site.
- Sharing one affine matrix per panel between
drawImage(atlasCanvas, ...)and nine-argumentdrawElementImage(el, ...)keeps map tiles, vector routes, and live HTML cards locked across both vertical and horizontal creases with no visible drift between layers. - Keeping the offscreen OpenStreetMap tile atlas on a plain non-drawable 2D canvas avoids creating extra drawable contexts and keeps pixel compositing fast during drags, while the fallback procedural renderer lets the experiment remain legible offline or when tile servers rate-limit requests.
- Switching back to unclipped five-argument
drawElementImage()whenfoldX <= 0.01andfoldY <= 0.01avoids the hit-box shrinkage caused by nine-argument slices while typing in#pin-filteror clicking pin route buttons. - This page hasn't been run in a live browser yet, so every runtime behaviour is not verified until the checks run in Chromium 157 and on Chrome 159 for Android (412 px touch, device pixel ratio 2.625).
- Not verified: real-device frame rate while folding live OpenStreetMap PNG tiles plus five
drawableHTML cards across eight clipped panels on Chrome 159 for Android, CORS delivery oftile.openstreetmap.orgtiles under mobile network throttling, touch panning vs margin-drag ergonomics at 412 px wide, and screen-reader focus order when pins are parked under the back cover.