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