Javascript 帆布及;彩色图像索引大小

Javascript 帆布及;彩色图像索引大小,javascript,angularjs,canvas,color-thief,Javascript,Angularjs,Canvas,Color Thief,我用它来分析图像的颜色。我正在使用画布和getImageData,在控制台中收到以下错误消息: Error: Index or size is negative or greater than the allowed amount CanvasImage.prototype.getImageData@http://localhost:3000/js/color-thief.js:51:12 ColorThief.prototype.getPalette@http://localhost:3000

我用它来分析图像的颜色。我正在使用画布和
getImageData
,在控制台中收到以下错误消息:

Error: Index or size is negative or greater than the allowed amount
CanvasImage.prototype.getImageData@http://localhost:3000/js/color-thief.js:51:12
ColorThief.prototype.getPalette@http://localhost:3000/js/color-thief.js:109:22
ColorThief.prototype.getColor@http://localhost:3000/js/color-thief.js:75:25
colorController/this.getColors@http://localhost:3000/js/ang.js:20:22
我不确定这是否是一个画布问题来自图像像素大小,或者是另一个库中的某个bug(我也在使用AngularJS),但我被卡住了,不确定我能做什么

我认为像素计数为0有一个问题,当我在color-thift.js中添加
console.log
行到下面的代码中时,一些图像的像素计数为“0”

关键点-->image.width和
image.height
仅对某些图像显示0,而其余图像显示的比率与1280 922或2000 1333类似

var CanvasImage = function (image) {
    this.canvas  = document.createElement('canvas');
    this.context = this.canvas.getContext('2d');

    document.body.appendChild(this.canvas);

    this.width  = this.canvas.width  = image.width;
    this.height = this.canvas.height = image.height;

    console.log(image.width, image.height);

    this.context.drawImage(image, 0, 0, this.width, this.height);
};

知道为什么会发生这种情况吗?

答案是添加
image.addEventListener(“load”,function(){…}
下载代码以正确加载图像。

我试图弄清楚为什么一些图像显示索引错误,而其他图像则没有。到目前为止,jpg或png、文件大小或分辨率维度之间似乎没有分离……所有情况下都有工作的图像和不工作的图像。另一个奇怪的事实是:在清理本地主机cach时e和加载页面时,100%的图像都会出现错误。如果刷新,网站只会显示部分错误。这是随机的,这意味着在刷新时,索引错误可能会随机更改哪些词和哪些词不存在。如果有任何帮助,我们将不胜感激。您在哪里将图像传递到函数?是否正在等待加载前夕nt?卸载的图像大小为零。