Bootstrap

DOMtoImage 将dom转化为图片

1.转化为png格式

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

2.转换为blob格式

domtoimage.toBlob(document.getElementById('my-node'))
    .then(function (blob) {
        window.saveAs(blob, 'my-node.png');
});

3.转换为JPEG 格式

domtoimage.toJpeg(document.getElementById('my-node'), { quality: 0.95 })
    .then(function (dataUrl) {
        var link = document.createElement('a');
        link.download = 'my-image-name.jpeg';
        link.href = dataUrl;
        link.click();
});

 

;