UNPKG

imagerot

Version:

A lightweight, cross-environment image library for applying unique effects via raw image buffers.

64 lines (63 loc) 3.63 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.rectangles = void 0; const math_1 = require("../../../constants/math"); const defaultOffset = 5; const defaultIntensity = 10; const defaultSizeModifier = 1; const defaultInvertChance = 0.15; const minDimensionFraction = 0.25; const minRectangularity = 1.5; const global = (_a, ...args_1) => __awaiter(void 0, [_a, ...args_1], void 0, function* ({ data, width, height }, options = null) { const { offset = defaultOffset, intensity = defaultIntensity, sizeModifier = defaultSizeModifier, invertChance = defaultInvertChance } = (options || {}); const newData = new Uint8Array(data); const numRects = intensity; const baseRectDim = (0, math_1.floor)((0, math_1.max)(20, (0, math_1.min)(width, height) / 10) * sizeModifier); for (let i = 0; i < numRects; i++) { let rectWidth, rectHeight; do { rectWidth = (0, math_1.floor)(minDimensionFraction * baseRectDim + (0, math_1.random)() * (1 - minDimensionFraction) * baseRectDim); rectHeight = (0, math_1.floor)(minDimensionFraction * baseRectDim + (0, math_1.random)() * (1 - minDimensionFraction) * baseRectDim); } while ((0, math_1.max)(rectWidth, rectHeight) / (0, math_1.min)(rectWidth, rectHeight) < minRectangularity); const rectX = (0, math_1.floor)((0, math_1.random)() * (width - rectWidth)); const rectY = (0, math_1.floor)((0, math_1.random)() * (height - rectHeight)); const displacementX = (0, math_1.floor)(((0, math_1.random)() - 0.5) * 2 * offset); const displacementY = (0, math_1.floor)(((0, math_1.random)() - 0.5) * 2 * offset); const brightness = 5 + (0, math_1.floor)((0, math_1.random)() * 15) * ((0, math_1.random)() > 0.5 ? 1 : -1); const invertColors = (0, math_1.random)() < invertChance; for (let y = rectY; y < rectY + rectHeight; y++) { for (let x = rectX; x < rectX + rectWidth; x++) { const origIndex = (y * width + x) * 4; const displacedIndex = (((y + displacementY + height) % height) * width + ((x + displacementX + width) % width)) * 4; for (let j = 0; j < 3; j++) { let adjustedPixel = data[displacedIndex + j] + brightness; if (invertColors) { adjustedPixel = 255 - adjustedPixel; } adjustedPixel = (0, math_1.max)(0, (0, math_1.min)(255, adjustedPixel)); if (invertColors && adjustedPixel < 50) { continue; } newData[origIndex + j] = adjustedPixel; } newData[origIndex + 3] = data[displacedIndex + 3]; } } } return newData; }); const rectangles = { name: 'rectangles', browser: global, node: global }; exports.rectangles = rectangles;