Path: blob/master/node_modules/@jimp/plugin-displace/src/index.js
2593 views
import { isNodePattern, throwError } from '@jimp/utils';12/**3* Displaces the image based on the provided displacement map4* @param {object} map the source Jimp instance5* @param {number} offset the maximum displacement value6* @param {function(Error, Jimp)} cb (optional) a callback for when complete7* @returns {Jimp} this for chaining of methods8*/9export default () => ({10displace(map, offset, cb) {11if (typeof map !== 'object' || map.constructor !== this.constructor) {12return throwError.call(this, 'The source must be a Jimp image', cb);13}1415if (typeof offset !== 'number') {16return throwError.call(this, 'factor must be a number', cb);17}1819const source = this.cloneQuiet();20this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function(21x,22y,23idx24) {25let displacement = (map.bitmap.data[idx] / 256) * offset;26displacement = Math.round(displacement);2728const ids = this.getPixelIndex(x + displacement, y);29this.bitmap.data[ids] = source.bitmap.data[idx];30this.bitmap.data[ids + 1] = source.bitmap.data[idx + 1];31this.bitmap.data[ids + 2] = source.bitmap.data[idx + 2];32});3334if (isNodePattern(cb)) {35cb.call(this, null, this);36}3738return this;39}40});414243