html2canvas源码修改,html2canvas

前言

一旦有了女朋友,人很颓废,加上换了工作,整整半年多未更新,懒得费解

前提:整个放canvas转成图片的区域

坑1:滚动截屏

将目标区域Dom克隆,并设置克隆Dom的狂傲,截图克隆区域即可

const targetDom = document.querySelector("#admin")

const copyDom = targetDom.cloneNode(true)

copyDom.style.width = targetDom.scrollWidth + 'px'

copyDom.style.height = targetDom.scrollHeight + 'px'

document.body.appendChild(copyDom)

html2canvas(copyDom, {

allowTaint: false,

useCORS: true,

height: targetDom.scrollHeight,

width: targetDom.scrollWidth

}).then(canvas => {

// canvas

});

坑2:截屏区域有跨域图片

若是百度会发现修改源码是最多的。其实那是老版本,新版本(1.x)只要配下参数即可即:

// allowTaint: false 不允许跨域图片污染画布

// useCORS: true 允许加载跨域图片

坑3:下载图片

移动端不好整,PC端就简单了

// 插入之前canvas下

html2canvas(copyDom, {

allowTaint: false,

useCORS: true,

height: targetDom.scrollHeight,

width: targetDom.scrollWidth

}).then(canvas => {

this.printShow = true

copyDom.parentNode.removeChild(copyDom)

// console.log(canvas.style.width)

canvas.style.width = parseFloat(canvas.style.width) * 0.8 + 'px'

canvas.style.height = parseFloat(canvas.style.height) * 0.8 + 'px'

setTimeout(() => {

const container = document.querySelector('#view')

while (container.hasChildNodes()) {

container.removeChild(container.firstChild)

}

// toImage

const dataImg = new Image()

dataImg.src = canvas.toDataURL('image/png')

document.querySelector('#view').appendChild(dataImg)

const alink = document.createElement("a");

alink.href = dataImg.src;

alink.download = "testImg.jpg";

alink.click();

}, 0)

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值