{"id":94,"date":"2013-04-08T11:19:39","date_gmt":"2013-04-08T11:19:39","guid":{"rendered":"http:\/\/www.w3.org\/community\/games\/?p=94"},"modified":"2013-04-17T17:28:03","modified_gmt":"2013-04-17T17:28:03","slug":"devicenormalpixelratio-proposal-for-zoom-independent-devicepixelratio-for-hd-retina-games","status":"publish","type":"post","link":"https:\/\/www.w3.org\/community\/games\/2013\/04\/08\/devicenormalpixelratio-proposal-for-zoom-independent-devicepixelratio-for-hd-retina-games\/","title":{"rendered":"deviceNormalPixelRatio: proposal for zoom-independent devicePixelRatio for HD Retina games"},"content":{"rendered":"<p>Reposted from <a href=\"\/community\/respimg\/2013\/04\/06\/devicenormalpixelratio-proposal-for-zoom-independent-devicepixelratio-for-hd-retina-games\">\/community\/respimg<\/a> per Marcos Caceres&#8217; request.<\/p>\n<p>This is a proposal for addition of a zoom-independent version of <code>window.devicePixelRatio<\/code> to HTML5.<\/p>\n<h1>The issue<\/h1>\n<p>Before Firefox 18 and Chrome 25, to make a 3D (WebGL) game drawn in HD, we could set the scale in <code>&lt;meta name=\"viewport\"&gt;<\/code> to 1 and multiply the size of 2D things (HUD elements, menus) by <code>window.devicePixelRatio<\/code>.<\/p>\n<p>However, since these versions of the browsers, <code>devicePixelRatio<\/code> started to take browser zoom level into account.<\/p>\n<p>While the change works perfectly for loading of high-resolution versions of <code>&lt;img&gt;<\/code> images and <code>background-image<\/code>s, it has been impossible to draw HUD elements in HD 3D games with correct sizes since that.<\/p>\n<h1>Glossary of this proposal<\/h1>\n<p>DPR &#8211; zoom-dependent <code>window.devicePixelRatio<\/code>.<br \/>\nOld DPR &#8211; <code>devicePixelRatio<\/code> behavior before Firefox 18 and Chrome 25.<br \/>\nNew DPR &#8211; <code>devicePixelRatio<\/code> behavior since Firefox 18 and Chrome 25.<\/p>\n<h1>Issue details<\/h1>\n<p>With the old DPR, when <code>&lt;meta name=\"viewport\"&gt;<\/code> scale is 1, we could simply multiply the size of HUD elements by DPR to get resolution-independent size of the element (so that it doesn&#8217;t look too small too dense displays):<\/p>\n<p><code>\/\/drawing a 48dip cross at (16dip;16dip)<br \/>\nctx.fillRect(32 * devicePixelRatio, 16 * devicePixelRatio, 16 * devicePixelRatio, 48 * devicePixelRatio);<br \/>\nctx.fillRect(16 * devicePixelRatio, 32 * devicePixelRatio, 48 * devicePixelRatio, 16 * devicePixelRatio);<\/code><\/p>\n<p>On screen, the cross would have the following size with the old behavior:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<th>Display DPI<\/th>\n<th>Zoom level<\/th>\n<th><code>devicePixelRatio<\/code><\/th>\n<th>Output size on screen<\/th>\n<\/tr>\n<tr>\n<td>96 (1x)<\/td>\n<td>100%<\/td>\n<td>1<\/td>\n<td>48&#215;48<\/td>\n<\/tr>\n<tr>\n<td>96 (1x)<\/td>\n<td>200%<\/td>\n<td>1<\/td>\n<td>96&#215;96<\/td>\n<\/tr>\n<tr>\n<td>96 (1x)<\/td>\n<td>400%<\/td>\n<td>1<\/td>\n<td>192&#215;192<\/td>\n<\/tr>\n<tr>\n<td>192 (2x)<\/td>\n<td>100%<\/td>\n<td>2<\/td>\n<td>96&#215;96<\/td>\n<\/tr>\n<tr>\n<td>192 (2x)<\/td>\n<td>200%<\/td>\n<td>2<\/td>\n<td>192&#215;192<\/td>\n<\/tr>\n<tr>\n<td>192 (2x)<\/td>\n<td>400%<\/td>\n<td>2<\/td>\n<td>384&#215;384<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>This is fine, on high-DPI displays 384&#215;384 looks like 192&#215;192 on low-DPI displays, so it&#8217;s correctly zoomed by 400% (48 * 4 = 192).<\/p>\n<p>However, with the new DPI, DPR is multiplied by the zoom level too, so if we multiply the size by DPR, we get the element scaled <b>twice<\/b> (in the script by DPR and in the browser by zoom):<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<th>Display DPI<\/th>\n<th>Zoom level<\/th>\n<th><code>devicePixelRatio<\/code><\/th>\n<th>Output size on screen<\/th>\n<\/tr>\n<tr>\n<td>96 (1x)<\/td>\n<td>100%<\/td>\n<td>1<\/td>\n<td>48&#215;48<\/td>\n<\/tr>\n<tr>\n<td>96 (1x)<\/td>\n<td>200%<\/td>\n<td>2<\/td>\n<td>192&#215;192<\/td>\n<\/tr>\n<tr>\n<td>96 (1x)<\/td>\n<td>400%<\/td>\n<td>4<\/td>\n<td><b>768&#215;768<\/b><\/td>\n<\/tr>\n<tr>\n<td>192 (2x)<\/td>\n<td>100%<\/td>\n<td>2<\/td>\n<td>96&#215;96<\/td>\n<\/tr>\n<tr>\n<td>192 (2x)<\/td>\n<td>200%<\/td>\n<td>4<\/td>\n<td>384&#215;384<\/td>\n<\/tr>\n<tr>\n<td>192 (2x)<\/td>\n<td>400%<\/td>\n<td>8<\/td>\n<td><b>1536&#215;1536<\/b><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>On high-DPI displays, 1536&#215;1536 looks like 768&#215;768 on low-DPI displays. 768&#215;768 is <b>1600%<\/b> larger than 48&#215;48, while we only wanted to zoom by <b>400%<\/b>.<\/p>\n<p>So, as I said before, we get the element zoomed by zoomLevel^2 instead of zoomLevel.et the element zoomed by zoomLevel^2 instead of zoomLevel.<\/p>\n<h1>Use case example<\/h1>\n<p>If you open <a href=\"http:\/\/webquake.quaddicted.com\" target=\"_blank\" rel=\"nofollow\">WebQuake<\/a> on a high-DPI display, you can see that the game is rendered in HD, but the HUD and the menus appear too small.<\/p>\n<p>This is because I set &lt;meta viewport&gt; scale to 1. If I didn&#8217;t do this, the HUD size would be correct, but the game would be blurry.<\/p>\n<p>What exactly I want is to render the game in HD while having the HUD drawn at correct size.<\/p>\n<h1>The solution<\/h1>\n<p>The solution is very simple.<\/p>\n<p>A constant value which behaves the same way as the old DPR &#8211; doesn&#8217;t take zoom level into account.<\/p>\n<p>Let it be named <code>window.deviceNormalPixelRatio<\/code> or <code>navigator.deviceNormalPixelRatio<\/code>.<\/p>\n<p>Another important thing is to make it viewport scale-independent. It would have no point then, since it is to be used when viewport scale is forced to 1 (for manual scaling of selected elements).<\/p>\n<p>It must be accessible from CSS queries too so we won&#8217;t have to use JavaScript to resize every element.<\/p>\n<p>For HD image loading, the old <code>devicePixelRatio<\/code> would be used, and for manual <code>&lt;meta name=\"viewport\"&gt;<\/code> correction, <code>deviceNormalPixelRatio<\/code> would be.<\/p>\n<h1>Another possible use<\/h1>\n<p>Along with DPI scaling, <code>deviceNormalPixelRatio<\/code> can be used to retrieve zoom level in a cross-browser way (there&#8217;s no way to retrieve zoom level yet!):<\/p>\n<p><code>var zoom = devicePixelRatio \/ deviceNormalPixelRatio;<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reposted from \/community\/respimg per Marcos Caceres&#8217; request. This is a proposal for addition of a zoom-independent version of window.devicePixelRatio to HTML5. The issue Before Firefox 18 and Chrome 25, to make a 3D (WebGL) game drawn in HD, we could &hellip; <a href=\"https:\/\/www.w3.org\/community\/games\/2013\/04\/08\/devicenormalpixelratio-proposal-for-zoom-independent-devicepixelratio-for-hd-retina-games\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3967,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"yes","footnotes":""},"categories":[1],"tags":[],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.w3.org\/community\/games\/wp-json\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3.org\/community\/games\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3.org\/community\/games\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3.org\/community\/games\/wp-json\/wp\/v2\/users\/3967"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3.org\/community\/games\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":4,"href":"https:\/\/www.w3.org\/community\/games\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":98,"href":"https:\/\/www.w3.org\/community\/games\/wp-json\/wp\/v2\/posts\/94\/revisions\/98"}],"wp:attachment":[{"href":"https:\/\/www.w3.org\/community\/games\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3.org\/community\/games\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3.org\/community\/games\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}