Path: blob/master/node_modules/@jimp/plugin-fisheye/src/index.js
2593 views
import { isNodePattern } from '@jimp/utils';12/**3* Creates a circle out of an image.4* @param {object} options (optional) r: radius of effect5* @param {function(Error, Jimp)} cb (optional) a callback for when complete6* @returns {Jimp} this for chaining of methods7*/8export default () => ({9fisheye(options = { r: 2.5 }, cb) {10if (typeof options === 'function') {11cb = options;12options = { r: 2.5 };13}1415const source = this.cloneQuiet();16const { width, height } = source.bitmap;1718source.scanQuiet(0, 0, width, height, (x, y) => {19const hx = x / width;20const hy = y / height;21const r = Math.sqrt(Math.pow(hx - 0.5, 2) + Math.pow(hy - 0.5, 2));22const rn = 2 * Math.pow(r, options.r);23const cosA = (hx - 0.5) / r;24const sinA = (hy - 0.5) / r;25const newX = Math.round((rn * cosA + 0.5) * width);26const newY = Math.round((rn * sinA + 0.5) * height);27const color = source.getPixelColor(newX, newY);2829this.setPixelColor(color, x, y);30});3132/* Set center pixel color, otherwise it will be transparent */33this.setPixelColor(34source.getPixelColor(width / 2, height / 2),35width / 2,36height / 237);3839if (isNodePattern(cb)) {40cb.call(this, null, this);41}4243return this;44}45});464748