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. CentOS 6 安装HBase集群教程

    hbase0.99.2安装包下载(链接:https://pan.baidu.com/s/1dR-HB3P6mzsXVW6sLI8uxQ 密码:4g1n) 首先需要安装  zookeeper(点击查看) ...

  2. arcEngine开发之根据点坐标创建Shp图层

    思路 根据点坐标创建Shapefile文件大致思路是这样的: (1)创建表的工作空间,通过 IField.IFieldsEdit.IField 等接口创建属性字段,添加到要素集中. (2)根据获取点的 ...

  3. Linux集群服务 LVS

    linux虚拟服务器(LVS)项目在linux操作系统上提供了最常见的负载均衡软件. 集群定义: 集群(cluster)技术是一种较新的技术,通过集群技术,可以在付出较低成本的情况下获得在性能.可靠性 ...

  4. PAT1017:Queueing at Bank

    1017. Queueing at Bank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Supp ...

  5. apache2.4 虚拟主机配置

    网上教程很多,仅记录我的配置,可供参考 一.修改httpd.conf 打开appserv的安装目录,找到httpd.conf文件,分别去掉下面两行文字前面的#号. #LoadModule vhost_ ...

  6. 如何给 mongodb 设置密码

    言简意赅,步骤如下: 连接mongo          mongo 进入admin数据库    use admin 创建管理员账户db.createUser({ user: "adminNa ...

  7. async 和 await 之异步编程的学习

    async修改一个方法,表示其为异步方法.而await表示等待一个异步任务的执行.js方面,在es7中开始得以支持:而.net在c#5.0开始支持.本文章将分别简单介绍他们在js和.net中的基本用法 ...

  8. Java开源生鲜电商平台-售后模块的设计与架构(源码可下载)

    Java开源生鲜电商平台-售后模块的设计与架构(源码可下载) 说明:任何一个的电商平台都有售后服务系统,那么对于我们这个生鲜的电商平台,售后系统需要思考以下几个维度. 1. 买家的需求维度 说明:买家 ...

  9. scrapy顺序执行多个爬虫

    # -*- coding:utf-8 -*- from scrapy import cmdline from scrapy.cmdline import execute import sys,time ...

  10. RocketMQ源码 — 十、 RocketMQ顺序消息

    RocketMQ本身支持顺序消息,在使用上发送顺序消息和非顺序消息有所区别 发送顺序消息 SendResult sendResult = producer.send(msg, new MessageQ ...