样本图像像素颜色

时间:2016-06-08 19:58:43

标签: javascript canvas

我目前正在尝试使用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],绝不是其他任何东西(即使图像上没有单个黑色像素)

0 个答案:

没有答案