使用 HTML5/Canvas/JavaScript 获取浏览器内屏幕截图
Google 的“报告错误”或“反馈工具”可让您选择浏览器窗口的一个区域来创建屏幕截图,并随有关错误的反馈一起提交。
Jason Small 的屏幕截图,发布在重复问题中。 >
他们是如何做到这一点的? Google 的 JavaScript 反馈 API 是从此处和反馈模块概述将演示屏幕截图功能。
Google's "Report a Bug" or "Feedback Tool" lets you select an area of your browser window to create a screenshot that is submitted with your feedback about a bug.
Screenshot by Jason Small, posted in a duplicate question.
How are they doing this? Google's JavaScript feedback API is loaded from here and their overview of the feedback module will demonstrate the screenshot capability.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
JavaScript 可以读取 DOM 并使用
canvas
呈现相当准确的表示。我一直在编写一个将 HTML 转换为画布图像的脚本。今天决定将其实施为发送您所描述的反馈。该脚本允许您创建反馈表单,其中包括在客户端浏览器上创建的屏幕截图以及表单。屏幕截图基于 DOM,因此可能无法 100% 准确地反映真实情况,因为它不会制作实际的屏幕截图,而是根据页面上的可用信息构建屏幕截图。
它不需要服务器进行任何渲染,因为整个图像是在客户端的浏览器上创建的。 HTML2Canvas 脚本本身仍处于实验状态,因为它几乎无法解析我想要的那么多 CSS3 属性,即使代理可用,它也不支持加载 CORS 图像。
浏览器兼容性仍然相当有限(不是因为无法支持更多浏览器,只是没有时间使其更加支持跨浏览器)。
有关更多信息,请查看此处的示例:
http://hertzen.com/experiments/jsfeedback/
编辑
html2canvas 脚本现在可单独在此处和一些示例。
编辑2
Google 使用非常相似的方法的另一个确认(事实上,根据文档,唯一的主要区别是它们的遍历/绘图的异步方法)可以在 Google 反馈团队的 Elliott Sprehn 的演示中找到:
JavaScript can read the DOM and render a fairly accurate representation of that using
canvas
. I have been working on a script which converts HTML into a canvas image. Decided today to make an implementation of it into sending feedbacks like you described.The script allows you to create feedback forms which include a screenshot, created on the client's browser, along with the form. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page.
It does not require any rendering from the server, as the whole image is created on the client's browser. The HTML2Canvas script itself is still in a very experimental state, as it does not parse nearly as much of the CSS3 attributes I would want it to, nor does it have any support to load CORS images even if a proxy was available.
Still quite limited browser compatibility (not because more couldn't be supported, just haven't had time to make it more cross browser supported).
For more information, have a look at the examples here:
http://hertzen.com/experiments/jsfeedback/
edit
The html2canvas script is now available separately here and some examples here.
edit 2
Another confirmation that Google uses a very similar method (in fact, based on the documentation, the only major difference is their async method of traversing/drawing) can be found in this presentation by Elliott Sprehn from the Google Feedback team:
http://www.elliottsprehn.com/preso/fluentconf/
您的网络应用程序现在可以使用
getUserMedia()
获取客户端整个桌面的“本机”屏幕截图:看看这个示例:
https://www.webrtc-experiment.com/Pluginfree-Screen-Sharing/
客户端必须使用 chrome(目前)并且需要在 chrome://flags 下启用屏幕捕获支持。
Your web app can now take a 'native' screenshot of the client's entire desktop using
getUserMedia()
:Have a look at this example:
https://www.webrtc-experiment.com/Pluginfree-Screen-Sharing/
The client will have to be using chrome (for now) and will need to enable screen capture support under chrome://flags.
使用 getDisplayMedia 以 Canvas 或 Jpeg Blob / ArrayBuffer 形式获取屏幕截图API:
修复 1:仅针对 Electron.js 使用 getUserMedia 和 chromeMediaSource
修复 2:抛出错误而不是返回空对象
修复 3:修复演示以防止错误:
必须从用户手势处理程序调用 getDisplayMedia
演示:
Get screenshot as Canvas or Jpeg Blob / ArrayBuffer using getDisplayMedia API:
FIX 1: Use the getUserMedia with chromeMediaSource only for Electron.js
FIX 2: Throw error instead return null object
FIX 3: Fix demo to prevent the error:
getDisplayMedia must be called from a user gesture handler
DEMO:
PoC
正如 Niklas 提到的,你可以使用 html2canvas 库在浏览器中使用 JS 进行截图。我将通过提供使用此库截取屏幕截图的示例来扩展他的答案(“概念证明”):
在获取图像作为数据 URI 后,在
onrendered
的report()
函数中,您可以将其显示给用户并允许他通过鼠标绘制“bug 区域”,然后发送屏幕截图和区域坐标到服务器。在 这个示例
async/await
版本中:使用了很好的makeScreenshot()
函数.更新
简单的示例,允许您截取屏幕截图,选择区域,描述错误并发送POST请求(这里jsfiddle)(主要函数是
report()
)。PoC
As Niklas mentioned you can use the html2canvas library to take a screenshot using JS in the browser. I will extend his answer in this point by providing an example of taking a screenshot using this library ("Proof of Concept"):
In
report()
function inonrendered
after getting image as data URI you can show it to the user and allow him to draw "bug region" by mouse and then send a screenshot and region coordinates to the server.In this example
async/await
version was made: with nicemakeScreenshot()
function.UPDATE
Simple example which allows you to take screenshot, select region, describe bug and send POST request (here jsfiddle) (the main function is
report()
).这是一个完整的屏幕截图示例,适用于 2021 年的 Chrome。最终结果是准备传输的 Blob。流程为:请求媒体>抓取框架>绘制到画布>转移到 blob。如果您想提高内存效率,请探索 OffscreenCanvas 或可能ImageBitmapRenderingContext
https://jsfiddle.net/v24hyd3q/1/
Here is a complete screenshot example that works with chrome in 2021. The end result is a blob ready to be transmitted. Flow is: request media > grab frame > draw to canvas > transfer to blob. If you want to do it more memory efficient explore OffscreenCanvas or possibly ImageBitmapRenderingContext
https://jsfiddle.net/v24hyd3q/1/
下面是一个使用的示例: getDisplayMedia
另外值得一看的是屏幕捕获 API 文档。
Heres an example using: getDisplayMedia
Also worth checking out is the Screen Capture API docs.
您可以尝试我的新 JS 库:screenshot.js。
它可以拍摄真实的屏幕截图。
您加载脚本:
并截取屏幕截图:
您可以在项目页面中阅读更多选项。
You can try my new JS library: screenshot.js.
It's enable to take real screenshot.
You load the script:
and take screenshot:
You can read more options in project page.
该方法使用流行的JS库html2canvas来捕获网页的屏幕截图。
该脚本执行以下步骤来从 HTML 页面捕获屏幕截图。
capture-screenshot 是按钮的 id,单击该按钮我们将捕获 DOM 的屏幕截图。
This method uses the popular JS library html2canvas to capture a screenshot from a webpage.
This script implements the below steps to capture a screenshot from the page HTML.
capture-screenshot is an id of button on click of that we will capture screen shot of DOM.