JS生成随机验证码

效果图
2020年3月3日 更新一篇 《》JS实现数字字母混合验证码(数字+大写字母+小写字母》)
在网站中我们很常见到形形色色的验证码,今天我们来用JS来生成一个随机的二维码。
我们需要用到canvas来进行验证码的绘制

什么是Canvas

HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像。
画布是一个矩形区域,您可以控制其每一像素。
canvas 拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。
点击学习

思路

我们要做的二维码首先要有随机的数字,其次就是要有随机的位置。

HTML

<canvas id="canvas" style="border: 1px solid red; width: 80px; height: 40px;"></canvas>

JS

function getVerification() {  //二维码
    var ctx = document.getElementById("canvas").getContext("2d");
    // 清空画布
    ctx.clearRect(0,0, 400, 400);
    // 设置字体
    ctx.font = "128px bold 黑体";
    // 设置垂直对齐方式
    ctx.textBaseline = "top";
    // 设置颜色
    ctx.fillStyle = randomColor();
    // 绘制文字(参数:要写的字,x坐标,y坐标)
    ctx.fillText(getRandomNum(10), 0, getRandomNum(50));
    ctx.fillStyle = randomColor();
    ctx.fillText(getRandomNum(10), 50, getRandomNum(50));
    ctx.fillStyle = randomColor();
    ctx.fillText(getRandomNum(10), 100, getRandomNum(50));
    ctx.fillStyle = randomColor();
    ctx.fillText(getRandomNum(10), 150, getRandomNum(50));
}
  • 我们使用ctx.fillStyle = randomColor();来设置随机的颜色,每写一个数字换一个颜色,randomColoe()函数代码如下,可以随机生成十六进制颜色码。
function randomColor() {
      var colorValue = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f";
      var colorArray = colorValue.split(",");
      var color = "#";
      for (var i = 0; i < 6; i++) {
            color += colorArray[Math.floor(Math.random() * 16)];
      }
      return color;
}
  • 我们使用getRandomNum()来获取随机显示的数字和随机每次字体的y轴方向的位置。验证码的每个数字分别进行获取。传入的参数n来确定随机数范围。代码如下:
function getRandomNum(n){
      return parseInt(Math.random() * n); 
}

完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2</title>
</head>

<body>
    <canvas id="canvas" style="border: 1px solid red; width: 80px; height: 40px;"></canvas>
    <span id="yanzhengma"></span><button onclick="getVerification()">看不清</button>
    <script>
        function randomColor() {
            var colorValue = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f";
            var colorArray = colorValue.split(",");
            var color = "#";
            for (var i = 0; i < 6; i++) {
                color += colorArray[Math.floor(Math.random() * 16)];
            }
            return color;
        }
        function getRandomNum(n){
            return parseInt(Math.random() * n); 
        }
        function getVerification() {
            var ctx = document.getElementById("canvas").getContext("2d");
            ctx.clearRect(0,0, 400, 400);
            // 设置字体
            ctx.font = "128px bold 黑体";
            // 设置垂直对齐方式
            ctx.textBaseline = "top";
            // 设置颜色
            ctx.fillStyle = randomColor();
            // 绘制文字(参数:要写的字,x坐标,y坐标)
            ctx.fillText(getRandomNum(10), 0, getRandomNum(50));
            ctx.fillStyle = randomColor();
            ctx.fillText(getRandomNum(10), 50, getRandomNum(50));
            ctx.fillStyle = randomColor();
            ctx.fillText(getRandomNum(10), 100, getRandomNum(50));
            ctx.fillStyle = randomColor();
            ctx.fillText(getRandomNum(10), 150, getRandomNum(50));
        }
        getVerification();
    </script>
</body>
</html>
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值