使用html2canvas截圖 以及相關(guān)問題

html2canvas
場景:使用 html2canvas 將長圖保存到 iOS 和 Android

使用最新的版本暫未發(fā)現(xiàn)圖片內(nèi)容偏移的問題

可能遇到的問題

1.\color{#ff4545}{如果出現(xiàn)內(nèi)容偏移,可能是 dom 未渲染完成造成的} 也可能是 \color{#ff4545}{Vant 組件 可能會引起的}

  1. Android 的部分手機會保存失敗,可能是 web 圖片的 base64 太長,超出了安卓的最大值
handleDown() {
            let _this = this
            Toast.loading({
                message: '生成海報...',
                forbidClick: true,
                duration: 0
            })
            setTimeout(() => {
                let img = _this.$refs['image']
                let isIos = navigator.appVersion.match(/(iphone|ipad|ipod)/gi) || false
                html2canvas(img, {
                    useCORS: true, // 允許圖片跨域
                    allowTaint: false,
                    scale: isIos ? 1 : 0.6,
                    taintTest: true,?
                    dpi: window.devicePixelRatio
                }).then(function(canvas) {
                    _this.photoUrl = canvas.toDataURL('image/png')
                    _this.downloadIamge(_this.photoUrl, 'poster.png')
                })
            }, 3000)
        },
        downloadIamge(imgsrc, name) {
            var image = new Image()
            var canvas = document.createElement('canvas')
            var context = canvas.getContext('2d')
            // 解決跨域 Canvas 污染問題
            image.setAttribute('crossOrigin', 'anonymous')
            image.src = imgsrc
            image.style.objectFit = 'contain'
            image.onload = () => {
                canvas.width = image.width
                canvas.height = image.height
                context.drawImage(image, 0, 0, image.width, image.height)
                // 得到圖片的base64編碼數(shù)據(jù)
                var url = canvas.toDataURL('image/png')
                let deleteString = 'data:image/png;base64,'
                var index = url.indexOf(deleteString)
                if (index === 0) {
                    let url2 = url.slice(deleteString.length)
                    utils.postMessage('save_image', url2) // 將圖片的base64字符串 傳給移動端
                    Toast.clear()
                } else {
                    this.$toast('保存失敗請重試')
                }
            }
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容