Skip to content

Instantly share code, notes, and snippets.

@st3phan
Created December 4, 2012 09:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st3phan/4202201 to your computer and use it in GitHub Desktop.
Save st3phan/4202201 to your computer and use it in GitHub Desktop.
Creating screenshots of HTML elements using PhantomJS
var page = require('webpage').create();
page.open('/url', function () {
var items = ['selector1', 'selector2', 'selector3'];
var items_length = items.length;
for (var i = 0; i < items_length; i++) {
var clipRect = page.evaluate(function (s) {
var cr = document.querySelector(s).getBoundingClientRect();
return cr;
}, '.'+items[i]);
page.clipRect = {
top: clipRect.top,
left: clipRect.left,
width: clipRect.width,
height: clipRect.height
};
page.render('../'+items[i]+'.png');
if (i < items_length) phantom.exit();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment