[英]Is it possible to make a picture of a specific part of your website?
My apologies if this is a stupid question, but is there a way to 'take a picture' from a certain part of the website and save it on the computer? 如果这是一个愚蠢的问题,我表示歉意,但是是否有办法从网站的某个部分“拍照”并将其保存在计算机上?
HTML HTML
<div id="red" color="red" width="100px" height="100px"></div>
<div id="green" color="green" width="100px" height="100px"></div>
<div id="blue" color="blue" width="100px" height="100px"></div>
For example I have a webpage with 3 divs next to each other and I want to take a picture of only the green div, and save it as a jpeg or something. 例如,我有一个3 div相邻的网页,我只想拍一张绿色div的照片,并将其另存为jpeg或其他内容。
Is this at all possible with JQuery/Javascript for example? 例如,使用JQuery / Javascript完全可以吗?
As grigno said, Phantom.js would be a good choice since it has screen capture capabilities. 正如grigno所说, Phantom.js具有屏幕捕获功能,将是一个不错的选择。
Here's an example of how you can take pictures of a specific element using getBoundingClientRect
. 这是一个示例,说明如何使用getBoundingClientRect
拍摄特定元素的图片。
var page = require('webpage').create();
address = 'https://stackoverflow.com/q/18466303/2129835';
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
window.setTimeout(function () {
page.clipRect = page.evaluate(function() {
return document.querySelector('.question .post-text').getBoundingClientRect();
});
page.render('output.png');
phantom.exit();
}, 200);
}
});
Not sure how this plays out, but you may be able to take advantage of the <canvas>
element and draw the <div>
there . 不确定如何播放,但是您可以利用<canvas>
元素并在其中绘制<div>
。 Then canvas has export options to convert to jpg, png, etc. 然后画布具有导出选项以转换为jpg,png等。
It is not easily done, but there are some projects which can help you out. 这并非易事,但有些项目可以为您提供帮助。 Have a look at: 看一下:
Basically, they use <canvas>
to draw each DOM element on it, and then save it as bitmap. 基本上,他们使用<canvas>
在其上绘制每个DOM元素,然后将其另存为位图。 Due to this mechanics, the output image may not be 100% accurate though. 由于这种机制,输出图像可能不是100%准确。
If you opt to use html2canvas, it accepts a list of DOM elements to render. 如果您选择使用html2canvas,它将接受要呈现的DOM元素列表。 Have a look at this example for usage. 看一下此示例的用法。
Other approach would be to create Chrome extension - it's easy then - if that's feasible in your use case. 如果在您的用例中可行的话,另一种方法是创建Chrome扩展程序-这很容易。 If so, have a look at chrome.tabs.captureVisibleTab() . 如果是这样,请查看chrome.tabs.captureVisibleTab() 。
I used phantomjs with screen capture to take a picture of a site. 我使用phantomjs和屏幕截图为网站拍照。 After saving the image on the web server you can cut the image file to get a specific area of your page. 将图像保存在Web服务器上后,您可以剪切图像文件以获取页面的特定区域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.