当前位置: 首页 > news >正文

【HTML】纯前台字符验证码

效果图:

大致思路:

1.在<canvas>画布里写出几个字符;

2.给字符一个随机的角度和颜色;

3.给字符上画出一些干扰线和干扰点。


<canvas width="100" height="30" id="canvasRef" @click="handleDrawCode"></canvas>
      /** 生成并渲染出验证码图形 */handleDrawCode () {const CanvasRef = document.getElementById('canvasRef');this.showCode = '';const canvasWidth = CanvasRef.width;const canvasHeight = CanvasRef.height;const context = CanvasRef.getContext('2d'); // 获取到canvas画图的环境context.clearRect(0, 0, canvasWidth, canvasHeight);const sCode = 'A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,1,2,3,4,5,6,7,8,9,0';const aCode = sCode.split(',');const aLength = aCode.length; // 获取到数组的长度// 这里获取4位验证码for (let i = 0; i < 4; i++) { const j = Math.floor(Math.random() * aLength); // 获取到随机的索引值const deg = Math.random() - 0.5; // 产生一个随机弧度const txt = aCode[j]; // 得到随机的一个内容this.showCode += txt.toLowerCase(); // 转小写const x = 10 + i * 20; // 文字在canvas上的x坐标const y = 20 + Math.random() * 8; // 文字在canvas上的y坐标context.font = '0.5rem 微软雅黑';context.translate(x, y);context.rotate(deg);context.fillStyle = this.getColor();context.fillText(txt, 0, 0);context.rotate(-deg);context.translate(-x, -y);}// 验证码上显示5根线条for (let i = 0; i <= 5; i++) { context.strokeStyle = this.getColor();context.beginPath();context.moveTo(Math.random() * canvasWidth, Math.random() * canvasHeight);context.lineTo(Math.random() * canvasWidth, Math.random() * canvasHeight);context.stroke();}// 验证码上添加20个小点for (let i = 0; i <= 20; i++) { context.strokeStyle = this.getColor(); // 随机生成context.beginPath();const x = Math.random() * canvasWidth;const y = Math.random() * canvasHeight;context.moveTo(x, y);context.lineTo(x + 1, y + 1);context.stroke();}},/** 得到随机的颜色值 */getColor () {const r = Math.floor(Math.random() * 256);const g = Math.floor(Math.random() * 256);const b = Math.floor(Math.random() * 256);return 'rgb(' + r + ',' + g + ',' + b + ')';},
canvas {margin-left: 2rem;vertical-align: middle;/*vertical-align属性设置一个元素的垂直对齐。*/box-sizing: border-box;border: 1px solid #ddd;cursor: pointer;background-color: #eee;}

本文参考 vue/js图形校验码验证 - 简书 来写,原文直接放来我的项目里有些问题,做了一些改动,作为学习笔记供有需要的人参考。

http://www.lryc.cn/news/421344.html

相关文章:

  • 如何在 Vue.js 项目中动态设置页面标题
  • Eval绕过限制参数限制
  • 计算机网络408考研 2021
  • element table表格树形数据展示
  • Ubuntu 安装 Snipaste
  • NET8环境WebAPI实现文件的压缩及下载
  • Ubuntu 18 使用NVIDIA上的HDMI输出声音
  • C#模拟量线性变换小程序
  • 跟《经济学人》学英文:2024年08月10日这期 How AI models are getting smarter
  • Spring Web MVC入门(上)
  • 【c++】公差判断函数 isInTolerance
  • 电脑新加的硬盘如何分区?新加硬盘分区选MBR还是GPT
  • 白骑士的Matlab教学基础篇 1.3 控制流
  • 设计模式 - 适配器模式
  • docker部署minIO
  • 「Pytorch」BF16 Mixed Precision Training
  • 论文阅读:Efficient Core Maintenance in Large Bipartite Graphs | SIGMOD 2024
  • LLMOps — 使用 BentoML 为 Llama-3 模型提供服务
  • 微软蓝屏事件揭秘:有问题的数据引发内存读取越界
  • NASA:北极ARCTAS差分吸收激光雷达(DIAL)遥感数据
  • Android 文件上传与下载
  • Java语言的充电桩系统Charging station system
  • RCE之无参数读取文件
  • Python GUI开发必看:Tkinter Button控件使用详解
  • 上海市计算机学会竞赛平台2024年7月月赛丙组得分排名
  • Can GPT-3 Perform Statutory Reasoning?
  • redis面试(十一)锁超时
  • C代码做底层及Matlab_SimuLink做应用层设计单片机程序
  • Cloud Kernel SIG 月度动态:ANCK OOT 驱动基线更新,发布 2 个 ANCK 版本
  • vue3仿飞书头像,根据不同名称生成不同的头像背景色