Examples12
strata

A still plate. Beds of stone are laid from the foot up, each on the top of the one before, and how thick and how dark they are holds for a while, then changes. Above a height the token chooses, the beds are pale. Then the stone cracks from top to bottom. Most blocks slip up or down from where the last one went, and each turns a little, so the same beds, in the same order, stand at different heights from block to block.
| Source | Ships as | Gzipped | Target |
|---|---|---|---|
| 72 lines | 3,615 bytes | 1,742 bytes | svg |
Nilo source
// strata: beds of stone laid one on another, then cracked into blocks; most slip, and each turns a little
plate(1920, 2400)
use noise v2, colour v2
random layout, bed, slip
let field = noise.table(layout)
let ramps = [#2b211c, #6b4a33, #b98b5a, #ecdcbc, #1f2629, #4d5b5c, #9aa39a, #e4e2d6,
#2a1d24, #7a3e32, #c98a5b, #efdcc2, #23272e, #5b6470, #b7a98e, #efe6d2]
let pick = layout.int(0 ..< 4) * 4
let ramp = [ramps[pick], ramps[pick + 1], ramps[pick + 2], ramps[pick + 3]]
let sky = colour.lighten(ramp[3], amount: 0.3)
let n = 97, step = 2200 / (n - 1) // points across each bed, from just off one side of the plate to the other
let ceiling = layout.float(700 ..< 1300), dip = layout.float(-0.12 ..< 0.12)
// the beds, from the foot up, each laid on the top of the one before (ys: the base, then the top of each bed, n heights a row); how thick
// and how dark holds a while, then changes; over the height the token chooses, the beds are pale and swell less
let ys = num[], inks = color[]
for i in 0 ..< n { ys.add(2900) }
var top: num = 2900, thick: num = 30, tone: num = 0.3, k = 0, stone = 0
while top > -300 {
let low = top > ceiling
if low { stone = k + 1 }
if bed.chance(0.2) { thick = bed.float(3 ..< 40) }
if bed.chance(0.25) { tone = bed.unit() }
for i in 0 ..< n {
let swell = noise.octaves(field, point: vec(i * 0.05, k * 0.7), layers: 3) * (low ? 0.5 : 0.2)
ys.add(ys[k * n + i] - thick * (1 + swell))
}
let ink = colour.at(ramp, along: tone + bed.float(-0.08 ..< 0.08)), pale = bed.float(0 ..< 0.06)
inks.add(low ? ink : colour.mix(sky, toward: ink, amount: pale))
top -= thick
k += 1
}
// the foot of bed b (row b of ys; row 0 is the base), bent by one broad fold and tipped, and everything over it
fn face(b: int): vec[] {
let pts = [vec(2100, -500), vec(-100, -500)]
for i in 0 ..< n {
let x = i * step - 100
pts.add(vec(x, ys[b * n + i] + 150 * noise.at(field, point: vec(x * 0.0005, 9)) + dip * (x - 1000)))
}
return pts
}
// the cracks, left to right, each wandering down the plate in 12 steps and kept at least 60 to the right of the one
// before; the first and the last are off the plate
let count = slip.int(3 ..< 12), heave = slip.float(100 ..< 300)
let cracks = vec[]
for c in 0 ..= count {
var x = c == 0 ? -400 : (c == count ? 2320 : -60 + c * 2040 / count)
for j in 0 ..= 12 {
if c > 0 { x = max(x, cracks[c * 13 + j - 13].x + 60) }
cracks.add(vec(x, j * 220 - 120))
x += slip.float(-30 ..< 30)
}
}
// each block slips from where the last one went, turns a little, and has the beds laid in it again, in their order
var drop: num = 0
for c in 0 ..< count {
if slip.chance(0.75) { drop = clamp(drop + slip.float(-heave ..< heave), -350, 350) }
// the outline of the block: down its left crack and back up the next
let next = vec[], block = vec[]
for j in 0 ..= 12 { next.add(cracks[c * 13 + 13 + j]) }
for j in 0 ..= 25 { block.add(j < 13 ? cracks[c * 13 + j] : next[25 - j]) }
let mid = lerp(block[6], next[6], 0.5)
place(clip: block) {
place(mid + vec(0, drop), turn: slip.float(-0.004 ..< 0.004)) {
place(vec(0, 0) - mid) {
for b in 0 ..< inks.len {
polygon(face(b), fill: inks[b], stroke: colour.darken(inks[b], amount: b < stone ? 0.12 : 0.02), width: 2)
}
}
}
}
polyline(next, stroke: sky, width: 5)
}Compiles to JavaScript
function piece(t){let{floor:e,imul:l,round:r,sqrt:o}=Math,s,n=(e,r=+("0x"+t.substr(e,8)))=>()=>(s=l((r=r+1831565813|0)^r>>>15,1|r),s^=s+l(s^s>>>7,61|s),((s^s>>>14)>>>0)/2**32),h=n(2),i=n(10),p=n(18),f='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19200 24000">',u=t=>r(10*t),g=t=>+t.toFixed(3),a=t=>t.map(t=>u(t[0])+","+u(t[1])).join(" "),c=t=>"#"+(1<<24|t[0]<<16|t[1]<<8|t[2]).toString(16).slice(1),w=0,m=(t,e=0,l=1)=>t<e?e:t>l?l:t,d=(t,e,l,r=.5-t*t-e*e,o)=>r<=0?0:(o=r*r,o*o*(S[l]*t+F[l]*e)),x=(t,l,r,o=(l+r)*B,s=e(l+o),n=e(r+o),h=(s+n)*z,i=l-s+h,p=r-n+h,f=i>p?1:0,u=1-f,g=s&255,a=n&255,c=d(i,p,t[g+t[a]]&7),w=d(i-f+z,p-u+z,t[g+f+t[a+u]]&7),x=d(i-1+2*z,p-1+2*z,t[g+1+t[a+1]]&7))=>m(70*(c+w+x),-1,1),v=(t,e,l,r,o)=>{let s=0,n=1,h=0,i=e,p=l;for(let e=0;e<r;e++)s+=n*x(t,i,p),h+=n,n*=o,i=2*i+17.3,p=2*p-9.1;return s/h},k=(t,e,l,o=m(l))=>[r(t[0]+(e[0]-t[0])*o),r(t[1]+(e[1]-t[1])*o),r(t[2]+(e[2]-t[2])*o)],y=(t,e)=>k(t,[0,0,0],e),M=(t,l,r,o)=>t.length===1?t[0]:(r=m(l)*(t.length-1),o=Math.min(e(r),t.length-2),k(t[o],t[o+1],r-o)),P=t=>{let e=[[2100,-500],[-100,-500]];for(let l=0;l<97;l++){let r=l*I-100;e.push([r,L[97*t+l]+150*x(E,5e-4*r,9)+A*(r-1e3)])}return e},b,j,q,S=[1,-1,1,-1,1,-1,0,0],F=[1,1,-1,-1,0,0,1,-1],B=(o(3)-1)/2,z=(3-o(3))/6,E=(t=>{let l=[];for(let t=0;t<256;t++)l.push(t);for(let r=0;r<255;r++){let o=255-r,s=e((o+1)*t()),n=l[o];l[o]=l[s],l[s]=n}for(let t=0;t<256;t++)l.push(l[t]);return l})(h),T=(4*h()|0)*4,N=[[[43,33,28],[107,74,51],[185,139,90],[236,220,188],[31,38,41],[77,91,92],[154,163,154],[228,226,214],[42,29,36],[122,62,50],[201,138,91],[239,220,194],[35,39,46],[91,100,112],[183,169,142],[239,230,210]][T],[[43,33,28],[107,74,51],[185,139,90],[236,220,188],[31,38,41],[77,91,92],[154,163,154],[228,226,214],[42,29,36],[122,62,50],[201,138,91],[239,220,194],[35,39,46],[91,100,112],[183,169,142],[239,230,210]][T+1],[[43,33,28],[107,74,51],[185,139,90],[236,220,188],[31,38,41],[77,91,92],[154,163,154],[228,226,214],[42,29,36],[122,62,50],[201,138,91],[239,220,194],[35,39,46],[91,100,112],[183,169,142],[239,230,210]][T+2],[[43,33,28],[107,74,51],[185,139,90],[236,220,188],[31,38,41],[77,91,92],[154,163,154],[228,226,214],[42,29,36],[122,62,50],[201,138,91],[239,220,194],[35,39,46],[91,100,112],[183,169,142],[239,230,210]][T+3]],R=k(N[3],[255,255,255],.3),I=2200/96,O=700+600*h(),A=-.12+.24*h(),L=[],C=[];for(let t=0;t<97;t++)L.push(2900);let D=2900,U=30,H=.3,G=0,Y=0;while(D>-300){let t=D>O;t&&(Y=G+1),.2>i()&&(U=3+37*i()),.25>i()&&(H=i());for(let e=0;e<97;e++){let l=v(E,.05*e,.7*G,3,.5)*(t?.5:.2);L.push(L[97*G+e]-U*(1+l))}let e=M(N,H+(-.08+.16*i())),l=.06*i();C.push(t?e:k(R,e,l)),D-=U,G+=1}let V=3+9*p()|0,W=100+200*p(),X=[];for(let t=0;t<=V;t++){let e=t===0?-400:t===V?2320:-60+2040*t/V;for(let l=0;l<=12;l++)t>0&&(e=Math.max(e,X[13*t+l-13][0]+60)),X.push([e,220*l-120]),e+=-30+60*p()}let K=0;for(let t=0;t<V;t++){.75>p()&&(K=m(K+((b=-W)+(W-b)*p()),-350,350));let e=[],l=[];for(let l=0;l<=12;l++)e.push(X[13*t+13+l]);for(let r=0;r<=25;r++)l.push(r<13?X[13*t+r]:e[25-r]);let o=(b=(j=l[6])[0])+((q=e[6])[0]-b)*.5,s=(b=j[1])+(q[1]-b)*.5,n=++w;f+='<clipPath id="c'+n+'"><polygon points="'+a(l)+'"/></clipPath><g clip-path="url(#c'+n+')">',f+='<g transform="translate('+r(10*o)+" "+r((s+K)*10)+") rotate("+g(360*(-.004+.008*p()))+')">',f+='<g transform="translate('+r((0-o)*10)+" "+r((0-s)*10)+')">';for(let t=0,e=C.length;t<e;t++)f+='<polygon points="'+a(P(t))+'" fill="'+c(C[t])+'" stroke="'+c(y(C[t],t<Y?.12:.02))+'" stroke-width="20"/>';f+="</g>",f+="</g>",f+="</g>",f+='<polyline points="'+a(e)+'" fill="none" stroke="'+c(R)+'" stroke-width="50"/>'}return f+"</svg>"}