Skip to content

Instantly share code, notes, and snippets.

@jamesdaniels
Created October 16, 2014 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesdaniels/bedf2227f10b75c52eb7 to your computer and use it in GitHub Desktop.
Save jamesdaniels/bedf2227f10b75c52eb7 to your computer and use it in GitHub Desktop.
// This will grab the largest image for the pixelRatio without oversampling (iOS style)
// Usage: assetUsingNativePixelRatio('/assets/images', 'logo', 'png', [1,2,3]);
// Output: /assets/images/logo@2x.png
function assetUsingNativePixelRatio(path, name, extension, pixelRatios) {
// Sort the pixelRatios in case they were put in the wrong order
var $pixelRatios = $(pixelRatios).sort();
// Price is right style
var matchingPixelRatio = $pixelRatios.not(function() {
return (this > window.devicePixelRatio);
}).last()[0];
// Append everything together but if the pixel ratio is 1x don't put an "@1x" that's just silly
return (path + '/' + name + (matchingPixelRatio > 1 ? ('@' + matchingPixelRatio + 'x') : '') + '.' + extension);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment