UNPKG

konva

Version:

HTML5 2d canvas library for interactive graphics, design editors, whiteboards, and diagrams.

33 lines (32 loc) 1.14 kB
import { Factory } from "../Factory.js"; import { Node } from "../Node.js"; import { getNumberValidator } from "../Validators.js"; /** * Threshold Filter. Pushes any value above the mid point to * the max and any value below the mid point to the min. * This affects the alpha channel. * @function * @name Threshold * @memberof Konva.Filters * @param {Object} imageData * @author ippo615 * @example * node.cache(); * node.filters([Konva.Filters.Threshold]); * node.threshold(0.1); */ export const Threshold = function (imageData) { const level = this.threshold() * 255, data = imageData.data, len = data.length; for (let i = 0; i < len; i += 1) { data[i] = data[i] < level ? 0 : 255; } }; Factory.addGetterSetter(Node, 'threshold', 0.5, getNumberValidator(), Factory.afterSetFilter); /** * get/set threshold. With {@link Konva.Filters.Threshold} it is a value between 0 and 1, default 0.5. With {@link Konva.Filters.Mask} it is the colour distance to the background, 0 to 255, default 10. * @name threshold * @method * @memberof Konva.Node.prototype * @param {Number} threshold * @returns {Number} */