Skip to content

Commit

Permalink
Add ability to capture specific DOM elements
Browse files Browse the repository at this point in the history
Fixes #100.
  • Loading branch information
kevva committed Aug 18, 2014
1 parent 8aef829 commit edfb910
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function showHelp() {
pageres todomvc.com yeoman.io 1366x768 1600x900
pageres [ yeoman.io 1366x768 1600x900 --no-crop ] [ todomvc.com 1024x768 480x320 ] --crop
pageres todomvc.com 1024x768 --name '<%= date %> - <%= url %>'
pageres yeoman.io 1366x768 --selector '.page-header'
pageres --delay 3 1366x768 < urls.txt
pageres unicorn.html 1366x768
cat screen-resolutions.txt | pageres todomvc.com yeoman.io
Expand All @@ -52,6 +53,7 @@ function showHelp() {
-c, --crop Crop to the set height
--cookie <cookie> Browser cookie, can be set multiple times
--name <template> Custom filename
--selector <element> Capture DOM element
<url> can also be a local file path.
Expand Down
6 changes: 6 additions & 0 deletions converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ page.open(options.url, function (status) {
phantom.exit(1);
}

if (options.selector) {
page.clipRect = page.evaluate(function (s) {
return document.querySelector(s).getBoundingClientRect();
}, options.selector);
}

page.evaluate(function () {
document.body.style.background = 'white';
});
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Example
pageres todomvc.com yeoman.io 1366x768 1600x900
pageres [ yeoman.io 1366x768 1600x900 --no-crop ] [ todomvc.com 1024x768 480x320 ] --crop
pageres todomvc.com 1024x768 --name '<%= date %> - <%= url %>'
pageres yeoman.io 1366x768 --selector '.page-header'
pageres --delay 3 1366x768 < urls.txt
pageres unicorn.html 1366x768
cat screen-resolutions.txt | pageres todomvc.com yeoman.io
Expand All @@ -50,6 +51,7 @@ Options
-c, --crop Crop to the set height
--cookie <cookie> Browser cookie, can be set multiple times
--name <template> Custom filename
--selector <element> Capture DOM element
<url> can also be a local file path.
Expand Down Expand Up @@ -148,6 +150,12 @@ Available variables:
- `crop`: Outputs `-cropped` when the crop option is true
- `date`: The current date

##### selector

Type: `string`

Capture a specific DOM element.


### pageres.src(url, sizes, options)

Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ test('rename image using the `name` option', function (t) {
});
});

test('capture a DOM element using the `selector` option', function (t) {
t.plan(5);

var pageres = new Pageres({ selector: '.page-header' })
.src('http://yeoman.io', ['1024x768']);

pageres.run(function (err, streams) {
t.assert(!err, err);
t.assert(streams[0].filename === 'yeoman.io-1024x768.png');

streams[0].pipe(concat(function (data) {
var size = imageSize(data);
t.assert(size.width === 1024);
t.assert(size.height === 80);
t.assert(data.length === 15016);
}));
});
});

test('support local relative files', function (t) {
t.plan(3);

Expand Down

2 comments on commit edfb910

@kevva
Copy link
Contributor Author

@kevva kevva commented on edfb910 Aug 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

687474703a2f2f7777772e7265616374696f6e676966732e636f6d2f77702d636f6e74656e742f67616c6c6572792f64616e63652d70617274792f74756d626c725f6d306f73727268714d6b31717a6376376e6f345f3235302e676966

Please sign in to comment.