Get a pixel from HTML Canvas?

后端 未结 10 2098
情深已故
情深已故 2020-11-22 12:41

Is it possible to query a HTML Canvas object to get the color at a specific location?

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 13:25

    // Get pixel data
    var imageData = context.getImageData(x, y, width, height);
    
    // Color at (x,y) position
    var color = [];
    color['red'] = imageData.data[((y*(imageData.width*4)) + (x*4)) + 0];
    color['green'] = imageData.data[((y*(imageData.width*4)) + (x*4)) + 1];
    color['blue'] = imageData.data[((y*(imageData.width*4)) + (x*4)) + 2];
    color['alpha'] = imageData.data[((y*(imageData.width*4)) + (x*4)) + 3];
    

提交回复
热议问题