Created
November 30, 2022 05:57
-
-
Save nornagon/7ec7070a9c70e7bb87e6ee91d9f4144a to your computer and use it in GitHub Desktop.
canvas boilerplate
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
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