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. python 計算兩個時間相差的分鐘數,超過一天時計算不對2. javascript - 使用form進行頁面跳轉,但是很慢,如何加一個Loading?3. javascript - 后臺管理系統左側折疊導航欄數據較多,怎么樣直接通過搜索去定位到具體某一個菜單項位置,并展開當前菜單4. javascript - ES6規范下 repeat 函數報錯 Invalid count value5. docker-machine添加一個已有的docker主機問題6. docker-compose中volumes的問題7. angular.js - 輸入郵箱地址之后, 如何使其自動在末尾添加分號?8. javascript - html5的data屬性怎么指定一個function函數呢?9. javascript - JS 里面的 delete object.key 到底刪除了什么?10. html5 - 為什么使使用vue cli 腳手架,post-css 沒有自動對css3屬性自動添加瀏覽器前綴呢?
