【css+js+canvas】canvas的验证码生成

来源:互联网 发布:知乎比较有趣的话题 编辑:程序博客网 时间:2024/05/29 18:05
<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>Document</title></head><body>    <canvas id="canvas" width="150" height="50"></canvas><script>    function randomNum(min,max){        return Math.floor(Math.random()*(max-min)+min);    }    function randomColor(min,max){        var _r = randomNum(min,max);        var _g = randomNum(min,max);        var _b = randomNum(min,max);        return "rgb("+_r+","+_g+","+_b+")";    }    document.getElementById("canvas").onclick = function(e){        e.preventDefault();        drawPic();    };    function drawPic(){        var $canvas = document.getElementById("canvas");        var _str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";        var _picTxt = "";        var _num = 6;        var _width = $canvas.width;        var _height = $canvas.height;        var ctx = $canvas.getContext("2d");        ctx.textBaseline = "bottom";        ctx.fillStyle = randomColor(180,240);        ctx.fillRect(0,0,_width,_height);        for(var i=0; i<_num; i++){            var x = (_width-10)/_num*i+10;            var y = randomNum(_height/2,_height); /*y轴位置*/            var deg = randomNum(-45,45);            var txt = _str[randomNum(0,_str.length)];/*验证码*/            _picTxt += txt;            ctx.fillStyle = randomColor(10,100);            ctx.font = randomNum(16,40)+"px SimHei";            ctx.translate(x,y);            ctx.rotate(deg*Math.PI/180);            ctx.fillText(txt, 0,0);            ctx.rotate(-deg*Math.PI/180);            ctx.translate(-x,-y);        }        for(var i=0; i<_num; i++){            ctx.strokeStyle = randomColor(90,180);            ctx.beginPath();            ctx.moveTo(randomNum(0,_width), randomNum(0,_height));            ctx.lineTo(randomNum(0,_width), randomNum(0,_height));            ctx.stroke();        }        for(var i=0; i<_num*10; i++){            ctx.fillStyle = randomColor(0,255);            ctx.beginPath();            ctx.arc(randomNum(0,_width),randomNum(0,_height), 1, 0, 2*Math.PI);            ctx.fill();        }        return _picTxt;    }    drawPic();</script></body></html>

转载:http://www.bcty365.com/content-151-250-1.html#

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 外祖父悖论 悖论的意思 斯密悖论 二元悖论 epr悖论 钱包悖论 超时空悖论 悖论的拼音 悖论读音 节俭的悖论 费米悖论为什么恐怖 悖论by流苏popo 悖论流苏popo 逃离方块悖论 悖论是什么意思 悖论什么意思 逃离方块悖论攻略 科洛弗悖论三部曲 阿基里斯悖论 悖论by流苏在线阅读 十个著名悖论 逃离方块悖论图文攻略 悖论by流苏全文 锈湖悖论攻略 极度恐怖的悖论 方块逃脱悖论 恐怖的哲学悖论 等腰三角形悖论 逃离方块悖论汉化版 超时空悖论 金属裂纹 逃离方块悖论下载 托克维尔悖论 细思极恐的科学悖论 静悟苍生2019 静悟苍生 悟饭模拟器 悟饭趣玩 勿小悟作品 悟组词 九悟作品 一杯清茶悟人生图片