GithubHelp home page GithubHelp logo

ddtramp / canvas-verification-code Goto Github PK

View Code? Open in Web Editor NEW

This project forked from levblanc/canvas-verification-code

0.0 1.0 0.0 37 KB

Simple JS function to generate verification code with canvas api. 前端利用 CANVAS API 生成随机图形验证码的JS方法。

JavaScript 100.00%

canvas-verification-code's Introduction

Generating Verification Code with Canvas API

sample image

Before you start

  1. READ and UNDERSTAND the code, detailed explainations are provided in comments. Also, check the reference links below
  2. TWEAK the settings (font style, color, translate pos, rotation, scale) to however you want them to be if needed
  3. HAVE FUN playing with the code, they are ALIVE!

使用代码前,推荐先做以下几件事:

  1. 读懂代码。源码中有详细的注释说明(英文),或者阅读 我的解读文章(中文) 及参考最下方的 References
  2. 调整源码中对字体、颜色、位移、旋转、缩放的设置,以切合自己的需求
  3. 代码是活的,享受每一次尝试的过程

Usage

  1. Copy canvas-verification-code.js and codeBg.png to your repo.
  2. Make sure your project has a es6 compiler (e.g.: Babel) to compile the code

使用方法:

  1. canvas-verification-code.js and codeBg.png复制到项目下
  2. 项目中需要有可以把 es6 编译为 es5 的工具,例如 Babel

Plain Html and Javascript

See it live on CODEPEN

<div class='verificationCode'>
  <!-- use css absolute positioning to put codeBg.png   
    underneath your verification code -->
  <div class='codeBackground'></div>
  <img class='codeImage' src='' alt='verificationCode' />
</div>

<!-- ... ... -->

<script src='/path/to/canvas-verification-code.js'>
<script>
var verificationCode = generateCode(120, 40)

var codeImage = document.getElementsByClassName('codeImage')[0]

codeImage.src = verificationCode.data
</script>

Using Vue.js 2.0

See it live on CODEPEN

<template lang='pug'>
  div(id='app' class='verificationCode')
    //- use css absolute positioning to put codeBg.png   
    //- underneath your verification code
    div(class='codeBackground')
    img(
      class='codeImage'
      :src='imageData'
      alt='verificationCode'
    )
    br
    input(v-model='codeInput' placeholder="enter code")
    p code input is: {{ validInput }}
</template>
import generateCode from '/path/to/canvas-verification-code.js'

export default {
  data() {
    return {
      codeInput: '',
      imageData: '',
      codeText : '',
    }
  },
  computed: {
    validInput() {
      return this.codeText === this.codeInput
        ? 'correct'
        : 'not correct'
    }
  },
  mounted() {
    const { code, data } = generateCode(120, 40)

    this.imageData = data
    this.codeText  = code
  }
}

Params

name type required meaning default options
width number true width of canvas background
height number true height of canvas background
type string false type of code characters "default"
(mix of letter and number)
"letter", "number"
codeLen number false length of code characters 4 positive integers

References

[1] Math.random() @ MDN

[2] Generating random whole numbers in JavaScript in a specific range?

[3] Canvas tutorial @ MDN

[4] html5 Canvas画图教程26:用transform来实现位移,缩放,旋转等

[5] shoestrong/validate-code-canvas

[6] KIDx/verify-code

[7] jovey-zheng/verify-code

[8] F-happy/verification-code

canvas-verification-code's People

Contributors

levblanc 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.