Skip to content

Instantly share code, notes, and snippets.

@dpk dpk/retina.js
Created Jun 17, 2015

Embed
What would you like to do?
if (('devicePixelRatio' in window && window.devicePixelRatio >= 1.5) ||
('matchMedia' in window && window.matchMedia("(-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3/2), (min-resolution: 1.5dppx)").matches))
{
document.addEventListener('DOMContentLoaded', function() {
var elements = document.getElementsByClassName('retina');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
if (element.tagName !== 'IMG') continue;
var match = element.src.match(/^(.+)\.(.+?)$/);
if (match) {
element.src = match[1] + '@2x.' + match[2];
} else {
element.src = element.src + '@2x';
}
element.onload = function(event) {
event.target.width = event.target.naturalWidth / 2;
};
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.