html2Canvas截取百度地图问题整理及解决方案


一、截图地图打点出不来

1.开启foreignObjectRendering 原因是地图打点中含有svg

var canvas = await html2canvas(obj, {
    useCORS: true, 
    foreignObjectRendering: true
  })

2.页面图片转base64,开启foreignObjectRendering页面打点能截取但地图瓦片和页面图片出不来,开启useCORS也无效



 let arr = [...$('img'), ...$('.backImg')] //获取页面Img标签和使用了背景图片的类名
 const length = arr.length
 arr.forEach(function (el, index) {
      getBase64Image(el, el.src, index, length)
 })
function getBase64Image(img, src, index, lenght) {
  let flag = false
  if (img.className === 'backImg') {
    src = img.style.getPropertyValue('background-image').slice(5, -2)
    flag = true
  }
  //转base64格式
  fetch(src)
    .then((response) => response.blob())
    .then((blob) => {
      const reader = new FileReader()
      reader.onloadend = () => {
        const base64data = reader.result
        if (flag) {
          img.style.backgroundImage = `url("${base64data}")`
        } else {
          img.src = base64data
        }
        if (index == lenght - 1) {  
          setTimeout(() => {
            screenshotCanvas() //截图
          }, 7000)  
        }
      }
      reader.readAsDataURL(blob)
    })
}

二、截图报错Uncaught (in promise)

在这里插入图片描述
这个问题是因为地图中打点中引入了gif
解决办法:gif转png再转base64或者若此gif没使用则直接删掉该节点再截图

$('.BMap_mask')[0].nextElementSibling.childNodes[1].remove()

在这里插入图片描述


html2Canvas属性大全

属性名默认值描述
allowTaintfalse是否允许不同源的图片污染画布
backgroudColor#ffffff画布背景颜色,如果 DOM 中没有指定,则默认为白色。设置 null 则为透明
canvasnull现有的 canvas 元素,用作绘图的基础
foreignObjectRenderingfalse如果浏览器支持 ForeignObject rendering,是否使用它
imageTimeout15000加载图片超时(毫秒)。设置 0 关闭超时
ignoreElements(element) => false布尔函数,用于从渲染中删除匹配元素。
loggingtrue启用日志记录以进行调试
onclonenull在克隆文档流进行渲染时调用的回调函数,可用于修改将在不影响原始源文档流的情况下呈现的内容
proxynullUrl 到代理,用于加载跨域图片资源。如果留空,则不会加载跨域图片。
removeContainertrue是否清理克隆的 DOM 元素,html2canvas 暂时创建。
scalewindow.devicePixelRatio用于渲染的比例,默认为浏览器设备像素比率。
useCORSfalse是否尝试使用 CORS 从服务器加载图片
widthElement widthcanvas 画布宽度
heightElement heightcanvas 画布高度
xElement x-offset裁剪画布 x 坐标
yElement y-offset裁剪画布 y 坐标
scrollXElement scrollX渲染元素时使用的 X 滚动位置(比如元素使用 position: fixed
scrollYElement scrollY渲染元素时使用的 Y 滚动位置(比如元素使用 position: fixed
windowWidthWindow.innerWidth渲染 Element 时要使用的窗口宽度,这可能会影响媒体查询等内容
windowHeightWindow.innerHeight渲染 Element 时要使用的窗口高度,这可能会影响媒体查询等内容
  • 15
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

H5_ljy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值