Created
December 12, 2025 18:45
-
-
Save greggman/97eeedb4ebd191bcaf00bb4259995cb2 to your computer and use it in GitHub Desktop.
Canvas2D: Color disc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :root { | |
| color-scheme: light dark; | |
| } | |
| canvas { | |
| border: 1px solld black; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*bug-in-github-api-content-can-not-be-empty*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const size = 256; | |
| const canvas = document.createElement('canvas'); | |
| canvas.width = size; | |
| canvas.height = size; | |
| document.body.append(canvas); | |
| ctx = canvas.getContext('2d'); | |
| const hsl = (h, s, l) => `hsl(${h * 360 | 0}, ${s * 100}%, ${l * 100 | 0}%)`; | |
| const lerp = (a, b, l) => a + (b - a) * l; | |
| const half = size / 2; | |
| const quarter = half / 2; | |
| for (let y = 0; y < size; ++y) { | |
| for (let x = 0; x < size; ++x) { | |
| const dx = half - x; | |
| const dy = half - y; | |
| const a = Math.atan2(dy, dx) / Math.PI * 0.5 + 0.5; | |
| const r = Math.sqrt(dx * dx + dy * dy); | |
| if (r < half) { | |
| const [s, l] = r < quarter | |
| ? [1, r / quarter * 0.5] | |
| : [1, r / half] | |
| ctx.fillStyle = hsl(a, 1, lerp(0, 0.9, Math.pow(r / half, 0.75))); | |
| ctx.fillRect(x, y, 1, 1); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {"name":"Canvas2D: Color disc","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