啥都不说了,复制代码吧!!!

<!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位验证码的更多相关文章

  1. js 做的随机8位验证码

    开发思路: 画出放置验证码的模块.一个写有“看不清…”的小块,以及输入验证码的文本框 获取各个模块 封装一个函数Yan_ma(),设置验证码为8位,里面含有数字,小写字母,小写字母和中文.每种类型出现 ...

  2. canvas画随机的四位验证码

    效果图如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  3. canvas画随机闪烁的星星

    canvas画一颗星星: 规则的星星有内切圆和外切圆,每两个点之间的角度是固定的,因此可得到星星的每个点的坐标,画出星星. function drawStars(x,y,radius1,radius2 ...

  4. js canvas画柱状图 没什么高端的 就是一篇偶尔思路的

    公司项目要用js画柱状图,本来想用个插件吧 chart.js 忽然一想 我们也用不了那么大的插件.自己写个吧,也能看看自己那点数学水平能够不! 有几个小亮点吧 1.函数x 和 函数y 对坐标进行了转化 ...

  5. [Python]random生成随机6位验证码

    #!/usr/bin/env pyhton # coding:utf-8 # @Time : 2020-02-16 10:07 # @Author : LeoShi # @Site : # @File ...

  6. canvas实现随机验证码

    canvas实现随机验证码 知识点 canvas生成背景图和文字 设置字体样式和大小 String的fromCharCode(code码)生成大小写字母和数字 str.toLowerCase()转小写 ...

  7. canvas制作随机验证码

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

  8. 【重点突破】——Canvas技术绘制随机改变的验证码

    一.引言 本文主要是我在学习Canvas技术绘图时的一个小练习,绘制随机改变的验证码图片,虽然真正的项目里不这么做,但这个练习是一个掌握Canvas技术很好的综合练习.(真正的项目中验证码图片使用服务 ...

  9. !JS实战之随机像素图

    JavaScript实例分享之----画随机像素图.随机像素图(作者自己取得名字)指的是一张图片上每一个像素的颜色都是随机的.此时应该能联想到这幅图多么眼花缭乱,好吧,我们用JS来实现它的原因是JS很 ...

随机推荐

  1. Jenkins(Extended E-mail Notification)邮箱配置正确但是并没有发送邮件

    废话 近期在把之前的接口自动化demo与jenkins集成,昨天发现了邮件配置正确但是没有发送邮件的问题,通过勾选系统设置 - >Extended E-mail Notification -&g ...

  2. windows系统下python setup.py install ---出现cl问题,cpp_extension.py:237: UserWarning: Error checking compiler version for cl: 'utf-8' codec can't decode byte 0xd3 in position 0: invalid continuation byte

    将cpp_extension.py文件中的 原始的是   compiler_info.decode() try: if sys.platform.startswith('linux'): minimu ...

  3. Dubbo整合Springboot框架

    本文使用的是alibaba的Dubbo. Dubbo整合Springboot可以分为四步: 第一步:首先需要了解Dubbo官方给的建议,至少有三个工程: 接口工程:主要存实体bean和业务接口 服务提 ...

  4. Qt之先用了再说系列-多线程方式2

    继Qt之先用了再说系列-多线程方式2 本次说说在Qt里多线程使用第2种方式,也是qt官方比较推荐用的方式,相对与直接继承QThread 重写run方法中灵活了一些,如果第一次使用可能会转不湾来,没有直 ...

  5. Web优化躬行记(4)——用户体验和工具

    一.用户体验 用户体验(UE/UX)是指一个人使用一个特定产品.系统或服务时的行为.情绪与态度,还包含用户对于系统的功能.易用和效率的感受,因此用户体验在本质上可以视为一个人对于系统的主观感受与主观想 ...

  6. Golang gRPC学习(03): grpc官方示例程序route_guide简析

    代码主要来源于grpc的官方examples代码: route_guide https://github.com/grpc/grpc-go/tree/master/examples/route_gui ...

  7. 根据pid获得路径 2006-10-25 19:28

    这是编写kill process时用到的 BOOL GetProcessModule(DWORD dwPID, DWORD dwModuleID, LPMODULEENTRY32 lpMe32, DW ...

  8. SpringBoot ---yml 整合 Druid(1.1.23) 数据源

    SpringBoot ---yml 整合 Druid(1.1.23) 数据源 搜了一下,网络上有在配置类写 @Bean 配置的,也有 yml 配置的. 笔者尝试过用配置类配置 @Bean 的方法,结果 ...

  9. 使用IDEA新建基于SpringBoot的Web项目(超详细)

    目前java的开发很多Learner都慢慢的学习使用IDEA这款工具,个人觉得其实相比Eclipse来说差不多,个人习惯的问题,但是我还是推荐IDEA这款工具.虽然它目前是收费的,但相信网上的各种破解 ...

  10. Jmeter系列(49)- 详解 HTTP Cookie 管理器

    如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html 简单介绍 功能一 首先,它像网络浏览器 ...