最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to save a html element as a png - Stack Overflow

programmeradmin1浏览0评论

Not a duplicate as I need to convert 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 <div> 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>

Not a duplicate as I need to convert 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 <div> 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>
Share Improve this question edited May 3, 2016 at 11:13 Mr Lister 46.6k15 gold badges113 silver badges155 bronze badges asked Apr 24, 2016 at 15:02 Morgan StephenMorgan Stephen 231 silver badge4 bronze badges 1
  • Possible duplicate of Capture HTML Canvas as gif/jpg/png/pdf? – Jongware Commented Apr 24, 2016 at 19:57
Add a ment  | 

1 Answer 1

Reset to default 4

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.

发布评论

评论列表(0)

  1. 暂无评论