我目前正在尝试使用HTML5 Canvas从图像中获取随机像素颜色。
var sampleCanvas = document.createElement('canvas');
sampleCanvas.width = 50;
sampleCanvas.height = 50;
sampleContext = sampleCanvas.getContext('2d');
var sampleImage = new Image();
sampleImage.onload = function() {
sampleContext.drawImage(sampleImage, 0, 0);
}
sampleImage.src = 'canvas_sample.png';
var calculatedFill = sampleContext.getImageData(Math.random() * sampleCanvas.width, Math.random() * sampleCanvas.height, 1, 1).data;
然而,calculatedFill始终是[0, 0, 0, 0]
,绝不是其他任何东西(即使图像上没有单个黑色像素)