0

I'm currently trying to get a random pixel colour from an image using 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;

However, calculatedFill is always [0, 0, 0, 0], never anything else (even though there isn't a single black pixel on the image)

j08691
  • 204,283
  • 31
  • 260
  • 272
user2036108
  • 1,227
  • 1
  • 10
  • 12

0 Answers0