Published
Edited
Dec 6, 2020
1 fork
Importers
16 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md`<hr>
## Utilities and libraries`
Insert cell
fonts = html`<link
href="https://fonts.googleapis.com/css2?family=Aldrich&family=Bungee&family=Concert+One" rel="stylesheet">`
Insert cell
font = {
let f = `${fontOptions.size}px '${fontOptions.family}'`;
await document.fonts.load(f);
return f
}
Insert cell
Insert cell
//
// returns imageData for our base picture
//
baseImage = function (dx=0,dy=0) {
let [w,h] = [800,600]
let ctx = DOM.context2d(w,h,1)
let margin = 20;
ctx.font = font
let m = ctx.measureText(fontOptions.inputText)
let {actualBoundingBoxLeft:l,actualBoundingBoxRight:r,
actualBoundingBoxAscent:a,actualBoundingBoxDescent:d} = m
ctx.fillText(fontOptions.inputText, margin-l+dx,a+margin+dy)
let imgData = ctx.getImageData(0,0,margin*2+r-l,margin*2+a+d)
imgData.canvas = ctx.canvas
return imgData
}
Insert cell
{
let {w,h,positive,negative} = baseCurves()
let ctx = DOM.context2d(w,h,1)
for (let curve of positive.concat(negative)) {
for (let {x,y} of curve) {
ctx.fillRect(x,y,1,1)
}
}
return ctx.canvas
}
Insert cell
//
// Generates a canvas and imageData for our base picture
//

baseCurves = function () {
let imgdata = baseImage();
let img = imgdata.canvas;
let [w,h] = [imgdata.width,imgdata.height]
let ctx = DOM.context2d(w,h,1)
let circ = getCirculations (imgdata)
let comps = circ.closed.slice(0);
comps.reverse();
function toCurve (array) {
let c = new Curve(...array);
let n = ~~c.perimeter()
return c.resample(n,true)
}
return {
w,h,
positive :comps.filter(comp => comp.inside.length%2 == 0).map(comp => toCurve(comp.points)),
negative :comps.filter(comp => comp.inside.length%2 != 0).map(comp => toCurve(comp.points))
}
}
Insert cell
baseCurves()
Insert cell
d3 = require("d3@6")
Insert cell
import {octave,perlin2,perlin3} from "@mbostock/perlin-noise"
Insert cell
import {combo} from "@esperanc/aggregated-inputs"
Insert cell
import {select,number,radio,text,checkbox} from "@jashkenas/inputs"
Insert cell
import {getCirculations, drawCirculations} from "@esperanc/bitmap-tracing"
Insert cell
import { Vec, Curve } from "@esperanc/2d-geometry-utils"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more