• 1
  • 新人请关照

html2canvas合成图片的时候,dom中的一些图片出不来,而且是偶现的,页面没有报错

html2canvas合成图片的时候,dom中的一些图片出不来,而且是偶现的,页面没有报错。
import html2canvas from 'html2canvas';
export default function convert2canvas(selector) {
const shareContent = document.querySelector(selector);
const width = shareContent.offsetWidth;
const height = shareContent.offsetHeight;
const canvas = document.createElement('canvas');
const scale = 2;
canvas.width = width * scale;
canvas.height = height * scale;
canvas.getContext('2d').scale(scale, scale);
const opts = {

scale: scale,
canvas: canvas,
taintTest: false,
useCORS: true,
allowTaint: false,
logging: true,
width: width,
height: height

};
html2canvas(shareContent, opts).then(function(canvas) {

const img = new Image();
const url = canvas.toDataURL();
img.src = url;
img.onload = function() {
  top.postMessage(url, '*');
};
img.style.position = 'fixed';
img.style.top = '0';
img.style.bottom = '0';
img.style.left = '0';
img.style.right = '0';
img.style.width = '100%';
img.style.height = '100%';
img.setAttribute('id', 'poster');
const previousImg = document.getElementById('poster');
if (previousImg) {
  console.info(1);
  document.body.replaceChild(img, previousImg);
  return;
}
document.body.appendChild(img);

});
}

阅读 597
评论
    0 个回答
    撰写回答

    登录后参与交流、获取后续更新提醒

    相似问题
    推荐文章