纯前端生成验证码

ゝ一世哀愁。 2022-10-12 15:58 261阅读 0赞

纯前端生成验证码

生成验证码的方法:

  1. function code_draw() {
  2. var canvas_width = $('#canvas').width();
  3. var canvas_height = $('#canvas').height();
  4. var canvas = document.getElementById("canvas"); //获取到canvas的对象,演员
  5. var context = canvas.getContext("2d"); //获取到canvas画图的环境,演员表演的舞台
  6. canvas.width = canvas_width;
  7. canvas.height = canvas_height;
  8. var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0"; //取值范围
  9. var aCode = sCode.split(",");
  10. var aLength = aCode.length; //获取到数组的长度
  11. var value = [];
  12. for (var i = 0; i <= 3; i++) {
  13. var j = Math.floor(Math.random() * aLength); //获取到随机的索引值
  14. var deg = Math.random() * 30 * Math.PI / 180; //产生0~30之间的随机弧度
  15. var txt = aCode[j]; //得到随机的一个内容
  16. value[i] = txt.toLowerCase();
  17. var x = 10 + i * 20; //文字在canvas上的x坐标
  18. var y = 20 + Math.random() * 8; //文字在canvas上的y坐标
  19. context.font = "bold 23px 微软雅黑";
  20. context.translate(x,y);
  21. context.rotate(deg);
  22. context.fillStyle = code_randomColor();
  23. context.fillText(txt,0);
  24. context.rotate(-deg);
  25. context.translate(-x,-y);
  26. }
  27. // 将生成的值以属性的方法添加到元素
  28. value = value.join("");
  29. $('#canvas').attr('data-code',value)
  30. //验证码上显示线条
  31. for (var i = 0; i <= 5; i++) {
  32. context.strokeStyle = code_randomColor();
  33. context.beginPath();
  34. context.moveTo(Math.random() * canvas_width,Math.random() * canvas_height);
  35. context.lineTo(Math.random() * canvas_width,Math.random() * canvas_height);
  36. context.stroke();
  37. }
  38. //验证码上显示小点
  39. for (var i = 0; i <= 30; i++) {
  40. context.strokeStyle = code_randomColor();
  41. context.beginPath();
  42. var x = Math.random() * canvas_width;
  43. var y = Math.random() * canvas_height;
  44. context.moveTo(x,y);
  45. context.lineTo(x + 1,y + 1);
  46. context.stroke();
  47. }
  48. }

生成验证码随机背景颜色:

  1. function code_randomColor() {
  2. //得到随机的颜色值
  3. var r = Math.floor(Math.random() * 256);
  4. var g = Math.floor(Math.random() * 256);
  5. var b = Math.floor(Math.random() * 256);
  6. return "rgb(" + r + "," + g + "," + b + ")";
  7. }

在html中引用:

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>jQuery canvas验证码</title>
  6. <style type="text/css">
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. Box-sizing: border-Box;
  11. }
  12. html {
  13. width: 100%;
  14. height: 100%;
  15. font-size: 16px;
  16. }
  17. body {
  18. width: 100%;
  19. height: 100%;
  20. -moz-user-select: none;
  21. /*火狐*/
  22. /*禁止用户在页面选择文字*/
  23. -webkit-user-select: none;
  24. /*webkit浏览器*/
  25. -ms-user-select: none;
  26. /*IE10*/
  27. -khtml-user-select: none;
  28. /*早期浏览器*/
  29. user-select: none;
  30. }
  31. .code {
  32. padding-top: 50px;
  33. width: 400px;
  34. margin: 0 auto;
  35. }
  36. .input-val {
  37. width: 295px;
  38. background: #ffffff;
  39. height: 2.8rem;
  40. padding: 0 2%;
  41. border-radius: 5px;
  42. border: none;
  43. border: 1px solid rgba(0,.2);
  44. font-size: 1.0625rem;
  45. }
  46. #canvas {
  47. float: right;
  48. display: inline-block;
  49. border: 1px solid #ccc;
  50. border-radius: 5px;
  51. cursor: pointer;
  52. }
  53. .btn {
  54. width: 100px;
  55. height: 40px;
  56. background: #f1f1f1;
  57. border: 1px solid #ccc;
  58. border-radius: 5px;
  59. margin: 20px auto 0;
  60. display: block;
  61. font-size: 1.2rem;
  62. color: #e22e1c;
  63. cursor: pointer;
  64. }
  65. </style>
  66. </head>
  67. <body>
  68. <div class="code">
  69. <input type="text" value="" placeholder="验证码" class="input-val" />
  70. <canvas id="canvas" width="100" height="43"></canvas>
  71. <button class="btn">提交</button>
  72. </div>
  73. </body>

发表评论

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

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

相关阅读

    相关 验证生成

    视图函数里 > 1,导入图片,画布,画笔,画笔上的字体,定义坐标 > 2,从外部导入字体(字体导入到static静态中) > 3,随机生成验证码,验证码颜色,验证