Examples11
weave

A still plate. Warp threads run down and weft threads run across, and at every crossing one of them lies on top, by a rule that gives a plain weave or a twill. Each thread keeps the colour of the one before it until the colour changes, the same down as across, so the stripes cross into checks. The cloth hangs in folds that push the threads aside and catch the light, and the warp is left loose at the foot.
| Source | Ships as | Gzipped | Target |
|---|---|---|---|
| 45 lines | 3,390 bytes | 1,808 bytes | svg |
Nilo source
// weave: a striped cloth, every thread going over and under the threads across it, hung in folds
plate(1920, 2400)
use noise v2, colour v2
random layout, dye, yarn
let field = noise.table(layout)
let warps = layout.int(40 ..< 72), cell = layout.float(1150 ..< 1450) / warps, wefts = floor(1640 / cell)
// a warp thread (down) lies over a weft (across) where (i + j * step) % period < over: plain, or a twill
let period = layout.int(2 ..< 5), over = layout.int(1 ..< period), step = layout.chance(0.5) ? 1 : period - 1
// the stripes: each thread keeps the colour of the one before, until it changes; warp and weft alike
let inks = [#1d2b3a, #8c3b24, #d9a441, #ece6d8, #22362b, #b0302a, #1b1e2b, #e8d9a8, #3a2a4d, #b5482f, #e2cf9a,
#f3ede2, #24382e, #6b3f6e, #c98fae, #f0e8dc, #2a2a2a, #8a8f96, #c8c3b8, #f2efe8, #12304d, #2c6f8f, #e3b23c, #f1ece0]
let pick = dye.int(0 ..< 6) * 4
let stripe = color[]
var held = dye.int(0 ..< 4)
for i in 0 ..< max(warps, wefts) {
if dye.chance(0.12) { held = dye.int(0 ..< 4) }
stripe.add(inks[pick + held])
}
// folds that run down the cloth: a point is pushed sideways by the fold, and lit by its slope
fn fold(p: vec) = noise.octaves(field, point: vec(p.x * 0.002, p.y * 0.0005), layers: 2)
fn at(p: vec) = vec(p.x + 50 * fold(p), p.y)
fn row(j: int): vec[] = [at(vec(0, (j + 0.5) * cell)), at(vec(warps * cell, (j + 0.5) * cell))]
background(#e9e4da)
place(vec(960 - warps * cell / 2, 1100 - wefts * cell / 2), turn: layout.float(-0.006 ..< 0.006)) {
// a shadow on the paper; then row by row, the dark between the threads, and the thread on top at each crossing
place(vec(18, 26), opacity: 0.2) { for j in 0 ..< wefts { polyline(row(j), stroke: #2b2420, width: cell) } }
for j in 0 ..< wefts {
polyline(row(j), stroke: colour.darken(stripe[j], amount: 0.75), width: cell)
for i in 0 ..< warps {
let mid = vec(i + 0.5, j + 0.5) * cell, up = (i + j * step) % period < over
let lit = 8 * (fold(mid - vec(6, 0)) - fold(mid + vec(6, 0))) + yarn.float(-0.06 ..< 0.06) - (up ? 0 : 0.1)
let ink = colour.mix(up ? stripe[i] : stripe[j], toward: lit > 0 ? #fff6e8 : #000000, amount: abs(lit) * 0.6)
// a little longer than its square, so a float over several squares shows no seam; a sheen along it
let along = (up ? vec(0, 0.53) : vec(0.53, 0)) * cell
let a = at(mid - along), b = at(mid + along)
polyline([a, b], stroke: ink, width: cell * 0.88)
polyline([lerp(a, b, 0.2), lerp(a, b, 0.8)], stroke: colour.lighten(ink, amount: 0.12), width: cell * 0.3)
}
}
// the warp left loose at the foot, each end hanging in a curve of its own
for i in 0 ..< warps {
let p = at(vec(i + 0.5, wefts) * cell), sway = yarn.float(-14 ..< 14), fall = yarn.float(120 ..< 220)
curve([p, p + vec(sway, fall / 2), p + vec(sway * 2.5, fall)], stroke: stripe[i], width: cell * 0.45)
}
}Compiles to JavaScript
function piece(t){let{floor:e,imul:l,max:r,min:o,round:n,sqrt:s}=Math,i,f=(e,r=+("0x"+t.substr(e,8)))=>()=>(i=l((r=r+1831565813|0)^r>>>15,1|r),i^=i+l(i^i>>>7,61|i),((i^i>>>14)>>>0)/2**32),h=f(2),p=f(10),u=f(18),a='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19200 24000">',g=t=>n(10*t),w=t=>t.map(t=>g(t[0])+","+g(t[1])).join(" "),d=t=>"#"+(1<<24|t[0]<<16|t[1]<<8|t[2]).toString(16).slice(1),k=t=>"M"+w(t.slice(0,1))+"C"+w(t.slice(1)),c=(t,e=0,l=1)=>t<e?e:t>l?l:t,m=(t,e,l,r=.5-t*t-e*e,o)=>r<=0?0:(o=r*r,o*o*(F[l]*t+B[l]*e)),y=(t,l,r,o=(l+r)*z,n=e(l+o),s=e(r+o),i=(n+s)*E,f=l-n+i,h=r-s+i,p=f>h?1:0,u=1-p,a=n&255,g=s&255,w=m(f,h,t[a+t[g]]&7),d=m(f-p+E,h-u+E,t[a+p+t[g+u]]&7),k=m(f-1+2*E,h-1+2*E,t[a+1+t[g+1]]&7))=>c(70*(w+d+k),-1,1),x=(t,e,l,r,o)=>{let n=0,s=1,i=0,f=e,h=l;for(let e=0;e<r;e++)n+=s*y(t,f,h),i+=s,s*=o,f=2*f+17.3,h=2*h-9.1;return n/i},v=(t,e,l,r=c(l))=>[n(t[0]+(e[0]-t[0])*r),n(t[1]+(e[1]-t[1])*r),n(t[2]+(e[2]-t[2])*r)],b=(t,e)=>v(t,[0,0,0],e),M=(t,e,l)=>{let n,s=t.length,i=[];if(s===0)return i;if(i.push(t[0]),s<2)return i;let f=e/6,h=l?s:s-1;for(let e=0;e<h;e++){let h=(n=t[l?(e+s-1)%s:r(e-1,0)])[0],p=n[1],u=(n=t[e])[0],a=n[1],g=(n=t[l?(e+1)%s:e+1])[0],w=n[1],d=(n=t[l?(e+2)%s:o(e+2,s-1)])[0],k=n[1];i.push([u+(g-h)*f,a+(w-p)*f]),i.push([g-(d-u)*f,w-(k-a)*f]),i.push([g,w])}return i},j=(t,e)=>x(T,.002*t,5e-4*e,2,.5),q=(t,e)=>[t+50*j(t,e),e],S=t=>[q(0,(t+.5)*R),q(N*R,(t+.5)*R)],C,F=[1,-1,1,-1,1,-1,0,0],B=[1,1,-1,-1,0,0,1,-1],z=(s(3)-1)/2,E=(3-s(3))/6,T=(t=>{let l=[];for(let t=0;t<256;t++)l.push(t);for(let r=0;r<255;r++){let o=255-r,n=e((o+1)*t()),s=l[o];l[o]=l[n],l[n]=s}for(let t=0;t<256;t++)l.push(l[t]);return l})(h),N=40+32*h()|0,R=(1150+300*h())/N,I=e(1640/R),O=2+3*h()|0,A=1+e((O-1)*h()),L=.5>h()?1:O-1,D=(6*p()|0)*4,U=[],H=4*p()|0;for(let t=0;t<r(N,I);t++).12>p()&&(H=4*p()|0),U.push([[29,43,58],[140,59,36],[217,164,65],[236,230,216],[34,54,43],[176,48,42],[27,30,43],[232,217,168],[58,42,77],[181,72,47],[226,207,154],[243,237,226],[36,56,46],[107,63,110],[201,143,174],[240,232,220],[42,42,42],[138,143,150],[200,195,184],[242,239,232],[18,48,77],[44,111,143],[227,178,60],[241,236,224]][D+H]);a+='<rect width="19200" height="24000" fill="#e9e4da"/>',a+='<g transform="translate('+n((960-N*R/2)*10)+" "+n((1100-I*R/2)*10)+") rotate("+(t=>+t.toFixed(3))(360*(-.006+.012*h()))+')">',a+='<g transform="translate(180 260)" opacity=".2">';for(let t=0;t<I;t++)a+='<polyline points="'+w(S(t))+'" fill="none" stroke="#2b2420" stroke-width="'+n(10*R)+'"/>';a+="</g>";for(let t=0;t<I;t++){a+='<polyline points="'+w(S(t))+'" fill="none" stroke="'+d(b(U[t],.75))+'" stroke-width="'+n(10*R)+'"/>';for(let e=0;e<N;e++){let l=(e+.5)*R,r=(t+.5)*R,o=(e+t*L)%O<A,s=8*(j(l-6,r)-j(l+6,r))+(-.06+.12*u())-(o?0:.1),i=v(o?U[e]:U[t],s>0?[255,246,232]:[0,0,0],.6*Math.abs(s)),f=(o?0:.53)*R,h=(o?.53:0)*R,p=(C=q(l-f,r-h))[0],g=C[1],k=(C=q(l+f,r+h))[0],c=C[1];a+='<polyline points="'+w([[p,g],[k,c]])+'" fill="none" stroke="'+d(i)+'" stroke-width="'+n(.88*R*10)+'"/>',a+='<polyline points="'+w([[p+(k-p)*.2,g+(c-g)*.2],[p+(k-p)*.8,g+(c-g)*.8]])+'" fill="none" stroke="'+d(v(i,[255,255,255],.12))+'" stroke-width="'+n(.3*R*10)+'"/>'}}for(let t=0;t<N;t++){let e=(C=q((t+.5)*R,I*R))[0],l=C[1],r=-14+28*u(),o=120+100*u();a+='<path d="'+k(M([[e,l],[e+r,l+o/2],[e+2.5*r,l+o]],1,!1))+'" fill="none" stroke="'+d(U[t])+'" stroke-width="'+n(.45*R*10)+'"/>'}return a+="</g>",a+"</svg>"}