Skip to content

Instantly share code, notes, and snippets.

@YingshanDeng
Created February 2, 2018 02:32
Show Gist options
  • Save YingshanDeng/5adb117a98d04a9d86ad7da26cb1542c to your computer and use it in GitHub Desktop.
Save YingshanDeng/5adb117a98d04a9d86ad7da26cb1542c to your computer and use it in GitHub Desktop.
hidp-canvas-polyfill
function hdpiCanvasPolyfill (canvas) {
var dpr = Math.max(1, window.devicePixelRatio || 1)
var logicalWidth = canvas.width
var logicalHeight = canvas.height
canvas.width = dpr * logicalWidth
canvas.height = dpr * logicalHeight
canvas.style.width = `${logicalWidth}px`
canvas.style.height = `${logicalHeight}px`
var ctx = canvas.getContext('2d');
ctx.scale(dpr, dpr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment