使用js做带干扰点的验证码

代码如下
<body onload="drawCoda()">
   <canvas id="mycanvas" onclick="drawCoda()"></canvas>
   <script type="text/javascript">
      function drawCoda(){
      	var c=document.getElementById('mycanvas');
      	var w=80;
      	var h=30;
      	c.width=80;
      	c.height=30;
      	c.style.border="1px solid balck"; 
            r1=Math.floor(Math.random()*255);
            g1=Math.floor(Math.random()*255);
            b1=Math.floor(Math.random()*255);
      	var context=c.getContext('2d');
      	context.fillStyle="#ccc";
      	context.fillRect(0,0,80,30);
      	context.fillStyle="red";
      	var text="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            context.font="25px 黑体"; 
            context.rotate(Math.random()*8*Math.PI/180)
             context.fillStyle="rgb("+r1+','+g1+','+b1+")";
            context.fillText(text[Math.floor(Math.random()*text.length)],10,20);
            r2=Math.floor(Math.random()*255);
            g2=Math.floor(Math.random()*255);
            b2=Math.floor(Math.random()*255);

            context.font="25px 黑体"; 
            context.rotate(Math.random()*8*Math.PI/180)
             context.fillStyle="rgb("+r2+','+g2+','+b2+")";
            context.fillText(text[Math.floor(Math.random()*text.length)],25,20);
            r3=Math.floor(Math.random()*255);
            g3=Math.floor(Math.random()*255);
            b3=Math.floor(Math.random()*255);

            context.font="25px 黑体"; 
            context.rotate(Math.random()*-8*Math.PI/180)
             context.fillStyle="rgb("+r3+','+g3+','+b3+")";
            context.fillText(text[Math.floor(Math.random()*text.length)],40,20);
            r4=Math.floor(Math.random()*255);
            g4=Math.floor(Math.random()*255);
            b4=Math.floor(Math.random()*255);
            context.font="25px 黑体"; 

            context.rotate(Math.random()*-8*Math.PI/180)
            context.fillStyle="rgb("+r4+','+g4+','+b4+")";
            context.fillText(text[Math.floor(Math.random()*text.length)],55,20);
            
      	for(var i=1;i<=100;i++){
                   r=Math.floor(Math.random()*255);
              g=Math.floor(Math.random()*255);
                   b=Math.floor(Math.random()*255);
                  x=Math.floor(Math.random()*w);
                  y=Math.floor(Math.random()*h);
      		context.fillStyle="rgb("+r+','+g+','+b+")";
      		context.beginPath();
      		context.arc(x,y,1,0,2*Math.PI);
      		context.fill();
      	}
      }
   </script>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值