UNPKG

image-js

Version:

Image processing and manipulation in JavaScript

44 lines 1.88 kB
import robustPointInPolygon from 'robust-point-in-polygon'; import { arrayPointsToObjects } from '../utils/arrayPointsToObjects.js'; import { getOutputImage } from '../utils/getOutputImage.js'; import { setBlendedVisiblePixel } from '../utils/setBlendedVisiblePixel.js'; import checkProcessable from '../utils/validators/checkProcessable.js'; import { validateColor } from '../utils/validators/validators.js'; import { deleteDuplicates } from './utils/deleteDuplicates.js'; /** * Draw a polygon defined by an array of points onto an image. * @param image - Image to process. * @param points - Polygon vertices. * @param options - Draw Line options. * @returns The image with the polygon drawing. */ export function drawPolygonOnImage(image, points, options = {}) { const { fillColor, origin = { column: 0, row: 0 }, ...otherOptions } = options; checkProcessable(image, { bitDepth: [8, 16], }); const newImage = getOutputImage(image, options, { clone: true }); if (fillColor === undefined) { return newImage.drawPolyline([...points, points[0]], { origin, ...otherOptions, }); } else { validateColor(fillColor, newImage); const filteredPoints = deleteDuplicates(points); const arrayPoints = arrayPointsToObjects(filteredPoints); for (let row = 0; row < newImage.height; row++) { for (let column = 0; column < newImage.width; column++) { if (robustPointInPolygon(arrayPoints, [column, row]) === -1) { setBlendedVisiblePixel(newImage, Math.round(origin.column) + column, Math.round(origin.row) + row, fillColor); } } } } return newImage.drawPolyline([...points, points[0]], { origin, ...otherOptions, }); } //# sourceMappingURL=drawPolygonOnImage.js.map