To see the effect, go to http://philippica.github.io/and click Fill
This function actually realize very low, the earliest high reading black book when saw the FloodFill algorithm feel good magic, then thought this is not BFS it!! By the tall noun cheated has the wood has
So first set a threshold var threshold; its meaning is the same as the meaning of tolerances in Photoshop magic wand, when it equals 0 o'clock the function of drawing is paint bucket
Then proceed to BFS, starting with the mouse button down the point to do
Well, JS inside shift unshift POP push the functions on these four arrays naturally let the two data structures of queue and stack be realized ah there are wood, just do not know the time complexity of these functions implementation
Very low code:
varDX =NewArray (0, 0,-1, 1,-1,-1, 1, 1); varDY =NewArray (1,-1, 0, 0,-1, 1,-1, 1); varThreshold = 0; $(' #range '). attr ("value", 0); $(' #range '). Unbind (); $(' #range '). Change (function() {threshold= parseint ($ (' #range '). Val ()) * 4; }); $(' #myCanvas '). Unbind (); $(' #myCanvas '). Click (function(e) {varPpimgdata = context.getimagedata (0, 0, Ppcanvaswidth, ppcanvasheight); varMouseX = E.pagex- This. offsetleft; varMousey = E.pagey- This. OffsetTop; varFILLR = 0; varFILLG = 255; varFILLB = 255; varR = ppimgdata.data[(Mousey * ppcanvaswidth + mousex) * 4]; varG = ppimgdata.data[(Mousey * ppcanvaswidth + mousex) * 4 + 1]; varB = ppimgdata.data[(Mousey * ppcanvaswidth + mousex) * 4 + 2]; varPpqueue =NewArray (); varPpvisit =NewArray (); varPpfield =dx.length; Ppqueue.push (NewPppoint (MouseX, Mousey)); while(Ppqueue.length! = 0) { varU =Ppqueue.shift (); for(vari = 0; i < Ppfield; i++) { varX = u.x +Dx[i]; varY = U.y +Dy[i]; if(X < 0 | | Y < 0 | | X > Ppcanvaswidth | | Y > Ppcanvasheight | | Ppvisit[y * ppcanvaswidth + X])Continue; Ppvisit[y* Ppcanvaswidth + X] =true; if(Math.Abs (ppimgdata.data[(Y * ppcanvaswidth + X) * 4]-R) > Threshold)Continue; if(Math.Abs (ppimgdata.data[(Y * ppcanvaswidth + X) * 4 + 1]-G) > Threshold)Continue; if(Math.Abs (ppimgdata.data[(Y * ppcanvaswidth + X) * 4 + 2]-B) > Threshold)Continue; ppimgdata.data[(Y* ppcanvaswidth + X) * 4] =Fillr; ppimgdata.data[(Y* ppcanvaswidth + X) * 4 + 1] =Fillg; ppimgdata.data[(Y* ppcanvaswidth + X) * 4 + 2] =fillb; Ppqueue.push (NewPppoint (X, Y)); }} context.putimagedata (Ppimgdata,0, 0); });
[Canvas into pit 3] similar to the function of the magic wand in PS or paint bucket in drawing