Fabric.js: Support for High-DPI Displays

Created on 15 Nov 2012  Â·  11Comments  Â·  Source: fabricjs/fabric.js

Considering all these newfangled Apple products coming out with Retina displays, it would be desirable to use window.devicePixelRatio to improve canvas resolution on said displays, while also preserving object selection functionality, for all Fabric canvases added to the document. The obvious means, doubling the height and width of the canvas and styling it down to regular size... doesn't seem to work.

possible_feature

Most helpful comment

I've been using this in my code and it seems to work pretty well.

if( window.devicePixelRatio !== 1 ){

  var c = canvas.getElement(); // canvas = fabric.Canvas
  var w = c.width, h = c.height;

// Scale the canvas up by two for retina
// just like for an image
  c.setAttribute('width', w*window.devicePixelRatio);
  c.setAttribute('height', h*window.devicePixelRatio);

// then use css to bring it back to regular size
// or set it here
//    c.setAttribute('style', 'width="'+w+'"; height="'+h+'";')
// or jQuery  $(c).css('width', w);
//      $(c).css('width', w);
//      $(c).css('height', h);

// finally set the scale of the context
  c.getContext('2d').scale(window.devicePixelRatio, window.devicePixelRatio);

}

It's basically the same strategy as with images, make a 2X larger version and scale it down with CSS.

All 11 comments

Some sort of "pixel density" customizable attribute like that would also be useful in easily creating higher quality rasters (.pngs).

edit: You can already do this with toDataURLWithMultiplier: function (format, multiplier, quality), thanks kangax that is fantastic!

You can already do this with toDataUrlWithMultiplier

On Feb 14, 2013, at 11:49 AM, Magrippinho [email protected] wrote:

Some sort of "pixel density" customizable attribute like that would also be useful in easily creating higher quality rasters (.pngs).

—
Reply to this email directly or view it on GitHub.

I've been using this in my code and it seems to work pretty well.

if( window.devicePixelRatio !== 1 ){

  var c = canvas.getElement(); // canvas = fabric.Canvas
  var w = c.width, h = c.height;

// Scale the canvas up by two for retina
// just like for an image
  c.setAttribute('width', w*window.devicePixelRatio);
  c.setAttribute('height', h*window.devicePixelRatio);

// then use css to bring it back to regular size
// or set it here
//    c.setAttribute('style', 'width="'+w+'"; height="'+h+'";')
// or jQuery  $(c).css('width', w);
//      $(c).css('width', w);
//      $(c).css('height', h);

// finally set the scale of the context
  c.getContext('2d').scale(window.devicePixelRatio, window.devicePixelRatio);

}

It's basically the same strategy as with images, make a 2X larger version and scale it down with CSS.

@paulkaplan Interesting. I would be curious to see this in action, if you could create jsfiddle or something similar.

:thumbsup: @paulkaplan your scaling method works very well. thanks!

A fiddle with how to do this with Fabric would be awesome...

Wow @philwinkle that is really cool!

Will be great!
you also need to take in account webkitBackingStorePixelRatio and not just devicePixelRatio

Was this page helpful?
0 / 5 - 0 ratings