diff --git a/README.md b/README.md index f0bead9..e68a4a2 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Allows you to get the device pixel ratio using JavaScript. ## Size and delivery -Currently, `getDevicePixelRatio.js` compresses to around 318 bytes (~0.3 KB), after minify and gzip. To minify, you might try these online tools: [Microsoft Ajax Minifier]:(http://ajaxmin.codeplex.com/), [Uglify]:(http://marijnhaverbeke.nl/uglifyjs), [Yahoo Compressor]:(http://refresh-sf.com/yui/), or [Closure Compiler](http://closure-compiler.appspot.com/home). Serve with gzip compression. +Currently, `getDevicePixelRatio.js` compresses to around 221 bytes (~0.2 KB), after minify and gzip. To minify, you might try these online tools: [Microsoft Ajax Minifier](http://ajaxmin.codeplex.com/), [Uglify](http://marijnhaverbeke.nl/uglifyjs), [Yahoo Compressor](http://refresh-sf.com/yui/), or [Closure Compiler](http://closure-compiler.appspot.com/home). Serve with gzip compression. ## Sample diff --git a/getDevicePixelRatio-min.js b/getDevicePixelRatio-min.js index 3a6783d..11a466b 100644 --- a/getDevicePixelRatio-min.js +++ b/getDevicePixelRatio-min.js @@ -1,2 +1,2 @@ /*! GetDevicePixelRatio | Author: Tyson Matanich, 2012 | License: MIT */ -(function(n){n.getDevicePixelRatio=function(){var t=1;return n.screen.systemXDPI!==undefined&&n.screen.logicalXDPI!==undefined&&n.screen.systemXDPI>n.screen.logicalXDPI?t=n.screen.systemXDPI/n.screen.logicalXDPI:n.devicePixelRatio!==undefined&&(t=n.devicePixelRatio),t}})(this); \ No newline at end of file +(function(b){b.getDevicePixelRatio=function(){var a=b.screen||{},c=a.systemXDPI||1,a=a.logicalXDPI||1;return c>a?c/a:b.devicePixelRatio||1}})(this); \ No newline at end of file diff --git a/getDevicePixelRatio.js b/getDevicePixelRatio.js index 59358d8..c3d65e2 100644 --- a/getDevicePixelRatio.js +++ b/getDevicePixelRatio.js @@ -1,15 +1,14 @@ /*! GetDevicePixelRatio | Author: Tyson Matanich, 2012 | License: MIT */ (function (window) { + // device pixel ratio even before dom ready + // http://stackoverflow.com/a/29913175 window.getDevicePixelRatio = function () { - var ratio = 1; - // To account for zoom, change to use deviceXDPI instead of systemXDPI - if (window.screen.systemXDPI !== undefined && window.screen.logicalXDPI !== undefined && window.screen.systemXDPI > window.screen.logicalXDPI) { - // Only allow for values > 1 - ratio = window.screen.systemXDPI / window.screen.logicalXDPI; - } - else if (window.devicePixelRatio !== undefined) { - ratio = window.devicePixelRatio; - } - return ratio; + var screen = window.screen || {}; + var systemXDPI = screen.systemXDPI || 1; + var logicalXDPI = screen.logicalXDPI || 1; + // To account for zoom, change to deviceXDPI instead of systemXDPI + if (systemXDPI > logicalXDPI) return systemXDPI / logicalXDPI; + // Fall back to legacy devicePixelRatio or return 1 + else return window.devicePixelRatio || 1; }; })(this); \ No newline at end of file