Last active
March 24, 2020 20:14
-
-
Save ellcom/c32de822d4a639986d335daca975f91b to your computer and use it in GitHub Desktop.
Retina Canvas Invisible Upscaling
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
(function(prototype) { | |
prototype.getContext = (function(_parent) { | |
return function(contextType, contextAttributes) { | |
var context = _parent.call(this, contextType, contextAttributes) | |
if (contextType != '2d') { | |
return context | |
} | |
if (this.hasAttribute('data-scaled')) { | |
return context | |
} | |
this.setAttribute('data-scaled', true) | |
const dpr = window.devicePixelRatio || 1 | |
const bsr = ( | |
context.webkitBackingStorePixelRatio || | |
context.mozBackingStorePixelRatio || | |
context.msBackingStorePixelRatio || | |
context.oBackingStorePixelRatio || | |
context.backingStorePixelRatio || 1 | |
) | |
const ratio = dpr / bsr | |
if (dpr == bsr) { | |
return context | |
} | |
this.style.width = this.width + 'px' | |
this.style.height = this.height + 'px' | |
this.width *= ratio | |
this.height *= ratio | |
context.scale(ratio, ratio) | |
return context | |
} | |
})(prototype.getContext) | |
})(HTMLCanvasElement.prototype) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gsgajahhshajs