npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@roboflow/floodfill

v2.1.4

Published

Zero dependency canvas floodfill

Downloads

8

Readme

Floodfill

HTML5 Canvas Floodfill via JavaScript

Robobflow modifications:

Updated to support node-canvas. Added blurEdges to help with background erasing and randomHSL to add basic image augmentation support.

Usage example via https://blog.roboflow.ai/how-to-create-a-synthetic-dataset

How to use it:

Include it old-school

<script type="text/javascript" src="floodfill.js"></script>

Or use the minified version

<script type="text/javascript" src="floodfill.min.js"></script>

This adds the floodfill method to the global window context, and the method CanvasRenderingContext2d.prototype.fillFlood for idiomatic context invocation.

context.fillFlood

Like other context fill methods (such as context.fillRect), set the context.fillStyle beforehand to a color. Patterns and gradients are currently unsupported, but any color supported by CSS can be used. For example:

  • context.fillStyle = 'blue';
  • context.fillStyle = '#0000FF';
  • context.fillStyle = 'rgb(0,0,255)';
  • context.fillStyle = 'rgba(0,0,255,1.0)';

When the fillStyle is set, call the method with the following arguments: (x , y [,tolerance=0] [,left=0] [,top=0] [,right=context.width] [,bottom=context.height]).

The method signature is:

CanvasRenderingContext2.prototype.fillFlood = function(uint x, uint y, byte tolerance, uint left, uint top, uint right, uint bottom)

Here is an example of filling a canvas using the method above, using red as the color and starting the fill algorithm at (x=50,y=75) with a tolerance of 32:

var canvas = document.getElementById('canvas_circle');
var context = canvas.getContext('2d');
context.fillStyle = "rgba(255,0,0,1.0)";
context.fillFlood(50, 75, 32);

The method arguments are documented below:

uint x, uint y (required)

The x,y coordinates are the origin point in which to begin the fill operation. On an arbitrary canvas the coordinates 10,50 will be the 10th pixel from the left and the 50th pixel from the top. They must fit within the following boundaries: 0 <= x < width and 0 <= y < height

byte tolerance (optional, default=0)

The tolerance, which allows for the algorithm to fill similar pixels. The default is 0, which fills pixels exactly matching that of the starting x,y coordinate. The maximum of 254 bleeds the fill over all other pixels. Tip: A typical fill tolerance for anti-alias is 128.

uint left (optional, default=0)

It is possible to set a bounding box smaller than the full context area. The left argument will prevent the fill from occuring at any pixel coordinate with x less than this left value.

uint top (optional, default=0)

It is possible to set a bounding box smaller than the full context area. The top argument will prevent the fill from occuring at any pixel coordinate with y less than this top value.

uint right (optional, default=context.width)

It is possible to set a bounding box smaller than the full context area. The right argument will prevent the fill from occuring at any pixel coordinate with x greater than this right value.

uint bottom (optional, default=context.height)

It is possible to set a bounding box smaller than the full context area. The bottom argument will prevent the fill from occuring at any pixel coordinate with y greater than this bottom value.

floodfill

The global floodfill method is available to be called by passing in any Uint8ClampedArray of bytes to be filled. This allows for asyncronous operations on the data itself (for example in a webworker), without requiring the browser context object.

When calling the method above with a coordinate, color, context, boundaries, and tolerance, the context area starting at the coordinate will be filled with the color in an area of colors similar to that of the coordinate. The similarity is determined by the tolerance parameter.

Call the method with the following arguments: (data, x , y, color, ,tolerance ,width, height).

Note that this method has no defaults, and all values must be supplied. Additionally, width and height must match the dimensions of the data object, in the ratio data.length===widthheight4.

The method signature is:

function floodfill(Uint8ClampedArray data, uint x, uint y, rgba color, byte tolerance, uint width, uint height)

All the arguments are described below in detail:

Uint8ClampedArray data (required)

The data object is an Uint8ClampedArray view of a Typed Array. The array is one-dimensional, but is treated as a two-dimensional array of blocks of 4 bytes (each byte representing an RGBA color object). The two dimensions are demarcated by using the width and height arguments as described below.

uint x, uint y (required)

The x,y coordinates are the origin point in which to begin the fill operation. On an arbitrary canvas the coordinates 10,50 will be the 10th pixel from the left and the 50th pixel from the top. They must fit within the following boundaries: 0 <= x < width and 0 <= y < height

rgba color (required)

The rgba color argument, represented as {r:BYTE,g:BYTE,b:BYTE,a:BYTE} where BYTE is a number from 0 to 255, is the color that will be used to fill the data object.

byte tolerance (required)

The tolerance, which allows for the algorithm to fill similar pixels. The default is 0, which fills pixels exactly matching that of the starting x,y coordinate. The maximum of 254 bleeds the fill over all other pixels. Tip: A typical fill tolerance for anti-alias is 128.

uint width, uint height

The width and height of the two-dimensional construct that is bound to the data object. As the data object is a two dimensional array of 4 byte blocks representing RGBA pixels, the width and height must match the dimensions of the data object. The values are validated during method call to ensure the ratio data.length===width*height*4. A mismatch will result in an error being thrown.

Examples

See the code in examples/tests.html