Scaling HTML canvases for HiDPI screens

I’ve been mocking up some UI additions for Dasher 360, today. A big chunk of my time was spent working out how to make flyouts work for vertical toolbars (something I’ll address in a future post, if there’s interest), but I’ve just been fighting another problem that’s probably not really an issue for anyone using standard DPI (and non-“retina”) screens.

I’ve been drawing text to a standard HTML canvas and it’s been really ugly. After some minutes of searching the web, I found out it was probably due to my MacBook Pro’s retina display. Here’s the code I integrated (from this post):

public rescaleCanvas(canvas: any): void {

 

  // finally query the various pixel ratios

 

  let ctx = canvas.getContext(‘2d’);

 

  let devicePixelRatio = window.devicePixelRatio || 1;

  let backingStoreRatio = ctx.webkitBackingStorePixelRatio ||

                      ctx.mozBackingStorePixelRatio ||

                     …

Read more