Published
Edited
Aug 25, 2020
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
num = 5000
Insert cell
height = width
Insert cell
angles = d3.range(10)
Insert cell
viewof angle = Scrubber(angles, { delay: 150 })
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
numTriangles = 30
Insert cell
Insert cell
Insert cell
chart9 = {
let radians = Math.PI / 180;
let canvas = html`<canvas></canvas>`;
canvas.width = width;
canvas.height = height;

let ctx = canvas.getContext("2d");

// dots.forEach(d => {
// ctx.fillRect(d.x, d.y, 3, 3);
// });
triangles.forEach(t => {
ctx.fillStyle = t.fill;
// ctx.fillRect(t.x, t.y, t.s, t.s);
ctx.beginPath();

ctx.moveTo(t.x, t.y);
ctx.lineTo(t.x + t.s, t.y);
ctx.lineTo(t.x + t.s / 2, t.y + t.s);
ctx.lineTo(t.x, t.y);

ctx.fill();
});

//https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/rotate
ctx.translate(width / 2, height / 2);
ctx.rotate(angleTriAnim * radians);
ctx.translate(-width / 2, -height / 2);
// ctx.scale(1 / scale3, 1 / scale3);

triangles.forEach(t => {
ctx.fillStyle = t.fill;
// ctx.fillRect(t.x, t.y, t.s, t.s);
ctx.beginPath();

ctx.moveTo(t.x, t.y);
ctx.lineTo(t.x + t.s, t.y);
ctx.lineTo(t.x + t.s / 2, t.y + t.s);
ctx.lineTo(t.x, t.y);

ctx.fill();
});

return canvas;
}
Insert cell
Insert cell
chart10 = {
let radians = Math.PI / 180;
let canvas = html`<canvas></canvas>`;
canvas.width = width;
canvas.height = height;

let ctx = canvas.getContext("2d");

// dots.forEach(d => {
// ctx.fillRect(d.x, d.y, 3, 3);
// });
triangles.forEach(t => {
ctx.fillStyle = t.fill;
// ctx.fillRect(t.x, t.y, t.s, t.s);
ctx.beginPath();

ctx.moveTo(t.x, t.y);
ctx.lineTo(t.x + t.s, t.y);
ctx.lineTo(t.x + t.s / 2, t.y + t.s / 2);
ctx.lineTo(t.x, t.y);

ctx.fill();
});

//https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/rotate
ctx.translate(width / 2, height / 2);
ctx.rotate(angleTri * radians);
ctx.translate(-width / 2, -height / 2);
// ctx.scale(1 / scale3, 1 / scale3);

triangles.forEach(t => {
ctx.fillStyle = t.fill;
// ctx.fillRect(t.x, t.y, t.s, t.s);
ctx.beginPath();

ctx.moveTo(t.x, t.y);
ctx.lineTo(t.x + t.s, t.y);
ctx.lineTo(t.x + t.s / 2, t.y + t.s / 2);
ctx.lineTo(t.x, t.y);

ctx.fill();
});

return canvas;
}
Insert cell
triangles = {
let xspacing = width / numTriangles;
let yspacing = height / numTriangles;
return d3.range(numTriangles * numTriangles).map(i => {
let xi = i % numTriangles;
let yi = Math.floor(i / numTriangles);
let startColor = yi % 2 ? "seagreen" : "#111";
let endColor = yi % 2 ? "#111" : "seagreen";
return {
x: xi * xspacing,
y: yi * yspacing,
s: xspacing,
fill: "#111" //i % 2 ? startColor : endColor
};
});
}
Insert cell
Insert cell
trianglesGrid = {
let numTriangles = 10;
let lines = [];
// https://observablehq.com/@sinclarius/html5-canvas-triangle-grid
// https://github.com/bit101/grid/blob/master/grid.js
var max = Math.sqrt(width * width + height * height),
cos60 = Math.cos(Math.PI / 3),
sin60 = Math.sin(Math.PI / 3),
cos120 = Math.cos((Math.PI * 2) / 3),
sin120 = Math.sin((Math.PI * 2) / 3);
let y = 0;
let x = 0;
let i = 0;

for (i = x; i <= x + width; i += numTriangles) {
lines.push([{ x: i, y: y }, { x: i + cos60 * max, y: y + sin60 * max }]);
lines.push([{ x: i, y: y }, { x: i + cos120 * max, y: y + sin120 * max }]);
}
let dy = sin60 * numTriangles,
maxX = Math.ceil(width / numTriangles) * numTriangles + x;

for (i = y; i <= y + height; i += dy) {
lines.push([{ x: x, y: i }, { x: x + width, y: i }]);
}

for (i = y + dy * 2; i <= y + height; i += dy * 2) {
if (i > 0) {
lines.push([{ x: x, y: i }, { x: x + cos60 * max, y: i + sin60 * max }]);
}

lines.push([
{ x: maxX, y: i },
{ x: maxX + cos120 * max, y: i + sin120 * max }
]);
}
return lines;
}
Insert cell
scaler = d3
.scaleLinear()
.domain([0, angles.length])
.range([0.92, 0.97])
Insert cell
angles.length * dots.length
Insert cell
color(0)
Insert cell
color = d3.scaleSequential(rndColorScale).domain([0, angles.length])
Insert cell
dots = {
let chance = new Chance(seed);

return d3.range(num).map(i => {
return {
x: chance.floating({ min: 0, max: width }),
y: chance.floating({ min: 0, max: height })
};
});
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
rndColorScale = {
let chance = new Chance(seed);
let scales = ["interpolateBlues", "interpolateRdYlBu"];

let chosenScale = chance.pick(scales);
return d3[chosenScale];
}
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