captcha-generator-alphanumeric
Advanced tools
Comparing version 1.8.1 to 1.9.0
@@ -6,3 +6,3 @@ import * as Canvas from "canvas"; | ||
private _color; | ||
constructor(_h?: number); | ||
constructor(_h?: number, _text?: string); | ||
get value(): string; | ||
@@ -13,3 +13,3 @@ get PNGStream(): Canvas.PNGStream; | ||
} | ||
export = Captcha; | ||
export default Captcha; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Canvas = require("canvas"); | ||
const crypto = require("crypto"); | ||
Canvas.registerFont(require("path").resolve(__dirname, "../assets/Swift.ttf"), { | ||
family: "swift" | ||
}); | ||
const randomText = () => Math.random() | ||
.toString(36) | ||
.replace(/[^a-z1-9]|[gkqrxi5ovm]+/gi, "") | ||
.substring(0, 6) | ||
.toUpperCase(), shuffleArray = (arr) => { | ||
const alphanumeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' | ||
.replace(/[RGQIOVMI01]+/g, ""); // excludes | ||
function randomize(seed, len) { | ||
const sourceArray = seed.split(''); | ||
let baselen = typeof len === 'undefined' ? sourceArray.length : len; | ||
const rnd = crypto.randomBytes(baselen); | ||
const result = []; | ||
let counter = 0, characterIndex, r; | ||
while (baselen > 0) { | ||
r = rnd[counter]; | ||
characterIndex = r % sourceArray.length; | ||
result.push(sourceArray.splice(characterIndex, 1)[0]); | ||
baselen--; | ||
counter++; | ||
} | ||
return result.join(''); | ||
} | ||
const randomText = () => { | ||
const alphaseed = randomize(alphanumeric); | ||
return randomize(alphaseed, 6); | ||
}; | ||
const shuffleArray = (arr) => { | ||
let i = arr.length, temp, randomIndex; | ||
@@ -25,5 +44,5 @@ // While there remain elements to shuffle... | ||
class Captcha { | ||
constructor(_h = 150) { | ||
constructor(_h = 120, _text = '') { | ||
// Make sure argument is a number, limit to a range from 250 to 400 | ||
_h = typeof _h !== "number" || _h < 150 ? 150 : _h > 400 ? 400 : _h; | ||
_h = typeof _h !== "number" || _h < 120 ? 120 : _h > 400 ? 400 : _h; | ||
// Initialize canvas | ||
@@ -46,5 +65,9 @@ this._canvas = Canvas.createCanvas(400, _h); | ||
ctx.save(); | ||
// Scale down text for size | ||
let scaling = _h / 150; | ||
if (scaling > 1.3) | ||
scaling = 1.3; | ||
// Set style for lines | ||
ctx.strokeStyle = this._color; | ||
ctx.lineWidth = 4; | ||
ctx.lineWidth = 4 * scaling; | ||
// Draw 10 lines | ||
@@ -84,3 +107,3 @@ ctx.beginPath(); | ||
Math.round(Math.random() * 360) + 20, // Y coordinate | ||
Math.round(Math.random() * 7) + 1, // Radius | ||
Math.round(Math.random() * 7 * scaling) + 1, // Radius | ||
0, // Start anglez | ||
@@ -92,3 +115,2 @@ Math.PI * 2 // End angle | ||
// Set style for text | ||
ctx.font = "bold 90px swift"; | ||
ctx.fillStyle = this._color; | ||
@@ -99,16 +121,21 @@ // Set position for text | ||
ctx.translate(0, _h); | ||
ctx.translate(Math.round(Math.random() * 40 - 20) + 20, -1 * Math.round(Math.random() * (_h / 8) - _h / 16) - _h / 2 + _h / 8); | ||
ctx.translate(Math.round(Math.random() * 40 - 20) + 30, -1 * Math.round(Math.random() * (_h / 8) - _h / 16) - _h / 2 + _h / 8); | ||
ctx.rotate(0.3 * (Math.random() - 0.5)); | ||
// Set text value and print it to canvas | ||
ctx.beginPath(); | ||
this._value = ""; | ||
while (this._value.length !== 6) | ||
this._value = randomText(); | ||
if (_text) { | ||
this._value = _text; | ||
} | ||
else { | ||
this._value = ""; | ||
while (this._value.length !== 6) | ||
this._value = randomText(); | ||
} | ||
let xCoord = 0; | ||
for (const k of this._value) { | ||
const size = (Math.random() - 0.5) * 40 + 70; | ||
const font = isNaN(parseInt(k)) ? 'swift' : 'serif'; | ||
for (const chr of this._value) { | ||
const size = ((Math.random() - 0.5) * 40 + 70) * scaling; | ||
const font = chr.match(/[a-z]/i) ? 'swift' : 'serif'; | ||
ctx.font = `bold ${size}px ${font}`; | ||
ctx.fillText(k, xCoord, 0); | ||
xCoord += ctx.measureText(k).width; | ||
ctx.fillText(chr, xCoord, 0); | ||
xCoord += ctx.measureText(chr).width; | ||
} | ||
@@ -146,3 +173,3 @@ // Draw foreground noise | ||
} | ||
module.exports = Captcha; | ||
exports.default = Captcha; | ||
//# sourceMappingURL=index.js.map |
// Import the module | ||
const Captcha = require("../"); | ||
const Captcha = require("../").default; | ||
@@ -4,0 +4,0 @@ const path = require("path"), |
{ | ||
"name": "captcha-generator-alphanumeric", | ||
"version": "1.8.1", | ||
"version": "1.9.0", | ||
"description": "An NPM package to generate alphanumeric captcha images", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -21,3 +21,3 @@ <h1 align="center">Captcha Generator</h1> | ||
// Import the module | ||
const Captcha = require("captcha-generator-alphanumeric"); | ||
const Captcha = require("captcha-generator-alphanumeric").default; | ||
@@ -41,3 +41,3 @@ // Create a new Captcha object | ||
fs = require("fs"), | ||
Captcha = require("captcha-generator-alphanumeric"); | ||
Captcha = require("captcha-generator-alphanumeric").default; | ||
@@ -54,3 +54,3 @@ let captcha = new Captcha(); | ||
```js | ||
const Captcha = require("captcha-generator-alphanumeric"); | ||
const Captcha = require("captcha-generator-alphanumeric").default; | ||
@@ -57,0 +57,0 @@ // Use this function for blocking certain commands or features from automated self-bots |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115353
257