How to convert the content of a div tag to an image and save it to a local folder via javascript ? I know there must be a way out but i am unable to find it . Any help would be greatly appreciated . Thanks in advance.
- 2what-have-you-tried?HashimR– HashimR2012-10-04 10:40:21 +00:00Commented Oct 4, 2012 at 10:40
- I tried rendering the canvas to an image and i achieved it but the problem is i want the entire div content converted to an image.Vyper– Vyper2012-10-04 10:42:28 +00:00Commented Oct 4, 2012 at 10:42
- What does canvas have to do with your div element? Do you want to take a screen shot of your div element?Ingo Kegel– Ingo Kegel2012-10-04 10:48:48 +00:00Commented Oct 4, 2012 at 10:48
- The canvas is within my div element. I have generated the graph dynamically through javascript and now i want the entire div to be converted to an image and saved to a local folder.Vyper– Vyper2012-10-04 10:52:39 +00:00Commented Oct 4, 2012 at 10:52
2 Answers
Canvas.drawImage supports Image, Canvas and Video elements. Some people say, that it may support DOMElement in the future.
There are many open-source browsers written in C (or C++). There is Emscripten project, which can convert C++ code to Javascript. So you can run your own browser inside a browser and use it to render your HTML.
Running browser inside a browser is not strange idea at all. E.g. there is a project, that emulates x86 machine in a browser - http://copy.sh/v24/.
Comments
Javascript cannot write to the file system if that is what you are trying to achieve.
You can make use of HTML5's localStorage if you need to store strings in javascript but they are sandboxed within the browser.
To get the contents of most HTML elements you can use document.getElementById('theDiv').innerHTML to get what is between the <div></div> tags.
You can pass the result of innerHTML to a PHP page, and from there save it to file.
EDIT
Ok based our comments, I recommend you use PHP. You use document.getElementById('theDiv').innerHTML to send the html to a php page. From there use http://www.rabuser.info/painty.zip Download their 'painty.php' page and pass the html to it. painty.php in theory will generate an img out of the html.