Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/node_modules/@jimp/plugin-displace/src/index.js
2593 views
1
import { isNodePattern, throwError } from '@jimp/utils';
2
3
/**
4
* Displaces the image based on the provided displacement map
5
* @param {object} map the source Jimp instance
6
* @param {number} offset the maximum displacement value
7
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
8
* @returns {Jimp} this for chaining of methods
9
*/
10
export default () => ({
11
displace(map, offset, cb) {
12
if (typeof map !== 'object' || map.constructor !== this.constructor) {
13
return throwError.call(this, 'The source must be a Jimp image', cb);
14
}
15
16
if (typeof offset !== 'number') {
17
return throwError.call(this, 'factor must be a number', cb);
18
}
19
20
const source = this.cloneQuiet();
21
this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function(
22
x,
23
y,
24
idx
25
) {
26
let displacement = (map.bitmap.data[idx] / 256) * offset;
27
displacement = Math.round(displacement);
28
29
const 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
35
if (isNodePattern(cb)) {
36
cb.call(this, null, this);
37
}
38
39
return this;
40
}
41
});
42
43