Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-mask/es/index.js
1126 views
1
"use strict";
2
3
Object.defineProperty(exports, "__esModule", {
4
value: true
5
});
6
exports["default"] = void 0;
7
8
var _utils = require("@jimp/utils");
9
10
/**
11
* 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.
12
* @param {Jimp} src the source Jimp instance
13
* @param {number} x the horizontal position to blit the image
14
* @param {number} y the vertical position to blit the image
15
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
16
* @returns {Jimp} this for chaining of methods
17
*/
18
var _default = function _default() {
19
return {
20
mask: function mask(src) {
21
var x = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
22
var y = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
23
var cb = arguments.length > 3 ? arguments[3] : undefined;
24
25
if (!(src instanceof this.constructor)) {
26
return _utils.throwError.call(this, 'The source must be a Jimp image', cb);
27
}
28
29
if (typeof x !== 'number' || typeof y !== 'number') {
30
return _utils.throwError.call(this, 'x and y must be numbers', cb);
31
} // round input
32
33
34
x = Math.round(x);
35
y = Math.round(y);
36
var w = this.bitmap.width;
37
var h = this.bitmap.height;
38
var baseImage = this;
39
src.scanQuiet(0, 0, src.bitmap.width, src.bitmap.height, function (sx, sy, idx) {
40
var destX = x + sx;
41
var destY = y + sy;
42
43
if (destX >= 0 && destY >= 0 && destX < w && destY < h) {
44
var dstIdx = baseImage.getPixelIndex(destX, destY);
45
var data = this.bitmap.data;
46
var avg = (data[idx + 0] + data[idx + 1] + data[idx + 2]) / 3;
47
baseImage.bitmap.data[dstIdx + 3] *= avg / 255;
48
}
49
});
50
51
if ((0, _utils.isNodePattern)(cb)) {
52
cb.call(this, null, this);
53
}
54
55
return this;
56
}
57
};
58
};
59
60
exports["default"] = _default;
61
//# sourceMappingURL=index.js.map
62