canvas验证码

关于canvas验证码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>点击图片生成验证码</title>
    <style>
        .box{
            position:absolute;
            left:50%;
            top:50%;
        }
    </style>
    <script>

        //随机生成图片和线条的颜色
        function randomRgbColor() { //随机生成RGB颜色
            var r = Math.floor(Math.random() * 256); //随机生成256以内r值
            var g = Math.floor(Math.random() * 256); //随机生成256以内g值
            var b = Math.floor(Math.random() * 256); //随机生成256以内b值
            return "rgb(" + r + "," + g + "," + b + ")"; //返回rgb(r,g,b)格式颜色
        }
        window.onload = function () {
                 draw();
            document.getElementById("changeImg").onclick=function(){
                draw();
            }
                document.getElementById("canvas").onclick=function(){           //点击图片刷新验证码
                draw();
            }
        }

        var code="";        //全局变量code声明

        function draw(){
            var canvas = document.getElementById("canvas");
            var context = canvas.getContext("2d");
            canvas.width = 120;
            canvas.height = 40;
            context.strokeRect(0, 0, 120, 40);

// var code="";

            var aCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,1,2,3,4,5,6,7,8,9";
            var aLength = aCode.split(",").length;          // split 通过“ ,”分割
            for (var i = 0; i <= 3; i++) {
                var x = 20 + i * 20;
                var y = 20 + Math.random() * 10;
                var j = Math.floor(Math.random() * aLength);
                var deg = Math.random() * 90 * Math.PI / 180;//随机弧度
                var txt = aCode.split(",")[j];

                code += aCode.split(",")[j];            //将四个随机数组合在一起

                context.fillStyle = randomRgbColor();   //文字的绘制颜色类型为随机生成的颜色
                context.font = "bold 20px 微软雅黑";    //文字的字体类型
                //修改坐标原点和旋转角度
                context.translate(x, y);
                context.rotate(deg);                    //根据随机角度进行旋转
                context.fillText(txt, 0, 0);
                //恢复坐标原点和旋转角度
                context.rotate(-deg);
                context.translate(-x, -y);
            }


            //干扰线
            for (var i = 0; i < 8; i++) {
                context.strokeStyle = randomRgbColor();
                context.beginPath();
                context.moveTo(Math.random() * 120, Math.random() * 40);
                context.lineTo(Math.random() * 120, Math.random() * 40);
                context.stroke();
            }
            /**绘制干扰点**/
            for (var i = 0; i < 20; i++) {
                context.fillStyle = randomRgbColor();
                context.beginPath();
                context.arc(Math.random() * 120, Math.random() * 40, 1, 0, 2 * Math.PI);
                context.fill();
            }
//alert(code);
        }

            function checkCode(){

                var inputCode = document.getElementById("codenumber").value;

                if(inputCode.length<=0){

                    alert("请输入您的验证码");

                }else if(inputCode.toUpperCase() != code.toUpperCase()){
                    alert("抱歉!您的验证码输入不正确");
                    code="";
                    draw();
                }else{
                    alert("恭喜您!您的验证码输入成功");
                }
            }

// alert(txt);
    </script>
</head>

<body>
<div class="box">
    <canvas id="canvas"></canvas>
    <a href="javascript:;" id="changeImg">再换一张</a>

    <input type="text" value="" id="codenumber">
    <button class="btn"  onClick="checkCode()">确认</button>
</div>
</body>

</html>

这里写图片描述

Canvas 验证码是一种基于 HTML5 Canvas 技术实现的验证码,它可以在前端页面上生成一个随机的图形验证码,并且可以通过 JavaScript 实现交互效果,提高用户体验。下面是实现 Canvas 验证码的步骤: 1. 在 HTML 页面中添加一个 Canvas 元素,设置宽度、高度和 ID,例如: ```html <canvas id="canvas" width="100" height="30"></canvas> ``` 2. 在 JavaScript 中获取 Canvas 元素和上下文对象,并设置绘制属性,例如: ```javascript var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.fillStyle = "#f2f2f2"; // 设置背景颜色 ctx.fillRect(0, 0, canvas.width, canvas.height); // 绘制背景 ctx.font = "20px Arial"; // 设置字体大小和样式 ctx.fillStyle = "#000"; // 设置字体颜色 ``` 3. 生成随机的验证码,并将其绘制在 Canvas 上,例如: ```javascript var code = ""; // 用于保存验证码字符串 var codeLength = 4; // 验证码长度 var codeChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // 验证码字符集合 for (var i = 0; i < codeLength; i++) { var index = Math.floor(Math.random() * codeChars.length); // 随机获取一个字符 var char = codeChars.charAt(index); code += char; var x = 20 + i * 20; // 计算每个字符的绘制位置 var y = 20 + Math.random() * 10; ctx.fillText(char, x, y); // 绘制字符 } ``` 4. 绘制干扰线或干扰点,增加验证码的难度,例如: ```javascript // 绘制干扰线 for (var i = 0; i < 5; i++) { var x1 = Math.random() * canvas.width; var y1 = Math.random() * canvas.height; var x2 = Math.random() * canvas.width; var y2 = Math.random() * canvas.height; ctx.strokeStyle = "#ccc"; ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke(); } // 绘制干扰点 for (var i = 0; i < 50; i++) { var x = Math.random() * canvas.width; var y = Math.random() * canvas.height; ctx.fillStyle = "#888"; ctx.beginPath(); ctx.arc(x, y, 1, 0, Math.PI * 2); ctx.fill(); } ``` 5. 将生成的验证码字符串返回给后端进行验证。 完整的 Canvas 验证码实现代码可以参考下面的示例: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Canvas 验证码</title> </head> <body> <canvas id="canvas" width="100" height="30"></canvas> <script> var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.fillStyle = "#f2f2f2"; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.font = "20px Arial"; ctx.fillStyle = "#000"; var code = ""; var codeLength = 4; var codeChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; for (var i = 0; i < codeLength; i++) { var index = Math.floor(Math.random() * codeChars.length); var char = codeChars.charAt(index); code += char; var x = 20 + i * 20; var y = 20 + Math.random() * 10; ctx.fillText(char, x, y); } for (var i = 0; i < 5; i++) { var x1 = Math.random() * canvas.width; var y1 = Math.random() * canvas.height; var x2 = Math.random() * canvas.width; var y2 = Math.random() * canvas.height; ctx.strokeStyle = "#ccc"; ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke(); } for (var i = 0; i < 50; i++) { var x = Math.random() * canvas.width; var y = Math.random() * canvas.height; ctx.fillStyle = "#888"; ctx.beginPath(); ctx.arc(x, y, 1, 0, Math.PI * 2); ctx.fill(); } console.log(code); // 将验证码输出到控制台 </script> </body> </html> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值