html2canvas擷取圖片並下載

yw3692582發表於2024-11-22
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
 })

相關文章