npm install --save html-to-image
/* ES6 */ import * as htmlToImage from 'html-to-image'; import { toPng, toJpeg, toBlob, toPixelData, toSvg } from 'html-to-image'; /* ES5 */ var htmlToImage = require('html-to-image');
html-to-image 所有顶级函数都接受 DOM 节点和渲染选项,并返回一个用相应的 dataURL:
var node = document.getElementById('my-node'); htmlToImage.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); });
html-to-image 下载,使用的是 download 库:
npm install downloadjs bower install downloadjs require("downloadjs")(data, strFileName, strMimeType);
html-to-image toPng download:
htmlToImage.toPng(document.getElementById('my-node')) .then(function (dataUrl) { download(dataUrl, 'my-node.png'); });