JS 实现图片验证码功能

左手的ㄟ右手 2023-10-05 13:04 115阅读 0赞

1 HTML代码

  1. <div style="margin: 0px auto 20px auto;width: 240px;">
  2. <input type="text" id="verify" style="margin: 10px 0px;width: 100px;height:26px;margin-left:5px;display: inline-block;float: left;">
  3. <canvas width="100" height="34" id="verifyCanvas" style="display: none;"></canvas>
  4. <img width="100" height="34" id="code_img" style="margin: 10px 10px;" onclick="imgRefresh('verifyCanvas','code_img','verify')">
  5. </div>

2 JS代码

  1. var nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
  2. 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
  3. //绘制验证码
  4. var currentVerifyCode="";
  5. function drawVerificationCode(canvasId,codeImgId){
  6. var canvas = document.getElementById(canvasId); //获取HTML端画布
  7. var context = canvas.getContext("2d"); //获取画布2D上下文
  8. context.fillStyle = "cornflowerblue"; //画布填充色
  9. context.fillRect(0, 0, canvas.width, canvas.height); //清空画布
  10. context.fillStyle = "white"; //设置字体颜色
  11. context.font = "25px Arial"; //设置字体
  12. var rand = new Array();
  13. var x = new Array();
  14. var y = new Array();
  15. currentVerifyCode="";
  16. for (var i = 0; i < 5; i++) {
  17. rand[i] = nums[Math.floor(Math.random() * nums.length)]
  18. x[i] = i * 16 + 10;
  19. y[i] = Math.random() * 20 + 20;
  20. context.fillText(rand[i], x[i], y[i]);
  21. currentVerifyCode+=rand[i];
  22. }
  23. //画3条随机线
  24. for (var i = 0; i < 3; i++) {
  25. drawline(canvas, context);
  26. }
  27. // 画30个随机点
  28. for (var i = 0; i < 30; i++) {
  29. drawDot(canvas, context);
  30. }
  31. convertCanvasToImage(canvasId,codeImgId,canvas);
  32. }
  33. // 随机线
  34. function drawline(canvas, context) {
  35. context.moveTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //随机线的起点x坐标是画布x坐标0位置,y坐标是画布高度的随机数
  36. context.lineTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //随机线的终点x坐标是画布宽度,y坐标是画布高度的随机数
  37. context.lineWidth = 0.5; //随机线宽
  38. context.strokeStyle = 'rgba(50,50,50,0.3)'; //随机线描边属性
  39. context.stroke(); //描边,即起点描到终点
  40. }
  41. // 随机点(所谓画点其实就是画1px像素的线,方法不再赘述)
  42. function drawDot(canvas, context) {
  43. var px = Math.floor(Math.random() * canvas.width);
  44. var py = Math.floor(Math.random() * canvas.height);
  45. context.moveTo(px, py);
  46. context.lineTo(px + 1, py + 1);
  47. context.lineWidth = 0.2;
  48. context.stroke();
  49. }
  50. // 绘制图片
  51. function convertCanvasToImage(canvasId,codeImgId,canvas) {
  52. document.getElementById(canvasId).style.display = "none";
  53. var image = document.getElementById(codeImgId);
  54. image.src = canvas.toDataURL("image/png");
  55. return image;
  56. }
  57. // 点击图片刷新
  58. function imgRefresh(canvasId,codeImgId,verifyTextId){
  59. $('#'+canvasId).remove();
  60. $('#'+verifyTextId).after('<canvas width="100" height="40" id="'+canvasId+'"></canvas>')
  61. drawVerificationCode(canvasId,codeImgId);
  62. }
  63. function getCurrentVerifyCode(){
  64. return currentVerifyCode;
  65. }
  66. $(function(){
  67. imgRefresh('verifyCanvas','code_img','verify');
  68. })

3 最终效果图

20201102213303978.png

发表评论

表情:
评论列表 (有 0 条评论,115人围观)

还没有评论,来说两句吧...

相关阅读

    相关 js实现验证功能

    \前面是拆解着讲的,不想看可以直接跳过,带注释的完整版代码和效果在后面 首先在页面中准备一个输入框,一个显示验证码的盒子和一个提交按钮 <body>