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. java - 自己制作一個視頻播放器,遇到問題,用的是內置surfaceview類,具體看代碼!2. 服務器端 - 采用nginx做web服務器,C++開發應用程序 出現拒絕連接請求?3. javascript - vue 移動端的input 數字輸入優化4. javascript - 有什么兼容性比較好的辦法來判斷瀏覽器窗口的類型?5. 關于docker下的nginx壓力測試6. 為什么我ping不通我的docker容器呢???7. javascript - Angular controlller控制域和原生js的關系8. python - pandas按照列A和列B分組,將列C求平均數,怎樣才能生成一個列A,B,C的dataframe9. javascript - nidejs環境設置操作一直出現這種問題怎么解決?10. javascript - npm start 運行’webpack-dev-server’報錯 Cannot find module ’webpack’
