html5 - canvas 跨域問題
問題描述
在微信上給用戶修改頭像的時候,用canvas來截圖。結果報錯: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’; }
問題解答
回答1:初步看了下代碼貌似沒什么問題的,排除掉圖片可能存在的跨域問題,還有一個問題樓主可以查看下就是getImageData 的傳參,需要是number類型,樓主可以先使用
console.log(typeof x, typeof y, typeof width, typeof height)
來看看
回答2:應該不是跨域吧,跨域會寫 The canvas has been tainted by cross-origin data
console.log一下getImageData的參數吧。The provided double value is non-finite有可能是吧string當數傳進來了。
相關文章:
1. javascript - h5 video層級太高導致浮在div上面,如何解決?2. mysql ER_BAD_DB_ERROR: Unknown database ’test’3. linux - Ubuntu下編譯Vim8(+python)無數次編譯失敗4. python - pyspider爬pdf爬了一小段時間后就不動了5. mysql - 記得以前在哪里看過一個估算時間的網站6. 如何合并兩張具有相同結構的mysql表7. javascript - Ajax加載Json時,移動端頁面向左上角縮小一截兒,加載完成后才正常顯示,這該如何解決?8. python中怎么對列表以區間進行統計?9. css - 請問B站頂部的模糊半透明導航條是怎么實現的呢?10. python運行后沒有任何反饋要怎么排查
