Canvas drawing app
A small sketch pad drawn on a 2D canvas. The check picks colors and a brush, draws with real mouse drags at points on the canvas, reads pixels back with getImageData to check what was drawn, and takes two screenshots: the canvas's own pixels, and the page.
Run it#
cd examples/canvas
npx brokit check --verbose # --verbose lists every step, as below$ brokit check --verbose
brokit check: 1 scenario, Chrome 158.0.8127.36, software drawing
✓ checks/sketch 18 steps, 1.7 s, 2 baselines written
✓ click [aria-label=Green] 187 ms
✓ drag {"from":{"selector":"canvas","offset":[30,250]},"to":{"selector":"canvas","offset":[450,250]}} 188 ms
✓ click [aria-label=Blue]
✓ drag {"from":{"selector":"canvas","offset":[60,250]},"to":{"selector":"canvas","offset":[170,100]}} 167 ms
✓ drag {"from":{"selector":"canvas","offset":[170,100]},"to":{"selector":"canvas","offset":[280,250]}} 166 ms
✓ drag {"from":{"selector":"canvas","offset":[230,250]},"to":{"selector":"canvas","offset":[320,150]}} 167 ms
✓ drag {"from":{"selector":"canvas","offset":[320,150]},"to":{"selector":"canvas","offset":[410,250]}} 167 ms
✓ click [aria-label=Orange]
✓ click [data-size='28']
✓ click canvas +[400,70]
✓ expect sketch.strokes.length equals 6
✓ expect [...document.querySelector('canvas').getContext('2d').getIma… equals [14,143,108,255]
✓ expect [...document.querySelector('canvas').getContext('2d').getIma… equals [245,158,11,255]
✓ screenshot drawing → brokit-out/checks/sketch/drawing.png (480×300) [baseline: new]
✓ screenshot app → brokit-out/checks/sketch/app.png (640×440) [baseline: new] 130 ms
✓ click #clear
✓ expect sketch.strokes.length equals 0
✓ expect [...document.querySelector('canvas').getContext('2d').getIma… equals [255,255,255,255]
1 scenario passed in 1.7 sThe first run writes the baselines (checks/baselines/sketch/drawing.png and app.png); the runs after it compare against them. In a real project you commit the baselines. They're not committed here, since they depend on the fonts of the machine that made them.

The baselines: the app's viewport, and the canvas's own pixels.
The page#
index.html has a toolbar and a 480×300 canvas. app.js draws on pointer events: a line from each pointer position to the next while the button is down, and a dot for a click. When it's set up, it sets window.sketch.ready, which the check waits for.
The check#
The check, checks/sketch.check.json:
{
"note": "Draw two mountains and a sun on the sketch pad, then read its pixels.",
"url": "/index.html",
"viewport": { "size": "640x440" },
"ready": "window.sketch?.ready",
"steps": [
{ "click": { "selector": "[aria-label=Green]" } },
{
"drag": { "from": { "selector": "canvas", "offset": [30, 250] }, "to": { "selector": "canvas", "offset": [450, 250] } },
"note": "the ground: points 30 and 450 px from the canvas's left edge, 250 px down"
},
{ "click": { "selector": "[aria-label=Blue]" } },
{ "drag": { "from": { "selector": "canvas", "offset": [60, 250] }, "to": { "selector": "canvas", "offset": [170, 100] } } },
{ "drag": { "from": { "selector": "canvas", "offset": [170, 100] }, "to": { "selector": "canvas", "offset": [280, 250] } } },
{ "drag": { "from": { "selector": "canvas", "offset": [230, 250] }, "to": { "selector": "canvas", "offset": [320, 150] } } },
{ "drag": { "from": { "selector": "canvas", "offset": [320, 150] }, "to": { "selector": "canvas", "offset": [410, 250] } } },
{ "click": { "selector": "[aria-label=Orange]" } },
{ "click": { "selector": "[data-size='28']" }, "note": "the large brush" },
{ "click": { "selector": "canvas", "offset": [400, 70] }, "note": "a click is a stroke of one point: a dot" },
{ "expect": "sketch.strokes.length", "equals": 6 },
{
"expect": "[...document.querySelector('canvas').getContext('2d').getImageData(240, 250, 1, 1).data]",
"equals": [14, 143, 108, 255],
"note": "the pixel at (240, 250) is the green of the ground, #0e8f6c"
},
{
"expect": "[...document.querySelector('canvas').getContext('2d').getImageData(400, 70, 1, 1).data]",
"equals": [245, 158, 11, 255],
"note": "the middle of the sun is orange, #f59e0b"
},
{
"screenshot": "drawing",
"from": "document.querySelector('canvas')",
"baseline": true,
"note": "the canvas's own pixels, 480×300, without the page around it"
},
{ "screenshot": "app", "baseline": true, "note": "the whole viewport, toolbar and all" },
{ "click": { "selector": "#clear" } },
{ "expect": "sketch.strokes.length", "equals": 0 },
{
"expect": "[...document.querySelector('canvas').getContext('2d').getImageData(240, 250, 1, 1).data]",
"equals": [255, 255, 255, 255],
"note": "blank paper again"
}
]
}"ready": "window.sketch?.ready"waits for the app's own signal, not just the load event: the check starts once the canvas has its paper.{ "selector": "canvas", "offset": [30, 250] }is a point 30 CSS pixels from the canvas's left edge and 250 down, wherever the canvas is on the page. Drawing apps think in canvas coordinates; so does the check.dragpresses the mouse button atfrom, moves totoin 8 steps, and releases. The page getspointerdown,pointermoveandpointerup, as from a real mouse.getImageData(240, 250, 1, 1).datais the pixel at (240, 250) as red, green, blue and alpha. The canvas is as big as it's drawn (480×300 pixels in 480×300 CSS pixels, at a device pixel ratio of 1), so canvas pixels and the points above line up."from": "document.querySelector('canvas')"takes the screenshot from the page itself: the canvas's pixels, without the toolbar or the page around it. It also takes anOffscreenCanvas,ImageDataor a PNG data URL. A WebGL canvas needspreserveDrawingBufferfor this; take a screenshot with"selector"instead.- The last screenshot has no
"from"or"selector": it's the viewport, as a person would see it.
brokit check draws with --gpu software by default, so a canvas gives the same pixels on every machine.
Learn more#
- Steps: points,
drag,clickandscreenshot. - Scenarios:
readyand other ways to know a page is ready. - Checks and baselines: baselines, tolerance and
--gpu.