js+canvas画随机4位验证码
啥都不说了,复制代码吧!!!
<!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>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html> <script>
class IdentifyCode{
constructor(canvasId, width, height){
this.canvas = document.querySelector(`#${canvasId}`);
this.ctx = this.canvas.getContext("2d");
this.canvas.width = width;
this.canvas.height = height;
this.arrRange = [];
this.code = "";
this.range();
this.buildCode();
this.drawBakcGround();
this.drawInterferingline();
this.drawInterferencePoint();
this.drawWord();
}
//生成一个随机数 randomNum(min, max){
return parseInt(Math.random()*(max - min) + min);
}
//生成随机颜色
randomColor(min, max){
var r = this.randomNum(min, max);
var g = this.randomNum(min, max);
var b = this.randomNum(min, max);
return `rgb(${r},${g},${b})`
}
//生成字母和数字
range(){
this.arrRange = [];
//0-9
for (let i = "0".charCodeAt(0); i <= "9".charCodeAt(0); i++) {
this.arrRange.push(String.fromCharCode(i))
}
//A-Z
for (let i = "A".charCodeAt(0); i <= "Z".charCodeAt(0); i++) {
this.arrRange.push(String.fromCharCode(i));
}
//a-z
for (let i = "a".charCodeAt(0); i < "z".charCodeAt(0); i++) {
this.arrRange.push(String.fromCharCode(i))
}
} //生成四位随机码
buildCode(){
var code = "";
for (let i = 0; i < 4; i++) {
code += this.arrRange[Math.floor(Math.random()*this.arrRange.length)]
}
this.code = code;
} //画背景
drawBakcGround(){
this.ctx.fillStyle = this.randomColor(0,255);
this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height);
this.ctx.fill()
}
//写字
drawWord(){
var bothSide = 15;//两边间距
var letterSpace = (this.canvas.width - 2*bothSide) / 4;
// console.log(letterSpace);
for(var i = 0; i < 4; i++){
this.ctx.font = `${this.randomNum(this.canvas.width/4, this.canvas.width/2)}px 黑体`;
this.ctx.fillStyle = this.randomColor(100, 200);
this.ctx.save();
this.ctx.translate(bothSide + letterSpace * i, this.canvas.height/2)
this.ctx.rotate(Math.random()*(Math.PI / 6) * (-1)**(Math.round(Math.random())));
this.ctx.textBaseline = "middle";
this.ctx.fillText(this.code[i], 0, 0);
this.ctx.restore();
} }
//画干扰线
drawInterferingline(){
for(var i=0; i<4; i++){
this.ctx.beginPath();
this.ctx.moveTo(this.randomNum(0, this.canvas.width), this.randomNum(0, this.canvas.height));
this.ctx.lineTo(this.randomNum(0, this.canvas.width), this.randomNum(0, this.canvas.height));
this.ctx.closePath();
this.ctx.strokeStyle = this.randomColor(0, 255);
this.ctx.lineWidth = Math.ceil(Math.random()*2);
this.ctx.stroke();
}
}
//画干扰点
drawInterferencePoint(){
var r = 4;
var num = this.canvas.width*this.canvas.width / 1000;
// console.log(num)
for(var i = 0; i < Math.floor(num); i++){
this.ctx.beginPath();
this.ctx.fillStyle = this.randomColor(0,255);
console.log(this.randomNum(0, this.canvas.width), this.randomNum(0, this.canvas.height), r, 0, 2 * Math.PI, true)
this.ctx.arc(this.randomNum(0,this.canvas.width),this.randomNum(0,this.canvas.height),r,0,2*Math.PI,true)
this.ctx.fill();
this.ctx.closePath();
}
}
//更换验证码
update(){
this.clear();
this.range();
this.buildCode();
this.drawBakcGround(); this.drawInterferingline();
this.drawInterferencePoint();
this.drawWord();
} //清除
clear(){
this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height)
}
} var identifyCode = new IdentifyCode("canvas",100,40); document.querySelector("#canvas").onclick = function(){
identifyCode.update()
}
</script>
js+canvas画随机4位验证码的更多相关文章
- js 做的随机8位验证码
开发思路: 画出放置验证码的模块.一个写有“看不清…”的小块,以及输入验证码的文本框 获取各个模块 封装一个函数Yan_ma(),设置验证码为8位,里面含有数字,小写字母,小写字母和中文.每种类型出现 ...
- canvas画随机的四位验证码
效果图如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- canvas画随机闪烁的星星
canvas画一颗星星: 规则的星星有内切圆和外切圆,每两个点之间的角度是固定的,因此可得到星星的每个点的坐标,画出星星. function drawStars(x,y,radius1,radius2 ...
- js canvas画柱状图 没什么高端的 就是一篇偶尔思路的
公司项目要用js画柱状图,本来想用个插件吧 chart.js 忽然一想 我们也用不了那么大的插件.自己写个吧,也能看看自己那点数学水平能够不! 有几个小亮点吧 1.函数x 和 函数y 对坐标进行了转化 ...
- [Python]random生成随机6位验证码
#!/usr/bin/env pyhton # coding:utf-8 # @Time : 2020-02-16 10:07 # @Author : LeoShi # @Site : # @File ...
- canvas实现随机验证码
canvas实现随机验证码 知识点 canvas生成背景图和文字 设置字体样式和大小 String的fromCharCode(code码)生成大小写字母和数字 str.toLowerCase()转小写 ...
- canvas制作随机验证码
看到人家彩色背景的验证码想测试一下: 创建html代码: <canvas id="myCanvas" width="200" height="1 ...
- 【重点突破】——Canvas技术绘制随机改变的验证码
一.引言 本文主要是我在学习Canvas技术绘图时的一个小练习,绘制随机改变的验证码图片,虽然真正的项目里不这么做,但这个练习是一个掌握Canvas技术很好的综合练习.(真正的项目中验证码图片使用服务 ...
- !JS实战之随机像素图
JavaScript实例分享之----画随机像素图.随机像素图(作者自己取得名字)指的是一张图片上每一个像素的颜色都是随机的.此时应该能联想到这幅图多么眼花缭乱,好吧,我们用JS来实现它的原因是JS很 ...
随机推荐
- javascript逻辑运算与循环笔记
短路运算(逻辑中断) 1.短路运算的原理:当有多个表达式(值)时,左边的表达式值可以确定结果的时候就不再继续运算右边的表达式的值 2.逻辑与 && 如果 ...
- 使用webgl(three.js)创建科技版3D机房,3D机房微模块详细介绍(升级版三)—— 1
上节课已经详细描述了微模块机房的实现过程,文章地址(https://www.cnblogs.com/yeyunfei/p/10484241.html) 紧接着上节课的内容 我们这节可来详细讲解科技版机 ...
- Flutter 打包程序 build android apk
Step-1 Java 路径 找到java路径, 可使用[flutter doctor -v] Step-2: 进入目录 找到路径后 C:\Program Files\Java\jre1.8.0_23 ...
- 聊聊mysql中的int(1)
昨天有个读者问了我这样一个问题在mysql中建表的时候,我设置一个字段为int类型,长度为1,但是我发现这个字段却可以存储任意长度的数字,这是什么情况?这个问题在我刚接触数据库的时候也遇到过,我觉得有 ...
- Dubbo系列之 (二)Registry注册中心-注册(2)
引导 本章主要介绍下AbstractRegistry.FailbackRegistry的作用和源码. AbstractRegistry 首先,直接引出这个类的作用,该类主要把服务提供者信息缓存本地文件 ...
- 使用动态链接为什么还需要静态库lib文件
在Windows上使用动态链接时,不光需要头文件 .dll文件 还需要一个.lib 文件. 不是动态链接吗?为什么还需要静态库.lib文件? 实际上,这个.lib文件并不是静态库,而是 导入库 文件, ...
- 精讲RestTemplate第9篇-如何通过HTTP Basic Auth认证
本文是精讲RestTemplate第9篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...
- Kubernetes实战总结 - 自定义Prometheus
一.概述 首先Prometheus整体监控结构略微复杂,一个个部署并不简单.另外监控Kubernetes就需要访问内部数据,必定需要进行认证.鉴权.准入控制, 那么这一整套下来将变得难上加难,而且还需 ...
- 笔记:html基础
一.HTML:超文本标记语言,是一种标签语言,不是编程语言,显示数据有双标签<body></body> 和单标签<img src=# / >, 标签大小写都可以 通 ...
- Ubuntu LNMP环境的搭建
一.安装nginx Step1:安装: sudo apt-get install nginx Step2:查看ngnix 运行状态 : service nginx status 查看80端口是否开启: ...