Skip to content

Instantly share code, notes, and snippets.

@jrc03c
Last active March 7, 2021 23:51
Show Gist options
  • Save jrc03c/9de51deacd79adb428e0263843a5f3f6 to your computer and use it in GitHub Desktop.
Save jrc03c/9de51deacd79adb428e0263843a5f3f6 to your computer and use it in GitHub Desktop.
Create a high-DPI-compatible canvas element
function createHighDPICanvas(width, height){
let dpi = window.devicePixelRatio || 1
let canvas = document.createElement("canvas")
canvas.width = width * dpi
canvas.height = height * dpi
canvas.style.width = width + "px"
canvas.style.height = height + "px"
canvas.getContext("2d").scale(dpi, dpi)
return canvas
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment