jQuery canvas draws picture verification code example

jQuery canvas draws picture verification code example

This article example shares the specific code of jquery canvas drawing picture verification code for your reference. The specific content is as follows

CSS

.identify-code{
    position: absolute;
    right: 6px;
    top: 50%;
    width: 118px;
    height: 40px;
    margin: -21px 0 0 0;
}

HTML

<span id="code" class="identify-code">
  <canvas class="show-captcha" id="canvas" style="width: 100%; height: 100%;"></canvas>
</span>

JS

/**Draw the verification code image**/
function drawPic() {
    var canvas = document.getElementById("canvas");
    var width = canvas.width;
    var height = canvas.height;
    //Get the 2D drawing environment of the canvas var ctx = canvas.getContext('2d'); 
    ctx.textBaseline = 'bottom';
    /**Draw the background color**/
    ctx.fillStyle = randomColor(180, 240);
    //If the color is too dark, it may not be clear ctx.fillRect(0,0,width,height);
    /**Draw text**/
    var str ='ABCEFGHJKLMNPQRSTWXY123456789abcefghjklmnpqrstwxy';
    var code="";
    //Generate four verification codes for(var i = 1;i <= 4; i++) {
        var txt = str[randomNum(0,str.length)];
        code=code+txt;
        ctx.fillStyle = randomColor(50,160);
        //Randomly generate font color ctx.font = randomNum(90,110) +'px SimHei';
        // Randomly generate font size var x = 10 + i * 50;
        var y = randomNum(100, 135);
        var deg = randomNum(-30, 30);
        //Modify the coordinate origin and rotation angle ctx.translate(x, y); 
        ctx.rotate(deg * Math.PI /180); 
        ctx.fillText(txt,0,0);
        //Restore the coordinate origin and rotation angle ctx.rotate(-deg * Math.PI /180);
        ctx.translate(-x, -y);
    }
        
    /**Draw interference line**/
    for(var i=0;i<3;i++) {
        ctx.strokeStyle = randomColor(40, 180);
        ctx.beginPath();
        ctx.moveTo(randomNum(0,width/2), randomNum(0,height/2));
        ctx.lineTo(randomNum(0,width/2), randomNum(0,height));
        ctx.stroke();
    }
    /**Draw interference points**/
    for(var i=0;i <50;i++) {
        ctx.fillStyle = randomColor(255);
        ctx.beginPath();
        ctx.arc(randomNum(0, width), randomNum(0, height),1,0,2* Math.PI);
        ctx.fill();
    }
    return code;
}
/**Generate a random number**/
function randomNum(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}
/**Generate a random color**/
function randomColor(min, max) {
    var r = randomNum(min, max);
    var g = randomNum(min, max);
    var b = randomNum(min, max);
    return "rgb(" + r + "," + g + "," + b + ")";
}

Calling Instance

$("#code").unbind('click').click(function(e){
    e.preventDefault();
    createCode();
});
//Generate verification code function createCode(){
  VerificationCodeErrCount = 0;
  randomCode = drawPic();
  return randomCode;
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JQuery implements a simple verification code prompt solution
  • jQuery implements the countdown code for sending verification code by mobile phone
  • jQuery implements sending verification code and 60-second countdown function
  • Jquery plug-in to prevent re-obtaining within 60 seconds after clicking to obtain the verification code
  • jQuery plugin to implement static HTML verification code verification
  • jQuery click to get the verification code button and countdown function
  • jQuery implements verification code function
  • jQuery implements a simple verification code function
  • jQuery implements form to obtain SMS verification code
  • jQuery implements the countdown effect code sharing of sending verification code by mobile phone

<<:  Web page creation basic declaration document type description (DTD

>>:  Practical record of MySQL 5.6 master-slave error reporting

Recommend

Linux CentOS MySQL database installation and configuration tutorial

Notes on installing MySQL database, share with ev...

MySQL 5.7 installation and configuration tutorial under CentOS7 64 bit

Installation environment: CentOS7 64-bit MINI ver...

How to write the Nofollow tag and how to use it

The "nofollow" tag was proposed by Goog...

base target="" controls the link's target open frame

<base target=_blank> changes the target fram...

Example of adding music video to HTML page

1. Video tag Supports automatic playback in Firef...

HTML Tutorial: Unordered List

<br />Original text: http://andymao.com/andy...

Detailed explanation of Vue's props configuration

<template> <div class="demo"&g...

HTML tags: sub tag and sup tag

Today I will introduce two HTML tags that I don’t...

How to deploy FastDFS in Docker

Install fastdfs on Docker Mount directory -v /e/f...

How to write HTML head in mobile device web development

Copy code The code is as follows: <head> &l...

Analyzing the troublesome Aborted warning in MySQL through case studies

This article mainly introduces the relevant conte...

Understand the principle of page replacement algorithm through code examples

Page replacement algorithm: The essence is to mak...

How to implement call, apply and bind in native js

1. Implement call step: Set the function as a pro...

VMWare virtual machine 15.X LAN network configuration tutorial diagram

Recently, I have been working on several virtual ...

Mysql Sql statement exercises (50 questions)

Table name and fields –1. Student List Student (s...