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?
You can use dom-to-image library (I'm the maintainer).
Here's how you could approach your problem:
And here is jsfiddle
the next code can be used in 2 modes, mode 1 save the html code to a image, mode 2 save the html code to a canvas.
this code work with the library: https://github.com/tsayen/dom-to-image
*the "id_div" is the id of the element html that you want to transform.
**the "canvas_out" is the id of the div that will contain the canvas so try this code. :
so, if you want to save to image just add this function:
Example of use:
Comment if that work. Comenten si les sirvio :)
Building on top of the Mozdev post that natevw references I've started a small project to render HTML to canvas in Firefox, Chrome & Safari. So for example you can simply do:
Source code and a more extensive example is here.
The easiest solution to animate the DOM elements is using CSS transitions/animations but I think you already know that and you try to use canvas to do stuff CSS doesn't let you to do. What about CSS custom filters? you can transform your elements in any imaginable way if you know how to write shaders. Some other link and don't forget to check the CSS filter lab.
Note: As you can probably imagine browser support is bad.
You could spare yourself the transformations, you could use CSS3 Transitions to flip
<div>
's and<ol>
's and any HTML tag you want. Here are some demos with source code explain to see and learn: http://www.webdesignerwall.com/trends/47-amazing-css3-animation-demos/There is a library that try to do what you say.
See this examples and get the code
http://hertzen.com/experiments/jsfeedback/
http://html2canvas.hertzen.com/
Reads the DOM, from the html and render it to a canvas, fail on some, but in general works.