Skip to content

Instantly share code, notes, and snippets.

@say2joe
Created October 5, 2012 17:16
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 say2joe/3841088 to your computer and use it in GitHub Desktop.
Save say2joe/3841088 to your computer and use it in GitHub Desktop.
Render Hi-Res IMGs for Retina Displays (JavaScript)
/**
* Retina image update script. Invoke with Retina.render().
* Retina.render() may be passed a selector for img selection.
* The default filter looks for imgs with data-scale="retina".
* @param {string|function} expr A CSS selector expression or jQuery filter function.
* @return {boolean|collection} Returns false or (for Retina displays) the images updated.
* @author Joe Johnson (joe.johnson@icrossing.com)
*/
(function(ns,$){
ns.Retina = {
render: (function(isRetina){
return isRetina ? function(expr){
var r1 = /retina/i, r2 = /\d{3}/,
filter = expr ? expr : function(){
return r1.test($(this).data("scale"));
};
return $("img").filter(filter).each(function(){
this.src = this.src.replace(r2,function(m){
return (parseInt(m,10)>=640 ? m : 2*m);
});
});
} : function(){ return false; };
})(ns.devicePixelRatio && (ns.devicePixelRatio > 1))
};
})(window,jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment