index.js 2.0 KB

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