imagerot
Version:
A lightweight, cross-environment image library for applying unique effects via raw image buffers.
43 lines (42 loc) • 2.33 kB
JavaScript
;
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.datamosh = void 0;
const datamosh = (_a, ...args_1) => __awaiter(void 0, [_a, ...args_1], void 0, function* ({ data, width, height }, options = {}) {
var _b, _c;
const blockSize = (_b = options.blockSize) !== null && _b !== void 0 ? _b : 16;
const intensity = (_c = options.intensity) !== null && _c !== void 0 ? _c : 0.2;
const tempData = new Uint8Array(data); // Copy for source
for (let by = 0; by < height; by += blockSize) {
for (let bx = 0; bx < width; bx += blockSize) {
if (Math.random() > intensity)
continue; // Skip most blocks
// Random offset for this block
const offsetX = Math.floor(Math.random() * width - bx);
const offsetY = Math.floor(Math.random() * height - by);
// Copy shifted block
const actualW = Math.min(blockSize, width - bx);
const actualH = Math.min(blockSize, height - by);
for (let y = 0; y < actualH; y++) {
for (let x = 0; x < actualW; x++) {
const srcIdx = (((by + y + offsetY + height) % height) * width + (bx + x + offsetX + width) % width) * 4;
const destIdx = ((by + y) * width + (bx + x)) * 4;
data[destIdx] = tempData[srcIdx];
data[destIdx + 1] = tempData[srcIdx + 1];
data[destIdx + 2] = tempData[srcIdx + 2];
data[destIdx + 3] = tempData[srcIdx + 3];
}
}
}
}
return data;
});
exports.datamosh = datamosh;