Skip to content

Instantly share code, notes, and snippets.

@maxwell
Created May 25, 2012 03:10
Show Gist options
  • Save maxwell/2785520 to your computer and use it in GitHub Desktop.
Save maxwell/2785520 to your computer and use it in GitHub Desktop.
the perfect raster.js
var page = new WebPage(),
address, output, div;
if (phantom.args.length < 2 || phantom.args.length > 4) {
console.log('Usage: rasterize.js URL filename div');
phantom.exit();
}
address = phantom.args[0];
output = phantom.args[1];
div = phantom.args[2];
//console.log(address, output, div)
function evaluate(page, func) {
var args = [].slice.call(arguments, 2);
var fn = "function() { return (" + func.toString() + ").apply(this, " + JSON.stringify(args) + ");}";
return page.evaluate(fn);
}
function returnDivDimensions(div){
var $el = jQuery(div);
var box = $el.offset()
box.height = $el.height();
box.width = $el.width();
return box;
}
page.onConsoleMessage = function (msg) {
//console.log("from page:" + msg);
};
page.viewportSize = { width: 1000, height: 1000 }
page.open(address, function (status) {
//once page loaded, include jQuery from cdn
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function(){jQuery.noConflict()})
var clip = evaluate(page, returnDivDimensions, div);
//take screenshot and exit
page.clipRect = clip;
page.render(output);
phantom.exit();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment