Path: blob/master/node_modules/@jimp/plugin-circle/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) radius, x, y5* @param {function(Error, Jimp)} cb (optional) a callback for when complete6* @returns {Jimp} this for chaining of methods7*/8export default () => ({9circle(options = {}, cb) {10if (typeof options === 'function') {11cb = options;12options = {};13}1415const radius =16options.radius ||17(this.bitmap.width > this.bitmap.height18? this.bitmap.height19: this.bitmap.width) / 2;2021const center = {22x: typeof options.x === 'number' ? options.x : this.bitmap.width / 2,23y: typeof options.y === 'number' ? options.y : this.bitmap.height / 224};2526this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function(27x,28y,29idx30) {31const curR = Math.sqrt(32Math.pow(x - center.x, 2) + Math.pow(y - center.y, 2)33);3435if (radius - curR <= 0.0) {36this.bitmap.data[idx + 3] = 0;37} else if (radius - curR < 1.0) {38this.bitmap.data[idx + 3] = 255 * (radius - curR);39}40});4142if (isNodePattern(cb)) {43cb.call(this, null, this);44}4546return this;47}48});495051