Public
Edited
Oct 31, 2022
3 stars
Insert cell
Insert cell
Plot.plot({
marks: [
sillyLink([{ x1: 0, y1: 0, x2: 1, y2: 1 }], {
x1: "x1",
x2: "x2",
y1: "y1",
y2: "y2",
style: "squiggle"
}),
sillyLink([{ x1: 0.2, y1: 2, x2: 1, y2: -1 }], {
x1: "x1",
x2: "x2",
y1: "y1",
y2: "y2",
style: "dots"
})
]
})
Insert cell
Insert cell
mutable debug = null
Insert cell
squiggle = (x1, y1, x2, y2, lineWidth, value, ctx /* added by Fil */) => {
const theta = angleOfInclination(x1, y1, x2, y2);

// Rotate the line parallel to x-axis
// The resultant line is (x1, y1), (x2a, y2a)
const [x2a, y2a] = rotatePoint(x2, y2, -theta, x1, y1);

// Do the point transformations
const parts = math.lerp(9, 90, value);
const shift = lineWidth;
const lines = partitionLine(x1, y1, x2a, y2a, parts);

let pts = [[x1, y1], ...lines.map((p) => p[1])];
pts = pts.map(([x, y], i) => [
x + (random.value() * 2 - 1) * lineWidth * math.lerp(0.35, 0.15, value),
y +
(i % 2 === 0 ? -1 : 1) * lineWidth * 0.5 +
(random.value() * 2 - 1) * lineWidth * 0.2
]);

// reflection of y-axis
if (random.boolean()) {
pts = pts.map(([x, y]) => {
const yDiff = y - y1;
return [x, y1 + -1 * yDiff];
});
}

// Rotate the new line back to original angle
pts = pts.map((p) => rotatePoint(p[0], p[1], theta, x1, y1));

const curve = d3
.line()
.curve(d3.curveNatural)
.x((d) => d[0])
.y((d) => d[1]);

ctx.beginPath();
ctx.lineCap = "round";
ctx.lineJoin = "round";
ctx.strokeStyle = palette.fg(math.lerp(0.4, 1, value));
ctx.lineWidth = 1 + math.lerp(0, 2, value);
curve.context(ctx);
curve(pts);
ctx.stroke();
}
Insert cell
dots = (x1, y1, x2, y2, lineWidth, value, ctx /* added by Fil */) => {
const theta = angleOfInclination(x1, y1, x2, y2);
const rectWidth = distance(x1, y1, x2, y2);
const rectHeight = lineWidth;
const dotRadius = 0.5 + math.lerp(0, 0.6, value);
const minDistance = math.lerp(
dotRadius * 5,
dotRadius * 1.3,
d3.easeCircleOut(value)
);
// Do the point transformations
let pts = poissonSampling(rectWidth, rectHeight, {
minDistance,
tries: 60
});

pts = pts.map(([x, y]) => [x + x1, y + y1 - lineWidth / 2]);

// Rotate the new line back to original angle
pts = pts.map((p) => rotatePoint(p[0], p[1], theta, x1, y1));

ctx.beginPath();
ctx.fillStyle = palette.fg();
pts.forEach(([cx, cy], i) => {
ctx.moveTo(cx, cy);
ctx.arc(cx, cy, dotRadius, 0, Math.PI * 2);
ctx.closePath();
});
ctx.fill();
}
Insert cell
import {
angleOfInclination,
distance,
math,
palette,
partitionLine,
poissonSampling,
random,
rotatePoint
} from "@saneef/15-silly-ways-to-draw-a-line"
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