tags:

views:

324

answers:

3

It would be incredibly useful to be able to temporarily convert a regular ol' 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?

+1  A: 

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 canvas could 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 drawImage and fillText, 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 using getComputedStyle and relative positions of parts of it using offsetTop et al.)

bobince
Well that's a bummer. It'd be great to be able to "freeze" an element and distort it in some manner and then be able to "unfreeze" it later.I disagree that it'd be a *new* security risk. It wouldn't be any different than being able to use JS to read the DOM and steal that info (and, of course, any sandbox constraints on JS would apply the same).Thanks for the confirmation of what I suspected would be the answer, bobince.
orangechicken
Yeah, it's just that applying Same Origin restrictions would be considerably more tricky for general HTML content than for cross-document scripting. You'd have to keep a record of every rectangle on-screen that contained any third-party content, and deny a render request that intersected any of them — images, iframes, video/audio, elements with background images, all objects/embeds/applets whether third-party or not (since they have their own cross-site rules), SVG and so on.
bobince
A: 

ajax(jquery), use ajax! then you can do all that!

jquery