UNPKG

imagerot

Version:

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

46 lines (45 loc) 2.21 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.channelShift = void 0; const global = (_a, ...args_1) => __awaiter(void 0, [_a, ...args_1], void 0, function* ({ data, width, height }, options = null) { var _b, _c, _d; const config = (options || {}); const rShift = (_b = config.redShift) !== null && _b !== void 0 ? _b : { x: 5, y: 0 }; const gShift = (_c = config.greenShift) !== null && _c !== void 0 ? _c : { x: -5, y: 0 }; const bShift = (_d = config.blueShift) !== null && _d !== void 0 ? _d : { x: 0, y: 5 }; const temp = new Uint8Array(data); for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { const idx = (y * width + x) * 4; // Shift RED const rx = (x + rShift.x + width) % width; const ry = (y + rShift.y + height) % height; data[idx] = temp[(ry * width + rx) * 4]; // Shift GREEN const gx = (x + gShift.x + width) % width; const gy = (y + gShift.y + height) % height; data[idx + 1] = temp[(gy * width + gx) * 4 + 1]; // Shift BLUE const bx = (x + bShift.x + width) % width; const by = (y + bShift.y + height) % height; data[idx + 2] = temp[(by * width + bx) * 4 + 2]; // Alpha remains unchanged } } return data; }); const channelShift = { name: 'channelShift', browser: global, node: global }; exports.channelShift = channelShift;