将 HTML 元素渲染到 <canvas>

2022-01-17 00:00:00 html html5-canvas

有没有办法在画布中呈现任意 HTML 元素(然后访问其缓冲区...).

Is there a way to have an arbitrary HTML element rendered in a canvas (and then access its buffer...).

推荐答案

目前你不会得到真正的 HTML 渲染到 <canvas> 本身,因为 canvas 上下文没有渲染函数HTML 元素.

You won't get real HTML rendering to <canvas> per se currently, because canvas context does not have functions to render HTML elements.

有一些模拟:

html2canvas 项目 http://html2canvas.hertzen.com/index.html(基本上是基于 HTML 渲染器的尝试Javascript + 画布)

html2canvas project http://html2canvas.hertzen.com/index.html (basically a HTML renderer attempt built on Javascript + canvas)

HTML 到 SVG 到 <canvas> 可能取决于您的用例:

HTML to SVG to <canvas> might be possible depending on your use case:

https://github.com/miohtama/Krusovice/blob/master/src/tools/html2svg2canvas.js

另外,如果您使用的是 Firefox 你可以破解一些扩展的权限,然后将 DOM 窗口渲染到 <canvas>

Also if you are using Firefox you can hack some extended permissions and then render a DOM window to <canvas>

https://developer.mozilla.org/en-US/docs/HTML/Canvas/Drawing_Graphics_with_Canvas?redirectlocale=en-US&redirectslug=Drawing_Graphics_with_Canvas#Rendering_Web_Content_Into_A_Canvas

相关文章