Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5 canvas game - how to add retina support

I'm creating an HTML5 canvas game for iPhone. I would like to support both retina and non-retina displays.

My question is, how do I support both retina and non-retina displays?

I.E., what is the general implementation for doing this? Do I write the game using the iPhone dimension and then add retina support? Or do I create the game retina size and add non-retina support? Is it best to have two images, one retina one non-retina? or just scale the retina image down? Do I have separate canvas sizes for retina and non-retina? Do I need to scale the mouse input?

Basically, I have no idea on the general idea/logic to implementing both.

Cheers, J

like image 527
Jarrod Avatar asked Sep 02 '12 02:09

Jarrod


2 Answers

You use devicePixelRatio to separate retina displays from normal displays

  • http://blog.iwalt.com/2010/08/generating-highresolution-graphics-with-html5s-canvas-element.html

Your game logic coordinates (sprite positions, etc.) must operate independently from the screen coordinates which will be always 2x multiplied on the retina display.

Your graphics assets must have two versions. High resolution version and 50% scaled down normal version. When you operate on retina display, you draw 2x size canvas, resized with CSS and on this canvas use high resolution assets.

like image 150
Mikko Ohtamaa Avatar answered Oct 18 '22 03:10

Mikko Ohtamaa


A new article has just been published on the topic over at html5rocks.com:

  • HIGH DPI CANVAS

upsize your canvas width and height by devicePixelRatio / webkitBackingStorePixelRatio and then use CSS to scale it back down to the logical pixel size you want. Taking our above case where Chrome reports a webkitBackingStorePixelRatio of 1 and a devicePixelRatio of 2 we would scale the dimensions of the canvas by 2 / 1, i.e. multiply them by 2, then we would use CSS to scale it back down.

like image 7
Jarrod Avatar answered Oct 18 '22 02:10

Jarrod