Get pixel color from canvas with HTML


To get the pixel color from canvas, use the following code. This returns the color in rgba −

var index = (Math.floor(y) * canvasWidth + Math.floor(x)) * 4

// color in rgba
var r = data[index]
var g = data[index + 1]
var b = data[index + 2]
var a = data[index + 3]

Updated on: 04-Mar-2020

212 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements