Morgan Stephen Morgan Stephen - 7 months ago 17
Javascript Question

How to save a html element as a png

Not a duplicate as I need to convery to a html canvas not from one




I am developing a site that puts some elements on the screen.

I need to be able to capture the content of a in a png

I have tried some chrome extensions but none worked unfortunately

<style type="text/css">
* {
margin: 0px 0px;
padding: 0px 0px;
}
#box {
background: #FFFF00;
width: 100px;
height: 50px;
}
</style>
<div id="box">
This is my box :)
</box>

Answer

html2canvas can help you.

From official web site:

This script allows you to take "screenshots" of webpages or parts of it, directly on the users browser. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page.

html2canvas(document.getElementById('box'), {
  onrendered: function(canvas) {
    document.body.appendChild(canvas);
  },
  width: 400,
  height: 400
});

Click here to see some live examples.