下面的内容均摘自https://www.cnblogs.com/zh-1721342390/p/9370794.html,如果想要了解详细,请到原文查看(PS:记得给博主点赞哦哦哦哦哦哦)
好处:减轻后端生成图片和传输过程的流量的传递,只获取验证码的数据,构建图片过程在客户端进行。
JS:
function drawCode(ImageCode) { var str = ''; var canvas = document.getElementById("verifyCanvas"); //获取HTML端画布 var context = canvas.getContext("2d"); //获取画布2D上下文 context.fillStyle = "cornflowerblue"; //画布填充色 context.fillRect(0, 0, canvas.width, canvas.height); //清空画布 context.fillStyle = "white"; //设置字体颜色 context.font = "25px Arial"; //设置字体 var rand = new Array(); var x = new Array(); var y = new Array(); for (var i = 0; i < 4; i++) { rand.push(rand[i]); rand[i] = ImageCode.charAt(i); x[i] = i * 20 + 10; y[i] = Math.random() * 20 + 20; context.fillText(rand[i], x[i], y[i]); } str = rand.join('').toUpperCase(); //画3条随机线 for (var i = 0; i < 20; i++) { drawline(canvas, context); } // 画30个随机点 for (var i = 0; i < 30; i++) { drawDot(canvas, context); } convertCanvasToImage(canvas); } // 随机线 function drawline(canvas, context) { context.moveTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //随机线的起点x坐标是画布x坐标0位置,y坐标是画布高度的随机数 context.lineTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //随机线的终点x坐标是画布宽度,y坐标是画布高度的随机数 context.lineWidth = 0.5; //随机线宽 context.strokeStyle = getBackgroud(0.1); //随机线描边属性 context.stroke(); //描边,即起点描到终点 } // 随机点(所谓画点其实就是画1px像素的线,方法不再赘述) function drawDot(canvas, context) { var px = Math.floor(Math.random() * canvas.width); var py = Math.floor(Math.random() * canvas.height); context.moveTo(px, py); context.lineTo(px + 1, py + 1); context.lineWidth = 0.2; context.strokeStyle = getBackgroud(0.1); context.stroke(); } // 绘制图片 function convertCanvasToImage(canvas) { document.getElementById("verifyCanvas").style.display = "none"; var image = document.getElementById("code_img"); image.src = canvas.toDataURL("image/png"); return image; } // 生成随机背景 function getBackgroud(Transparency) { var RColor = 255 * Math.random(); var GColor = 255 * Math.random(); var BColor = 255 * Math.random(); return "rgba(" + RColor + ", " + GColor + ", " + BColor + ", " + Transparency + ")"; }
aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DeleteWebAPI.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <%-- 根据验证码构建验证码图片 --%> <div id="verificationCode"> <canvas width="100" height="40" id="verifyCanvas"></canvas> <img id="code_img" /> </div> <script type="text/javascript" src="Script/jquery-3.4.1.min.js"></script> <script type="text/javascript" src="Script/ImageCode.js"></script> <script type="text/javascript"> $(function () { $.ajax({ url: "/api/Default/ImageCode", type: "get", contentType: "application/json", success: function (data) { //console.log(data["Result"]); drawCode(data["Result"]); // 获取传递过来的验证码信息 }, error: function (xmlRequest) { alert(xmlRequest.responseText); } }); }); </script> </body> </html>