問題描述利用canvas改變圖片的顏色, 谷歌運(yùn)行正常, 火狐報(bào)錯(cuò)IndexSizeError: Index or size is negative or greater than the allowed amount問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法相關(guān)代碼// 請把代碼文本粘貼到下方(請勿用圖片代替代碼)revertColor(type) { let config = { attention: { url: this.local.myAttention.map(it => it.dataIconUrl), className: '.myAttention-icon', size: 32, padding: 4 }, dataProduct: { url: this.local.dataProductList.map(it => it.productIcon), className: '.dataProduct-icon', size: 45.6, padding: 0 } }; let {url, className, size, padding} = config[type]; size = Math.floor(size) url = url.slice(0, 8); (Array.from($(className)) || []).forEach((node, idx) => { let canvas = node.getContext('2d'); let img = new Image(); img.src = url[idx]; img.onload = function () { canvas.drawImage(img, padding, padding, size, size); let imgData = canvas.getImageData(padding, padding, img.width, img.height) || {}; let colorData = imgData.data; for (let i = 0; i < colorData.length; i += 4) { if (!(colorData[i] === 75 && colorData[i + 1] === 164 && colorData[i + 2] === 190)) { // 將除底色以外的所有顏色重置 colorData[i] = 255; colorData[i + 1] = 255; colorData[i + 2] = 255; } } canvas.clearRect(0, 0, $(className).width(), $(className).height()); canvas.putImageData(imgData, padding, padding); } });}你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?火狐瀏覽器發(fā)現(xiàn)圖片根本沒有被繪制上去, 應(yīng)該是drawImage的時(shí)候就已經(jīng)出問題了
火狐報(bào)錯(cuò) Index or size is negative...
梵蒂岡之花
2019-03-21 14:19:09