index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 {object} options (optional) r: radius of effect
  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. fisheye: function fisheye() {
  16. var _this = this;
  17. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
  18. r: 2.5
  19. };
  20. var cb = arguments.length > 1 ? arguments[1] : undefined;
  21. if (typeof options === 'function') {
  22. cb = options;
  23. options = {
  24. r: 2.5
  25. };
  26. }
  27. var source = this.cloneQuiet();
  28. var _source$bitmap = source.bitmap,
  29. width = _source$bitmap.width,
  30. height = _source$bitmap.height;
  31. source.scanQuiet(0, 0, width, height, function (x, y) {
  32. var hx = x / width;
  33. var hy = y / height;
  34. var r = Math.sqrt(Math.pow(hx - 0.5, 2) + Math.pow(hy - 0.5, 2));
  35. var rn = 2 * Math.pow(r, options.r);
  36. var cosA = (hx - 0.5) / r;
  37. var sinA = (hy - 0.5) / r;
  38. var newX = Math.round((rn * cosA + 0.5) * width);
  39. var newY = Math.round((rn * sinA + 0.5) * height);
  40. var color = source.getPixelColor(newX, newY);
  41. _this.setPixelColor(color, x, y);
  42. });
  43. /* Set center pixel color, otherwise it will be transparent */
  44. this.setPixelColor(source.getPixelColor(width / 2, height / 2), width / 2, height / 2);
  45. if ((0, _utils.isNodePattern)(cb)) {
  46. cb.call(this, null, this);
  47. }
  48. return this;
  49. }
  50. };
  51. };
  52. exports["default"] = _default;
  53. //# sourceMappingURL=index.js.map