Giter Site home page Giter Site logo

node-captcha-lite's Introduction

node-captcha-lite

Lite captcha generator by Pure Javascript.

Usage

node-captcha-lite just extends node-pnglib, you can check its document for constructor options since we only add some public methods on it.

Let's try to draw number:

'use strict';

const fs = require('fs');
const path = require('path');
const Captcha = require('..');
const FONTS = Captcha.FONTS;

function rand(min, max) {
  let comp = max - min;
  return (Math.random() * comp + min) | 0;
}

function getRandFont(fonts) {
  const fontNames = Object.keys(fonts);
  return fonts[fontNames[rand(0, fontNames.length)]];
}

let png = new Captcha(200, 100, 8, [255, 255, 255, 255]);
let chars = getRandFont(FONTS).chars;

for (let i = 0; i < chars.length; ++i) {
  let font = getRandFont(FONTS);
  let ch = chars[i];
  png.drawChar(ch, 0 + 2 * i * font.w, 50, font, '#00FF00');
}

fs.writeFileSync(path.resolve(__dirname, './char.png'), png.getBuffer());

Output:

line

Let's try to draw bezier curve:

'use strict';

const fs = require('fs');
const path = require('path');
const Captcha = require('..');

const png = new Captcha(300, 300, 8, [255,255,255,255]);

const bezierPoints = [
  [250, 50],
  [250, 250],
  [260, 70],
  [60, 70]
];

png.drawBezier(bezierPoints, {
  tolerance: 0, // 曲线线条扩宽的距离
  step: 1/1000, // 每两个点之间绘制1000个点
  fixEndPointCurve: true, // 是否绘制开始和结束点的曲线
  handlerRatio: 0.5, // 贝塞尔曲线的句柄长度(连续贝塞尔模型)
  color: '#cc0044' // 曲线的颜色
});

fs.writeFileSync(path.resolve(__dirname, './bezier.png'), png.getBuffer());

Output:

bezier

node-captcha-lite's People

Contributors

geemo avatar lellansin avatar smallpath avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.