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#

shell
cd examples/canvas
npx brokit check --verbose      # --verbose lists every step, as below
terminal
$ 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 s

The 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.

A sketch pad with blue mountains, green ground and an orange sun, under a toolbar of colors and brush sizes Only the canvas's own pixels: the mountains and the sun

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:

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"
    }
  ]
}

brokit check draws with --gpu software by default, so a canvas gives the same pixels on every machine.

Learn more#

Edit this page on GitHub