drift
An animated piece. Strands ripple and dots orbit in a loop of eight seconds. How many of each, and how far they swing, is rolled from the token.
A frame partway through the loop. While it is on screen, the page runs the compiled file live.
| Source | Ships as | Gzipped | Target | Loop |
|---|---|---|---|---|
| 51 lines | 896 bytes | 574 bytes | canvas | 8 seconds |
// drift: an animated canvas piece. Strands that ripple, dots that orbit.
plate(1920, 2400)
random layout, mark
param dotCount = layout.int(1500 ..< 2500)
param strandCount = layout.int(300 ..< 500)
param swing = mark.float(8 ..< 28)
let paper = #f3efe6
let inks = [#1b1b1f, #c2452d, #2d5f8a]
let segs = 24
kind spot { pos: vec, r: num, ph: num, turns: int, reach: num }
kind strand { from: vec, dir: vec, ph: num, amp: num }
let dots = spot[]
let strands = strand[]
once {
for i in 0 ..< dotCount {
dots.add(spot(
pos: vec(layout.float(120 ..< 1800), layout.float(120 ..< 2280)),
r: mark.float(2 ..< 7),
ph: mark.unit(),
turns: mark.int(1 ..= 3),
reach: swing * mark.float(0.4 ..< 1)
))
}
for i in 0 ..< strandCount {
let a = layout.float(0 ..< TAU)
strands.add(strand(
from: vec(layout.float(200 ..< 1720), layout.float(200 ..< 2200)),
dir: polar(a, layout.float(8 ..< 22)),
ph: mark.unit(),
amp: mark.float(4 ..< 18)
))
}
}
frame(t: loop 8s) {
background(paper)
for s in strands {
let side = perp(s.dir) / len(s.dir)
move(s.from)
for k in 1 ..= segs {
let w = s.amp * sin(t * 2 + s.ph + k / segs) * sin(k / segs * PI)
to(s.from + s.dir * k + side * w)
}
}
stroke(inks[0], 1.5)
for c in 0 ..< 3 {
for d in dots[floor(c * dotCount / 3) ..< floor((c + 1) * dotCount / 3)] {
circle(d.pos + orbit(t * d.turns + d.ph, d.reach), d.r)
fill(inks[c])
}
}
}