Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save the contents of a div as a image?

Basically, I am doing what the heading states, attempting to save the contents of a div as an image.

I plan on making a small online application for the iPad.

One function that is a must is having a 'Save' button that can save the full webpage as an image, and save that image to the iPad's camera roll. I wish to save the full contents of a div, not just the visible area.

I have searched briefly online, but couldn't find much documentation on anything. I found a lot on HTML5 Canvas. Here is some code I put together:

<script> function saveimg() {     var c = document.getElementById('mycanvas');     var t = c.getContext('2d');     window.location.href = image;      window.open('', document.getElementById('mycanvas').toDataURL()); } </script>  <div id="mycanvas"> This is just a test<br /> 12344<br /> </div>  <button onclick="saveimg()">Save</button> 

Although, I am getting this error:

TypeError: c.getContext is not a function 

This application will be built only with HTML, CSS and jQuery (or other Javascript libraries).

like image 366
Fizzix Avatar asked Sep 02 '13 22:09

Fizzix


People also ask

How do you put a div inside an image?

One way is to add position: relative; to your image and then abosolute position the div according to it. fredda: One way is to add position: relative; to your image and then abosolute position the div according to it.

How do I copy a div in HTML?

First, select the div element which need to be copy into another div element. Select the target element where div element is copied. Use the appendTo() method to copy the element as its child.

How do I use canvas2image?

Use html2canvas. js to take a screenshot of a specific div and then use canvas2image. js to download the screenshot as an image locally to your filesystem.


1 Answers

There are several of this same question (1, 2). One way of doing it is using canvas. Here's a working solution. Here you can see some working examples of using this library.

like image 154
Glen Swift Avatar answered Oct 11 '22 16:10

Glen Swift