Skip to content

Instantly share code, notes, and snippets.

@greggman
Created November 12, 2024 20:22
Show Gist options
  • Save greggman/875a015fc080586923a33695bff0299f to your computer and use it in GitHub Desktop.
Save greggman/875a015fc080586923a33695bff0299f to your computer and use it in GitHub Desktop.
Canvas 2D: Test stroke with color vs stroke with pattern

Canvas 2D: Test stroke with color vs stroke with pattern

view on jsgist

/*bug-in-github-api-content-can-not-be-empty*/
<canvas width="64" height="64" style="
width: 512px;
height: 512px;
image-rendering: pixelated;
image-rendering: crisp-edges;
"></canvas>
const ctx = document.querySelector('canvas').getContext('2d');
const patternCanvas = document.createElement('canvas');
patternCanvas.width = 1;
patternCanvas.height = 1;
const pCtx = patternCanvas.getContext('2d');
pCtx.fillStyle = '#00F';
pCtx.fillRect(0, 0, 1, 1);
const pattern = ctx.createPattern(patternCanvas, 'repeat');
ctx.beginPath();
ctx.arc(16, 16, 12, 0, Math.PI * 2);
ctx.strokeStyle = '#F00';
ctx.stroke();
ctx.beginPath();
ctx.arc(48, 48, 12, 0, Math.PI * 2);
ctx.strokeStyle = pattern;
ctx.stroke();
{"name":"Canvas 2D: Test stroke with color vs stroke with pattern","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