@thi.ng/rasterize
Version:
Headless 2D shape drawing, filling & rasterization for arbitrary targets/purposes (no canvas required)
30 lines (29 loc) • 875 B
JavaScript
import { isIterable } from "@thi.ng/checks/is-iterable";
import { isPrimitive } from "@thi.ng/checks/is-primitive";
import { equiv } from "@thi.ng/equiv";
import { floodFill as $fill } from "@thi.ng/grid-iterators/flood-fill";
import { isInBounds2D } from "./checks.js";
import { __draw2D } from "./draw.js";
const floodFill = (grid, x, y, val) => isInBounds2D(grid, x, y) ? __draw2D(
$fill(__pred(grid, x, y), x, y, grid.size[0], grid.size[1]),
grid,
val
) : grid;
const __pred = (img, x, y) => {
const {
data,
offset,
stride: [stride, rowStride]
} = img;
let srcVal = img.getAtUnsafe(x, y);
if (isPrimitive(srcVal)) {
return (x2, y2) => data[offset + x2 * stride + y2 * rowStride] === srcVal;
}
if (isIterable(srcVal)) {
srcVal = [...srcVal];
}
return (x2, y2) => equiv(img.getAtUnsafe(x2, y2), srcVal);
};
export {
floodFill
};