"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);
-};
-var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
-};
-var __generator = (this && this.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [op[0] & 2, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
-};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CaptchaGenerator = void 0;
-var skia_canvas_1 = require("skia-canvas");
-var _1 = require(".");
-var constants_1 = require("./constants");
-var util_1 = require("./util");
+const skia_canvas_1 = require("skia-canvas");
+const _1 = require(".");
+const constants_1 = require("./constants");
+const util_1 = require("./util");
/**
* Captcha generator class.
*/
-var CaptchaGenerator = /** @class */ (function () {
+class CaptchaGenerator {
/**
* Initatiates the creation of captcha image generation.
* @example const captcha = new CaptchaGenerator({height: 200, width: 600});
@@ -151,8 +104,7 @@ CaptchaGenerator.js
* @param {integer} [options.width=300] Width of captcha image.
* @since 2.0.0
*/
- function CaptchaGenerator(options) {
- if (options === void 0) { options = { height: 100, width: 300 }; }
+ constructor(options = { height: 100, width: 300 }) {
this.height = options.height;
this.width = options.width;
this.captcha = constants_1.defaultCaptchaOption;
@@ -160,18 +112,14 @@ CaptchaGenerator.js
this.decoy = constants_1.defaultDecoyOptions;
this.captcha.text = (0, util_1.randomText)(this.captcha.characters);
}
- Object.defineProperty(CaptchaGenerator.prototype, "text", {
- /**
- * Get the text of captcha.
- * @type {string}
- * @since 2.0.3
- */
- get: function () {
- return this.captcha.text;
- },
- enumerable: false,
- configurable: true
- });
+ /**
+ * 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.
@@ -186,11 +134,11 @@ CaptchaGenerator.js
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
- CaptchaGenerator.prototype.setDimension = function (height, width) {
+ setDimension(height, width) {
this.height = height;
this.width = width;
return this;
- };
+ }
/**
* Set background for captcha image.
* @param {buffer} image Buffer/url/path of image.
@@ -204,10 +152,10 @@ CaptchaGenerator.js
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
- CaptchaGenerator.prototype.setBackground = function (image) {
+ setBackground(image) {
this.background = image;
return this;
- };
+ }
/**
* Change captcha text options
* @param {SetCaptchaOptions} options Captcha appearance options.
@@ -222,14 +170,14 @@ CaptchaGenerator.js
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
- CaptchaGenerator.prototype.setCaptcha = function (option) {
- this.captcha = __assign(__assign({}, this.captcha), option);
+ 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.
@@ -244,19 +192,19 @@ CaptchaGenerator.js
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
- CaptchaGenerator.prototype.setTrace = function (option) {
- this.trace = __assign(__assign({}, this.trace), option);
+ setTrace(option) {
+ this.trace = { ...this.trace, ...option };
return this;
- };
+ }
/**
* Change decoy options
* @param {SetDecoyOptions} options Decoy characters customisation options
* @since 2.0.0
*/
- CaptchaGenerator.prototype.setDecoy = function (option) {
- this.decoy = __assign(__assign({}, this.decoy), option);
+ setDecoy(option) {
+ this.decoy = { ...this.decoy, ...option };
return this;
- };
+ }
/**
* Method which returns image buffer
* @async
@@ -270,31 +218,18 @@ CaptchaGenerator.js
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
- CaptchaGenerator.prototype.generate = function () {
- return __awaiter(this, void 0, void 0, function () {
- var captchaCanvas, _a, _b;
- return __generator(this, function (_c) {
- switch (_c.label) {
- case 0:
- captchaCanvas = new _1.Captcha(this.width, this.height);
- if (!this.background) return [3 /*break*/, 2];
- _b = (_a = captchaCanvas).drawImage;
- return [4 /*yield*/, (0, skia_canvas_1.loadImage)(this.background)];
- case 1:
- _b.apply(_a, [_c.sent()]);
- _c.label = 2;
- case 2:
- 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 [2 /*return*/, captchaCanvas.png];
- }
- });
- });
- };
+ 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
@@ -312,9 +247,8 @@ CaptchaGenerator.js
* fs.writeFileSync("image.png", captcha);
* @since 2.2.0
*/
- CaptchaGenerator.prototype.generateSync = function (option) {
- if (option === void 0) { option = {}; }
- var captchaCanvas = new _1.Captcha(this.width, this.height);
+ generateSync(option = {}) {
+ const captchaCanvas = new _1.Captcha(this.width, this.height);
captchaCanvas.async = false;
if (option.background)
captchaCanvas.drawImage(option.background);
@@ -325,9 +259,8 @@ CaptchaGenerator.js
if (this.trace.opacity)
captchaCanvas.drawTrace(this.trace);
return captchaCanvas.png;
- };
- return CaptchaGenerator;
-}());
+ }
+}
exports.CaptchaGenerator = CaptchaGenerator;
diff --git a/docs/captcha.js.html b/docs/captcha.js.html
index 26836da..42d544e 100644
--- a/docs/captcha.js.html
+++ b/docs/captcha.js.html
@@ -68,7 +68,7 @@
"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;
-var skia_canvas_1 = require("skia-canvas");
-var constants_1 = require("./constants");
-var util_1 = require("./util");
+const skia_canvas_1 = require("skia-canvas");
+const constants_1 = require("./constants");
+const util_1 = require("./util");
/**
* Captcha Generator
*/
-var Captcha = /** @class */ (function () {
+class Captcha {
/**
* Start captcha image creation.
* @param {number} [width] Width of captcha image.
* @param {number} [height] Height of captcha image.
* @constructor
*/
- function Captcha(width, height) {
- if (width === void 0) { width = constants_1.defaultDimension.width; }
- if (height === void 0) { height = constants_1.defaultDimension.height; }
+ 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;
- var canvas = new skia_canvas_1.Canvas(width, height);
- var ctx = canvas.getContext('2d');
+ const canvas = new skia_canvas_1.Canvas(width, height);
+ const ctx = canvas.getContext('2d');
ctx.lineJoin = 'miter';
ctx.textBaseline = 'middle';
this._canvas = canvas;
@@ -129,57 +116,48 @@ captcha.js
this.async = true;
this._coordinates = [];
}
- 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
- });
+ /**
+ * 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}
*/
- Captcha.prototype.drawImage = function (image) {
+ 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}
*/
- Captcha.prototype.addDecoy = function (decoyOption) {
- if (decoyOption === void 0) { decoyOption = {}; }
- var option = __assign(__assign({}, this._decoy), decoyOption);
+ addDecoy(decoyOption = {}) {
+ const option = { ...this._decoy, ...decoyOption };
if (!option.total)
option.total = Math.floor(this._width * this._height / 10000);
- var decoyText = (0, util_1.randomText)(option.total);
- this._ctx.font = option.size + "px " + option.font;
+ 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 (var i = 0; i < decoyText.length; i++) {
+ 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.
*
@@ -187,32 +165,30 @@ captcha.js
* @param {SetTraceOptions} [traceOption]
* @returns {Captcha}
*/
- Captcha.prototype.drawTrace = function (traceOption) {
- if (traceOption === void 0) { traceOption = {}; }
- var option = __assign(__assign({}, this._trace), traceOption);
+ 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);
- var coordinates = this._coordinates;
+ 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 (var i = 1; i < coordinates.length; i++) {
+ 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}
*/
- Captcha.prototype.drawCaptcha = function (captchaOption) {
+ drawCaptcha(captchaOption = {}) {
var _a;
- if (captchaOption === void 0) { captchaOption = {}; }
- var option = __assign(__assign({}, this._captcha), captchaOption);
+ const option = { ...this._captcha, ...captchaOption };
if (captchaOption.text)
option.characters = captchaOption.text.length;
if (!captchaOption.text && captchaOption.characters)
@@ -222,11 +198,11 @@ captcha.js
this._captcha = option;
if (!this._coordinates[0])
this._coordinates = (0, util_1.getRandomCoordinate)(this._height, this._width, option.characters || 6);
- var coordinates = this._coordinates;
- this._ctx.font = option.size + "px " + option.font;
+ const coordinates = this._coordinates;
+ this._ctx.font = `${option.size}px ${option.font}`;
this._ctx.globalAlpha = option.opacity;
this._ctx.fillStyle = option.color;
- for (var n = 0; n < coordinates.length; n++) {
+ for (let n = 0; n < coordinates.length; n++) {
this._ctx.save();
this._ctx.translate(coordinates[n][0], coordinates[n][1]);
if (option.skew) {
@@ -243,9 +219,8 @@ captcha.js
}
;
return this;
- };
- return Captcha;
-}());
+ }
+}
exports.Captcha = Captcha;
diff --git a/docs/constants.js.html b/docs/constants.js.html
index 5be2a53..a588f31 100644
--- a/docs/constants.js.html
+++ b/docs/constants.js.html
@@ -68,7 +68,7 @@
-
+
diff --git a/docs/extra.js.html b/docs/extra.js.html
index bdcce8b..47a2600 100644
--- a/docs/extra.js.html
+++ b/docs/extra.js.html
@@ -68,7 +68,7 @@
-
+
@@ -88,8 +88,8 @@ extra.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCaptchaSync = exports.createCaptcha = void 0;
-var _1 = require(".");
-var captchaValue = {};
+const _1 = require(".");
+const captchaValue = {};
/**
* Create custom captcha from scratch.
* @async
@@ -99,8 +99,8 @@ extra.js
* @returns
*/
function createCaptcha(width, height, text) {
- var captcha = new _1.Captcha(width, height);
- var decoyCount = Math.floor(width * height / 2500);
+ const captcha = new _1.Captcha(width, height);
+ const decoyCount = Math.floor(width * height / 2500);
captcha.addDecoy({
total: decoyCount,
opacity: 1
@@ -126,8 +126,8 @@ extra.js
* @returns
*/
function createCaptchaSync(width, height, text) {
- var captcha = new _1.Captcha(width, height);
- var decoyCount = Math.floor(width * height / 2500);
+ const captcha = new _1.Captcha(width, height);
+ const decoyCount = Math.floor(width * height / 2500);
captcha.async = false;
captcha.addDecoy({
total: decoyCount,
diff --git a/docs/global.html b/docs/global.html
index 374e2fd..0b91d3b 100644
--- a/docs/global.html
+++ b/docs/global.html
@@ -66,7 +66,7 @@
-
+
@@ -150,151 +150,6 @@
-
-
Members
-
-
-
-
-
- #
-
-
- Captcha
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- View Source
-
- captcha.js , line 21
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #
-
-
- CaptchaGenerator
-
-
-
-
-
-
-
-
-
Captcha generator class.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- View Source
-
- CaptchaGenerator.js , line 58
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -784,350 +639,6 @@
Parameters:
-
-
-
-
-
-
-
-
- #
-
-
-
- get() → {string}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- View Source
-
- captcha.js , line 50
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
-
-
-
-
-
- Type:
-
-string
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #
-
-
-
- get() → {Buffer}
-
-
-
-
-
-
-
-
-
Get png image of captcha.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- View Source
-
- captcha.js , line 61
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Returns:
-
-
-
-
-
-
-
Get png image of captcha created.
-
-
-
- Type:
-
-Buffer
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #
-
-
-
- get()
-
-
-
-
-
-
-
-
-
Get the text of captcha.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Since:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- View Source
-
- CaptchaGenerator.js , line 82
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/docs/index.html b/docs/index.html
index f3aebc1..200979d 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -66,7 +66,7 @@
-
+
diff --git a/js-script/CaptchaGenerator.d.ts b/js-script/CaptchaGenerator.d.ts
index 6730c82..7e816d3 100644
--- a/js-script/CaptchaGenerator.d.ts
+++ b/js-script/CaptchaGenerator.d.ts
@@ -10,7 +10,7 @@ export declare class CaptchaGenerator {
private captcha;
private trace;
private decoy;
- private background;
+ private background?;
/**
* Initatiates the creation of captcha image generation.
* @example const captcha = new CaptchaGenerator({height: 200, width: 600});
diff --git a/js-script/CaptchaGenerator.js b/js-script/CaptchaGenerator.js
index 89af15c..46ec47f 100644
--- a/js-script/CaptchaGenerator.js
+++ b/js-script/CaptchaGenerator.js
@@ -1,61 +1,14 @@
"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);
-};
-var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
-};
-var __generator = (this && this.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [op[0] & 2, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
-};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CaptchaGenerator = void 0;
-var skia_canvas_1 = require("skia-canvas");
-var _1 = require(".");
-var constants_1 = require("./constants");
-var util_1 = require("./util");
+const skia_canvas_1 = require("skia-canvas");
+const _1 = require(".");
+const constants_1 = require("./constants");
+const util_1 = require("./util");
/**
* Captcha generator class.
*/
-var CaptchaGenerator = /** @class */ (function () {
+class CaptchaGenerator {
/**
* Initatiates the creation of captcha image generation.
* @example const captcha = new CaptchaGenerator({height: 200, width: 600});
@@ -64,8 +17,7 @@ var CaptchaGenerator = /** @class */ (function () {
* @param {integer} [options.width=300] Width of captcha image.
* @since 2.0.0
*/
- function CaptchaGenerator(options) {
- if (options === void 0) { options = { height: 100, width: 300 }; }
+ constructor(options = { height: 100, width: 300 }) {
this.height = options.height;
this.width = options.width;
this.captcha = constants_1.defaultCaptchaOption;
@@ -73,18 +25,14 @@ var CaptchaGenerator = /** @class */ (function () {
this.decoy = constants_1.defaultDecoyOptions;
this.captcha.text = (0, util_1.randomText)(this.captcha.characters);
}
- Object.defineProperty(CaptchaGenerator.prototype, "text", {
- /**
- * Get the text of captcha.
- * @type {string}
- * @since 2.0.3
- */
- get: function () {
- return this.captcha.text;
- },
- enumerable: false,
- configurable: true
- });
+ /**
+ * 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.
@@ -99,11 +47,11 @@ var CaptchaGenerator = /** @class */ (function () {
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
- CaptchaGenerator.prototype.setDimension = function (height, width) {
+ setDimension(height, width) {
this.height = height;
this.width = width;
return this;
- };
+ }
/**
* Set background for captcha image.
* @param {buffer} image Buffer/url/path of image.
@@ -117,10 +65,10 @@ var CaptchaGenerator = /** @class */ (function () {
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
- CaptchaGenerator.prototype.setBackground = function (image) {
+ setBackground(image) {
this.background = image;
return this;
- };
+ }
/**
* Change captcha text options
* @param {SetCaptchaOptions} options Captcha appearance options.
@@ -135,14 +83,14 @@ var CaptchaGenerator = /** @class */ (function () {
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
- CaptchaGenerator.prototype.setCaptcha = function (option) {
- this.captcha = __assign(__assign({}, this.captcha), option);
+ 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.
@@ -157,19 +105,19 @@ var CaptchaGenerator = /** @class */ (function () {
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
- CaptchaGenerator.prototype.setTrace = function (option) {
- this.trace = __assign(__assign({}, this.trace), option);
+ setTrace(option) {
+ this.trace = { ...this.trace, ...option };
return this;
- };
+ }
/**
* Change decoy options
* @param {SetDecoyOptions} options Decoy characters customisation options
* @since 2.0.0
*/
- CaptchaGenerator.prototype.setDecoy = function (option) {
- this.decoy = __assign(__assign({}, this.decoy), option);
+ setDecoy(option) {
+ this.decoy = { ...this.decoy, ...option };
return this;
- };
+ }
/**
* Method which returns image buffer
* @async
@@ -183,31 +131,18 @@ var CaptchaGenerator = /** @class */ (function () {
* fs.writeFileSync("image.png", buffer)
* @since 2.0.0
*/
- CaptchaGenerator.prototype.generate = function () {
- return __awaiter(this, void 0, void 0, function () {
- var captchaCanvas, _a, _b;
- return __generator(this, function (_c) {
- switch (_c.label) {
- case 0:
- captchaCanvas = new _1.Captcha(this.width, this.height);
- if (!this.background) return [3 /*break*/, 2];
- _b = (_a = captchaCanvas).drawImage;
- return [4 /*yield*/, (0, skia_canvas_1.loadImage)(this.background)];
- case 1:
- _b.apply(_a, [_c.sent()]);
- _c.label = 2;
- case 2:
- 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 [2 /*return*/, captchaCanvas.png];
- }
- });
- });
- };
+ 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
@@ -225,9 +160,8 @@ var CaptchaGenerator = /** @class */ (function () {
* fs.writeFileSync("image.png", captcha);
* @since 2.2.0
*/
- CaptchaGenerator.prototype.generateSync = function (option) {
- if (option === void 0) { option = {}; }
- var captchaCanvas = new _1.Captcha(this.width, this.height);
+ generateSync(option = {}) {
+ const captchaCanvas = new _1.Captcha(this.width, this.height);
captchaCanvas.async = false;
if (option.background)
captchaCanvas.drawImage(option.background);
@@ -238,7 +172,6 @@ var CaptchaGenerator = /** @class */ (function () {
if (this.trace.opacity)
captchaCanvas.drawTrace(this.trace);
return captchaCanvas.png;
- };
- return CaptchaGenerator;
-}());
+ }
+}
exports.CaptchaGenerator = CaptchaGenerator;
diff --git a/js-script/captcha.js b/js-script/captcha.js
index f34eaab..7cef369 100644
--- a/js-script/captcha.js
+++ b/js-script/captcha.js
@@ -1,40 +1,27 @@
"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;
-var skia_canvas_1 = require("skia-canvas");
-var constants_1 = require("./constants");
-var util_1 = require("./util");
+const skia_canvas_1 = require("skia-canvas");
+const constants_1 = require("./constants");
+const util_1 = require("./util");
/**
* Captcha Generator
*/
-var Captcha = /** @class */ (function () {
+class Captcha {
/**
* Start captcha image creation.
* @param {number} [width] Width of captcha image.
* @param {number} [height] Height of captcha image.
* @constructor
*/
- function Captcha(width, height) {
- if (width === void 0) { width = constants_1.defaultDimension.width; }
- if (height === void 0) { height = constants_1.defaultDimension.height; }
+ 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;
- var canvas = new skia_canvas_1.Canvas(width, height);
- var ctx = canvas.getContext('2d');
+ const canvas = new skia_canvas_1.Canvas(width, height);
+ const ctx = canvas.getContext('2d');
ctx.lineJoin = 'miter';
ctx.textBaseline = 'middle';
this._canvas = canvas;
@@ -42,57 +29,48 @@ var Captcha = /** @class */ (function () {
this.async = true;
this._coordinates = [];
}
- 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
- });
+ /**
+ * 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}
*/
- Captcha.prototype.drawImage = function (image) {
+ 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}
*/
- Captcha.prototype.addDecoy = function (decoyOption) {
- if (decoyOption === void 0) { decoyOption = {}; }
- var option = __assign(__assign({}, this._decoy), decoyOption);
+ addDecoy(decoyOption = {}) {
+ const option = { ...this._decoy, ...decoyOption };
if (!option.total)
option.total = Math.floor(this._width * this._height / 10000);
- var decoyText = (0, util_1.randomText)(option.total);
- this._ctx.font = option.size + "px " + option.font;
+ 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 (var i = 0; i < decoyText.length; i++) {
+ 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.
*
@@ -100,32 +78,30 @@ var Captcha = /** @class */ (function () {
* @param {SetTraceOptions} [traceOption]
* @returns {Captcha}
*/
- Captcha.prototype.drawTrace = function (traceOption) {
- if (traceOption === void 0) { traceOption = {}; }
- var option = __assign(__assign({}, this._trace), traceOption);
+ 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);
- var coordinates = this._coordinates;
+ 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 (var i = 1; i < coordinates.length; i++) {
+ 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}
*/
- Captcha.prototype.drawCaptcha = function (captchaOption) {
+ drawCaptcha(captchaOption = {}) {
var _a;
- if (captchaOption === void 0) { captchaOption = {}; }
- var option = __assign(__assign({}, this._captcha), captchaOption);
+ const option = { ...this._captcha, ...captchaOption };
if (captchaOption.text)
option.characters = captchaOption.text.length;
if (!captchaOption.text && captchaOption.characters)
@@ -135,11 +111,11 @@ var Captcha = /** @class */ (function () {
this._captcha = option;
if (!this._coordinates[0])
this._coordinates = (0, util_1.getRandomCoordinate)(this._height, this._width, option.characters || 6);
- var coordinates = this._coordinates;
- this._ctx.font = option.size + "px " + option.font;
+ const coordinates = this._coordinates;
+ this._ctx.font = `${option.size}px ${option.font}`;
this._ctx.globalAlpha = option.opacity;
this._ctx.fillStyle = option.color;
- for (var n = 0; n < coordinates.length; n++) {
+ for (let n = 0; n < coordinates.length; n++) {
this._ctx.save();
this._ctx.translate(coordinates[n][0], coordinates[n][1]);
if (option.skew) {
@@ -156,7 +132,6 @@ var Captcha = /** @class */ (function () {
}
;
return this;
- };
- return Captcha;
-}());
+ }
+}
exports.Captcha = Captcha;
diff --git a/js-script/extra.js b/js-script/extra.js
index 606d110..0382234 100644
--- a/js-script/extra.js
+++ b/js-script/extra.js
@@ -1,8 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCaptchaSync = exports.createCaptcha = void 0;
-var _1 = require(".");
-var captchaValue = {};
+const _1 = require(".");
+const captchaValue = {};
/**
* Create custom captcha from scratch.
* @async
@@ -12,8 +12,8 @@ var captchaValue = {};
* @returns
*/
function createCaptcha(width, height, text) {
- var captcha = new _1.Captcha(width, height);
- var decoyCount = Math.floor(width * height / 2500);
+ const captcha = new _1.Captcha(width, height);
+ const decoyCount = Math.floor(width * height / 2500);
captcha.addDecoy({
total: decoyCount,
opacity: 1
@@ -39,8 +39,8 @@ exports.createCaptcha = createCaptcha;
* @returns
*/
function createCaptchaSync(width, height, text) {
- var captcha = new _1.Captcha(width, height);
- var decoyCount = Math.floor(width * height / 2500);
+ const captcha = new _1.Captcha(width, height);
+ const decoyCount = Math.floor(width * height / 2500);
captcha.async = false;
captcha.addDecoy({
total: decoyCount,
diff --git a/js-script/index.js b/js-script/index.js
index edcdf60..b8df6f4 100644
--- a/js-script/index.js
+++ b/js-script/index.js
@@ -10,4 +10,4 @@ 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;
+exports.version = require(`${__dirname}/../package.json`).version;
diff --git a/js-script/util.js b/js-script/util.js
index 2bae758..12c2689 100644
--- a/js-script/util.js
+++ b/js-script/util.js
@@ -1,10 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRandomCoordinate = exports.randomText = exports.getRandom = void 0;
-var crypto_1 = require("crypto");
-function getRandom(start, end) {
- if (start === void 0) { start = 0; }
- if (end === void 0) { end = 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;
@@ -15,18 +13,18 @@ function randomText(characters) {
exports.randomText = randomText;
;
function getRandomCoordinate(height, width, size) {
- var coordinates = [];
- for (var i = 0; i < size; i++) {
- var widthGap = Math.floor(width / size);
- var coordinate = [];
- var randomWidth = widthGap * (i + 0.2);
+ 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);
- var randomHeight = getRandom(30, height - 30);
+ const randomHeight = getRandom(30, height - 30);
coordinate.push(randomHeight);
coordinates.push(coordinate);
}
;
- coordinates = coordinates.sort(function (a, b) { return a[0] - b[0]; });
+ coordinates = coordinates.sort((a, b) => a[0] - b[0]);
return coordinates;
}
exports.getRandomCoordinate = getRandomCoordinate;
diff --git a/tsconfig.json b/tsconfig.json
index 55c6ec2..5d6695e 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,12 +1,24 @@
{
"compilerOptions": {
- "target": "es5",
- "module": "commonjs",
- "declaration": true,
- "outDir": "./js-script",
"strict": true,
- "strictNullChecks": false,
- "resolveJsonModule": true
+ "moduleResolution": "node",
+ "declaration": true,
+ "removeComments": false,
+ "alwaysStrict": true,
+ "pretty": false,
+ "module": "commonjs",
+ "target": "es2019",
+ "lib": [
+ "esnext",
+ "esnext.array",
+ "esnext.asynciterable",
+ "esnext.intl",
+ "esnext.symbol"
+ ],
+ "sourceMap": false,
+ "skipDefaultLibCheck": true,
+ "outDir": "./js-script"
},
- "include": [ "ts-script" ]
+ "exclude": [".github/", "assets/", "docs/", "examples/", "node_modules/"],
+ "include": ["ts-script/"]
}
\ No newline at end of file