Created
December 4, 2012 09:37
-
-
Save st3phan/4202201 to your computer and use it in GitHub Desktop.
Creating screenshots of HTML elements using PhantomJS
This file contains 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(); | |
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