javascript / jquery getImageData颜色选择图像

时间:2013-07-31 09:03:05

标签: javascript jquery image colors getimagedata

我有这个小提琴:http://jsfiddle.net/R6rGp/

我尝试拾取图像/画布的颜色,但我总是得到RGB 000。

但显然,图像没有真正的黑色像素。这有什么问题?

document.getElementById("canvas").onload=function(){
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var img=document.getElementById("canvas");
ctx.drawImage(img,0,0);
var imgData=ctx.getImageData(0,0,c.width,c.height);

red=imgData.data[0];
green=imgData.data[1];
blue=imgData.data[2];
alpha=imgData.data[3];

console.log(red + " " + green + " " + blue + " " + alpha); //always 0 0 0


ctx.putImageData(imgData,0,0);


};

1 个答案:

答案 0 :(得分:1)

jsfiddle.net不允许跨源数据:

Unable to get image data from canvas because the canvas has been tainted by cross-origin data.

似乎您的脚本中还存在另一个问题:

Uncaught Error: SecurityError: DOM Exception 18