Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 拍摄元素的截图_Javascript_Jquery_Node.js_Phantomjs - Fatal编程技术网

Javascript 拍摄元素的截图

Javascript 拍摄元素的截图,javascript,jquery,node.js,phantomjs,Javascript,Jquery,Node.js,Phantomjs,嘿,我正在尝试用phantomjs呈现一个特定元素的屏幕截图。我正在为nodejs使用phantomjs桥:phantomjs 到目前为止,我得到的是: page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'); var elementBounds = page.evaluate(function () { var clipRect = document.querySelector(

嘿,我正在尝试用phantomjs呈现一个特定元素的屏幕截图。我正在为nodejs使用phantomjs桥:phantomjs

到目前为止,我得到的是:

page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
var elementBounds = page.evaluate(function () {
    var clipRect = document.querySelector("#yw0").getBoundingClientRect();
    return {
        top: clipRect.top,
        left: clipRect.left,
        width: clipRect.width,
        height: clipRect.height
    };
});
var oldClipRect = page.clipRect;
page.clipRect = elementBounds;
page.render('element.png');
page.clipRect = oldClipRect;

不知怎么的,它不起作用,我总是能看到整个页面的截图!我做错了什么?

用噩梦解决了这个问题。这是我的密码:

var Nightmare = require('nightmare');
var Screenshot = require('nightmare-screenshot');
var nightmare = new Nightmare();
nightmare.goto('url');
nightmare.use(Screenshot.screenshotSelector('element.png', 'img[id="yw0"]'));
nightmare.run();

如果不使用phantomjs,还有一个更简单的方法,有兴趣吗?@MohitBhasi我需要使用phantomjs,因为我的代码是更大的应用程序的一部分programm@MohitBhasi也许你可以用这种方式作为答案。所以我可以尝试在phantom中实现它,它只使用html的画布并绘制image@MohitBhasi因此,我可以将其转换为dataUrl,当它返回时,我可以使用nodejs编写文件