diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml
deleted file mode 100644
index aa47c4e..0000000
--- a/.github/workflows/npm-publish.yml
+++ /dev/null
@@ -1,47 +0,0 @@
-# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
-# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
-
-name: Node.js Package
-
-on:
- release:
- types: [created]
-
-jobs:
- build:
- runs-on: ubuntu-latest
- steps:
- - uses: actions/checkout@v2
- - uses: actions/setup-node@v1
- with:
- node-version: 12
- - run: npm ci
- - run: npm test
-
- publish-npm:
- needs: build
- runs-on: ubuntu-latest
- steps:
- - uses: actions/checkout@v2
- - uses: actions/setup-node@v1
- with:
- node-version: 12
- registry-url: https://registry.npmjs.org/
- - run: npm ci
- - run: npm publish
- env:
- NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
-
- publish-gpr:
- needs: build
- runs-on: ubuntu-latest
- steps:
- - uses: actions/checkout@v2
- - uses: actions/setup-node@v1
- with:
- node-version: 12
- registry-url: https://npm.pkg.github.com/
- - run: npm ci
- - run: npm publish
- env:
- NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
diff --git a/assets/captcha/default.png b/assets/captcha/default.png
index 9322378..e5c886b 100644
Binary files a/assets/captcha/default.png and b/assets/captcha/default.png differ
diff --git a/docs/Captcha.html b/docs/Captcha.html
new file mode 100644
index 0000000..3a30fa3
--- /dev/null
+++ b/docs/Captcha.html
@@ -0,0 +1,1226 @@
+
+
+
+
+
+
- text |
+ option |
-string
+CreateCaptchaOptions
@@ -599,7 +599,7 @@ Parameters:
View Source
- extra.js, line 41
+ extra.js, line 36
@@ -653,6 +653,230 @@ Type Definitions
+
+ #
+
+
+ CreateCaptchaOptions
+
+
+
+
+
+
+
+
+ Create captcha options in functions.
+
+
+
+
+
+
+ Properties:
+
+
+
+
+
+
+
+ Name |
+
+
+ Type |
+
+
+ Attributes |
+
+
+
+
+ Description |
+
+
+
+
+
+
+
+
+ captcha |
+
+
+
+
+
+SetCaptchaOptions
+
+
+
+ |
+
+
+
+
+ <optional>
+
+
+
+ |
+
+
+
+
+ Captcha text options to customise text appearance and value. |
+
+
+
+
+
+
+ decoy |
+
+
+
+
+
+SetDecoyOptions
+
+
+
+ |
+
+
+
+
+ <optional>
+
+
+
+ |
+
+
+
+
+ |
+
+
+
+
+
+
+ trace |
+
+
+
+
+
+SetTraceOptions
+
+
+
+ |
+
+
+
+
+ <optional>
+
+
+
+ |
+
+
+
+
+ |
+
+
+
+
+
+
+ background |
+
+
+
+
+
+Image
+
+
+
+ |
+
+
+
+
+ <optional>
+
+
+
+ |
+
+
+
+
+ |
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ View Source
+
+ constants.js, line 66
+
+
+
+
+
+
+
+
+
+
+
+
+
#
diff --git a/examples/all.png b/examples/all.png
index b9d9114..716f88d 100644
Binary files a/examples/all.png and b/examples/all.png differ
diff --git a/examples/captcha.png b/examples/captcha.png
index b5af094..ef1f0bb 100644
Binary files a/examples/captcha.png and b/examples/captcha.png differ
diff --git a/examples/default.png b/examples/default.png
index e6e0ec8..466b322 100644
Binary files a/examples/default.png and b/examples/default.png differ
diff --git a/examples/dimension.png b/examples/dimension.png
index 32d86bc..55b3252 100644
Binary files a/examples/dimension.png and b/examples/dimension.png differ
diff --git a/js-script/CaptchaGenerator.d.ts b/js-script/CaptchaGenerator.d.ts
deleted file mode 100644
index 7e816d3..0000000
--- a/js-script/CaptchaGenerator.d.ts
+++ /dev/null
@@ -1,131 +0,0 @@
-///
-import { Image } from "skia-canvas";
-import { SetCaptchaOption, SetDecoyOption, SetTraceOption } from "./constants";
-/**
- * Captcha generator class.
- */
-export declare class CaptchaGenerator {
- private height;
- private width;
- private captcha;
- private trace;
- private decoy;
- 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=100] Height of captcha image.
- * @param {integer} [options.width=300] Width of captcha image.
- * @since 2.0.0
- */
- constructor(options?: {
- height: number;
- width: number;
- });
- /**
- * Get the text of captcha.
- * @type {string}
- * @since 2.0.3
- */
- get text(): string;
- /**
- * set dimension for your captcha image
- * @param {integer} height Height of captcha image.
- * @param {integer} 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: string | Buffer): this;
- /**
- * 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(option: SetCaptchaOption): this;
- /**
- * 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(option: SetTraceOption): this;
- /**
- * Change decoy options
- * @param {SetDecoyOptions} options Decoy characters customisation options
- * @since 2.0.0
- */
- setDecoy(option: SetDecoyOption): this;
- /**
- * Method which returns image buffer
- * @async
- * @returns {Promise}
- * @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;
- /**
- * 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 {object} [options] Options to add extra values
- * @param {Image} [options.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
- */
- generateSync(option?: {
- background?: Image;
- }): Buffer;
-}
diff --git a/js-script/CaptchaGenerator.js b/js-script/CaptchaGenerator.js
deleted file mode 100644
index 46ec47f..0000000
--- a/js-script/CaptchaGenerator.js
+++ /dev/null
@@ -1,177 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-exports.CaptchaGenerator = void 0;
-const skia_canvas_1 = require("skia-canvas");
-const _1 = require(".");
-const constants_1 = require("./constants");
-const util_1 = require("./util");
-/**
- * Captcha generator class.
- */
-class CaptchaGenerator {
- /**
- * 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=100] Height of captcha image.
- * @param {integer} [options.width=300] Width of captcha image.
- * @since 2.0.0
- */
- constructor(options = { height: 100, width: 300 }) {
- this.height = options.height;
- this.width = options.width;
- this.captcha = constants_1.defaultCaptchaOption;
- this.trace = constants_1.defaultTraceOptions;
- this.decoy = constants_1.defaultDecoyOptions;
- this.captcha.text = (0, util_1.randomText)(this.captcha.characters);
- }
- /**
- * Get the text of captcha.
- * @type {string}
- * @since 2.0.3
- */
- get text() {
- return this.captcha.text;
- }
- /**
- * set dimension for your captcha image
- * @param {integer} height Height of captcha image.
- * @param {integer} 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, width) {
- this.height = height;
- this.width = width;
- return this;
- }
- /**
- * 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) {
- this.background = image;
- return this;
- }
- /**
- * 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(option) {
- this.captcha = { ...this.captcha, ...option };
- if (option.text)
- this.captcha.characters = option.text.length;
- if (!option.text && option.characters)
- this.captcha.text = (0, util_1.randomText)(option.characters);
- return this;
- }
- /**
- * 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(option) {
- this.trace = { ...this.trace, ...option };
- return this;
- }
- /**
- * Change decoy options
- * @param {SetDecoyOptions} options Decoy characters customisation options
- * @since 2.0.0
- */
- setDecoy(option) {
- this.decoy = { ...this.decoy, ...option };
- return this;
- }
- /**
- * Method which returns image buffer
- * @async
- * @returns {Promise}
- * @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
- */
- async generate() {
- const captchaCanvas = new _1.Captcha(this.width, this.height);
- if (this.background)
- captchaCanvas.drawImage(await (0, skia_canvas_1.loadImage)(this.background));
- if (this.decoy.opacity)
- captchaCanvas.addDecoy(this.decoy);
- if (this.captcha.opacity)
- captchaCanvas.drawCaptcha(this.captcha);
- if (this.trace.opacity)
- captchaCanvas.drawTrace(this.trace);
- return captchaCanvas.png;
- }
- /**
- * 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 {object} [options] Options to add extra values
- * @param {Image} [options.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
- */
- generateSync(option = {}) {
- const captchaCanvas = new _1.Captcha(this.width, this.height);
- captchaCanvas.async = false;
- if (option.background)
- captchaCanvas.drawImage(option.background);
- if (this.decoy.opacity)
- captchaCanvas.addDecoy(this.decoy);
- if (this.captcha.opacity)
- captchaCanvas.drawCaptcha(this.captcha);
- if (this.trace.opacity)
- captchaCanvas.drawTrace(this.trace);
- return captchaCanvas.png;
- }
-}
-exports.CaptchaGenerator = CaptchaGenerator;
diff --git a/js-script/captcha.d.ts b/js-script/captcha.d.ts
deleted file mode 100644
index c2d3429..0000000
--- a/js-script/captcha.d.ts
+++ /dev/null
@@ -1,60 +0,0 @@
-///
-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;
-}
diff --git a/js-script/captcha.js b/js-script/captcha.js
deleted file mode 100644
index 7cef369..0000000
--- a/js-script/captcha.js
+++ /dev/null
@@ -1,137 +0,0 @@
-"use strict";
-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");
-/**
- * Captcha Generator
- */
-class Captcha {
- /**
- * 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) {
- this._height = height;
- this._width = width;
- this._captcha = constants_1.defaultCaptchaOption;
- this._trace = constants_1.defaultTraceOptions;
- this._decoy = constants_1.defaultDecoyOptions;
- const canvas = new skia_canvas_1.Canvas(width, height);
- const 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;
- }
- /**
- * Draw image on your captcha.
- * @param {Image} image Choose image you want to add.
- * @returns {Captcha}
- */
- drawImage(image) {
- this._ctx.drawImage(image, 0, 0);
- 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 };
- 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}`;
- this._ctx.globalAlpha = option.opacity;
- this._ctx.fillStyle = option.color;
- for (let 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 };
- if (!this._coordinates[0])
- this._coordinates = (0, util_1.getRandomCoordinate)(this._height, this._width, this._captcha.characters || 6);
- const 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++) {
- 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 = {}) {
- var _a;
- const option = { ...this._captcha, ...captchaOption };
- if (captchaOption.text)
- option.characters = captchaOption.text.length;
- if (!captchaOption.text && captchaOption.characters)
- option.text = (0, util_1.randomText)(option.characters);
- if (!option.text)
- option.text = (0, util_1.randomText)(option.characters);
- this._captcha = option;
- if (!this._coordinates[0])
- this._coordinates = (0, util_1.getRandomCoordinate)(this._height, this._width, option.characters || 6);
- const 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++) {
- this._ctx.save();
- this._ctx.translate(coordinates[n][0], coordinates[n][1]);
- if (option.skew) {
- this._ctx.transform(1, Math.random(), (0, util_1.getRandom)(20) / 100, 1, 0, 0);
- }
- if (option.rotate > 0) {
- this._ctx.rotate((0, util_1.getRandom)(-option.rotate, option.rotate) * Math.PI / 180);
- }
- if (((_a = option.colors) === null || _a === void 0 ? void 0 : _a.length) > 2) {
- this._ctx.fillStyle = option.colors[(0, util_1.getRandom)(option.colors.length - 1)];
- }
- this._ctx.fillText(option.text[n], 0, 0);
- this._ctx.restore();
- }
- ;
- return this;
- }
-}
-exports.Captcha = Captcha;
diff --git a/js-script/constants.d.ts b/js-script/constants.d.ts
deleted file mode 100644
index acb8077..0000000
--- a/js-script/constants.d.ts
+++ /dev/null
@@ -1,64 +0,0 @@
-export interface SetDimensionOption {
- height: number;
- width: number;
-}
-export interface SetCaptchaOption {
- characters?: number;
- text?: string;
- color?: string;
- font?: string;
- skew?: boolean;
- colors?: string[] | [];
- rotate?: number;
- size?: number;
- opacity?: number;
-}
-export interface SetDecoyOption {
- color?: string;
- font?: string;
- size?: number;
- opacity?: number;
- total?: number;
-}
-export interface SetTraceOption {
- color?: string;
- size?: number;
- opacity?: number;
-}
-/**
- * Captcha text options to customise text appearance and value.
- * @typedef SetCaptchaOptions
- * @property {integer} [characters=6] Length of captcha text.
- * @property {string} [text="Random UpperCase string"] Text of captcha
- * @property {hexCode} [color="#32cf7e"] Color of captcha text.
- * @property {font} [font="Sans"] Font of captcha text.
- * @property {boolean} [skew=true] Skew captcha text.
- * @property {array} [colors=[]] Array of hexCode will override color property.
- * @property {number} [rotate=5] Range of angle to rotate text.
- * @property {number} [size=40] Size of captcha text.
- * @property {float} [opacity=1] Opcaity of captcha text.
- */
-export declare const defaultCaptchaOption: SetCaptchaOption;
-/**
- * @typedef SetTraceOptions
- * @property {hexCode} [color="#32cf7e"] Color of trace line.
- * @property {number} [size=3] Width of trace line.
- * @property {float} [opacity=1] Opacoty of trace line.
- */
-export declare const defaultTraceOptions: SetTraceOption;
-/**
- * @typedef SetDecoyOptions
- * @property {hexCode} [color="#646566"] Color of decoy characters.
- * @property {font} [font="Sans"] Font of decoy characters.
- * @property {number} [size=20] Size of decoy characters.
- * @property {float} [opacity=0.8] Opacity of decoy characters.
- * @property {number} [total] Total count of decoy characters.
- */
-export declare const defaultDecoyOptions: SetDecoyOption;
-/**
- * Customise dimension of captcha image.
- * @typedef SetDimensionOption
- * @property {integer} [height=100] Height of captcha image.
- * @property {integer} [width=300] Width of captcha image.
- */
-export declare const defaultDimension: SetDimensionOption;
diff --git a/js-script/constants.js b/js-script/constants.js
deleted file mode 100644
index 63b39a5..0000000
--- a/js-script/constants.js
+++ /dev/null
@@ -1,65 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-exports.defaultDimension = exports.defaultDecoyOptions = exports.defaultTraceOptions = exports.defaultCaptchaOption = void 0;
-;
-;
-;
-;
-/**
- * Captcha text options to customise text appearance and value.
- * @typedef SetCaptchaOptions
- * @property {integer} [characters=6] Length of captcha text.
- * @property {string} [text="Random UpperCase string"] Text of captcha
- * @property {hexCode} [color="#32cf7e"] Color of captcha text.
- * @property {font} [font="Sans"] Font of captcha text.
- * @property {boolean} [skew=true] Skew captcha text.
- * @property {array} [colors=[]] Array of hexCode will override color property.
- * @property {number} [rotate=5] Range of angle to rotate text.
- * @property {number} [size=40] Size of captcha text.
- * @property {float} [opacity=1] Opcaity of captcha text.
- */
-exports.defaultCaptchaOption = {
- characters: 6,
- size: 40,
- font: 'Sans',
- skew: true,
- colors: [],
- rotate: 5,
- color: '#32cf7e',
- opacity: 0.8,
-};
-/**
- * @typedef SetTraceOptions
- * @property {hexCode} [color="#32cf7e"] Color of trace line.
- * @property {number} [size=3] Width of trace line.
- * @property {float} [opacity=1] Opacoty of trace line.
- */
-exports.defaultTraceOptions = {
- size: 3,
- color: '#32cf7e',
- opacity: 1,
-};
-/**
- * @typedef SetDecoyOptions
- * @property {hexCode} [color="#646566"] Color of decoy characters.
- * @property {font} [font="Sans"] Font of decoy characters.
- * @property {number} [size=20] Size of decoy characters.
- * @property {float} [opacity=0.8] Opacity of decoy characters.
- * @property {number} [total] Total count of decoy characters.
- */
-exports.defaultDecoyOptions = {
- color: '#646566',
- font: 'Sans',
- size: 20,
- opacity: 0.8,
-};
-/**
- * Customise dimension of captcha image.
- * @typedef SetDimensionOption
- * @property {integer} [height=100] Height of captcha image.
- * @property {integer} [width=300] Width of captcha image.
- */
-exports.defaultDimension = {
- height: 100,
- width: 300
-};
diff --git a/js-script/extra.d.ts b/js-script/extra.d.ts
deleted file mode 100644
index e7be30a..0000000
--- a/js-script/extra.d.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-///
-declare const captchaValue: {};
-interface captchaValueSync {
- image: Buffer;
- text: string;
-}
-interface captchaValue {
- image: Promise;
- text: string;
-}
-/**
- * Create custom captcha from scratch.
- * @async
- * @param {number} width Width of captcha image.
- * @param {number} height Height of captcha image.
- * @param {string} [text] Captcha text.
- * @returns
- */
-export declare function createCaptcha(width: number, height: number, text?: string): captchaValue;
-/**
- * Create captcha in sync mode.
- * @param {number} width captcha image width.
- * @param {number} height captcha image height.
- * @param {string} [text] Captcha text.
- * @returns
- */
-export declare function createCaptchaSync(width: number, height: number, text?: string): captchaValueSync;
-export {};
diff --git a/js-script/extra.js b/js-script/extra.js
deleted file mode 100644
index 0382234..0000000
--- a/js-script/extra.js
+++ /dev/null
@@ -1,61 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-exports.createCaptchaSync = exports.createCaptcha = void 0;
-const _1 = require(".");
-const captchaValue = {};
-/**
- * Create custom captcha from scratch.
- * @async
- * @param {number} width Width of captcha image.
- * @param {number} height Height of captcha image.
- * @param {string} [text] Captcha text.
- * @returns
- */
-function createCaptcha(width, height, text) {
- const captcha = new _1.Captcha(width, height);
- const decoyCount = Math.floor(width * height / 2500);
- captcha.addDecoy({
- total: decoyCount,
- opacity: 1
- });
- if (text) {
- captcha.drawCaptcha({ text: text });
- }
- else {
- captcha.drawCaptcha();
- text = captcha.text;
- }
- captcha.drawTrace();
- captcha.addDecoy({ opacity: 1 });
- return { image: captcha.png, text: captcha.text };
-}
-exports.createCaptcha = createCaptcha;
-;
-/**
- * Create captcha in sync mode.
- * @param {number} width captcha image width.
- * @param {number} height captcha image height.
- * @param {string} [text] Captcha text.
- * @returns
- */
-function createCaptchaSync(width, height, text) {
- const captcha = new _1.Captcha(width, height);
- const decoyCount = Math.floor(width * height / 2500);
- captcha.async = false;
- captcha.addDecoy({
- total: decoyCount,
- opacity: 1
- });
- if (text) {
- captcha.drawCaptcha({ text: text });
- }
- else {
- captcha.drawCaptcha();
- text = captcha.text;
- }
- captcha.drawTrace();
- captcha.addDecoy({ opacity: 1 });
- return { image: captcha.png, text: captcha.text };
-}
-exports.createCaptchaSync = createCaptchaSync;
-;
diff --git a/js-script/index.d.ts b/js-script/index.d.ts
deleted file mode 100644
index f81c149..0000000
--- a/js-script/index.d.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-export { createCaptcha, createCaptchaSync } from "./extra";
-export { loadImage as resolveImage } from "skia-canvas";
-export { Captcha } from "./captcha";
-export { CaptchaGenerator } from "./CaptchaGenerator";
-export declare const version: string;
diff --git a/js-script/index.js b/js-script/index.js
deleted file mode 100644
index b8df6f4..0000000
--- a/js-script/index.js
+++ /dev/null
@@ -1,13 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-exports.version = exports.CaptchaGenerator = exports.Captcha = exports.resolveImage = exports.createCaptchaSync = exports.createCaptcha = void 0;
-var extra_1 = require("./extra");
-Object.defineProperty(exports, "createCaptcha", { enumerable: true, get: function () { return extra_1.createCaptcha; } });
-Object.defineProperty(exports, "createCaptchaSync", { enumerable: true, get: function () { return extra_1.createCaptchaSync; } });
-var skia_canvas_1 = require("skia-canvas");
-Object.defineProperty(exports, "resolveImage", { enumerable: true, get: function () { return skia_canvas_1.loadImage; } });
-var captcha_1 = require("./captcha");
-Object.defineProperty(exports, "Captcha", { enumerable: true, get: function () { return captcha_1.Captcha; } });
-var CaptchaGenerator_1 = require("./CaptchaGenerator");
-Object.defineProperty(exports, "CaptchaGenerator", { enumerable: true, get: function () { return CaptchaGenerator_1.CaptchaGenerator; } });
-exports.version = require(`${__dirname}/../package.json`).version;
diff --git a/js-script/util.d.ts b/js-script/util.d.ts
deleted file mode 100644
index 9709291..0000000
--- a/js-script/util.d.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export declare function getRandom(start?: number, end?: number): number;
-export declare function randomText(characters: number): string;
-export declare function getRandomCoordinate(height: number, width: number, size: number): number[][];
diff --git a/js-script/util.js b/js-script/util.js
deleted file mode 100644
index 12c2689..0000000
--- a/js-script/util.js
+++ /dev/null
@@ -1,31 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-exports.getRandomCoordinate = exports.randomText = exports.getRandom = void 0;
-const crypto_1 = require("crypto");
-function getRandom(start = 0, end = 0) {
- return Math.round(Math.random() * Math.abs(end - start)) + Math.min(start, end);
-}
-exports.getRandom = getRandom;
-;
-function randomText(characters) {
- return (0, crypto_1.randomBytes)(characters).toString('hex').toUpperCase().substr(0, characters);
-}
-exports.randomText = randomText;
-;
-function getRandomCoordinate(height, width, size) {
- let coordinates = [];
- for (let i = 0; i < size; i++) {
- const widthGap = Math.floor(width / size);
- const coordinate = [];
- const randomWidth = widthGap * (i + 0.2);
- coordinate.push(randomWidth);
- const randomHeight = getRandom(30, height - 30);
- coordinate.push(randomHeight);
- coordinates.push(coordinate);
- }
- ;
- coordinates = coordinates.sort((a, b) => a[0] - b[0]);
- return coordinates;
-}
-exports.getRandomCoordinate = getRandomCoordinate;
-;
diff --git a/package-lock.json b/package-lock.json
index d894d90..9f7c304 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "captcha-canvas",
- "version": "3.0.3",
+ "version": "3.1.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "captcha-canvas",
- "version": "3.0.3",
+ "version": "3.1.0",
"license": "Apache-2.0",
"dependencies": {
"skia-canvas": "^0.9.25"
@@ -15,6 +15,7 @@
"@types/node": "^14.11.8",
"@types/skia-canvas": "^0.9.2",
"better-docs": "^2.3.2",
+ "canvas": "^2.8.0",
"eslint": "^7.27.0",
"jsdoc": "^3.6.7",
"typescript": "^4.0.2"
@@ -971,6 +972,56 @@
"url": "https://opencollective.com/browserslist"
}
},
+ "node_modules/canvas": {
+ "version": "2.8.0",
+ "resolved": "https://registry.npmjs.org/canvas/-/canvas-2.8.0.tgz",
+ "integrity": "sha512-gLTi17X8WY9Cf5GZ2Yns8T5lfBOcGgFehDFb+JQwDqdOoBOcECS9ZWMEAqMSVcMYwXD659J8NyzjRY/2aE+C2Q==",
+ "dev": true,
+ "hasInstallScript": true,
+ "dependencies": {
+ "@mapbox/node-pre-gyp": "^1.0.0",
+ "nan": "^2.14.0",
+ "simple-get": "^3.0.3"
+ },
+ "engines": {
+ "node": ">=6"
+ }
+ },
+ "node_modules/canvas/node_modules/decompress-response": {
+ "version": "4.2.1",
+ "resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-4.2.1.tgz",
+ "integrity": "sha512-jOSne2qbyE+/r8G1VU+G/82LBs2Fs4LAsTiLSHOCOMZQl2OKZ6i8i4IyHemTe+/yIXOtTcRQMzPcgyhoFlqPkw==",
+ "dev": true,
+ "dependencies": {
+ "mimic-response": "^2.0.0"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/canvas/node_modules/mimic-response": {
+ "version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-2.1.0.tgz",
+ "integrity": "sha512-wXqjST+SLt7R009ySCglWBCFpjUygmCIfD790/kVbiGmUgfYGuB14PiTd5DwVxSV4NcYHjzMkoj5LjQZwTQLEA==",
+ "dev": true,
+ "engines": {
+ "node": ">=8"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/sindresorhus"
+ }
+ },
+ "node_modules/canvas/node_modules/simple-get": {
+ "version": "3.1.0",
+ "resolved": "https://registry.npmjs.org/simple-get/-/simple-get-3.1.0.tgz",
+ "integrity": "sha512-bCR6cP+aTdScaQCnQKbPKtJOKDp/hj9EDLJo3Nw4y1QksqaovlW/bnptB6/c1e+qmNIDHRK+oXFDdEqBT8WzUA==",
+ "dev": true,
+ "dependencies": {
+ "decompress-response": "^4.2.0",
+ "once": "^1.3.1",
+ "simple-concat": "^1.0.0"
+ }
+ },
"node_modules/catharsis": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/catharsis/-/catharsis-0.9.0.tgz",
@@ -2424,6 +2475,12 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
+ "node_modules/nan": {
+ "version": "2.15.0",
+ "resolved": "https://registry.npmjs.org/nan/-/nan-2.15.0.tgz",
+ "integrity": "sha512-8ZtvEnA2c5aYCZYd1cvgdnU6cqwixRoYg70xPLWUws5ORTa/lnw+u4amixRS/Ac5U5mQVgp9pnlSUnbNWFaWZQ==",
+ "dev": true
+ },
"node_modules/nanocolors": {
"version": "0.2.12",
"resolved": "https://registry.npmjs.org/nanocolors/-/nanocolors-0.2.12.tgz",
@@ -4638,6 +4695,45 @@
"integrity": "sha512-doiV5dft6yzWO1WwU19kt8Qz8R0/8DgEziz6/9n2FxUasteZNwNNYSmJO3GLBH8lCVE73AB1RPDPAeYbcO5Cvw==",
"dev": true
},
+ "canvas": {
+ "version": "2.8.0",
+ "resolved": "https://registry.npmjs.org/canvas/-/canvas-2.8.0.tgz",
+ "integrity": "sha512-gLTi17X8WY9Cf5GZ2Yns8T5lfBOcGgFehDFb+JQwDqdOoBOcECS9ZWMEAqMSVcMYwXD659J8NyzjRY/2aE+C2Q==",
+ "dev": true,
+ "requires": {
+ "@mapbox/node-pre-gyp": "^1.0.0",
+ "nan": "^2.14.0",
+ "simple-get": "^3.0.3"
+ },
+ "dependencies": {
+ "decompress-response": {
+ "version": "4.2.1",
+ "resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-4.2.1.tgz",
+ "integrity": "sha512-jOSne2qbyE+/r8G1VU+G/82LBs2Fs4LAsTiLSHOCOMZQl2OKZ6i8i4IyHemTe+/yIXOtTcRQMzPcgyhoFlqPkw==",
+ "dev": true,
+ "requires": {
+ "mimic-response": "^2.0.0"
+ }
+ },
+ "mimic-response": {
+ "version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-2.1.0.tgz",
+ "integrity": "sha512-wXqjST+SLt7R009ySCglWBCFpjUygmCIfD790/kVbiGmUgfYGuB14PiTd5DwVxSV4NcYHjzMkoj5LjQZwTQLEA==",
+ "dev": true
+ },
+ "simple-get": {
+ "version": "3.1.0",
+ "resolved": "https://registry.npmjs.org/simple-get/-/simple-get-3.1.0.tgz",
+ "integrity": "sha512-bCR6cP+aTdScaQCnQKbPKtJOKDp/hj9EDLJo3Nw4y1QksqaovlW/bnptB6/c1e+qmNIDHRK+oXFDdEqBT8WzUA==",
+ "dev": true,
+ "requires": {
+ "decompress-response": "^4.2.0",
+ "once": "^1.3.1",
+ "simple-concat": "^1.0.0"
+ }
+ }
+ }
+ },
"catharsis": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/catharsis/-/catharsis-0.9.0.tgz",
@@ -5768,6 +5864,12 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
+ "nan": {
+ "version": "2.15.0",
+ "resolved": "https://registry.npmjs.org/nan/-/nan-2.15.0.tgz",
+ "integrity": "sha512-8ZtvEnA2c5aYCZYd1cvgdnU6cqwixRoYg70xPLWUws5ORTa/lnw+u4amixRS/Ac5U5mQVgp9pnlSUnbNWFaWZQ==",
+ "dev": true
+ },
"nanocolors": {
"version": "0.2.12",
"resolved": "https://registry.npmjs.org/nanocolors/-/nanocolors-0.2.12.tgz",
diff --git a/package.json b/package.json
index 8da922a..f2c9eb7 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "captcha-canvas",
- "version": "3.0.4",
+ "version": "3.1.0",
"description": "A captcha generator by using skia-canvas module.",
"main": "./js-script/index.js",
"files": [
@@ -32,6 +32,7 @@
"@types/node": "^14.11.8",
"@types/skia-canvas": "^0.9.2",
"better-docs": "^2.3.2",
+ "canvas": "^2.8.0",
"eslint": "^7.27.0",
"jsdoc": "^3.6.7",
"typescript": "^4.0.2"
diff --git a/test.js b/test.js
deleted file mode 100644
index e87e650..0000000
--- a/test.js
+++ /dev/null
@@ -1,9 +0,0 @@
-"use strict";
-exports.__esModule = true;
-var fs_1 = require("fs");
-var js_script_1 = require("./js-script");
-var captcha = new js_script_1.CaptchaGenerator()
- .setDimension(200, 200);
-console.log(__filename + captcha.text);
-(0, fs_1.writeFileSync)('assets/CaptchaGenerator/' + captcha.text + 'captcha.png', captcha.generateSync());
-console.log(captcha.text);
diff --git a/test.ts b/test.ts
deleted file mode 100644
index 02c1c0f..0000000
--- a/test.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-import { writeFileSync } from "fs";
-import { CaptchaGenerator } from "./js-script";
-
-const captcha = new CaptchaGenerator()
-.setDimension(200, 200);
-console.log(__filename + captcha.text);
-writeFileSync('assets/CaptchaGenerator/' + captcha.text + 'captcha.png', captcha.generateSync());
-
-console.log(captcha.text);
diff --git a/ts-script/CaptchaGenerator.ts b/ts-script/CaptchaGenerator.ts
index 522aa9f..7e11275 100644
--- a/ts-script/CaptchaGenerator.ts
+++ b/ts-script/CaptchaGenerator.ts
@@ -26,14 +26,14 @@ export class CaptchaGenerator {
this.captcha = defaultCaptchaOption;
this.trace = defaultTraceOptions;
this.decoy = defaultDecoyOptions;
- this.captcha.text = randomText(this.captcha.characters);
+ this.captcha.text = randomText(this.captcha.characters || 6);
}
/**
* Get the text of captcha.
* @type {string}
* @since 2.0.3
*/
- get text(): string {
+ get text(): string | undefined {
return this.captcha.text;
}
/**
diff --git a/ts-script/captcha.ts b/ts-script/captcha.ts
index ea37812..4710583 100644
--- a/ts-script/captcha.ts
+++ b/ts-script/captcha.ts
@@ -45,9 +45,9 @@ export class Captcha {
}
/**
* Get png image of captcha.
- * @returns {Buffer} Get png image of captcha created.
+ * @returns {Buffer | Promise} Get png image of captcha created.
*/
- get png(): Buffer {
+ get png(): Buffer | Promise {
this._canvas.async = this.async;
return this._canvas.png;
}
@@ -57,7 +57,7 @@ export class Captcha {
* @returns {Captcha}
*/
drawImage(image: Image): Captcha {
- this._ctx.drawImage(image, 0, 0);
+ this._ctx.drawImage(image, 0, 0, this._width, this._height);
return this;
}
/**
@@ -111,8 +111,8 @@ export class Captcha {
drawCaptcha(captchaOption: SetCaptchaOption = {}): Captcha {
const option = { ...this._captcha, ...captchaOption };
if(captchaOption.text) option.characters = captchaOption.text.length;
- if(!captchaOption.text && captchaOption.characters) option.text = randomText(option.characters);
- if(!option.text) option.text = randomText(option.characters);
+ if(!captchaOption.text && captchaOption.characters) option.text = randomText(option.characters || 6);
+ if(!option.text) option.text = randomText(option.characters || 6);
this._captcha = option;
if(!this._coordinates[0]) this._coordinates = getRandomCoordinate(this._height, this._width, option.characters || 6);
@@ -126,12 +126,16 @@ export class Captcha {
this._ctx.save();
this._ctx.translate(coordinates[n][0], coordinates[n][1]);
if (option.skew) {this._ctx.transform(1, Math.random(), getRandom(20) / 100, 1, 0, 0);}
- if (option.rotate > 0) {this._ctx.rotate(getRandom(-option.rotate, option.rotate) * Math.PI / 180);}
- if (option.colors?.length > 2) {this._ctx.fillStyle = option.colors[getRandom(option.colors.length - 1)];}
+ if (option.rotate && option.rotate > 0) {this._ctx.rotate(getRandom(-option.rotate, option.rotate) * Math.PI / 180);}
+ if (option.colors && option.colors?.length > 2) {this._ctx.fillStyle = option.colors[getRandom(option.colors.length - 1)];}
this._ctx.fillText(option.text[n], 0, 0);
this._ctx.restore();
};
return this;
}
+
+ toBuffer() {
+ this._canvas.toBuffer('png');
+ }
}
diff --git a/ts-script/constants.ts b/ts-script/constants.ts
index cbc480e..dd30c19 100644
--- a/ts-script/constants.ts
+++ b/ts-script/constants.ts
@@ -1,3 +1,5 @@
+import { Image } from "skia-canvas";
+
export interface SetDimensionOption {
height: number;
width: number;
@@ -28,6 +30,13 @@ export interface SetTraceOption {
size?: number;
opacity?: number;
};
+
+export interface CreateCaptchaOptions {
+ captcha?: SetCaptchaOption;
+ trace?: SetTraceOption;
+ decoy?: SetDecoyOption;
+ background?: Image;
+}
/**
* Captcha text options to customise text appearance and value.
* @typedef SetCaptchaOptions
@@ -86,3 +95,12 @@ export const defaultDimension: SetDimensionOption = {
height: 100,
width: 300
};
+/**
+ * Create captcha options in functions.
+ * @typedef CreateCaptchaOptions
+ * @property {SetCaptchaOptions} [captcha] Captcha text options to customise text appearance and value.
+ * @property {SetDecoyOptions} [decoy]
+ * @property {SetTraceOptions} [trace]
+ * @property {Image} [background]
+ */
+export const CreateCaptchaOptions = {};
\ No newline at end of file
diff --git a/ts-script/extra.ts b/ts-script/extra.ts
index 26314bb..e5b37ee 100644
--- a/ts-script/extra.ts
+++ b/ts-script/extra.ts
@@ -1,6 +1,6 @@
import { Captcha } from ".";
+import { CreateCaptchaOptions } from "./constants";
-const captchaValue = {}
interface captchaValueSync {
image: Buffer,
text: string
@@ -10,32 +10,31 @@ interface captchaValue {
image: Promise,
text: string
}
+
/**
* Create custom captcha from scratch.
* @async
* @param {number} width Width of captcha image.
* @param {number} height Height of captcha image.
- * @param {string} [text] Captcha text.
+ * @param {CreateCaptchaOptions} [option] Captcha text.
* @returns
*/
-export function createCaptcha(width: number, height: number, text?: string): captchaValue {
+export function createCaptcha(width: number, height: number, option: CreateCaptchaOptions = {}): captchaValue {
const captcha = new Captcha(width, height);
const decoyCount = Math.floor(width*height/2500);
- captcha.addDecoy({
- total: decoyCount,
- opacity: 1
- });
+ if(!option.decoy) option.decoy = {};
+ if(!option.decoy.total) option.decoy.total = decoyCount;
+
+ captcha.addDecoy(option.decoy);
+
+
+ captcha.drawCaptcha(option.captcha);
+ const text = captcha.text;
- if (text) {
- captcha.drawCaptcha({ text: text });
- } else {
- captcha.drawCaptcha();
- text = captcha.text;
- }
+ captcha.drawTrace(option.trace);
- captcha.drawTrace();
- captcha.addDecoy({opacity: 1});
+ captcha.addDecoy({ opacity: 1 });
return { image: captcha.png, text: captcha.text };
};
@@ -43,28 +42,25 @@ export function createCaptcha(width: number, height: number, text?: string): cap
* Create captcha in sync mode.
* @param {number} width captcha image width.
* @param {number} height captcha image height.
- * @param {string} [text] Captcha text.
+ * @param {CreateCaptchaOptions} [option] Captcha text.
* @returns
*/
-export function createCaptchaSync(width: number, height: number, text?: string): captchaValueSync {
+export function createCaptchaSync(width: number, height: number, option: CreateCaptchaOptions = {}): captchaValueSync {
const captcha = new Captcha(width, height);
const decoyCount = Math.floor(width*height/2500);
captcha.async = false;
- captcha.addDecoy({
- total: decoyCount,
- opacity: 1
- });
+ if(!option.decoy) option.decoy = {};
+ if(!option.decoy.total) option.decoy.total = decoyCount;
+
+ captcha.addDecoy(option.decoy);
+
+
+ captcha.drawCaptcha(option.captcha);
- if (text) {
- captcha.drawCaptcha({ text: text });
- } else {
- captcha.drawCaptcha();
- text = captcha.text;
- }
+ captcha.drawTrace(option.trace);
- captcha.drawTrace();
- captcha.addDecoy({opacity: 1});
+ captcha.addDecoy({ opacity: 1 });
return { image: captcha.png, text: captcha.text };
};
|