Public
Edited
Oct 26, 2022
1 fork
7 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
maxLineWidth = width / numOfLines
Insert cell
padding = (paddingRatio * maxLineWidth) / 2
Insert cell
breakSize = {
const max = height / 2;
const min = height / 16;
return CSMath.lerp(min, max, breakSizeRatio);
}
Insert cell
palette = {
shuffle;
if (colorScheme === "monochrome") {
return {
bg: "#e9e5da",
fg: "#101419"
};
}


const fg = getRandomColor();
const bg = getRandomContrastColor(fg);
return {
bg,
fg
};
}
Insert cell
seedData = CSMath.linspace(numOfLines, true).map((u) => ({
p: [u, 0],
// w: 0,
// m: 0
}))
Insert cell
mutable t = 0
Insert cell
data = {
while (animate) {
mutable t = t + 1;
yield updateState(seedData, t);
}

yield updateState(seedData, t);
}
Insert cell
updateState = (data, t) =>
chunk(data, chunkSize)
.map((c, i) => {
const m = 0.5 * CSRandom.noise2D(i / 2, t / 200) + 0.5;
return c.map((d) => {
const [u] = d.p;
return {
...d,
// w: 0.5 * CSRandom.noise1D(u + t / 2000, 4) + 0.5,
w: 0.5 * CSRandom.noise2D(u * 4, t / 500) + 0.5,
m
};
});
})
.flat()
Insert cell
ctx = DOM.context2d(width, height)
Insert cell
drawBg = () => {
ctx.fillStyle = palette.bg;
ctx.beginPath();
ctx.fillRect(0, 0, width, height);
}
Insert cell
drawLines = () => {
const maxWidth = maxLineWidth - padding;

ctx.fillStyle = palette.fg;
ctx.beginPath();
data.forEach((l) => {
const {
p: [u1, v1],
w,
m
} = l;
const x = CSMath.lerp(maxWidth / 2, width - maxWidth / 2, u1);
const y = CSMath.lerp(0, height, v1);
const breakY = CSMath.lerp(breakSize / 2, height - breakSize / 2, m);
const lineWidth = CSMath.lerp(maxWidth / 10, maxWidth, w);
const breakLineWidth = CSMath.lerp(maxWidth, maxWidth / 8, w);

// First segment
ctx.rect(x - lineWidth / 2, y, lineWidth, breakY - breakSize / 2);

// Break segment
ctx.rect(
x - breakLineWidth / 2,
breakY - breakSize / 2,
breakLineWidth,
breakSize
);

// Second segment
ctx.rect(
x - lineWidth / 2,
breakY + breakSize / 2,
lineWidth,
height - (breakY + breakSize / 2)
);
});
ctx.fill();
}
Insert cell
Insert cell
width
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more