用canvas制作一个验证码

        首先说一下要求:

          1. 绘制一个随机验证码

          2. 验证码中有10 个干扰线条 30 个干扰点 验证内容 包含数字以及字母 随机位置 长度为 5


        html部分主要包括一个canvas标签以及一个用来刷新的button, css部分基本每内容就不说了。

<canvas width="400" height="300"></canvas>
<button id="newCode">验证码无法识别 点击刷新</button>

        js部分我们再来一步步分析。
        首先用到一个取随机数的function 和一些声明:

 var data = [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']
 var canvas = document.getElementsByTagName('canvas')[0]
 var ctx = canvas.getContext('2d')
 var btn = document.getElementById('newCode')
function getRandom(min, max) {
            return Math.random() * (max - min) + min;
 }

        然后就是取验证码的function:

function testCode() {
            ctx.clearRect(0, 0, 400, 300)
            // 加干扰线条
            for (var i = 0; i < 10; i++) {
                ctx.beginPath()
                // 随机宽度
                ctx.lineWidth = getRandom(1, 10)
                // 随机颜色
                ctx.strokeStyle = `rgb(${getRandom(0, 255)}, ${getRandom(0, 255)}, ${getRandom(0, 255)})`
                // 随机位置
                var x = getRandom(1, 399)
                var y = getRandom(1, 299)
                ctx.moveTo(x, y)
                ctx.lineTo(x + getRandom(10, 100), y + getRandom(10, 100))
                ctx.stroke()
                ctx.closePath()
            }

            // 加干扰点
            for (var i = 0; i < 29; i++) {
                ctx.beginPath()
                // 随机颜色
                ctx.strokeStyle = `rgb(${getRandom(0, 255)}, ${getRandom(0, 255)}, ${getRandom(0, 255)})`
                // 随机位置
                var x = getRandom(1, 399)
                var y = getRandom(1, 299)
                // 绘制随机半径的圆
                ctx.arc(x, y, getRandom(1, 20), 0, Math.PI * 2)
                ctx.fillStyle = `rgb(${getRandom(0, 255)}, ${getRandom(0, 255)}, ${getRandom(0, 255)})`
                ctx.fill()
                ctx.closePath()
            }

            // 加验证码
            var x = 20
            for (var i = 0; i < 5; i++) {
                x += getRandom(20, 70)
                ctx.font = `${getRandom(30, 40)}px serif`;
                var a = parseInt(getRandom(0, data.length - 1))
                ctx.fillStyle = `rgb(${getRandom(0, 255)}, ${getRandom(0, 255)}, ${getRandom(0, 255)})`
                ctx.fillText(`${data[a]}`, x, getRandom(30, 270));
            }
        }

        最后 让网页打开时运行一次验证码函数 以及给button绑定上这个函数

testCode()
btn.onclick = function () {
     testCode()
}

         以上就是取验证码的分析。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值