DA.
DA.

Reputation: 40673

Canvas drawing and Retina display: doable?

Working with phoneGap implementing drawing with Canvas. The catch we've run into is that canvas expects specific pixel dimensions. This is fine except that the iPhone 4's Retina display, from a CSS/Webkit POV is still 320px wide, even though technically there are 640 actual screen pixels.

Is there anyway to accommodate the retina display using Canvas on Webkit while preserving compatibility with non-retina displays?

Upvotes: 24

Views: 22034

Answers (6)

Curtis
Curtis

Reputation: 2535

Another option is to remove this line from your HTML you will get the default width for viewing a webpage on your phone. Should be 980px on iPhones.

<meta name='viewport' content='width=device-width' />

Then you can just do:

canvas.width = innerWidth
canvas.height = innerHeight
canvas.style.width = innerWidth+'px'
canvas.style.height = innerHeight+'px'

The advantage is that you don't need to scale and it renders faster. But it probably won't match the amount of physical pixels that the device has. It works well in most cases though. Not all pixellated like 320px. But not as clear as retina. Keep in mind that the higher res you go the more laggy it will be.

Upvotes: 2

Joubert Nel
Joubert Nel

Reputation: 3214

I sat with the same problem last week and discovered how to solve it -

var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');

if (window.devicePixelRatio > 1) {
    var canvasWidth = canvas.width;
    var canvasHeight = canvas.height;

    canvas.width = canvasWidth * window.devicePixelRatio;
    canvas.height = canvasHeight * window.devicePixelRatio;
    canvas.style.width = canvasWidth + "px";
    canvas.style.height = canvasHeight + "px";

    ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
}

Full code on gist, demo on jsfiddle

Upvotes: 50

Spencer Evans
Spencer Evans

Reputation: 489

EDIT: Just noticed I posted the wrong link for the demo!

Retina (or other hdpi display) canvas resolution is definitely possible. There is a working example here:

http://spencer-evans.com/share/github/canvas-resizer/

I've also bumped into this a few times. The accepted answer code is essentially correct, but you could also use a library solution. I whipped one up to handle intelligent canvas re-sizing to make the element more responsive and higher resolution (as shown in the demo).

https://github.com/swevans/canvas-resizer

Upvotes: 0

guido
guido

Reputation: 1418

There is a very good polyfill by TJ Holowaychuk:

https://www.npmjs.com/package/autoscale-canvas

Upvotes: 0

jondavidjohn
jondavidjohn

Reputation: 62392

There is a drop-in polyfill that will take care of most basic drawing operations for you, and remove the ambiguity between browsers that handle this automatically for you (safari) and others that don't.

https://github.com/jondavidjohn/hidpi-canvas-polyfill

You simply include it before your drawing code and it should give you decent retina support automatically.

Upvotes: 3

atreidp
atreidp

Reputation: 1

WebCode (http://www.webcodeapp.com) is a vector drawing app that generates JavaScript HTML5 Canvas code for you. The code is Retina-compatible, you can check out how they do it.

Upvotes: 0

Related Questions