<t-sketch>
A whiteboard with no edges: pen, highlighter, eraser and lasso on a canvas you pan and zoom. The ink is vector strokes, not pixels, so it stays sharp at any zoom. Anything you put inside the board, vangle elements included, becomes a card on it that you can move around and draw arrows between. When the board is done, Export gives you the HTML of a view-only copy to paste into a page. It's the board of Sketcher, and its JSON is the format Sketcher saves.
The board has
For a fixed-size canvas whose drawing is a picture, see <t-paint>.
Attributes
| Attribute | Default | Description |
|---|---|---|
mode | edit | edit draws and moves things, and has Preview and Export. view only pans and zooms: the ink stays put and the cards are live. |
name | Puts the board in a page variable of that name, as JSON, so other elements and your code can read it, save it or set it. See the board as a variable. Without it, nothing else on the page sees the board. | |
height | 400 | The board's height in pixels. It's as wide as its container. |
background | dots | The paper of an empty board: plain, dots or grid. A loaded board brings its own, and the palette's background button cycles through them. Shown while editing; view mode and Preview use plain paper. |
src | A JSON file of a board to start from. A <script type="application/json"> inside the board does the same, inline. | |
label | the name | The accessible name of the board. |
Interaction
The palette in the top-left corner has the tools, three line widths, six colors, the background, undo and redo. Right-click the board for the rest.
| Input | Effect |
|---|---|
| Drag | Draws with the current tool. Mouse, pen and touch all work; a pen's eraser end erases. In view mode, or a preview, it pans. |
| P, M, E, S | Pen, marker (a highlighter), eraser, select. |
| 1–6, [ ] | A color, a thinner or thicker line. |
| Shift while drawing | A straight line, at a multiple of 15°. |
| Option/Alt while drawing | A straight line from grid point to grid point. With Shift too, the end lands on the grid where that keeps the angle. |
| Select, then drag | Draw a lasso around strokes to select them. Drag the selection to move it, or its round handle to resize it. |
| ⌫ | Deletes the selection. |
| ⌘/Ctrl + C, X, V | Copy, cut and paste strokes, also between boards. A paste lands under the pointer. |
| ⌘/Ctrl + Z, Shift+⌘/Ctrl + Z | Undo and redo. |
| ⌘/Ctrl + A | Selects everything. |
| Scroll, or two fingers on a trackpad | Pans, once you've clicked the board: its border turns the accent color. Before that, the page scrolls, and the board says how to scroll it instead. |
| Ctrl/⌘ + scroll, or pinch | Zooms around the pointer, from 10% to 800%. |
| Space + drag, middle-drag, two fingers on a touch screen | Pans (and with two fingers, zooms). |
| ⌘/Ctrl + 0, Shift+0, ⌘/Ctrl + = - | Zoom to fit, to 100%, in and out. |
| Shift+⌘/Ctrl + E, Shift+⌘/Ctrl + C | Downloads a PNG of the board, copies one (of the selection, if any). |
| Esc | Cancels the current stroke, or clears the selection. In preview, goes back to editing. |
The eye, the </> button | Preview the board as readers will see it; Export it. |
| The arrows in the corner | Full screen, in edit and view mode. Esc or the button again goes back. |
Line widths are in screen pixels when you draw: zoom out to draw big, zoom in for detail. Overlapping highlights of one color don't get darker. Strokes in the six palette colors follow the page's light or dark theme, so black ink becomes white chalk in dark mode.
Cards
The board's children are cards: a paragraph, a figure, a chart, anything. Each one sits at
data-x, data-y in board coordinates and is data-width wide (280 without one). A card without
a place goes to the right of the others. Cards pan and zoom with the board, and the ink is
on top of them, so you can circle a number or draw an arrow from one chart to another.
A circle with radius
has an area of
While editing, a card is a thing on the board: the select tool picks it up (click it, or lasso its middle) and drags it, and its outline is dashed. Only ink resizes, and deleting removes only ink, since the cards are the page's HTML. Preview shows the board as a reader will see it. There the cards are live, so dragging a number drags the number, and dragging the paper pans. Esc goes back to editing.
Children that don't show, like a <t-let>, aren't cards. Put the <t-let>s a board needs
outside it, next to it, as the mixture gallery page does.
Export
Export, the last button in the palette (or Copy HTML for a Page in the right-click menu), copies the HTML of a view-only copy of the board:
<t-sketch mode="view" height="300">
<p data-x="0" data-y="0" data-width="200">A circle with radius …</p>
<p data-x="340" data-y="120" data-width="220">has an area of …</p>
<script type="application/json">{"background":"dots","math":[],"strokes":[…],"version":2}</script>
</t-sketch>
The cards are the HTML they were written as, at the places you moved them to, and the ink is
JSON with the points rounded to a tenth. Paste it over the board you edited to publish the
result. exportHTML() on the element gives the same text.
The board as a variable
name="notes" puts the board in a page variable called notes, the way a <t-num name="r"> puts
its number in r. Other elements can use it in their expressions (notes.strokes.length), and
code can read it or set it with scopeOf(el).get("notes") and .set("notes", …). The variable
holds the board's ink as JSON; the cards aren't in it. It changes when a
stroke or an edit is done, not while you draw.
{
"background": "dots",
"math": [],
"strokes": [
{
"color": { "a": 1, "b": 0.12, "g": 0.11, "r": 0.11 },
"id": "5C4F3B9E-…",
"points": [[0, 0], [12.5, 3], [30, 4]],
"swatch": 0,
"tool": "pen",
"width": 2.5
}
],
"version": 2
}
A stroke's points are in board coordinates, with y pointing down, and its width is in the same
units. tool is pen or marker. swatch is the palette color (0 to 5), and color is what
it was when drawn, for a stroke without one. math is Sketcher's layer of typeset LaTeX: a
<t-sketch> keeps it, but doesn't show it.
The variable works both ways. Set it to a board, as an object or as JSON text, and the board shows
it, zoomed to fit, and undo starts over. An empty value ("" or null) leaves the board as it is,
and the variable goes back to holding it. A board with a src holds an empty board until the file
has loaded.
JSON.stringify of the variable is a .json file of the board. Its keys are in the order the
Sketcher Mac app writes them, so the app opens it too (it calls its files .sketcher).
As a picture
The element's toPNG() gives the ink as a PNG data URL, cropped to it with a margin, on the
board's paper, at twice the size. Cards aren't in the picture. It's null when there's no ink.
const sketch = document.querySelector("t-sketch");
image.src = sketch.toPNG(); // or toPNG({ scale: 1, padding: 16 })
The element fires a change event whenever the board changes, with or without a name.
Styling
The board is in a shadow root, so the page's styles for buttons don't reach it. It uses
--tangle-surface for the paper and the palette, and --tangle-accent for the selection and the
current tool. Its pieces are shadow parts:
| Selector | Matches |
|---|---|
t-sketch::part(board) | The box around the canvas. Its border is the accent color while the board has focus. Without a height attribute, you can set its height here. |
t-sketch::part(canvas) | The canvas. |
t-sketch::part(palette) | The floating palette of tools. |
t-sketch::part(viewbar) | The zoom-to-fit and full-screen buttons (and in a preview, the way back to editing). |
t-sketch::part(button) | A button in the palette or the selection bar. The current tool has aria-pressed="true". |
t-sketch::part(swatch) | A color button. |
t-sketch::part(selection-bar) | The bar with the delete button above a selection. |
t-sketch::part(menu) | The right-click menu. |
t-sketch::part(board) { height: 70vh; border-radius: 0; }
t-sketch::part(palette) { box-shadow: none; }