canvas实现随机验证码

知识点

  • canvas生成背景图和文字 设置字体样式和大小
  • String的fromCharCode(code码)生成大小写字母和数字 str.toLowerCase()转小写
  • 随机抽取不重复的6位数字组成验证码字符串
  • 效果图

html:

    <div class="wraper">
<input type="text" maxlength="6" placeholder="请输入验证码,不区分大小写" class="input">
<span class="icon"></span>
<p class="error">验证码输入有误,请重新输入</p>
<div class="canvas-box">
<canvas id="myCanvas" width="200" height="50"></canvas>
<input type="button" class="refresh">
</div>
<div class="btn">
<button class="submit">提交</button>
</div>
</div>

css:

    * {
margin: 0;
padding: 0;
} html,
body {
width: 100%;
height: 100%;
background: hotpink;
display: flex;
justify-content: center;
align-items: center;
} .wraper {
width: 345px;
margin: 30px;
padding: 15px;
border-radius: 5px;
border: 1px solid #ccc;
background: #fff;
} .input {
width: 300px;
padding: 15px;
border-radius: 5px;
border: 1px solid #ccc;
box-sizing: border-box;
} .icon {
float: right;
width: 20px;
height: 20px;
margin-top: 13px;
background: url('./img/yes.png') no-repeat;
background-size: 100% 100%;
display: none;
} .error {
margin-top: 10px;
color: red;
font-size: 12px;
display: none;
} .canvas-box {
margin-top: 15px;
position: relative;
} #myCanvas {
width: 300px;
height: 60px;
} .canvas-box .refresh {
position: absolute;
right: 0;
top: 50%;
margin-top: -10px;
display: inline-block;
width: 20px;
height: 20px;
background: url('./img/refresh.png') no-repeat;
background-size: 100% 100%;
border: none;
outline: none;
cursor: pointer;
} .btn {
margin-top: 15px;
} .btn .submit {
width: 80px;
height: 40px;
border-radius: 5px;
background: blue;
color: #fff;
border: none;
outline: none;
cursor: pointer;
}

js:

    var arr = []; //筛选验证码的数组
var value = '';
//48-57 数字 65-90 大写字母 97-122 小写字母
for (var i = 48; i <= 57; i++) {
arr.push(String.fromCharCode(i));
}
for (let i = 65; i <= 90; i++) {
arr.push(String.fromCharCode(i));
}
for (let i = 97; i <= 122; i++) {
arr.push(String.fromCharCode(i));
} //生成随机验证码
function getVerification() {
var codeStr = '';
var codeArr = [];
value = '';
while (true) {
let a = Math.floor(Math.random() * arr.length);
if (codeArr.indexOf(a) == -1) {
codeArr.push(arr[a]);
}
if (codeArr.length == 6) {
break;
}
}
codeStr = codeArr.join(' ');
value = codeArr.join('').toLowerCase();
console.log(value)
var myCanvas = document.getElementById('myCanvas');
var ctx = myCanvas.getContext('2d');
var img = new Image();
img.src = './img/bg_pic.jpg';
img.onload = function() {
var pat = ctx.createPattern(img, 'no-repeat');
ctx.fillStyle = pat;
ctx.fillRect(0, 0, myCanvas.width, myCanvas.height);
ctx.textAlign = 'center';
ctx.fillStyle = '#ccc';
ctx.font = '30px Roboto Slab';
ctx.setTransform(1, -0.12, 0.3, 1, 0, 12);
ctx.fillText(codeStr, myCanvas.width / 2, 35);
}
}
getVerification(); //事件
var refresh = document.getElementsByClassName('refresh')[0];
var submit = document.getElementsByClassName('submit')[0];
var inputValue = document.getElementsByClassName('input')[0];
var icon = document.getElementsByClassName('icon')[0];
var error = document.getElementsByClassName('error')[0]; refresh.onclick = function() {
getVerification();
}
submit.onclick = function() {
if (inputValue.value.toLowerCase() === value) {
icon.style.display = 'inline-block';
icon.style.background = "url('./img/yes.png') no-repeat";
icon.style.backgroundSize = "100% 100%";
error.style.display = 'none';
getVerification();
} else {
icon.style.display = 'inline-block';
icon.style.background = "url('./img/error.png') no-repeat";
icon.style.backgroundSize = "100% 100%";
error.style.display = 'block';
inputValue.value = '';
}
}

