Rendering HTML elements to <canvas>

gotch4 picture gotch4 · Sep 29, 2012 · Viewed 104.6k times · Source

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

Answer

Mikko Ohtamaa picture Mikko Ohtamaa · Sep 30, 2012

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

There are some emulations:

html2canvas project http://html2canvas.hertzen.com/index.html (basically a HTML renderer attempt built on Javascript + 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

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