# Image Annotator

> Numbered notes pinned to places on a picture, listed beside it, such as feedback on a design or a bug on a screenshot.

Page: https://uicontext.com/docs/components/image-annotator

Group: Canvas.

## When to use

Numbered notes pinned to places on a picture, listed beside it, such as feedback on a design or a bug on a screenshot. Use [Canvas](https://uicontext.com/docs/components/canvas.md) instead when people draw and arrange shapes, not leave notes on a fixed picture.

## Usage

```ts
import { ImageAnnotator, type ImageNote } from "./ui/uicontext/components/ImageAnnotator.ts"

export const Review = <M>(h: Html.H<M>, src: string, notes: ReadonlyArray<ImageNote>, onAdd: (x: number, y: number, text: string) => M, onResolve: (id: string, resolved: boolean) => M) =>
	ImageAnnotator(h, {
		props: { label: "Design review", src, alt: "The design under review", notes },
		onEmit: {
			RequestedAdd: ({ x, y, text }) => onAdd(x, y, text),
			RequestedResolve: ({ id, resolved }) => onResolve(id, resolved),
		},
	})
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Reviewing a design

Click the picture to add a note, or press Add a note and move the pin with the arrow keys.

```ts title="src/demo.ts" source="www/src/examples/image-annotator/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { ImageAnnotator, ImageNote } from "./ui/uicontext/components/ImageAnnotator.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { checkoutScreen } from "./checkout-screen.ts"

const styles = stylex.create({ wrap: { inlineSize: "100%" } })

const start: ReadonlyArray<ImageNote> = [
	{ id: "n1", x: 0.33, y: 0.75, author: "Priya Raman", when: "2h ago", text: "The Pay button looks disabled while it isn't. Use the accent color." },
	{ id: "n2", x: 0.31, y: 0.44, author: "Wei Chen", when: "1h ago", text: "Card number needs the card brand's mark once it's typed." },
	{ id: "n3", x: 0.9, y: 0.6, author: "Ada Okafor", when: "Yesterday", text: "Show the tax line, even when it's zero.", resolved: true },
	{ id: "n4", x: 0.2, y: 0.93, author: "Tomás García", when: "20m ago", text: "This line fails contrast. Darken it, and link the terms." },
]

const Message = UiSchema.TaggedEnum({
	Added: { x: Schema.Number, y: Schema.Number, text: Schema.String },
	Resolved: { id: Schema.String, resolved: Schema.Boolean },
})

// a design review: the app keeps the notes and saves them; here they live in the page
export const DesignReview = Html.component({
	name: "DesignReview",
	model: Schema.Struct({ notes: Schema.Array(ImageNote), next: Schema.Number }),
	message: Message,
}).make({
	init: () => ({ notes: start, next: 5 }),
	update: {
		Added: ($, { x, y, text }) => $.evolve({ notes: [...$.model.notes, { id: `n${$.model.next}`, x, y, text, author: "You", when: "Just now" }], next: $.model.next + 1 }),
		Resolved: ($, { id, resolved }) => $.evolve({ notes: $.model.notes.map((n) => (n.id === id ? { ...n, resolved } : n)) }),
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.wrap)).children(
			ImageAnnotator(h, {
				key: "review",
				props: { label: "Checkout screen, v3", src: checkoutScreen, alt: "A checkout page: a card form with a Pay button, and the order beside it", notes: model.notes },
				onEmit: {
					RequestedAdd: ({ x, y, text }) => Message.Added({ x, y, text }),
					RequestedResolve: ({ id, resolved }) => Message.Resolved({ id, resolved }),
				},
			}),
		),
})
```

### Read only

`readOnly` keeps the notes for reading: no adding, no resolving.

```ts title="src/closed.ts" source="www/src/examples/image-annotator/closed.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { ImageAnnotator } from "./ui/uicontext/components/ImageAnnotator.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { checkoutScreen } from "./checkout-screen.ts"

const styles = stylex.create({ wrap: { inlineSize: "min(100%, 44rem)" } })

// a bug report's screenshot, read by the engineer who picks it up: notes to read, not to add
export const BugScreenshot = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(
		ImageAnnotator(h, {
			key: "bug",
			props: {
				label: "Screenshot attached to BUG-3120",
				src: checkoutScreen,
				alt: "The checkout page as the customer saw it",
				readOnly: true,
				notes: [
					{ id: "b1", x: 0.42, y: 0.75, author: "Customer", text: "Pressed Pay three times, nothing happened." },
					{ id: "b2", x: 0.9, y: 0.6, author: "Support", text: "Total is right; the card was charged once." },
				],
			},
		}),
	)
```

### Sample data

The examples above import these files.

```ts title="src/checkout-screen.ts" source="www/src/examples/_shared/checkout-screen.ts"
// a drawn checkout screen, so the demos need no image files
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 500" font-family="system-ui, sans-serif">
<rect width="800" height="500" fill="#f8fafc"/>
<rect width="800" height="56" fill="#ffffff"/><rect y="56" width="800" height="1" fill="#e2e8f0"/>
<rect x="32" y="18" width="88" height="20" rx="4" fill="#0f172a"/>
<text x="680" y="34" font-size="14" fill="#475569">Cart (2)</text>
<text x="40" y="104" font-size="24" font-weight="600" fill="#0f172a">Checkout</text>
<rect x="40" y="128" width="440" height="300" rx="10" fill="#ffffff" stroke="#e2e8f0"/>
<text x="64" y="164" font-size="14" font-weight="600" fill="#0f172a">Payment</text>
<text x="64" y="196" font-size="12" fill="#64748b">Card number</text>
<rect x="64" y="204" width="392" height="40" rx="6" fill="#fff" stroke="#cbd5e1"/>
<text x="76" y="229" font-size="14" fill="#94a3b8">1234 1234 1234 1234</text>
<text x="64" y="272" font-size="12" fill="#64748b">Expiry</text>
<rect x="64" y="280" width="186" height="40" rx="6" fill="#fff" stroke="#cbd5e1"/>
<text x="270" y="272" font-size="12" fill="#64748b">CVC</text>
<rect x="270" y="280" width="186" height="40" rx="6" fill="#fff" stroke="#cbd5e1"/>
<rect x="64" y="352" width="392" height="44" rx="8" fill="#94a3b8"/>
<text x="216" y="380" font-size="15" font-weight="600" fill="#ffffff">Pay $124.00</text>
<rect x="512" y="128" width="248" height="220" rx="10" fill="#ffffff" stroke="#e2e8f0"/>
<text x="536" y="164" font-size="14" font-weight="600" fill="#0f172a">Order</text>
<text x="536" y="196" font-size="13" fill="#334155">Canvas tote</text><text x="700" y="196" font-size="13" fill="#334155">$68.00</text>
<text x="536" y="224" font-size="13" fill="#334155">Mug × 2</text><text x="700" y="224" font-size="13" fill="#334155">$48.00</text>
<text x="536" y="252" font-size="13" fill="#334155">Shipping</text><text x="700" y="252" font-size="13" fill="#334155">$8.00</text>
<rect x="536" y="268" width="200" height="1" fill="#e2e8f0"/>
<text x="536" y="300" font-size="14" font-weight="600" fill="#0f172a">Total</text><text x="692" y="300" font-size="14" font-weight="600" fill="#0f172a">$124.00</text>
<text x="40" y="468" font-size="11" fill="#cbd5e1">By paying you agree to the terms of sale.</text>
</svg>`

export const checkoutScreen = `data:image/svg+xml,${encodeURIComponent(svg)}`
```

## API reference

**Props**

| Name | Type | Description |
| --- | --- | --- |
| `label` (required) | `string` | What the picture is: "Checkout screen, v3". |
| `src` (required) | `string` | — |
| `alt` (required) | `string` | — |
| `notes` (required) | `ImageNote[]` | — |
| `readOnly` | `boolean` | Notes can't be added or resolved: a review that's closed. |
| `locale` | `string` | For the counts; the browser's by default. |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedAdd` | `{ x: number; y: number; text: string }` | A note was written: where it goes and what it says. |
| `RequestedResolve` | `{ id: string; resolved: boolean }` | Resolve or Reopen was pressed on a note. |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/image-annotator
```

With npm: `npx shadcn@latest add checkfu/uicontext/image-annotator`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/image-annotator`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/image-annotator`.

It writes these files:

- `src/ui/uicontext/components/ImageAnnotator.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/touch.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/ImageAnnotator.ts](https://github.com/checkfu/uicontext/blob/main/src/ImageAnnotator.ts)
- [src/touch.stylex.ts](https://github.com/checkfu/uicontext/blob/main/src/touch.stylex.ts)

## Used in

- [SaaS dashboard](https://uicontext.com/blocks/saas-dashboard.md)
- [Workflow builder](https://uicontext.com/blocks/automation-builder.md)
- [Team chat](https://uicontext.com/blocks/team-chat.md)
- [Whiteboard](https://uicontext.com/blocks/whiteboard.md)

## Related

- [Canvas](https://uicontext.com/docs/components/canvas.md) (`canvas`): An infinite canvas to pan and zoom, with shapes, selection handles and smart guides.
- [Canvas Model](https://uicontext.com/docs/components/canvas-model.md) (`canvas-model`): The scene, camera and undo behind every canvas, as pure functions you can test on a server.
- [Toolbar](https://uicontext.com/docs/components/toolbar.md) (`toolbar`): A canvas's floating toolbar, with tool flyouts and a mode switch.
- [Layer Tree](https://uicontext.com/docs/components/layer-tree.md) (`layer-tree`): A canvas's layers in stacking order, to hide, lock, rename and reorder.
- [Number Scrubber](https://uicontext.com/docs/components/number-scrubber.md) (`number-scrubber`): A number field you drag by its label, step with arrow keys or type maths into.
- [Color Picker](https://uicontext.com/docs/components/color-picker.md) (`color-picker`): Pick a color from a square and sliders, as hex, RGB or HSL.