参考至腾讯课堂渡一教育

canvas实现随机验证码的更多相关文章

  1. canvas制作随机验证码

    看到人家彩色背景的验证码想测试一下: 创建html代码: <canvas id="myCanvas" width="200" height="1 ...

  2. 用Canvas生成随机验证码(后端前端都可以)

    一 .使用前端生成验证码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  3. canvas绘制随机验证码

    效果图: 思路: 1, 绘制canvas画布,进行基础设置 2.绘制一个矩形 3.设置验证码的随机数 4.设置验证码随机数的随机颜色 5.绘制随机干扰线 6,绘制随机干扰点 经过以上六个步骤,验证码的 ...

  4. 微信小程序 canvas 生成随机验证码

    转载:https://blog.csdn.net/qq_16646819/article/details/81020245?utm_source=blogxgwz0 js // pages/bind/ ...

  5. Android实现随机验证码——自定义View

    一.问题描述 熟悉web开发中童鞋们都知道为了防止恶意破解.恶意提交.刷票等我们在提交表单数据时,都会使用随机验证码功能.在Android应用中我们同样需要这一功能,该如何实现呢,下面我们就自定义一个 ...

  6. JavaScript随机验证码

    利用canvas制作一个随机验证码: 1.clearRect:context.clearRect(x,y,width,height);清空给定矩形内的指定像素 2.fillStyle:设置画笔的颜色 ...

  7. PYTHON 随机验证码生成

    # 生成一个六位随机验证码 import random # random 生成随机数 temp = '' for i in range(6): num = random.randrange(0,6) ...

  8. Java生成随机验证码

    package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  9. js用正则表达式验证用户和密码的安全性,生成随机验证码

    制作了一个表单,表单验证用户.密码.随机验证码 html页面

随机推荐

  1. c语言,数据结构,链表的一些操作总结

    下面是自己的一些学习操作以及总结,能用我会很开心,有不足之处,欢迎大家提出宝贵的意见! c语言链表是一种基本的数据结构,与顺序表一样属于线性表,但是顺序表在内存中的存储单元是连续的,这样就对内存的要求 ...

  2. Vlan 原理

    VLAN(Virtual LAN),翻译成中文是"虚拟局域网".LAN可以是由少数几台家用计算机构成的网络,也可以是数以百计的计算机构成的企业网络.VLAN所指的LAN特指使用路由 ...

  3. mysql的内建日期处理函数

    下面的表格列出了MySQL 中最重要的内建日期函数: 函数 描述 NOW() 返回当前的日期和时间 CURDATE() 返回当前的日期 CURTIME() 返回当前的时间 DATE() 提取日期或日期 ...

  4. spring中bean的scope属性理解

    bean的scope属性有prototype,singleton,request, session几个属性 spring和struts2整合的时候,struts2的action要配置成scope=&q ...

  5. left join 后的条件 位置不同,查询的结果不同

    表t_a id name 1 a1 2 a2 表t_b a1_id name num 2 b2 1 3 b3 100 left join 后加查询条件 select a.* from t_a a le ...

  6. TensorFlow练习13: 制作一个简单的聊天机器人

    现在很多卖货公司都使用聊天机器人充当客服人员,许多科技巨头也纷纷推出各自的聊天助手,如苹果Siri.Google Now.Amazon Alexa.微软小冰等等.前不久有一个视频比较了Google N ...

  7. 微信H5中静默登录及非静默登录的正确使用姿势

    在微信中打开网页且需要调用微信登录接口时,微信官方给我们提供了两种登录调用方式:静默登录和非静默登录:但是官方文档中却没有说明在何种情况下使用静默登录,何种情况下使用非静默登录,所以在这里,我想将之前 ...

  8. 时间戳转换成时间js(年-月-日,例如“2017-04-22”)

    function GetDateByShiJianChuo(timespan) { var date = new Date(parseInt(timespan.replace("/Date( ...

  9. MVC之图片验证码

    MVC之图片验证码 controller中的action方法public ActionResult GetValidateCode() { ValidateCode vCode = new Valid ...

  10. TProfiler部署文档--笔记

    TProfiler是一个可以在生产环境长期使用的性能分析工具.它同时支持剖析和采样两种方式,记录方法执行的时间和次数,生成方法热点 对象创建热点 线程状态分析等数据,为查找系统性能瓶颈提供数据支持. ...