r/javaScriptStudyGroup • • May 15 '16

Check out my latest CodPen!

http://codepen.io/ForScale/full/rebPvL
2 Upvotes

28 comments sorted by

View all comments

Show parent comments

1

u/Volv May 17 '16

Now has (very) rudimentary fill function.
Was able to make this - Pic
 
Ctrl-click to fill colour. It will cause a stack overflow and fail disastrously on any regions bigger than about 10% of canvas :)
Need to come up with a less recursive flat function I think. Make it work first... time for optimization.
 
Codepen

2

u/ForScale May 17 '16

Ah! That's awesome!

I am having trouble getting the fill to work though... If I understand correctly, I'm supposed to hold ctrl and click the left mouse button within a closed off shape? I got it to work once, but I can't seem to get it to work again...

2

u/Volv May 18 '16

No longer breaks on big areas.
Next up - efficiency.
Codepen

2

u/ForScale May 18 '16

Ah, nice! Fill is working well now!

If you have a moment, would you mind explaining at a high level... like even higher than pseudo code; just kind of layman's terms... how the fill function works? I see the whole getImageData deal going on but I'm not too familiar with it. And I see the coordinates deal in there, but I'm having trouble putting it all together conceptually...

Irrespective of my lack of understanding, nice work!! :)

2

u/Volv May 18 '16 edited May 18 '16

Thanks. Was fun (if a bit tricky) working on a bigger problem.
I've added more comments throughout the fill part.
 
Algorithm:

  • Looks at the pixel you clicked. Say it was black.

  • Check pixels at all 8 surrounding points. top left, top, top right etc

  • If they match original colour (black) change their colour to fill colour

  • Add them to array to check next

  • Use result array to start next round of checking.

  • Uses mem object so it doesn't check previously checked

 
So spreads out from clicked point until it hits a significantly different colour then stops.

→ More replies (0)