Skip to content

Instantly share code, notes, and snippets.

@mojoaxel
Forked from tracend/handlebars.retina.js
Last active August 29, 2015 13:56
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 mojoaxel/8845881 to your computer and use it in GitHub Desktop.
Save mojoaxel/8845881 to your computer and use it in GitHub Desktop.
/*
* Retina Images using Handlebars.js
*
* Created by [Makis Tracend](@tracend)
* Released under the [MIT license](http://makesites.org/licenses/MIT)
*
* Usage: <img src="{{retina 'http://mydoma.in/path/to/image.jpg'}}">
*/
Handlebars.registerHelper('retina', function( src ) {
// first check if it's a retina screen
var retina_screen = (typeof window === 'undefined')
? /iPad/.test( request.headers['user-agent'] ) // server-side monitoring currently limited to iPad, based on the user-agent
: ( window.devicePixelRatio > 1 );
// output relevant image source
return ( retina_screen )
? src.replace(/\.\w+$/, function(match) { return "_x2" + match; })
: src;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment