UNPKG

konva

Version:

<p align="center"> <img src="https://raw.githubusercontent.com/konvajs/konvajs.github.io/master/apple-touch-icon-180x180.png" alt="Konva logo" height="180" /> </p>

64 lines (63 loc) 2.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Factory_1 = require("../Factory"); var Util_1 = require("../Util"); var Node_1 = require("../Node"); var Validators_1 = require("../Validators"); exports.Pixelate = function (imageData) { var pixelSize = Math.ceil(this.pixelSize()), width = imageData.width, height = imageData.height, x, y, i, red, green, blue, alpha, nBinsX = Math.ceil(width / pixelSize), nBinsY = Math.ceil(height / pixelSize), xBinStart, xBinEnd, yBinStart, yBinEnd, xBin, yBin, pixelsInBin; imageData = imageData.data; if (pixelSize <= 0) { Util_1.Util.error('pixelSize value can not be <= 0'); return; } for (xBin = 0; xBin < nBinsX; xBin += 1) { for (yBin = 0; yBin < nBinsY; yBin += 1) { red = 0; green = 0; blue = 0; alpha = 0; xBinStart = xBin * pixelSize; xBinEnd = xBinStart + pixelSize; yBinStart = yBin * pixelSize; yBinEnd = yBinStart + pixelSize; pixelsInBin = 0; for (x = xBinStart; x < xBinEnd; x += 1) { if (x >= width) { continue; } for (y = yBinStart; y < yBinEnd; y += 1) { if (y >= height) { continue; } i = (width * y + x) * 4; red += imageData[i + 0]; green += imageData[i + 1]; blue += imageData[i + 2]; alpha += imageData[i + 3]; pixelsInBin += 1; } } red = red / pixelsInBin; green = green / pixelsInBin; blue = blue / pixelsInBin; alpha = alpha / pixelsInBin; for (x = xBinStart; x < xBinEnd; x += 1) { if (x >= width) { continue; } for (y = yBinStart; y < yBinEnd; y += 1) { if (y >= height) { continue; } i = (width * y + x) * 4; imageData[i + 0] = red; imageData[i + 1] = green; imageData[i + 2] = blue; imageData[i + 3] = alpha; } } } } }; Factory_1.Factory.addGetterSetter(Node_1.Node, 'pixelSize', 8, Validators_1.getNumberValidator(), Factory_1.Factory.afterSetFilter);