Skip to content

Instantly share code, notes, and snippets.

@ellcom
Last active March 24, 2020 20:14
Show Gist options
  • Save ellcom/c32de822d4a639986d335daca975f91b to your computer and use it in GitHub Desktop.
Save ellcom/c32de822d4a639986d335daca975f91b to your computer and use it in GitHub Desktop.
Retina Canvas Invisible Upscaling
(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)
@jaydenrw222
Copy link

gsgajahhshajs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment