generate svg captcha in browser
npm install --save svg-captcha-browser
step1: Store Comismsh.ttf under the Fonts directory under the project under your own server
step2: write code
import svgCaptcha from 'svg-captcha-browser'
svgCaptcha.loadFont('http://yourhosts.com/fonts/Comismsh.ttf').then(() => {
let captcha = svgCaptcha.create();
console.log(captcha);
// {data: '<svg.../svg>', text: 'abcd'}
}).catch(e => {
//加载字体出错
console.log(e)
})
If no option is passed, you will get a random string of four characters and corresponding svg.
size
: 4 // size of random stringignoreChars
: '0o1i' // filter out some characters like 0o1inoise
: 1 // number of noise linescolor
: true // characters will have distinct colors instead of grey, true if background option is setbackground
: '#cc9966' // background color of the svg image
This function returns an object that has the following property:
data
: string // svg path datatext
: string // captcha text
Similar to create api, you have the above options plus 3 additional:
mathMin
: 1 // the minimum value the math expression can bemathMax
: 9 // the maximum value the math expression can bemathOperator
: + // The operator to use,+
,-
or+-
(for random+
or-
)
This function returns an object that has the following property:
data
: string // svg of the math expressiontext
: string // the answer of the math expression
Load your own font and override the default font.
url
: string // path to your font This api is a wrapper around loadFont api of opentype.js. Your may need experiment around various options to make your own font accessible. See the following api.
Gain access to global setting object. It is used for create and createMathExpr api as the default options.
In addition to size, noise, color, and background, you can also set the following property:
width
: number // width of captchaheight
: number // height of captchafontSize
: number // captcha text sizecharPreset
: string // random character preset
return a random string.
return a svg captcha based on text provided.
In pre 1.1.0 version you have to call these two functions, now you can call create() to save some key strokes ;).
default captcha image:
math expression image with color options:
本仓库源码对 svg-captcha@1.4.0(仓库地址)进行了修改,修改后本库适用为浏览器端