It would be incredibly useful to be able to temporarily convert a regular element into a canvas. For example, say I have a styled div that I want to flip. I want to dynamically create a canvas, “render” the HTMLElement into the canvas, hide the original element and animate the canvas.
Can it be done?
Sorry, the browser won’t render HTML into a canvas.
It would be a potential security risk if you could, as HTML can include content (in particular images and iframes) from third-party sites. If
canvascould turn HTML content into an image and then you read the image data, you could potentially extract privileged content from other sites.To get a canvas from HTML, you’d have to basically write your own HTML renderer from scratch using
drawImageandfillText, which is a potentially huge task. There’s one such attempt here but it’s a bit dodgy and a long way from complete. (It even attempts to parse the HTML/CSS from scratch, which I think is crazy! It’d be easier to start from a real DOM node with styles applied, and read the styling usinggetComputedStyleand relative positions of parts of it usingoffsetTopet al.)