UNPKG

duckengine

Version:
43 lines (36 loc) 1.33 kB
// https://gist.github.com/callumlocke/cc258a193839691f60dd export default function dprScale( canvas: HTMLCanvasElement, context: CanvasRenderingContext2D | any, width: number, height: number ) { // assume the device pixel ratio is 1 if the browser doesn't specify it const devicePixelRatio = window.devicePixelRatio || 1; // determine the 'backing store ratio' of the canvas context const backingStoreRatio = context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 1; // determine the actual ratio we want to draw at const ratio = devicePixelRatio / backingStoreRatio; if (devicePixelRatio !== backingStoreRatio) { // set the 'real' canvas size to the higher width/height canvas.width = width * ratio; canvas.height = height * ratio; // ...then scale it back down with CSS canvas.style.width = width + 'px'; canvas.style.height = height + 'px'; } else { // this is a normal 1:1 device; just scale it simply canvas.width = width; canvas.height = height; canvas.style.width = ''; canvas.style.height = ''; } // scale the drawing context so everything will work at the higher ratio context.scale(ratio, ratio); }