# Canvas



Use `ccp visualize` to create and preview local visual documents. Creating and previewing one doesn't log in, create a cloud App, or publish anything.

## Formats [#formats]

| `--format`            | Use it for                                                                               |
| --------------------- | ---------------------------------------------------------------------------------------- |
| `explainer` (default) | An interactive React document edited in source. Explainers don't support companion notes |
| `canvas`              | An editable diagram stored in `diagram.json`, with an optional companion `note.md`       |
| `note`                | A standalone Markdown document stored in `note.md`                                       |

## Create and Preview [#create-and-preview]

```bash
ccp visualize init ./vector-lab --title "Vector lab"
ccp visualize inspect ./vector-lab
ccp visualize typecheck ./vector-lab
ccp visualize dev ./vector-lab
```

To start with a diagram or a note instead:

```bash
ccp visualize init ./retry-flow --title "Retry flow" --format canvas
ccp visualize init ./release-notes --title "Release notes" --format note
```

| Command                                     | Description                                                                                                                                                                              |
| ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ccp visualize init <path> --title <title>` | Create a visual in a new directory. Use `--format` to choose the format. For explainers, `--no-install` skips dependency installation. Diagrams and notes don't install npm dependencies |
| `ccp visualize inspect <path>`              | Validate and print the project's metadata as JSON                                                                                                                                        |
| `ccp visualize typecheck <path>`            | Typecheck an explainer with TypeScript, or validate the content files of a diagram or note                                                                                               |
| `ccp visualize dev <path>`                  | Run a protected local preview                                                                                                                                                            |

`canvas.json` holds a version, a stable random ID, a title, and the visual's format. Don't change the ID when revising an existing visual.

## Editing an Explainer [#editing-an-explainer]

Start with `CANVAS.md` and `src/App.tsx`. The starter composes typed `Canvas`, `Section`, `Columns`, `Stat`, `Table`, and `Callout` components from `./canvas-ui`. Edit files under `src/`, extend `src/styles.css` for figures, and put static assets in `public/`. Canvas uses plain CSS; Tailwind isn't processed.

The preview doesn't run the server handler, read the project `.env`, or use your CCP login. Imports must resolve inside `src/` or `node_modules/`, and no package scripts run during preview.

Dependencies install with npm scripts disabled and are kept outside the source folder. If you used `--no-install`, preview, typecheck, or build installs them when needed.

## Interaction State [#interaction-state]

Use `useCanvasState<T>(key, initial)` from `./canvas` for JSON interaction state that should survive a preview restart; ordinary React state is lost. If another viewer saved first, a stale save refreshes the view and asks you to repeat your edit.

## Editing a Diagram or Note [#editing-a-diagram-or-note]

Use the local preview to edit and save a diagram or note. Saves update the source files in the visual directory. If you move or replace that directory while a preview is open, reopen the visual before continuing to edit.

To edit from the CLI, run `ccp visualize content <path>`. It prints JSON with the current `revision` and the visual's content.

To replace one part, pass the revision you just read and send the new contents on standard input:

```bash
ccp visualize content ./retry-flow
# Replace REVISION with the revision printed above.
ccp visualize content ./retry-flow --part note --revision REVISION < ./companion.md
```

Use `--part diagram` with graph JSON (`name`, `nodes`, and `edges`) for an editable canvas, or `--part note` with Markdown for either an editable canvas or a standalone note. This replaces the named part without changing the other one. If the revision is stale, the command fails without applying the edit. Read the current content before reapplying your changes.

## Preview Events [#preview-events]

`ccp visualize dev` always emits the same version 1 JSON events as [`ccp dev --json`](/docs/ccp/dev#json-events). Its startup event is `canvas_listening`, with `data.id` and `data.url`.

Open that exact URL to start the preview session. The URL grants access to the preview, so don't share or publish it. The preview listens only on a local loopback port, so it isn't reachable through another machine's `localhost`.

Explainer source edits rebuild and reload the preview automatically. `build_failed` keeps the previous good build, and `ready` marks a successful rebuild. Process exit is authoritative.

## Publishing [#publishing]

Canvas commands never publish. To publish, run `ccp visualize typecheck .` and then `ccp deploy` in the visual folder — it builds and deploys like any other App. See [Deploying](/docs/ccp/deploying).

Published explainers keep interaction state in each reader's browser; your local state and preview URL aren't uploaded. Published diagrams and notes are read-only snapshots of the saved content at build time, including any companion note. Redeploy to publish later edits.
