Created
May 14, 2014 18:09
-
-
Save marcelduran/1e91b64849e4c0572de2 to your computer and use it in GitHub Desktop.
PhantomJS script to pseudo-retina screenshots
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
var page = require('webpage').create(), | |
address, output, size; | |
page.onConsoleMessage = function(msg, lineNum, sourceId) { | |
console.log('CONSOLE: ' + msg); | |
}; | |
if (phantom.args.length < 2 || phantom.args.length > 6) { | |
console.log('Usage: rasterize.js URL filename WxH retina ua'); | |
phantom.exit(); | |
} else { | |
address = phantom.args[0]; | |
output = phantom.args[1]; | |
size = /(\d+)x(\d+)/i.exec(phantom.args[2]) || []; | |
page.viewportSize = { width: parseInt(size[1], 10) || 1280, | |
height: parseInt(size[2], 10) || 1024 }; | |
phantom.args[4] && (page.settings.userAgent = phantom.args[4]); | |
page.open(address, function (status) { | |
if (status !== 'success') { | |
console.log('Unable to load the address!'); | |
} else { | |
page.evaluate(function (retina) { | |
console.log(retina); | |
if (retina) { | |
/* scale the whole body */ | |
document.body.style.webkitTransform = "scale(2)"; | |
document.body.style.webkitTransformOrigin = "0% 0%"; | |
/* fix the body width that overflows out of the viewport */ | |
document.body.style.width = "50%"; | |
} | |
}, /(?:1|true|yes)/i.test(phantom.args[3])); | |
window.setTimeout(function () { | |
page.render(output); | |
phantom.exit(); | |
}, 200); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that
phantom.args
has been removed, in favor ofsystem.args
.Add
at the top of the file and then replace all subsequent calls to
phantom.args
withargs
(withrasterize.js
beingargs[0]
)