Skip to content

Instantly share code, notes, and snippets.

@tracend
Last active December 16, 2015 04:59
Show Gist options
  • Save tracend/5380987 to your computer and use it in GitHub Desktop.
Save tracend/5380987 to your computer and use it in GitHub Desktop.
Retina Images using Handlebars.js #cc #handlebars #js
/*
* 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 "@2x" + match; })
: src;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment