html5 - canvas 跨域問(wèn)題
問(wèn)題描述
在微信上給用戶修改頭像的時(shí)候,用canvas來(lái)截圖。結(jié)果報(bào)錯(cuò):Owechat_login.js:226 Uncaught TypeError: Failed to execute ’getImageData’ on ’CanvasRenderingContext2D’: The provided double value is non-finite.代碼:function cropImage(targetCanvas, x, y, width, height) {
var targetctx = targetCanvas.getContext(’2d’);var targetctxImageData = targetctx.getImageData(x, y, width, height); // sx, sy, sWidth, sHeight var c = document.createElement(’canvas’);var ctx = c.getContext(’2d’); c.width = width;c.height = height; ctx.rect(0, 0, width, height);ctx.fillStyle = ’white’;ctx.fill();ctx.putImageData(targetctxImageData, 0, 0); // imageData, dx, dy document.getElementById(’image’).src = c.toDataURL(’image/jpeg’, 0.92);document.getElementById(’image’).style.display = ’initial’; }
問(wèn)題解答
回答1:初步看了下代碼貌似沒(méi)什么問(wèn)題的,排除掉圖片可能存在的跨域問(wèn)題,還有一個(gè)問(wèn)題樓主可以查看下就是getImageData 的傳參,需要是number類(lèi)型,樓主可以先使用
console.log(typeof x, typeof y, typeof width, typeof height)
來(lái)看看
回答2:應(yīng)該不是跨域吧,跨域會(huì)寫(xiě) The canvas has been tainted by cross-origin data
console.log一下getImageData的參數(shù)吧。The provided double value is non-finite有可能是吧string當(dāng)數(shù)傳進(jìn)來(lái)了。
相關(guān)文章:
1. PHPExcel表格導(dǎo)入數(shù)據(jù)庫(kù)怎么導(dǎo)入2. macos - 無(wú)法source activate python273. 預(yù)訂金和尾款分別支付4. thinkphp6使用驗(yàn)證器 信息如何輸出到前端頁(yè)面5. empty比isset更嚴(yán)格一點(diǎn)6. javascript - 微信網(wǎng)頁(yè)開(kāi)發(fā)從菜單進(jìn)入頁(yè)面后,按返回鍵沒(méi)有關(guān)閉瀏覽器而是刷新當(dāng)前頁(yè)面,求解決?7. 我在導(dǎo)入模板資源時(shí)遇到無(wú)法顯示的問(wèn)題,請(qǐng)老師解答下8. python - 調(diào)用api輸出頁(yè)面,會(huì)有標(biāo)簽出現(xiàn),請(qǐng)問(wèn)如何清掉?9. 運(yùn)行python程序時(shí)出現(xiàn)“應(yīng)用程序發(fā)生異常”的內(nèi)存錯(cuò)誤?10. javascript - h5微信中怎么禁止橫屏

網(wǎng)公網(wǎng)安備