Last active
March 11, 2017 12:50
-
-
Save sahithyen/43f373437b72aa77a48b0502d6f9806b to your computer and use it in GitHub Desktop.
This file contains 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
var canvas = document.querySelector('.myCanvas'); | |
var ctx = canvas.getContext('2d'); | |
var | |
canvasWidth = 400, | |
canvasHeight = 300; | |
// Calculate how much more pixels are needed | |
var devicePixelRatio = window.devicePixelRatio || 1; | |
var backingStoreRatio = ctx.backingStorePixelRatio || 1; | |
var ratio = devicePixelRatio / backingStoreRatio; | |
// Assign the number of needed pixels | |
canvas.width = canvasWidth * ratio; | |
canvas.height = canvasHeight * ratio; | |
// Assign the actual number of CSS pixels | |
canvas.style.width = elW + 'px'; | |
canvas.style.height = elH + 'px'; | |
// Scale everything that will be drawn later | |
ctx.scale(ratio, ratio); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment