# Image markup

A picture to mark over the whole window, in a person's inks, handed back as a new picture and its marks as data.

Live variants: https://design.arcadia.is/components/image-markup

```tsx
import { ImageMarkup } from "@arcadia/ui";
```

```tsx
import { useState, useEffect } from "react";
import { Button, ImageMarkup } from "@arcadia/ui";

function MarkingUp({ initial }: { initial?: "annotating" | "cropped" } = {}) {
  const [rulers, setRulers] = useState(true);
  const [open, setOpen] = useState<(Capture & { marks: readonly Mark[]; crop?: CropRect }) | null>(null);
  const [last, setLast] = useState<{ source: Capture; result: MarkupResult; url: string } | null>(null);
  useEffect(() => () => { if (last) URL.revokeObjectURL(last.url); }, [last]);
  useEffect(() => {
    let live = true;
    if (initial || new URLSearchParams(window.location.search).get("preview") === "markup") {
      void capture().then((p) => {
        if (live) setOpen({ ...p, marks: [], crop: initial === "cropped" ? { x: 520, y: 0, width: 2280, height: 1400 } : undefined });
      });
    }
    return () => { live = false; };
  }, [initial]);
  return (
    <div {...sx("cmp-markup-stage", ImageMarkupStyles.stage)}>
      <div {...sx("cmp-bar", cmp.bar)}>
        <Button variant="secondary" onClick={() => void capture().then((p) => setOpen({ ...p, marks: [] }))}>Mark up a capture</Button>
        {last && <Button variant="ghost" onClick={() => setOpen({ ...last.source, marks: last.result.marks, crop: last.result.crop })}>Go on marking</Button>}
      </div>
      {last && (
        <>
          <img src={last.url} width={last.source.width} height={last.source.height} alt="The capture with its marks" {...sx("cmp-markup-result", ImageMarkupStyles.result)} />
          <pre data-scroll-inset="" tabIndex={0} role="group" aria-label="The marks as data" {...sx("cmp-markup-data", ImageMarkupStyles.data)}>{JSON.stringify({ marks: last.result.marks, ...(last.result.crop ? { crop: last.result.crop } : {}) }, null, 1)}</pre>
        </>
      )}
      {open && (
        <ImageMarkup
          image={open.image} width={open.width} height={open.height} marks={open.marks} crop={open.crop} pixelRatio={2} rulers={rulers} onRulersChange={setRulers} label="Annotate"
          onCancel={() => setOpen(null)}
          onDone={(result) => { setLast({ source: { image: open.image, width: open.width, height: open.height }, result, url: URL.createObjectURL(result.image) }); setOpen(null); }}
        />
      )}
    </div>
  );
}
```

## Accessibility

The picture is an `application` named by the tool in hand, so its keys reach it; a circle, a rectangle, an arrow, a spacing marker and words each have a handle a key moves, and so does each guide, named by its place. Each ruler is a button that adds a guide. The tools are one radio group and the inks another, each a `fieldset` named by its `legend`, the size a horizontal `slider`, and, while the spacing tool is in hand and the capture is 1x, 2x or 3x, a segmented control of 1x, 2x and 3x with the current ratio marked when the product gives `onPixelRatioChange`. Escape closes an open colour popover first, or else leaves the eyedropper, stops what is being drawn, finishes editing words, lets go of the selected mark, or closes the editor while nothing is drawn or cropped; once something is, only Cancel throws it away. The eyedropper follows the pointer with a loupe: eleven pixels around it at eight times their size, a ring in the colour under them, and that colour as a hex below; a press keeps it as the ink and returns to the tool in hand. Under forced colours, which drop every fill, the tool in hand stands on the system's highlight.

- V, P, S, C, R, A, M, E, T, I: Select, pen, measure, circle, rectangle, arrow, marker, eraser, words, the eyedropper
- Command or Ctrl Z, Shift Z: Undo, redo
- Enter on the picture: Words, a circle, a rectangle, an arrow or a spacing marker at its middle
- Shift-click with Pen: Draw a straight segment from the last pen point; Shift-drag constrains the angle
- Arrow keys on a mark handle: Move the mark; with Option or Alt, turn an arrow's head, move a spacing end or resize a shape
- Arrow keys on a crop handle: Move the crop edge or corner by 1 px, or 10 px with Shift
- Delete on a mark handle: Remove the mark
- Shift R: Show or hide the rulers and their guides
- Enter on a ruler: A guide across the middle of the picture in view
- Arrow keys on a guide: Move the guide by 1 px, or 10 px with Shift
- Delete on a guide: Remove the guide
- Option or Alt drag with Select: Move a copy of the mark and leave the mark in place
- Command or Ctrl C, then V: Copy the selected mark, then paste a copy beside it; in words being typed, copy and paste the text
- Space and a drag: Move the picture
- Command or Ctrl Enter: Done
- Escape: Closes the topmost: an open popover, the eyedropper, a drag, the words being edited, the selection, or the editor while nothing is drawn

## API reference

### ImageMarkup

- `image` (`Blob | ImageBitmap`, required): The picture, as its bytes or already decoded.
- `width` (`number`, required): Its own size in pixels: the frame of every mark and the size of the PNG.
- `height` (`number`, required)
- `marks` (`readonly Mark[]`): Marks to go on from.
- `crop` (`CropRect`)
- `guides` (`readonly Guide[]`): Guides to go on from, in the picture's pixels.
- `rulers` (`boolean`): The rulers and their guides show; left out, they start shown when `guides` holds one and Shift R turns them.
- `onRulersChange` (`(on: boolean) => void`): The rulers were turned on or off by Shift R.
- `guidesInExport` (`boolean`, default `true`): Done draws the guides into the PNG while the rulers show; false keeps them out of the PNG, and `guides` still hands them back.
- `pixelRatio` (`number`, default `1`): The capture's device pixel ratio: 2 for a Retina capture, which holds two image pixels for each CSS pixel. A spacing label divides the picture's pixels by it.
- `label` (`string`, default `"Mark up"`): Its name for assistive technology, in the product's words ("Annotate").
- `onDone` (`(result: MarkupResult) => void`, required): Done: the new picture and its marks.
- `onPixelRatioChange` (`(ratio: 1 | 2 | 3) => void`): Given, the spacing tool offers 1x, 2x and 3x when the capture is 1x, 2x or 3x, the current one marked; the product sets `pixelRatio` to the choice.
- `onCancel` (`() => void`, required): Nothing is handed back. Escape asks it too, only while the session has drawn and cropped nothing.
- `rootStyle` (`RootStyle<ImageMarkupKnobs>`)
