It will be very similar to Flood fill . If you decide to implement one of the related algorithms, you should use a non-recursive queue based on JavaScript, as it is not very good for recursive calls. I did not implement Flood filling myself, but it is very similar to Magic Wand.
Jonas source share