Render HTML to an image

后端 未结 17 2057
栀梦
栀梦 2020-11-22 16:06

Is there a way to render html to image like PNG? I know that it is possible with canvas but I would like to render standard html element like div for example.

17条回答
  •  庸人自扰
    2020-11-22 16:22

    May I recommend dom-to-image library, that was written solely to address this problem (I'm the maintainer).
    Here is how you use it (some more here):

    var node = document.getElementById('my-node');
    
    domtoimage.toPng(node)
        .then (function (dataUrl) {
            var img = new Image();
            img.src = dataUrl;
            document.appendChild(img);
        })
        .catch(function (error) {
            console.error('oops, something went wrong!', error);
        });
    

提交回复
热议问题