Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-circle/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) radius, x, y
6
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
7
* @returns {Jimp} this for chaining of methods
8
*/
9
export default () => ({
10
circle(options = {}, cb) {
11
if (typeof options === 'function') {
12
cb = options;
13
options = {};
14
}
15
16
const radius =
17
options.radius ||
18
(this.bitmap.width > this.bitmap.height
19
? this.bitmap.height
20
: this.bitmap.width) / 2;
21
22
const center = {
23
x: typeof options.x === 'number' ? options.x : this.bitmap.width / 2,
24
y: typeof options.y === 'number' ? options.y : this.bitmap.height / 2
25
};
26
27
this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function(
28
x,
29
y,
30
idx
31
) {
32
const curR = Math.sqrt(
33
Math.pow(x - center.x, 2) + Math.pow(y - center.y, 2)
34
);
35
36
if (radius - curR <= 0.0) {
37
this.bitmap.data[idx + 3] = 0;
38
} else if (radius - curR < 1.0) {
39
this.bitmap.data[idx + 3] = 255 * (radius - curR);
40
}
41
});
42
43
if (isNodePattern(cb)) {
44
cb.call(this, null, this);
45
}
46
47
return this;
48
}
49
});
50
51