Skip to content

Instantly share code, notes, and snippets.

@greggman
Last active November 23, 2025 05:04
Show Gist options
  • Select an option

  • Save greggman/3e076511f1c504a921201a99d5cf25ab to your computer and use it in GitHub Desktop.

Select an option

Save greggman/3e076511f1c504a921201a99d5cf25ab to your computer and use it in GitHub Desktop.
Pattern uses ramp 256
#pattern {
height: 100px;
}
#c {
display: block;
width: 100%;
height: 100px;
}
<div id="pattern"></div>
<canvas id="c"></canvas>
const range = (n, fn) => new Array(n).fill(0).map((_, i) => fn(i));
const elem = document.querySelector('#pattern');
const ctx = document.querySelector('#c').getContext('2d');
function render() {
const numStops = ctx.canvas.width;
elem.style.background = `linear-gradient(to right, ${
range(numStops, i => `${i % 2 ? 'red' : 'white'} ${i / numStops * 100}%`).join(',')
})`;
for (let x = 0; x < ctx.canvas.width; ++x) {
ctx.fillStyle = x % 2 ? 'red' : 'white';
ctx.fillRect(x, 0, 1, ctx.canvas.height);
}
}
const observer = new ResizeObserver(entries => {
const e = entries[0];
e.target.width = e.contentBoxSize[0].inlineSize;
e.target.height = e.contentBoxSize[0].blockSize;
render();
});
observer.observe(ctx.canvas);
{"name":"Pattern uses ramp 256","settings":{},"filenames":["index.html","index.css","index.js"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment