Created
July 9, 2015 08:07
-
-
Save juice49/487dc8f7d195ad2656fc to your computer and use it in GitHub Desktop.
Retinafy
This file contains hidden or 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
'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