Skip to content

Commit

Permalink
updated types
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashank3736 committed Oct 5, 2021
1 parent 543bba0 commit c107b5b
Show file tree
Hide file tree
Showing 14 changed files with 510 additions and 231 deletions.
60 changes: 60 additions & 0 deletions js-script/captcha.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/// <reference types="node" />
import { Canvas, CanvasRenderingContext2D, Image } from "skia-canvas";
import { SetCaptchaOption, SetDecoyOption, SetTraceOption } from "./constants";
/**
* Captcha Generator
*/
export declare class Captcha {
protected _height: number;
protected _width: number;
protected _captcha: SetCaptchaOption;
protected _trace: SetTraceOption;
protected _decoy: SetDecoyOption;
protected _canvas: Canvas;
protected _ctx: CanvasRenderingContext2D;
protected _coordinates: number[][];
async: Boolean;
/**
* Start captcha image creation.
* @param {number} [width] Width of captcha image.
* @param {number} [height] Height of captcha image.
* @constructor
*/
constructor(width?: number, height?: number);
/**
* Get Captcha text.
* @returns {string} Get captcha text.
*/
get text(): string;
/**
* Get png image of captcha.
* @returns {Buffer} Get png image of captcha created.
*/
get png(): Buffer;
/**
* Draw image on your captcha.
* @param {Image} image Choose image you want to add.
* @returns {Captcha}
*/
drawImage(image: Image): Captcha;
/**
* Add decoy on your captcha image.
* @param {SetDecoyOptions} [decoyOption] Decoy option you want to customise
* @returns {Captcha}
*/
addDecoy(decoyOption?: SetDecoyOption): Captcha;
/**
* Draw trace line over your captcha.
*
* Note: If you want to use custom text or change size of captcha text then drawCaptcha before drawTrace.
* @param {SetTraceOptions} [traceOption]
* @returns {Captcha}
*/
drawTrace(traceOption?: SetTraceOption): Captcha;
/**
* Draw captcha text on captcha image.
* @param {SetCaptchaOptions} [captchaOption]
* @returns {Captcha}
*/
drawCaptcha(captchaOption?: SetCaptchaOption): Captcha;
}
109 changes: 67 additions & 42 deletions js-script/captcha.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,141 @@
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Captcha = void 0;
const skia_canvas_1 = require("skia-canvas");
const constants_1 = require("./constants");
const util_1 = require("./util");
var skia_canvas_1 = require("skia-canvas");
var constants_1 = require("./constants");
var util_1 = require("./util");
/**
* Captcha Generator
*/
class Captcha {
var Captcha = /** @class */ (function () {
/**
* Start captcha image creation.
* @param {number} [width] Width of captcha image.
* @param {number} [height] Height of captcha image.
* @constructor
*/
constructor(width = constants_1.defaultDimension.width, height = constants_1.defaultDimension.height) {
function Captcha(width, height) {
if (width === void 0) { width = constants_1.defaultDimension.width; }
if (height === void 0) { height = constants_1.defaultDimension.height; }
this._height = height;
this._width = width;
this._captcha = constants_1.defaultCaptchaOption;
this._captcha.text = (0, util_1.randomText)(this._captcha.characters || 6);
this._trace = constants_1.defaultTraceOptions;
this._decoy = constants_1.defaultDecoyOptions;
const canvas = new skia_canvas_1.Canvas(width, height);
const ctx = canvas.getContext('2d');
var canvas = new skia_canvas_1.Canvas(width, height);
var ctx = canvas.getContext('2d');
ctx.lineJoin = 'miter';
ctx.textBaseline = 'middle';
this._canvas = canvas;
this._ctx = ctx;
this.async = true;
this._coordinates = [];
}
/**
* Get Captcha text.
* @returns {string} Get captcha text.
*/
get text() {
return this._captcha.text || "";
}
/**
* Get png image of captcha.
* @returns {Buffer} Get png image of captcha created.
*/
get png() {
this._canvas.async = this.async;
return this._canvas.png;
}
Object.defineProperty(Captcha.prototype, "text", {
/**
* Get Captcha text.
* @returns {string} Get captcha text.
*/
get: function () {
return this._captcha.text || "";
},
enumerable: false,
configurable: true
});
Object.defineProperty(Captcha.prototype, "png", {
/**
* Get png image of captcha.
* @returns {Buffer} Get png image of captcha created.
*/
get: function () {
this._canvas.async = this.async;
return this._canvas.png;
},
enumerable: false,
configurable: true
});
/**
* Draw image on your captcha.
* @param {Image} image Choose image you want to add.
* @returns {Captcha}
*/
drawImage(image) {
Captcha.prototype.drawImage = function (image) {
this._ctx.drawImage(image);
return this;
}
};
/**
* Add decoy on your captcha image.
* @param {SetDecoyOptions} [decoyOption] Decoy option you want to customise
* @returns {Captcha}
*/
addDecoy(decoyOption = {}) {
const option = { ...this._decoy, ...decoyOption };
Captcha.prototype.addDecoy = function (decoyOption) {
if (decoyOption === void 0) { decoyOption = {}; }
var option = __assign(__assign({}, this._decoy), decoyOption);
if (!option.total)
option.total = Math.floor(this._width * this._height / 10000);
const decoyText = (0, util_1.randomText)(option.total);
this._ctx.font = `${option.size}px ${option.font}`;
var decoyText = (0, util_1.randomText)(option.total);
this._ctx.font = option.size + "px " + option.font;
this._ctx.globalAlpha = option.opacity;
this._ctx.fillStyle = option.color;
for (let i = 0; i < decoyText.length; i++) {
for (var i = 0; i < decoyText.length; i++) {
this._ctx.fillText(decoyText[i], (0, util_1.getRandom)(30, this._width - 30), (0, util_1.getRandom)(30, this._height - 30));
}
return this;
}
};
/**
* Draw trace line over your captcha.
*
* Note: If you want to use custom text or change size of captcha text then drawCaptcha before drawTrace.
* @param {SetTraceOptions} [traceOption]
* @returns {Captcha}
*/
drawTrace(traceOption = {}) {
const option = { ...this._trace, ...traceOption };
Captcha.prototype.drawTrace = function (traceOption) {
if (traceOption === void 0) { traceOption = {}; }
var option = __assign(__assign({}, this._trace), traceOption);
if (!this._coordinates[0])
this._coordinates = (0, util_1.getRandomCoordinate)(this._height, this._width, this._captcha.characters || 6);
const coordinates = this._coordinates;
var coordinates = this._coordinates;
this._ctx.strokeStyle = option.color;
this._ctx.globalAlpha = option.opacity;
this._ctx.beginPath();
this._ctx.moveTo(coordinates[0][0], coordinates[0][1]);
this._ctx.lineWidth = option.size;
for (let i = 1; i < coordinates.length; i++) {
for (var i = 1; i < coordinates.length; i++) {
this._ctx.lineTo(coordinates[i][0], coordinates[i][1]);
}
this._ctx.stroke();
return this;
}
};
/**
* Draw captcha text on captcha image.
* @param {SetCaptchaOptions} [captchaOption]
* @returns {Captcha}
*/
drawCaptcha(captchaOption = {}) {
Captcha.prototype.drawCaptcha = function (captchaOption) {
var _a, _b, _c;
const option = { ...this._captcha, ...captchaOption };
if (captchaOption === void 0) { captchaOption = {}; }
var option = __assign(__assign({}, this._captcha), captchaOption);
if (((_a = option.text) === null || _a === void 0 ? void 0 : _a.length) !== option.characters)
option.characters = (_b = option.text) === null || _b === void 0 ? void 0 : _b.length;
if (!this._coordinates[0])
this._coordinates = (0, util_1.getRandomCoordinate)(this._height, this._width, this._captcha.characters || 6);
const coordinates = this._coordinates;
this._ctx.font = `${option.size}px ${option.font}`;
var coordinates = this._coordinates;
this._ctx.font = option.size + "px " + option.font;
this._ctx.globalAlpha = option.opacity;
this._ctx.fillStyle = option.color;
for (let n = 0; n < coordinates.length; n++) {
for (var n = 0; n < coordinates.length; n++) {
this._ctx.save();
this._ctx.translate(coordinates[n][0], coordinates[n][1]);
if (option.skew) {
Expand All @@ -128,6 +152,7 @@ class Captcha {
}
;
return this;
}
}
};
return Captcha;
}());
exports.Captcha = Captcha;
126 changes: 126 additions & 0 deletions js-script/captchaGen.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/// <reference types="node" />
import { Image } from "skia-canvas";
import { Captcha } from "./captcha";
import { SetCaptchaOption, SetDecoyOption, SetTraceOption } from "./constants";
interface ConstructorOption {
width?: number;
height?: number;
}
/**
* Captcha Generator.
*/
export declare class CaptchaGenerator extends Captcha {
private background?;
/**
* Initatiates the creation of captcha image generation.
* @example const captcha = new CaptchaGenerator({height: 200, width: 600});
* @param {object} options Options for constructor.
* @param {integer} options.height Height of captcha image.
* @param {integer} options.width Width of captcha image.
* @since 2.0.0
*/
constructor(options?: ConstructorOption);
/**
* Get Captcha text.
* @returns {string} Get captcha text.
*/
get text(): string;
/**
* Change captcha text options
* @param {SetCaptchaOptions} options Captcha appearance options.
* @example
* const { CaptchaGenerator } = require("captcha-canvas");
* const fs = require("fs")
* const captcha = new CaptchaGenerator();
* const options = {font: "Comic Sans", size: 60}
* captcha.setCaptcha(options)
* const buffer = await captcha.generate() //generate image
*
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
setCaptcha(captchaOption: SetCaptchaOption): CaptchaGenerator;
/**
* set dimension for your captcha image
* @param {number} height Height of captcha image.
* @param {number} width Width of captcha image.
* @example
* const { CaptchaGenerator } = require("captcha-canvas");
* const fs = require("fs")
* const captcha = new CaptchaGenerator();
* captcha.setDimension(200, 600);
* const buffer = await captcha.generate() //generate image
*
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
setDimension(height: number, width: number): CaptchaGenerator;
/**
* Set background for captcha image.
* @param {buffer} image Buffer/url/path of image.
* @example
* const { CaptchaGenerator } = require("captcha-canvas");
* const fs = require("fs")
* const captcha = new CaptchaGenerator();
* captcha.setBackground("./path/toFile");
* const buffer = await captcha.generate() //generate image
*
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
setBackground(image: Buffer | string): CaptchaGenerator;
/**
* Change trace creation options.
* @param {SetTraceOptions} options Trace Line appearance options.
* @example
* const { CaptchaGenerator } = require("captcha-canvas");
* const fs = require("fs")
* const captcha = new CaptchaGenerator();
* const options = {size: 5, color: "deeppink"}
* captcha.setTrace(options)
* const buffer = await captcha.generate() //generate image
*
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
setTrace(traceOption: SetTraceOption): CaptchaGenerator;
/**
* Change decoy options
* @param {SetDecoyOptions} options Decoy characters customisation options
* @since 2.0.0
*/
setDecoy(decoyOption: SetDecoyOption): CaptchaGenerator;
/**
* Method which returns image buffer
* @async
* @returns {Promise<Buffer>}
* @example
* const { CaptchaGenerator } = require("captcha-canvas");
* const fs = require("fs")
* const captcha = new CaptchaGenerator();
* const buffer = await captcha.generate() //generate image
*
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
generate(): Promise<Buffer>;
/**
* Non asynchronous method to generate captcha image.
* > Note: It do not use `setBackground` method value for background image. If you want to set background
* and also use generateSync method then use background option in genrateSync method.
* @param {Image} [background] Add background image.
* @example
* const { CaptchaGenerator, resolveImage } = require("captcha-canvas");
* const fs = require("fs");
* const img = await resolveImage("./path/to/file");
*
* const captcha = new CaptchaGenerator()
* .generateSync(background: img);
*
* fs.writeFileSync("image.png", captcha);
* @since 2.2.0
* @returns {Buffer}
*/
generateSync(background?: Image): Buffer;
}
export {};
Loading

0 comments on commit c107b5b

Please sign in to comment.