q-floodfill
Optimized, non-recursive flood fill algorithm using a scan line search.
Implemented in TypeScript. Zero dependencies and bundle size around 1.8k (gzipped and minified).
Demo - https://pavelkukov.github.io/q-floodfill/
🙌 Acknowledgments
The work here is heavily inspired from QuickFill algorithm by John R. Shaw.
https://www.codeproject.com/Articles/6017/QuickFill-An-Efficient-Flood-Fill-Algorithm
📈 Performance
Needs ~30-40ms to fill 800x660 canvas. For comparison, wasm-flood-fill needs ~50-60ms for the same operation.
🧩 Installing
npm install --save q-floodfill
✔ Usage
import FloodFill from 'q-floodfill'
const context = canvas.getContext('2d')
const imgData = context.getImageData(0, 0, canvas.width, canvas.height)
const floodFill = new FloodFill(imgData)
floodFill.fill(fillColor, x, y, 0)
context.putImageData(floodFill.imageData, 0, 0)
Options
- Get modified pixels count
const floodFill = new FloodFill(imgData)
floodFill.fill(fillColor, x, y, 0)
const count = floodFill.modifiedPixelsCount
- Collect modified pixels. This option is having a significant impact on performance.
const floodFill = new FloodFill(imgData)
floodFill.collectModifiedPixels = true
floodFill.fill(fillColor, x, y, 0)
const pixels = floodFill.modifiedPixels
Useful exports
{
isSameColor,
setColorAtPixel,
getColorAtPixel,
colorToRGBA,
hex2RGBA,
ColorRGBA,
}
🧾 Notes
👋 Author
Pavel Kukov pavelkukov@gmail.com
© LICENSE (MIT)
See LICENSE.txt in the root directory
Similar packages