index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = void 0;
  7. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  8. var _utils = require("@jimp/utils");
  9. /**
  10. * Displaces the image based on the provided displacement map
  11. * @param {object} map the source Jimp instance
  12. * @param {number} offset the maximum displacement value
  13. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  14. * @returns {Jimp} this for chaining of methods
  15. */
  16. var _default = function _default() {
  17. return {
  18. displace: function displace(map, offset, cb) {
  19. if ((0, _typeof2["default"])(map) !== 'object' || map.constructor !== this.constructor) {
  20. return _utils.throwError.call(this, 'The source must be a Jimp image', cb);
  21. }
  22. if (typeof offset !== 'number') {
  23. return _utils.throwError.call(this, 'factor must be a number', cb);
  24. }
  25. var source = this.cloneQuiet();
  26. this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
  27. var displacement = map.bitmap.data[idx] / 256 * offset;
  28. displacement = Math.round(displacement);
  29. var ids = this.getPixelIndex(x + displacement, y);
  30. this.bitmap.data[ids] = source.bitmap.data[idx];
  31. this.bitmap.data[ids + 1] = source.bitmap.data[idx + 1];
  32. this.bitmap.data[ids + 2] = source.bitmap.data[idx + 2];
  33. });
  34. if ((0, _utils.isNodePattern)(cb)) {
  35. cb.call(this, null, this);
  36. }
  37. return this;
  38. }
  39. };
  40. };
  41. exports["default"] = _default;
  42. //# sourceMappingURL=index.js.map