brush v1
One stroke style, the sweep: a run of thin streaks laid close along one path, so their colours blend the way a wide brush dragged fast does. The rims thin out, the ends stagger and narrow, and a few streaks are drawn sharp (a dark hair through the pale, a lit ridge through the dark).
use brush v1The svg target only. A sweep is made of marks (polyline for the streaks, polygon for the body under
them), and the canvas target does not draw marks yet. A canvas build refuses the call, at the call’s line.
Build with --target svg.
Names
brush.sweep(r, path, width, ramp, opacity: 0.7, density: 1, feather: 0.3, grain: 1, crisp: 0.1, body: 0, taper: 0.35)
| value | ||
|---|---|---|
r | random | where its random numbers come from. A sweep rolls many, in order. |
path | vec[] | the line it follows, two points or more, in plate units. Any spacing: it is walked in even steps. |
width | num | across the sweep, in plate units. Held under a third of the path’s length, so a short drag never lands as a wide stub. |
ramp | color[] | its colours, at least one: the first on the left of travel, the last on the right (see colour v1 for ramps). |
opacity | num | of the streaks, 0 to 1 (each varies about it) |
density | num | how close the streaks lie: 1 is about 1.7 streaks per unit of width (30 to 900 a sweep) |
feather | num | how much of each rim thins out, 0 (a hard edge) to 1 |
grain | num | how far a streak’s colour wanders off the ramp; 0 for none |
crisp | num | the share of streaks drawn sharp, 0 to 1 |
body | num | an opaque ground under the streaks, as a share of the width (0 for none, 0.5 to 0.9 to hide the paper) |
taper | num | how much of each end narrows to a point, 0 (square ends) to 0.5 |
Everything after ramp may be left out. A path with no length, or an empty ramp, draws nothing.
Example
plate(1920, 2400)
use brush v1
random layout, marks
background(#f3efe6)
for i in 0 ..< 30 {
// a gentle arc across the plate
let y = layout.float(200 ..< 2200)
let bend = layout.float(-300 ..< 300)
let path = vec[]
for k in 0 ..= 20 {
let u = k / 20
path.add(vec(100 + 1720 * u, y + bend * sin(PI * u)))
}
brush.sweep(marks, path, layout.float(40 ..< 120), [#1d2b3a, #3e5c76, #9fb4c7], body: 0.6)
}Notes
- Give the brush a named random of its own (
random layout, marks), so the many numbers a sweep rolls do not move the other rolls when a sweep changes. - Paint order is call order: broad sweeps first, finer ones over them.
- Size of the SVG it writes: a sweep 60 units wide is about 100 streaks, each a polyline of up to 160 points. A plate of 150 sweeps is 5 to 9 MB of SVG text, written in under a tenth of a second.
- What it costs: about 2,060 bytes of shipped JavaScript, and about 230 more from colour v1 (
at,mix,lighten,darken), which it uses. Only what the piece calls is shipped.
The whole library, 133 lines
// brush v1: one stroke style, the sweep.
//
// A sweep is a run of thin streaks laid close along one path, so their colours
// blend the way a wide brush dragged fast does. Each streak is a polyline (a
// mark), so a sweep draws on the svg target.
use colour v1
// the length of a path, point to point
fn _length(path: vec[]): num {
var total = 0
for i in 1 ..< path.len { total += len(path[i] - path[i - 1]) }
return total
}
// count points in even steps along path, from its first point to its last
fn _resample(path: vec[], count: int): vec[] {
let cum = num[]
cum.add(0)
for i in 1 ..< path.len { cum.add(cum[i - 1] + len(path[i] - path[i - 1])) }
let total = cum[cum.len - 1]
let out = vec[]
var j = 1
for s in 0 ..< count {
let d = total * s / (count - 1)
while j < cum.len - 1 && cum[j] < d { j += 1 }
let seg = max(1e-9, cum[j] - cum[j - 1])
out.add(lerp(path[j - 1], path[j], clamp((d - cum[j - 1]) / seg, 0, 1)))
}
return out
}
// a number from -j up to j
fn _jitter(r: random, j: num) = r.float(-j ..< j)
// A sweep along path, width across, its streaks coloured across the ramp: the ramp's
// first colour on the left of travel, its last on the right. The rims thin out, the
// ends stagger and narrow, and a few streaks are drawn sharp (a dark hair through the
// pale, a lit ridge through the dark). The width is held under a third of the path's
// length, so a short drag never lands as a wide stub. A path with no length, or an
// empty ramp, draws nothing. Every number it rolls comes from r, in order.
fn sweep(r: random, path: vec[], width: num, ramp: color[], opacity: num = 0.7, density: num = 1,
feather: num = 0.3, grain: num = 1, crisp: num = 0.1, body: num = 0, taper: num = 0.35) {
if path.len < 2 || ramp.len == 0 { return }
// the chance of keeping a streak at rim distance d (0 the middle, 1 the rim)
fn keep(d: num): num {
let start = 1 - feather
if d <= start { return 1 }
return 1 - smoothstep(0, 1, (d - start) / feather) * 0.92
}
// even steps along the path, about one per 14 units, and the normal at each
let total = _length(path)
if total <= 0 { return }
let w = min(width, total * 0.33)
let n = clamp(round(total / 14), 8, 160)
let pts = _resample(path, n)
let nrm = vec[]
for i in 0 ..< n {
let d = pts[min(i + 1, n - 1)] - pts[max(i - 1, 0)]
nrm.add(perp(d) / max(1e-6, len(d)))
}
// one drift and one breathing width for the whole sweep, so the streaks travel together;
// the breathing narrows toward each end by taper
let drift = num[], breathe = num[]
var dr = 0, dd = r.chance(0.5) ? 1 : -1
var br = 1, bd = r.chance(0.5) ? 1 : -1
for i in 0 ..< n {
dr += dd * r.float(0 ..< 0.01)
if dr > 0.06 { dd = -1 }
if dr < -0.06 { dd = 1 }
br += bd * r.float(0 ..< 0.012)
if br > 1.08 { bd = -1 }
if br < 0.85 { bd = 1 }
let u = i / (n - 1)
let e = taper > 0 ? min(1, u / (taper * 0.5), (1 - u) / taper) : 1
drift.add(dr)
breathe.add(br * max(0.06, e * (2 - e)))
}
let half = w / 2
// the point at step i, seat s across the sweep (-1 its left rim, 1 its right)
fn seat(i: int, s: num) = pts[i] + nrm[i] * ((s * breathe[i] + drift[i]) * half)
// the opaque body under the streaks, in seven strips off the ramp, its ends short of theirs
if body > 0 {
let a = round(n * 0.06), b = n - 1 - round(n * 0.1)
for k in 0 ..< 7 {
let s0 = body * (2 * k / 7 - 1)
let s1 = body * (2 * (k + 1) / 7 - 1) + 0.02
let strip = vec[]
for i in a ..= b { strip.add(seat(i, s0)) }
for i in 0 ..= b - a { strip.add(seat(b - i, s1)) }
polygon(strip, fill: colour.at(ramp, ((s0 + s1) / 2 + 1) / 2))
}
}
let count = clamp(round(w * 1.7 * density), 30, 900)
let baseW = w / count * 2.6
for q in 0 ..< count {
// its seat across the sweep, thinned toward the rims
var s = r.float(-1 ..< 1)
var tries = 1
while tries < 6 {
if r.unit() <= keep(abs(s)) { break }
s = r.float(-1 ..< 1)
tries += 1
}
let rim = abs(s)
// rim streaks start later and stop sooner: a feathered, staggered end
let a = round(n * r.float(0 ..< 0.08 + rim * 0.35))
let b = round(n - 1 - n * r.float(0 ..< 0.12 + rim * 0.4))
if b - a < 2 { continue }
// its colour off the ramp at its seat, wandering by grain
var c = colour.at(ramp, (s + 1) / 2 + _jitter(r, 0.09 * grain))
let shade = _jitter(r, 0.06 * grain)
c = shade < 0 ? colour.darken(c, -shade) : colour.lighten(c, shade)
var sw = 0, so = 0
if r.chance(crisp) {
if (0.299 * c.r + 0.587 * c.g + 0.114 * c.b) / 255 > 0.45 {
c = colour.darken(ramp[0], 0.3)
} else {
c = colour.lighten(ramp[ramp.len - 1], r.float(0.2 ..< 0.5))
}
sw = r.float(0.5 ..< 1.6)
so = r.float(0.6 ..< 1)
} else {
sw = baseW * r.float(0.4 ..< 2.2)
so = clamp(opacity * r.float(0.5 ..< 1.5), 0.05, 1)
}
// its line, wandering a little off its seat
var wan = 0, wd = r.chance(0.5) ? 1 : -1
let run = vec[]
for k in a ..= b {
wan += wd * r.float(0 ..< 0.006)
if wan > 0.02 { wd = -1 }
if wan < -0.02 { wd = 1 }
run.add(seat(k, s) + nrm[k] * (wan * half))
}
polyline(run, stroke: c, width: sw, opacity: so, caps: "round")
}
}