• HTML5 canvas绘制验证码


    <!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>
      <h1>canvas绘制验证码</h1>
      <canvas id="randomCode" width="150px;" height="50px" style="border:1px solid #bbbbbb;cursor: pointer;" title="点击更换验证码">
      </canvas>
      <script>
        var c=document.getElementById("randomCode");
        var w=c.offsetWidth;
        var h=c.offsetHeight;
        randomStr();
        function randomStr(){
          var ctx=c.getContext('2d');
          ctx.clearRect(0,0,150,150);    //先清除canvas画布
          var Letter="ABCDEFGHIJKLMNOPQRSTUVWHYZ1234567890";
          Letter=Letter.split('');
          for(let i=0;i<4;i++){
            var char=Letter[Math.floor(Math.random()*36)];
            var fs=returnNum(30,60);//字体的大小
            var deg=returnNum(-30,30);//字体的旋转角度
            ctx.font=fs+'px Simhei';
            ctx.textBaseline="top";
            ctx.fillStyle=randomColor();
            ctx.save();
            ctx.translate(30*i+15,15);
            ctx.rotate(deg*Math.PI/180);
            ctx.fillText(char,-15+5,-15);
            ctx.restore();
          }
          for(let q=0;q<6;q++){
            ctx.beginPath();
            ctx.lineWidth="1";
            ctx.strokeStyle=randomColor(); // Green path
            ctx.moveTo(returnNum(0,w),returnNum(0,h));
            ctx.lineTo(returnNum(0,w),returnNum(0,h));
            ctx.stroke(); // Draw it
          }
        }
        function randomColor(){
          color = '#'+Math.floor(Math.random()*16777215).toString(16);  
          if(color.length==6){
            color+="0"
          }
          return color;
        }
        function returnNum(min,max){
          return  parseInt(Math.random()*(max-min)+min);
        }
        c.addEventListener("click",function(){
          randomStr()
        })
      </script>
    </body>
    </html>
  • 相关阅读:
    链表--反转链表(leetcode 206
    链表--重排链表(leetcode 143
    链表--删除链表中的结点(leetcode 237
    链表--K个一组反转链表(leetcode 25
    链表--相交链表(leetcode 160
    链表--两数相加II(leetcode 445
    链表--复制含有随机指针节点的链表(leetcode138
    链表--回文链表(leetcode234
    链表--环形链表(leetcode 141,142
    链表--分隔链表(leetcode86
  • 原文地址:https://www.cnblogs.com/banyuege/p/11158720.html
Copyright © 2020-2023  润新知