js canvas implements verification code and obtains verification code function

js canvas implements verification code and obtains verification code function

This article example shares the specific code of js canvas to make and obtain verification codes for your reference. The specific content is as follows

I have written some small plug-ins recently. Today I will talk about the js code that packages a verification code, as follows:

/**Package**/
var xh_digital_code = function(option) {
    this.el = option.el;
    var self = this;
    var click_code = '';

    var canvas_id = "xh_canvas_" + xh_randomWord(false, 30); // Generate a random id
    $(self.el).html('<canvas class="xh_canvas" id="' + canvas_id + '"></canvas>');

    var code = xh_drawPic(canvas_id);

    $('body').on('click', self.el, function() {
        click_code = xh_drawPic(canvas_id);
        self.code = click_code;
        return;
    });
    
    self.code = code;
}

/**Draw the verification code image**/
function xh_drawPic(canvasid) {
    var canvas = document.getElementById(canvasid);
    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 = xh_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[xh_randomNum(0, str.length)];
        code = code + txt;
        ctx.fillStyle = xh_randomColor(50, 160);
        //Randomly generate font color ctx.font = xh_randomNum(90, 110) + 'px SimHei';
        // Randomly generate font size var x = 10 + i * 50;
        var y = xh_randomNum(100, 135);
        var deg = xh_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 = xh_randomColor(40, 180);
        ctx.beginPath();
        ctx.moveTo(xh_randomNum(0, width / 2), xh_randomNum(0, height / 2));
        ctx.lineTo(xh_randomNum(0, width / 2), xh_randomNum(0, height));
        ctx.stroke();
    }
    /**Draw interference points**/
    for (var i = 0; i < 50; i++) {
        ctx.fillStyle = xh_randomColor(255);
        ctx.beginPath();
        ctx.arc(xh_randomNum(0, width), xh_randomNum(0, height), 1, 0, 2 * Math.PI);
        ctx.fill();
    }
    return code;
}



/**Generate a random number**/
function xh_randomNum(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}

/**Generate a random color**/
function xh_randomColor(min, max) {
    var r = xh_randomNum(min, max);
    var g = xh_randomNum(min, max);
    var b = xh_randomNum(min, max);
    return "rgb(" + r + "," + g + "," + b + ")";
}

/**Generate a random code**/
function xh_randomWord(randomFlag, min, max) {
    var str = "",
        range = min,
        arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', ​​'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

    // Randomly generate if (randomFlag) {
        range = Math.round(Math.random() * (max - min)) + min;
    }
    for (var i = 0; i < range; i++) {
        pos = Math.round(Math.random() * (arr.length - 1));
        str += arr[pos];
    }
    return str;
}

The above is the verification code js I packaged. You can just copy it and use it. The following is the reference code:

<!--- Quote--->
<span class="identify-code"></span>
<button class="xh-btn xh-btn-success" id="get_code">Get verification code</button>

<script type="text/javascript">
  var c = new xh_digital_code({
        el: '.identify-code' // .class name#id name});

    $('#get_code').click(function(){
     // This is a pop-up information plug-in I wrote. Ignore it. c.code can get the information in the verification code. $(this).xh_prompt('success', 'The current verification code is: '+c.code, 1000);
    });
</script>

The effect diagram is as follows:

The above is the effect picture

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:
  • Native js+canvas to implement verification code
  • js+h5 canvas realizes image verification code
  • js+canvas draws graphic verification code
  • JavaScript uses canvas to draw random verification codes
  • JS+HTML5 canvas drawing verification code example
  • JavaScript Canvas implements verification code
  • js+canvas realizes verification code function
  • js+canvas realizes the function of sliding puzzle verification code
  • How to generate verification code using canvas and js
  • js realizes the countdown effect of clicking to get the verification code

<<:  Nginx forwarding based on URL parameters

>>:  Implementation of nginx proxy port 80 to port 443

Recommend

Summary of Ubuntu backup methods (four types)

Method 1: To use respin, follow these steps: sudo...

Detailed explanation of CSS margin overlap and solution exploration

I recently reviewed some CSS-related knowledge po...

How to install Nginx in a specified location in Centos system

How to install Nginx in a specified location in C...

Detailed explanation of component development of Vue drop-down menu

This article example shares the specific code for...

...

Postman automated interface testing practice

Table of contents Background Description Creating...

How to solve the error "ERROR 1045 (28000)" when logging in to MySQL

Today, I logged into the server and prepared to m...

CSS to achieve horizontal lines on both sides of the middle text

1. The vertical-align property achieves the follo...

How to install MySQL database on Debian 9 system

Preface Seeing the title, everyone should be thin...

Specific use of Linux man command

01. Command Overview Linux provides a rich help m...

Solve the problem of MySql8.0 checking transaction isolation level error

Table of contents MySql8.0 View transaction isola...

How to install mysql via yum on centos7

1. Check whether MySQL is installed yum list inst...

Troubleshooting and solutions for MySQL auto-increment ID oversize problem

introduction Xiao A was writing code, and DBA Xia...

What is ssh? How to use? What are the misunderstandings?

Table of contents Preface What is ssh What is ssh...