Skip to content

Instantly share code, notes, and snippets.

@greggman
Created April 19, 2025 06:44
Show Gist options
  • Save greggman/6e638063dc0c7f409714743a188a53b0 to your computer and use it in GitHub Desktop.
Save greggman/6e638063dc0c7f409714743a188a53b0 to your computer and use it in GitHub Desktop.
Canvas 2D Matrix Math
canvas {
background-color: #888;
}
<canvas></canvas>
const ctx = document.querySelector('canvas').getContext('2d');
// Canvas 2d starts with CSS pixel space so we don't need to transform to clip space
ctx.translate(25, 25);
ctx.rotate(45 * Math.PI / 180);
ctx.translate(-25, -25);
ctx.fillRect(0, 0, 50, 50);
{"name":"Canvas 2D Matrix Math","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