Public
Edited
Jan 8, 2023
Fork of Cheery Logo
4 forks
Importers
24 stars
Insert cell
Insert cell
canvas = {
const context = DOM.context2d(width, height);
context.canvas.style = "width: 100%; height: auto;";
return Object.assign(context.canvas, {
update(mouse) {
let {left: cx, top: cy} = context.canvas.getBoundingClientRect();
let {x: mx, y: my} = mouse;
mx -= cx;
my -= cy;
context.clearRect(0, 0, width, height);
for (let i = 0, n = circles.length; i < n; ++i) {
const {x, y, r} = circles[i];
context.save();
context.translate(x, y);
context.beginPath();
context.arc(0, 0, r, 0, 2 * Math.PI);
context.fillStyle = d3.schemePastel2[i % 8];
context.fill();
const dx = mx - x, dy = my - y, l = Math.sqrt(dx ** 2 + dy ** 2);
context.beginPath();
if (l < r * 2 / 3) context.arc(dx, dy, r / 3, 0, 2 * Math.PI);
else context.arc(dx / l * r * 2 / 3, dy / l * r * 2 / 3, r / 3, 0, 2 * Math.PI);
context.fillStyle = "#333";
context.fill();
context.restore();
}
}
});
}
Insert cell
update = canvas.update(mouse)
Insert cell
mouse = Generators.observe(notify => {
const moved = (event) => notify({ x: event.clientX, y: event.clientY });
addEventListener("mousemove", moved);
notify({x: innerWidth / 2, y: innerHeight / 2});
return () => removeEventListener("mousemove", moved);
})
Insert cell
height = 600
Insert cell
padding = 3
Insert cell
random = d3.randomUniform(padding + 5, padding + 100)
Insert cell
circles = {
const radii = d3.range(100).map(random);
const circles = d3.packSiblings(radii.map(r => ({r})));
for (const circle of circles) {
circle.x += width / 2;
circle.y += height / 2;
circle.r -= padding;
}
return circles;
}
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