Skip to content

Instantly share code, notes, and snippets.

@Sakurina
Created January 31, 2012 23:47
Show Gist options
  • Save Sakurina/1713940 to your computer and use it in GitHub Desktop.
Save Sakurina/1713940 to your computer and use it in GitHub Desktop.
hotaru - automagically replaces images with retina display variants

hotaru

Just add a retina-src attribute to img tags pointing to a Retina Display version of your image, load this script, and Retina Display resources will automatically get subbed in when appropriate.

License

Copyright (c) 2012 Yanik Magnan ([email protected])

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# hotaru - automagically replaces images with retina display variants
# by Yanik Magnan - http://r-ch.net
hotaru = () ->
return if !window.devicePixelRatio? or window.devicePixelRatio != 2
for el in document.getElementsByTagName "img"
do (el) ->
retina_src = el.getAttribute "retina-src"
if retina_src?
el.setAttribute "width", el.offsetWidth
el.setAttribute "height", el.offsetHeight
el.setAttribute "src", retina_src
window.addEventListener "load", hotaru, false
(function() {
var hotaru;
hotaru = function() {
var el, _i, _len, _ref, _results;
if (!(window.devicePixelRatio != null) || window.devicePixelRatio !== 2) {
return;
}
_ref = document.getElementsByTagName("img");
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
el = _ref[_i];
_results.push((function(el) {
var retina_src;
retina_src = el.getAttribute("retina-src");
if (retina_src != null) {
el.setAttribute("width", el.offsetWidth);
el.setAttribute("height", el.offsetHeight);
return el.setAttribute("src", retina_src);
}
})(el));
}
return _results;
};
window.addEventListener("load", hotaru, false);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment