Skip to content

Instantly share code, notes, and snippets.

@nornagon
Created November 30, 2022 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nornagon/7ec7070a9c70e7bb87e6ee91d9f4144a to your computer and use it in GitHub Desktop.
Save nornagon/7ec7070a9c70e7bb87e6ee91d9f4144a to your computer and use it in GitHub Desktop.
canvas boilerplate
document.head.appendChild(document.createElement('style')).textContent = `
body { margin: 0; overflow: hidden; }
canvas { width: 100%; height: 100%; }
`;
const canvas = document.body.appendChild(document.createElement('canvas'));
(window.onresize = () => {
const {width, height} = canvas.getBoundingClientRect()
canvas.width = width * devicePixelRatio
canvas.height = height * devicePixelRatio
frame()
})();
function frame() {
const ctx = canvas.getContext('2d')
ctx.scale(devicePixelRatio, devicePixelRatio)
render(ctx)
}
function render(ctx) {
ctx.moveTo(10, 10)
ctx.lineTo(100, 100)
ctx.stroke()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment