我正在尝试运行以下命令,但它显示“无法在'CanvasRenderingContext2D'上执行'getImageData':画布已被跨域数据污染。” 谁能帮我解决这个问题?运行正常,现在我正在尝试,并且发生了错误。
function doit(){
// convert image to convas
var img = document.getElementById('my-image');
var canvas = document.createElement('canvas');
// Get convas information
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
// Find the click pixel color information
var currentPixelData = canvas.getContext('2d').getImageData(event.offsetX, event.offsetY, 1, 1).data;
imgObj.setAttribute('crossOrigin', '');
// show the color information in box
document.getElementById('colorstat').innerHTML = "Chosed Color specification:" + "<br> R : " + currentPixelData[0] + " G: " + currentPixelData[1] + " B: " + currentPixelData[2] + " Alpha : " +currentPixelData[3] + "<br>Image size: " + img.width + "x" + img.height + "<br>Waiting ...";
// Start counting the occurance of the color in all pixels (Could take time)
setTimeout(function() {
colorCounter = 0;
totaltxt = "";
for (i=0;i<=img.width;i++)
{
for (j=0;j<=img.height;j++)
{
var PixelData = canvas.getContext('2d').getImageData(j, i, 1, 1).data;
if(JSON.stringify(PixelData)==JSON.stringify(currentPixelData))
{colorCounter += 1;}
}
}
document.getElementById('colorstat').innerHTML = "Chosed Color specification:" + "<br> R : " + currentPixelData[0] + " G: " + currentPixelData[1] + " B: " + currentPixelData[2] + " Alpha : " +currentPixelData[3] + "<br>Image size: " + img.width + "x" + img.height + "<br>Total Repeat: " + colorCounter + "<br> Portion: " + colorCounter/(img.width*img.height)*100 + "%";
}, 200);
}