Skip to content

Instantly share code, notes, and snippets.

@juice49
Created July 9, 2015 08:07
Show Gist options
  • Save juice49/487dc8f7d195ad2656fc to your computer and use it in GitHub Desktop.
Save juice49/487dc8f7d195ad2656fc to your computer and use it in GitHub Desktop.
Retinafy
'use strict';
const path = require('path');
const retina = require('is-retina');
module.exports = function applyRetinafy(selector) {
if(!retina()) {
return;
}
const img = document.querySelectorAll(selector);
[].forEach.call(img, retinafy);
};
function retinafy(img) {
const src = img.src || img.style.backgroundImage.slice(4, -1);
const extension = path.extname(src);
const [ pathName ] = src.split(extension);
const retinaSrc = `${pathName}@2x${extension}`;
return img.dataset.retina === 'background' ?
img.style.backgroundImage = `url(${retinaSrc})` :
img.setAttribute('src', retinaSrc);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment