UNPKG

imagerot

Version:

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

39 lines (38 loc) 1.81 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.noise = void 0; const defaultIntensity = 10; const pixelOp = ({ index, data }, options = null) => { const { intensity = defaultIntensity, ratio = null } = (options || {}); const intensityRatio = ratio ? ratio : intensity / 100; for (let j = 0; j < 3; j++) { const noise = Math.random() * 2 - 1; const adjustedNoise = noise * intensityRatio; const newColorChannelValue = data[index + j] + adjustedNoise * 255; data[index + j] = Math.max(0, Math.min(255, newColorChannelValue)); } }; const global = (_a, ...args_1) => __awaiter(void 0, [_a, ...args_1], void 0, function* ({ data }, options = null) { const { intensity = defaultIntensity, ratio = null } = (options || {}); const intensityRatio = ratio ? ratio : intensity / 100; for (let i = 0; i < data.length; i += 4) { pixelOp({ index: i, data }, { ratio: intensityRatio }); } return data; }); const noise = { name: 'noise', browser: global, node: global, pixelOp: pixelOp }; exports.noise = noise;