index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. * Creates a circle out of an image.
  9. * @param {function(Error, Jimp)} options (optional) radius, x, y
  10. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  11. * @returns {Jimp} this for chaining of methods
  12. */
  13. var _default = function _default() {
  14. return {
  15. circle: function circle() {
  16. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  17. var cb = arguments.length > 1 ? arguments[1] : undefined;
  18. if (typeof options === 'function') {
  19. cb = options;
  20. options = {};
  21. }
  22. var radius = options.radius || (this.bitmap.width > this.bitmap.height ? this.bitmap.height : this.bitmap.width) / 2;
  23. var center = {
  24. x: typeof options.x === 'number' ? options.x : this.bitmap.width / 2,
  25. y: typeof options.y === 'number' ? options.y : this.bitmap.height / 2
  26. };
  27. this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
  28. var curR = Math.sqrt(Math.pow(x - center.x, 2) + Math.pow(y - center.y, 2));
  29. if (radius - curR <= 0.0) {
  30. this.bitmap.data[idx + 3] = 0;
  31. } else if (radius - curR < 1.0) {
  32. this.bitmap.data[idx + 3] = 255 * (radius - curR);
  33. }
  34. });
  35. if ((0, _utils.isNodePattern)(cb)) {
  36. cb.call(this, null, this);
  37. }
  38. return this;
  39. }
  40. };
  41. };
  42. exports["default"] = _default;
  43. //# sourceMappingURL=index.js.map