JavaScript implementation of a flood-fill algorithm