# [Canvas into pit 3] similar to the function of the magic wand in PS or paint bucket in drawing

Source: Internet
Author: User

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

Related Keywords:

### Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.