Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-shadow/src/index.js
1126 views
1
import { isNodePattern } from '@jimp/utils';
2
3
/**
4
* Creates a circle out of an image.
5
* @param {function(Error, Jimp)} options (optional)
6
* opacity - opacity of the shadow between 0 and 1
7
* size,- of the shadow
8
* blur - how blurry the shadow is
9
* x- x position of shadow
10
* y - y position of shadow
11
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
12
* @returns {Jimp} this for chaining of methods
13
*/
14
export default () => ({
15
shadow(options = {}, cb) {
16
if (typeof options === 'function') {
17
cb = options;
18
options = {};
19
}
20
21
const { opacity = 0.7, size = 1.1, x = -25, y = 25, blur = 5 } = options;
22
23
// clone the image
24
const orig = this.clone();
25
const shadow = this.clone();
26
27
// turn all it's pixels black
28
shadow.scan(
29
0,
30
0,
31
shadow.bitmap.width,
32
shadow.bitmap.height,
33
(x, y, idx) => {
34
shadow.bitmap.data[idx] = 0x00;
35
shadow.bitmap.data[idx + 1] = 0x00;
36
shadow.bitmap.data[idx + 2] = 0x00;
37
// up the opacity a little,
38
shadow.bitmap.data[idx + 3] = shadow.constructor.limit255(
39
shadow.bitmap.data[idx + 3] * opacity
40
);
41
42
this.bitmap.data[idx] = 0x00;
43
this.bitmap.data[idx + 1] = 0x00;
44
this.bitmap.data[idx + 2] = 0x00;
45
this.bitmap.data[idx + 3] = 0x00;
46
}
47
);
48
49
// enlarge it. This creates a "shadow".
50
shadow
51
.resize(shadow.bitmap.width * size, shadow.bitmap.height * size)
52
.blur(blur);
53
54
// Then blit the "shadow" onto the background and the image on top of that.
55
this.composite(shadow, x, y);
56
this.composite(orig, 0, 0);
57
58
if (isNodePattern(cb)) {
59
cb.call(this, null, this);
60
}
61
62
return this;
63
}
64
});
65
66