Last active
December 16, 2015 04:59
-
-
Save tracend/5380987 to your computer and use it in GitHub Desktop.
Retina Images using Handlebars.js #cc #handlebars #js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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