Hello again
The past two weeks have really been very eventful as far as the coloring app goes.
I have spent most of my spring break writing code and I am not the least bit regretful… well perhaps a little bit. I at least managed to squeeze in a few flicks this week so not a total loss. I started this week with a very slow flood fill algorithm a few minor inconsistencies with clicking and several other short comings. As I mentioned In last week blog update though, I got the flood fill algorithm working very fast and I will explain how. Aside from that I have completely abandoned redraw because it is slow with flood fill. In addition to all this I rewrote all the event to work with jQuery mobile and even rigged up new toggle button for tools and menus to fix issues on the iPad. I also added fading scroll indication arrows to the scroll menu this week, cleaned up all the code and segmented each piece of functionality with comments.
So lets start with redraw and why we are not going to use it. Though I don’t doubt there is some way to make redraw efficient, I have found a much easier solution to the problem redraw had solved for me. Originally I switched to redrawing each event click so I could avoid drawing over the coloring outline by drawing after each click. This allowed the flood fill to interact with the outline image and anything drawn to screen as well. The problem was the redrawing of flood fill is very costly and after a few flood fills had been applied the code became sluggish. The solution was actually quite simple in the end. The flood fill algorithm has to check the canvas pixel memory directly during it process for speed, its very fast. The pixels data is divided into 4 bytes one for Red Green Blue and Alpha(Transparency). The flood fill algorithm has to compare RGB values for the drawing canvas so it knows what to color, so why not make comparison to the outline canvas at the same time, it certainly would be more efficient than redrawing. Well that’s exactly what I did, I wrote a simple method that just checks the alpha value for the outline canvas and added the necessary checks to the algorithm.
That leads me to the topic of pixel memory access. As it turns out, it’s easy to access the pixel memory it just can be a bit confusing. So you can get pixel data by calling getImageData() on the context of your canvas. This gives you a pixel at an x and y coordinates. The pixel memory is ordered top to bottom left to right. Imagine a 9 pixel canvas ordered like this.
X and Y Memory Address
00 01 02 0 12 24
10 11 12 = 4 16 28
20 21 22 8 20 32
If you are at the pixel memory address of 12 you can traverse to 24 with (y * widthOfGrid + x) *4) because at memory address 12, x and y = (0, 1) so you can see that(1* 3 + 0) * 4 = 12 so Our location + (y * widthOfGrid + x) *4) brings us to the pixel to our right and like wise you would subtract to move to the left. That is the secret to fast flood fill algorithms in javascript. That and don’t use recursion due to stack limitation but that’s a whole different topic.
A few other small changes where the size menu was set to close when you clicked the button but not the menu itself. It was suggested that that would be better so that was fixed right away this week along with a few other changes to buttons. The Worcester Art Museum had also suggested disappearing scroll arrows would be nice for the coloring outline selection menu, so I did that this week too. I found an easy tutorial at jsfiddle.net that worked quite well, I modified it to work with two arrows and that was that. I also did a lot of work rigging and making toggle buttons for the tools size and color menus.
I think that about wraps it up, Next week spray paint!
Till next time.
Jason Hintlian
From the blog jasonhintlian » cs-wsu by jasonhintlian and used with permission of the author. All other rights reserved by the author.