1、html2canvas使用时所涉及的图片,必须遵循同源策略,即:域名,协议,端口相同
2、在不同源的情况下会报一下错误
Uncaught (in promise) DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported. at Object.onrendered
实例代码如下:
html2canvas(document.getElementById("canvasImg"), { //所要截取div ID
allowTaint: true,
taintTest: false,
background: "#fff",
useCORS: true,
onrendered: function(canvas) {
canvas.id = "mycanvas";
//alert(canvas.toDataURL("image/png"))
dataUrl = canvas.toDataURL("image/png",1).replace("image/png","image/octet-stream");
//生成base64图片数据
app.uploadBase64(dataUrl, function(data) {
alert("data="+JSON.stringify(data));
if(data.status == "true") {
var headImgUrl = "http://appadmin.xhd.cn" + data.body.responseText;
share.shareImageUrl = headImgUrl;
} else {
mui.toast(data.message);
}
});
}
});