利用canvas写一个验证码小功能
刚刚开始接触canvas,写个验证码小功能练练手,实现效果图如下:

主要代码如下:
html
<!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>Document</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<div class="wrapper">
<div class="inputBox">
<input type="text" class = 'inputCaptcha' placeholder = "请输入验证码">
<span class="captchaIcon"></span>
</div>
<p class="errorText"></p>
<div class="canvasBox">
<div class="imageBox">
<canvas width =300 height=80 id = 'canvasCaptcha'></canvas>
<input type="button" class='refresh'>
</div>
</div>
<button class="captchaSubmit">submit</button>
</div>
<script src='./jquery.js'></script>
<script src = './index.js'></script>
</body>
</html>
css
* {
margin:;
padding:;
}
.wrapper {
width: 345px;
border: 1px solid #ccc;
border-radius: 10px;
padding: 20px 10px;
margin: 30px 30px;
}
.inputBox {
width: 345px;
margin: 0 auto 10px;
position: relative;
}
.inputBox .inputCaptcha {
display: inline-block;
height: 50px;
width: 86%;
text-indent: 1em;
border: 1px solid #ddd;
border-radius: 5px;
}
.inputBox .captchaIcon {
display: none;
position: absolute;
top: 50%;
right: 0px;
margin-top: -16px;
width: 32px;
height: 32px;
background-size: 100% 100%;
}
.errorText {
width: 345px;
margin: 0 auto;
font-size: 12px;
color: red;
display: none;
}
.canvasBox {
width: 345px;
margin: 10px auto;
position: relative;
}
#canvasCaptcha {
border-radius: 10px;
}
.canvasBox .refresh {
width: 34px;
height: 34px;
position: absolute;
right: 0px;
top: 50%;
margin-top: -17px;
border:;
border-radius: 6px;
background-image: url('./images/update.png');
background-size: cover;
}
.captchaSubmit {
padding: 10px 20px;
background-color: #62b900;
border:;
font-size: 20px;
border-radius: 5px;
color: #fff;
}
js
var arr = [0,1,2,3,4,5,6,7,8,9];
for(var i = 65;i < 122;i++){
if(i>90&&i<97){
continue;
}
arr.push(String.fromCharCode(i));
}
//每个验证码可能出现的字母或数字(区分大小写)
var len = arr.length;
//验证码的长度
var canvasStr,value;
//验证码值
function createCanvas(){
canvasStr = '';
value = '';
for(var i =0 ;i < 6;i++){
var a = arr[Math.floor(Math.random()*len)];
canvasStr += a+' ';
value += a;
}
//canvas
var oCanvas = document.getElementById('canvasCaptcha');
var ctx = oCanvas.getContext('2d');
var w = oCanvas.width;
var h = oCanvas.height;
var oImg = new Image();
oImg.src = './images/bg.jpg';
oImg.onload = function(){
var pattern = ctx.createPattern(oImg,'repeat');
ctx.fillStyle = pattern;
ctx.fillRect(0,0,w,h);
ctx.fillStyle = '#ccc';
ctx.textAlign = 'center';
ctx.font = '46px Roboto Slab';
ctx.setTransform(1,-0.12,0.2,1,0,12);
ctx.fillText(canvasStr,w/2,60);
}
} createCanvas();
//验证输入的验证码与图中验证码时候相等
function captcha(e){
if(e == value){
return true;
}
else{
return false;
}
} //点击提交按钮时的结果
function showResult(){
var inputValue = $('.inputCaptcha').val(); if(inputValue == '' ||inputValue == null || inputValue == 'undefined'){
$('.errorText').css({display:'inline-block'}).html('验证码不能为空,请重新输入!');
$('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/false.png')"});
}else{
var flag = captcha(inputValue);
if(!flag){
$('.errorText').css({display:'inline-block'}).html('验证码错误,请重新输入!');
$('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/false.png')"});
}else{
$('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/true.png')"});
}
}
}
$('.captchaSubmit').click(showResult);//提交
$('.refresh').click(createCanvas);//刷新
//点击input框
$('.inputCaptcha').focus(function(){
$('.errorText').add($('.captchaIcon')).fadeOut(100);
})
利用canvas写一个验证码小功能的更多相关文章
- 【MFC】利用MFC写一个计时器小程序
1整体设计 创建对话框程序,并且设计对话框相关控件如图 相应的ID和对应的成员变量如图: 我的想法是这样的,只读属性的编辑框添加有CString类型的成员变量(如s_hour),在xxxDlg.h里另 ...
- 10分钟,利用canvas画一个小的loading界面
首先利用定义下canvas得样式 <canvas width="1024" height="720" id="canvas" styl ...
- 学了C语言,如何利用CURL写一个下载程序?—用nmake编译CURL并安装
在这一系列的前一篇文章学了C语言,如何为下载狂人写一个磁盘剩余容量监控程序?中,我们为下载狂人写了一个程序来监视磁盘的剩余容量,防止下载的东西撑爆了硬盘.可是,这两天,他又抱怨他的下载程序不好用,让我 ...
- 学了C语言,如何利用cURL写一个程序验证某个网址的有效性?
在<C程序设计伴侣>以及这几篇关于cURL的文章中,我们介绍了如何利用cURL写一个下载程序,从网络下载文件.可是当我们在用这个程序下载文件时,又遇到了新问题:如果这个网址是无效的,那么我 ...
- 利用 Python 写一个颜值测试小工具
我们知道现在有一些利用照片来测试颜值的网站或软件,其实使用 Python 就可以实现这一功能,本文我们使用 Python 来写一个颜值测试小工具. 很多人学习python,不知道从何学起.很多人学习p ...
- html5入门:教你用canvas写一个时钟
今天的时间比较充裕,心血来潮,为大家分享一个html5的小例子,希望对刚学html5或者是没学html5正准备学的“童鞋们”展示一个小案例,希望对你们的学习有帮助!高手嘛!请跳过吧! 好了,闲话少数, ...
- Spring Security 实战干货:从零手写一个验证码登录
1. 前言 前面关于Spring Security写了两篇文章,一篇是介绍UsernamePasswordAuthenticationFilter,另一篇是介绍 AuthenticationManag ...
- 手把手教你写一个RN小程序!
时间过得真快,眨眼已经快3年了! 1.我的第一个App 还记得我14年初写的第一个iOS小程序,当时是给别人写的一个单机的相册,也是我开发的第一个完整的app,虽然功能挺少,但是耐不住心中的激动啊,现 ...
- 用canvas画一个的小画板(PC端移动端都能用)
前言 本篇的内容主要包括: canvas标签简介 画板的功能简介 画板的JS部分(包括:1.获取画布 2.使画板全屏幕显示且自适应 3.如何绘制直线 4.绘画时的三种状态(鼠标点击.移动.离开)5.画 ...
随机推荐
- firefly安装步骤
本来公司一个网游服务器端选定了pomelo框架,后来出了个Firefly,为做一个对比,决定研究一下Firefly.看了一下Firefly,感觉头大,python的,本人python小白,只好慢慢折腾 ...
- P5169 xtq的异或和(FWT+线性基)
传送门 我咋感觉我学啥都是白学-- 首先可以参考一下这一题,从中我们可以知道只要知道两点间任意一条路径以及整个图里所有环的线性基,就可以得知这两个点之间的所有路径的异或和 然而我好像并不会求线性基能张 ...
- 大型系统的Redis性能优化
问题描述 系统背景:大型线上Java服务集群(活跃用户数上千万),业务重度使用Redis存储个管理Session,业务并发量>1WQPS,基本上每个请求都需要访问Redis(可能是多次),使用了 ...
- vlc media player
还是很好用的目前来看 倍速播放: [ 减速播放 ] 加速播放 = 恢复原速度
- 基于Tcp协议的上传下载
目录格式: 构建此目录就可随意使用! client端 import socket import sys import os import json import struct sk = socket. ...
- vue文件中style标签的几个标识符
.vue文件中style标签的几个标识符 在人生就要绝望的时候, 被编辑器所提示的一个scopedSlots所拯救. 卧槽, 写到最后才发现这个属性的具体卵用. 详情见最后解决办法. 问题背景 问题由 ...
- ForeignKeyConstraint 外键约束的使用及作用的学习[转]
原文链接 da.SelectCommand.CommandText="select au_id,au_fname,au_lname from authors"; da.Fill(d ...
- ubuntu用户自定义的命令alias永久生效
cd ~ vi .bash_profile alias ll='ls -ltr' . .bash_profile ps:写在.bashrc终端断开就没了
- Git-merge & rebase(变基)
在 Git 中整合来自不同分支的修改主要有两种方法:merge 以及 rebase. merge: merge有两种方法,fast-forward(快速合并)和three-way merge(三方合并 ...
- ubuntu下安装方式汇总
apt-get 可辅助通过 apt-cache search curl | grep php 查找已支持的插件,然后通过下面apt-get下载安装,例: apt-get install php5-cu ...