Public
Edited
Sep 19, 2023
1 star
Insert cell
Insert cell
Insert cell
Insert cell
{
const canvas = DOM.canvas(config.width, config.height);
canvas.width *= window.devicePixelRatio;
canvas.height *= window.devicePixelRatio;
canvas.style.width = `${config.width}px`;

drawUVGrid(canvas.getContext("2d"), {
nx: config.nx, ny: config.ny,
colors: { M: [199,35,199], R: [229,20,20], B: [20,20,229], G: [34,158,34] },
});
return canvas;
}
Insert cell
function drawUVGrid(ctx, config) {
drawCells(ctx, config);
drawCoords(ctx, config);
drawGrid(ctx, config);
}
Insert cell
function drawGrid(ctx, { nx, ny }) {
const H = ctx.canvas.height, W = ctx.canvas.width;
ctx.lineWidth = 1;
ctx.strokeStyle = 'white';

for (let i = 0; i <= ny; i++) {
const y = ctx.lineWidth * .5 + (H - ctx.lineWidth) / ny * i;
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(W, y);
ctx.stroke();
ctx.closePath();
}

for (let i = 0; i <= nx; i++) {
const x = ctx.lineWidth * .5 + (W - ctx.lineWidth) / nx * i;
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, H);
ctx.stroke();
ctx.closePath();
}
}
Insert cell
function drawCells(ctx, { colors: { R, G, B, M}, nx, ny }) {
const H = ctx.canvas.height, W = ctx.canvas.width;
const sx = W / nx, sy = H / ny;
const im = ctx.getImageData(0, 0, W, H);
const data = new Uint32Array(im.data.buffer);
for (let i = 0; i < H; i++) for (let j = 0; j < W; j++) {
const ty = i / (H - 1), tx = j / (W - 1);
const mr = Array.from({ length: 3 }, (_, i) => lerp(M[i],R[i],ty));
const bg = Array.from({ length: 3 }, (_, i) => lerp(B[i],G[i],ty));
let [r, g, b] = Array.from({ length: 3 }, (_, i) => lerp(mr[i],bg[i],tx));
if (((i / sy | 0) + (j / sx | 0)) % 2 == 0)
r *= 1.08, g *= 1.08, b *= 1.08;
data[i * W + j] = (0xff << 24) | (b << 16) | (g << 8) | r;
}
ctx.putImageData(im, 0, 0);

for (let i = 1; i < 10; i++) for (let j = 1; j < 10; j++) {
ctx.beginPath();
ctx.arc(i * sx, j * sy, 1.5 * window.devicePixelRatio, 0, 2 * Math.PI, false);
ctx.fillStyle = 'white';
ctx.fill();
ctx.closePath();
}
}
Insert cell
function drawCoords(ctx, { nx, ny }) {
const H = ctx.canvas.height, W = ctx.canvas.width;
const fontSize = Math.round(Math.min(W, H) / Math.max(ny, nx) * .16);
ctx.textRendering = "optimizeLegibility";
ctx.fillStyle = 'white';
ctx.letterSpacing = "-.2px";

const sx = W / nx, sy = H / ny;
ctx.font = `${fontSize * 2}px Arial,sans-serif`;
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
ctx.fillText("0,1", sx * .5, sy * .5);
ctx.fillText("1,1", W - sx * .5, sy * .5);
ctx.fillText("0,0", sx * .5, H - sy * .5);
ctx.fillText("1,0", W - sx * .5, H - sy * .5);

ctx.font = `${fontSize}px Arial,sans-serif`;
for (let i = 0; i < ny; i++) for (let j = 0; j < nx; j++) {
ctx.textBaseline = 'bottom';
ctx.textAlign = 'end';
ctx.fillText((1 - (i + 1) / ny).toFixed(1), (j + 1) * sx - 4, (i + 1) * sy);

ctx.textAlign = 'start';
ctx.textBaseline = 'top';
ctx.fillText((j / nx).toFixed(1), j * sx + 4, i * sy + 6);
}
}
Insert cell
lerp = (a, b, t) => a * (1 - t) + b * t
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