Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/node_modules/@jimp/plugin-fisheye/src/index.js
2593 views
1
import { isNodePattern } from '@jimp/utils';
2
3
/**
4
* Creates a circle out of an image.
5
* @param {object} options (optional) r: radius of effect
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
fisheye(options = { r: 2.5 }, cb) {
11
if (typeof options === 'function') {
12
cb = options;
13
options = { r: 2.5 };
14
}
15
16
const source = this.cloneQuiet();
17
const { width, height } = source.bitmap;
18
19
source.scanQuiet(0, 0, width, height, (x, y) => {
20
const hx = x / width;
21
const hy = y / height;
22
const r = Math.sqrt(Math.pow(hx - 0.5, 2) + Math.pow(hy - 0.5, 2));
23
const rn = 2 * Math.pow(r, options.r);
24
const cosA = (hx - 0.5) / r;
25
const sinA = (hy - 0.5) / r;
26
const newX = Math.round((rn * cosA + 0.5) * width);
27
const newY = Math.round((rn * sinA + 0.5) * height);
28
const color = source.getPixelColor(newX, newY);
29
30
this.setPixelColor(color, x, y);
31
});
32
33
/* Set center pixel color, otherwise it will be transparent */
34
this.setPixelColor(
35
source.getPixelColor(width / 2, height / 2),
36
width / 2,
37
height / 2
38
);
39
40
if (isNodePattern(cb)) {
41
cb.call(this, null, this);
42
}
43
44
return this;
45
}
46
});
47
48