By William Malone: C P B T Html5 J S
By William Malone: C P B T Html5 J S
0,0,0,255, 255,255,255,255,
203,53,148,255].
CREATE A PAINT BUCKET
TOOL IN HTML5 AND
JAVASCRIPT
// imageData.data = [
// 255,255,255,255, 255,255,255,255,
0,0,0,255,
// 255,255,255,255, 255,255,255,255,
255,255,255,255,
// 0,0,0,255, 255,255,255,255,
255,255,255,255,
var imgData = context.getImageData(0, 0, // 255,255,255,255, 255,255,255,255,
1, 1); 255,255,255,255,
http://slidepdf.com/reader/full/create-a-paint-bucket-tool-in-html5-and-javascript 1/5
7/31/2019 Create a Paint Bucket Tool in HTML5 and JavaScript
Now that we have demonstrated how we will get pixel newPos = pixelStack.pop();
data from the HTML5 Canvas via the imageData object x = newPos[0];
we will go step by step through the process of a flood y = newPos[1];
fill algorithm implemented with JavaScript.
...
The following animation shows the steps of the flood fill
algorithm on our example image:
}
function matchStartColor(pixelPos)
{
var r = colorLayer.data[pixelPos];
var g = colorLayer.data[pixelPos+1];
var b = colorLayer.data[pixelPos+2];
Let's start by coloring our first pixel at (1,0) with the fill
color purple.
while(pixelStack.length)
{
http://slidepdf.com/reader/full/create-a-paint-bucket-tool-in-html5-and-javascript 2/5
7/31/2019 Create a Paint Bucket Tool in HTML5 and JavaScript
pixelPos += canvasWidth * 4;
++y;
reachLeft = false;
reachRight = false;
while(y++ < canvasHeight-1 && if(x < canvasWidth-1)
matchStartColor(pixelPos)) {
{ if(matchStartColor(pixelPos + 4))
colorPixel(pixelPos); {
if(!reachRight)
{
... pixelStack.push([x + 1, y]);
} reachRight = true;
}
}
After filling the pixel at (1,0) we must now determine if
else if(reachRight)
its neighbors to the left and right need filling too. First
let's investigate the pixel to the left. {
reachRight = false;
Ignoring any pixels with x position less than zero we }
check if the color matches the start color. In our }
example the pixel to the left is white just like our start
pixel, so we add it to the stack to handle later. We also pixelPos += canvasWidth * 4;
set the boolean reachLeft to true. This will prevent
us adding pixels that will eventually handled by the
downward march of the pixel we just added. The pixel below matches our starting pixel so we color
it purple.
if(x > 0)
{ We look to the left of our new purple pixel and find it
if(matchStartColor(pixelPos - 4)) matches, but reachLeft is true so we don't add it to
{ the stack. The right pixel also matches but its
if(!reachLeft){ variable reachRight is false so we do add it to the
pixelStack.push([x - 1, y]); stack.
reachLeft = true;
}
}
else if(reachLeft)
{
reachLeft = false;
}
}
http://slidepdf.com/reader/full/create-a-paint-bucket-tool-in-html5-and-javascript 3/5
7/31/2019 Create a Paint Bucket Tool in HTML5 and JavaScript
Color the next pixel. Look to the left, no match this time
so we change reachLeft to false. Looking to the
right matches but reachRight is true so we do
nothing.
We have reached the bottom of the image thus are
done with the column of our starting pixel.
http://slidepdf.com/reader/full/create-a-paint-bucket-tool-in-html5-and-javascript 4/5
7/31/2019 Create a Paint Bucket Tool in HTML5 and JavaScript
Eight pixels later we have filled all the matching pixels reachLeft = false;
in our example image. Although our example image }
was small the process is the same for larger images. }
pixelPos += canvasWidth * 4;
}
}
context.putImageData(colorLayer, 0, 0);
function matchStartColor(pixelPos)
{
var r = colorLayer.data[pixelPos];
var g = colorLayer.data[pixelPos+1];
var b = colorLayer.data[pixelPos+2];
http://slidepdf.com/reader/full/create-a-paint-bucket-tool-in-html5-and-javascript 5/5