Path: blob/master/node_modules/@jimp/plugin-shadow/src/index.js
1126 views
import { isNodePattern } from '@jimp/utils';12/**3* Creates a circle out of an image.4* @param {function(Error, Jimp)} options (optional)5* opacity - opacity of the shadow between 0 and 16* size,- of the shadow7* blur - how blurry the shadow is8* x- x position of shadow9* y - y position of shadow10* @param {function(Error, Jimp)} cb (optional) a callback for when complete11* @returns {Jimp} this for chaining of methods12*/13export default () => ({14shadow(options = {}, cb) {15if (typeof options === 'function') {16cb = options;17options = {};18}1920const { opacity = 0.7, size = 1.1, x = -25, y = 25, blur = 5 } = options;2122// clone the image23const orig = this.clone();24const shadow = this.clone();2526// turn all it's pixels black27shadow.scan(280,290,30shadow.bitmap.width,31shadow.bitmap.height,32(x, y, idx) => {33shadow.bitmap.data[idx] = 0x00;34shadow.bitmap.data[idx + 1] = 0x00;35shadow.bitmap.data[idx + 2] = 0x00;36// up the opacity a little,37shadow.bitmap.data[idx + 3] = shadow.constructor.limit255(38shadow.bitmap.data[idx + 3] * opacity39);4041this.bitmap.data[idx] = 0x00;42this.bitmap.data[idx + 1] = 0x00;43this.bitmap.data[idx + 2] = 0x00;44this.bitmap.data[idx + 3] = 0x00;45}46);4748// enlarge it. This creates a "shadow".49shadow50.resize(shadow.bitmap.width * size, shadow.bitmap.height * size)51.blur(blur);5253// Then blit the "shadow" onto the background and the image on top of that.54this.composite(shadow, x, y);55this.composite(orig, 0, 0);5657if (isNodePattern(cb)) {58cb.call(this, null, this);59}6061return this;62}63});646566