English 中文(简体)
Phantom JS - clipRect - Javascript Help
原标题:

i m using phantom js to screen shot a page

http://code.google.com/p/phantomjs/wiki/QuickStart#Rendering

it has a feature called clipRect

http://code.google.com/p/phantomjs/wiki/Interface#clipRect_(object)

can someone show me how i would modify the following code to us clipRect so i only get a partial screenshot and not the whole thing?

if (phantom.state.length === 0) {
if (phantom.args.length !== 2) {
    console.log( Usage: rasterize.js URL filename );
    phantom.exit();
} else {
    var address = phantom.args[0];
    phantom.state =  rasterize ;
    phantom.viewportSize = { width: 600, height: 600 };
    phantom.open(address);
}
} else {
    var output = phantom.args[1];
    phantom.sleep(200);
    phantom.render(output);
    phantom.exit();
}    
最佳回答

What was happening is i was using brew and it was installing v 1.0.0 where clipRect and almost every other function wasn t supported as v 1.0.0 is the oldest version.

If you follow these instructions: http://code.google.com/p/phantomjs/wiki/BuildInstructions#Mac_OS_X

then right click on the complied file and click show/view contents (on mac) then copy the executable bin/phantomjs.app/Contents/MacOS/phantomjs to some directory in your PATH.

Feel free to post on here i m monitoring this and i can help if needed.

问题回答

If you are trying to get a screenshot of a particular element, you could get the necessary information for clipRect from getBoundingClientRect as per the bottom of this article:

page.clipRect = page.evaluate(function() {
    return document.getElementById(THE_ELEMENT_YOU_WANT).getBoundingClientRect(); 
});

From the fine manual:

clipRect (object)

This property defines the rectangular area of the web page to be rasterized when render() is invoked. If no clipping rectangle is set, render() will process the entire web page.

Example: phantom.clipRect = { top: 14, left: 3, width: 400, height: 300 }

So try setting clipRect right before you call render:

var output = phantom.args[1];
phantom.sleep(200);
phantom.clipRect = { top: 14, left: 3, width: 400, height: 300 }
phantom.render(output);
phantom.exit();

You d have to figure out where the upper left corner (top and left) is and how big (width and height) you want the clipping rectangle to be.

You can probably set the clipRect any time before render() is called but start with that and see what happens.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签