Drawing with napkin script: quickstart#
API hub · Reference · Quickstart · Cheatsheet
A napkin script runs into a sketch book of pages, layers and marks - the same drawing the app makes by hand - and this is the path from a script to the picture it draws, from a shell and from code.
The whole thing in five steps#
- Install the command line, which puts
napkin-sketchon your PATH, and the API in a folder of your own:
npm install -g @isocialpractice/napkin-sketch
npm install @isocialpractice/napkin-sketch
From a clone, npm install, npm run build and npm link do the same, and
the files below go in the clone.
- Save this as
badge.napkin. Eachlayerline starts a layer, and the paint line sets the ink, the width and the fill for every mark after it:
napkin 1
page 320 240
name "badge"
background #ffffff
layer "Plate"
color #1f2328 width 3 fill #ffe08a
rect 20 20 280 200 r 24
layer "Star"
fill #ff8a65
star 160 110 60 26 5
layer "Words"
text "Acme Corp" at 160 180 size 20 align center
- Draw it. This writes
badge.svgandbadge.pngbeside the script:
napkin-sketch draw badge.napkin --to svg,png
Add
seed 7andrough 0.6after thepageline and draw again: every mark afterroughis drawn as a hand would, and the seed makes it the same hand on every run.Draw it from code instead. Save this as
badge.mjsbesidebadge.napkinand runnode badge.mjs:
import { readFileSync, writeFileSync } from 'node:fs';
import { evaluate, renderSketch } from '@isocialpractice/napkin-sketch';
const { ok, book, diagnostics, output } = evaluate(readFileSync('badge.napkin', 'utf8'));
writeFileSync('badge.svg', renderSketch(book.sketches[0], output));
writeFileSync('badge.png', renderSketch(book.sketches[0], { format: 'png', ...output }));
console.log(ok ? 'drew badge' : diagnostics);
A worked example#
A curve through five points, a sun drawn by hand, and a row of dots from a
repeat:
napkin 1
page 480 240
name "hills"
seed 7
background #ffffff
layer "Hills"
color #326478 width 3
through 20 200, 120 140, 240 190, 360 130, 460 180
layer "Sun"
rough 0.6
color #c0392b fill #ffe08a
circle 380 70 40
layer "Dots"
rough off
fill #1f2328
repeat 5 as i {
circle (40 + i * 30) 40 6
}
through puts an anchor at each point and runs the curve smoothly between
them. The dots keep the sun's red ink: paint carries to every mark after it
until something changes it, and rough off turns only the hand-drawn pass
off. Put the dots' paint in a group, or between push and pop, to keep it
to them.
Where to go next#
- Running a script:
evaluate's options and result. - Paint and Transforms: what carries from one instruction to the next.
- Shapes, the shape library and Paths: every outline.
- The hand-drawn pass: what
roughchanges, and by how much. - Text, Images and Linked graphics: what is placed rather than drawn.
- Effects: a blur, a shadow or a color shift for the next layer, group or mark.
- Writing a drawing out: every format, and the box it is cut to.