html2canvas截取图片并下载

let element = document.getElementById('pic')
html2canvas(element).then(canvas => {
      let layer_pic = canvas.toDataURL('image/png')
      let byteCharacters = atob(layer_pic.split(',')[1])
      let byteNumbers = new Array(byteCharacters.length)
      for (let i = 0; i < byteCharacters.length; i++) {
       byteNumbers[i] = byteCharacters.charCodeAt(i)
      }
      let byteArray = new Uint8Array(byteNumbers)
      let blob = new Blob([byteArray], { type: 'image/jpeg' })
      let url = URL.createObjectURL(blob)
      let a = document.createElement('a')
      a.href = url
      a.download = 'JHDGap.jpg'
      a.click()
      URL.revokeObjectURL(url)
      return
 })

 

posted @ 2024-11-22 17:12  yw3692582  阅读(2)  评论(0编辑  收藏  举报