Map

A cloth map of San Francisco. Hold it to fold it, drag the pin to a place. It stays dark in either theme, like a printed map.

Click the map to unfold it

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/map.json

This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/map.tsx
"use client";

import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { FoldMap as FoldMapEngine } from "@/lib/fabricui/map-engine";
import "./map.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;

/** A place on the map, in the sheet's own px (600 × 400, 26 px per km, north up). */
export type MapPlace = {
  id: string;
  name: string;
  /** What it is, on its card ("Park", "Summit, 282 m"). */
  kind: string;
  x: number;
components/fabricui/map.css
/*
 * Map — a folded map. Shared by every page with one; map.js drives it
 * (class FoldMap). The map is printed on one soft sheet of fabric, folded
 * like a walking map.
 *
 *   <div class="fmap">
 *     <div class="fmap__body"><div class="fmap__inner">…</div></div>   the live map
 *     <div class="cloth fmap__cloth"><canvas></canvas></div>          the sheet while it folds
 *     <button class="fmap__packet">…</button>          the folded packet: click to open
 *   </div>
 *
 * Effects:
 *  - fabric: it unfolds a fold at a time and folds back up (cloth-drop.js
 *    fold), and a place's card pops out of the pin as cloth;
 *  - dither: the island is a 1-bit dithered relief with contour lines, and
 *    the folds are shaded in dots as they turn;
lib/fabricui/map-engine.js
import { ClothDrop } from "./cloth-drop";
import { Dither } from "./dither";

// text that goes into HTML (the title and places can come from data)
const esc = s => String(s).replace(/[&<>"]/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);

/*
 * map-engine.js — a folded map (see map.css). Needs cloth-drop.js.
 *
 *   const map = new FoldMap(el, { onChange: open => … });
 *   map.open(); map.close();          // unfold / fold back up
 *
 * One sheet of cloth folded like a walking map: rolled in thirds, then in
 * half, with a cover printed on the back. Click the packet and it opens a
 * fold at a time (ClothDrop fold), lies flat and hands off to the live map.
 * On the map, the pin is a drop you drag (goo): it chases your finger with a

Usage

Usage
"use client";

import * as React from "react";
import { FoldMap, type FoldMapHandle } from "@/components/fabricui/map";

export function Walk() {
  const map = React.useRef<FoldMapHandle>(null);
  return (
    <>
      <FoldMap ref={map} onOpenChange={open => console.log(open ? "unfolding" : "folding")} />
      <button onClick={() => map.current?.close()}>Fold</button>
    </>
  );
}

Props

PropTypeDefault
places

The places, each with a dot, a name and a card; x and y on the 600 × 400 map. The first is home: where the pin starts and distances are from. Defaults to San Francisco's.

{ id, name, kind, x, y, note, side?, water? }[]—
trail

The walk: place ids in order, drawn as a dashed line through them.

string[]—
title

The map's name, printed on it and on its folded cover.

string—
open

Open, if you control it. Leave out and it unfolds when the folded map is clicked.

boolean—
onOpenChange

Called as it starts to unfold (true) or fold up (false).

(open: boolean) => void—
onReady

Called once it lies folded, ready to be opened.

() => void—
ref

Unfold it or fold it back up (each returns a promise that settles when it's done).

{ open(), close(), isOpen }—