Path: blob/master/node_modules/@jimp/plugin-mask/es/index.js
1126 views
"use strict";12Object.defineProperty(exports, "__esModule", {3value: true4});5exports["default"] = void 0;67var _utils = require("@jimp/utils");89/**10* Masks a source image on to this image using average pixel colour. A completely black pixel on the mask will turn a pixel in the image completely transparent.11* @param {Jimp} src the source Jimp instance12* @param {number} x the horizontal position to blit the image13* @param {number} y the vertical position to blit the image14* @param {function(Error, Jimp)} cb (optional) a callback for when complete15* @returns {Jimp} this for chaining of methods16*/17var _default = function _default() {18return {19mask: function mask(src) {20var x = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;21var y = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;22var cb = arguments.length > 3 ? arguments[3] : undefined;2324if (!(src instanceof this.constructor)) {25return _utils.throwError.call(this, 'The source must be a Jimp image', cb);26}2728if (typeof x !== 'number' || typeof y !== 'number') {29return _utils.throwError.call(this, 'x and y must be numbers', cb);30} // round input313233x = Math.round(x);34y = Math.round(y);35var w = this.bitmap.width;36var h = this.bitmap.height;37var baseImage = this;38src.scanQuiet(0, 0, src.bitmap.width, src.bitmap.height, function (sx, sy, idx) {39var destX = x + sx;40var destY = y + sy;4142if (destX >= 0 && destY >= 0 && destX < w && destY < h) {43var dstIdx = baseImage.getPixelIndex(destX, destY);44var data = this.bitmap.data;45var avg = (data[idx + 0] + data[idx + 1] + data[idx + 2]) / 3;46baseImage.bitmap.data[dstIdx + 3] *= avg / 255;47}48});4950if ((0, _utils.isNodePattern)(cb)) {51cb.call(this, null, this);52}5354return this;55}56};57};5859exports["default"] = _default;60//# sourceMappingURL=index.js.map6162