I have this discussion which indicates how to make a pencil tool.
How can I detect whether the mouse is hovering on drawn area/points/images using the pencil tool? T
You can get the (per pixel) image data from a canvas: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getImageData. Then check if the mouse is hovering over a green pixel.