日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

获取验证码canvas

發布時間:2023/12/31 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 获取验证码canvas 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

1,html部分

<div class="box"><label class="side">驗證碼</label><input class="box-flex input-full" id="verify" type="text" placeholder="請輸入驗證碼" ><div class="side" style="width: 100px;margin-left:10px;"><canvas width="100" height="40" id="verifyCanvas"></canvas><img id="code_img"></div></div>

2,js部分

//下面為驗證碼var nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R','S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x','y', 'z'];drawCode();// 繪制驗證碼function drawCode() {var canvas = document.getElementById("verifyCanvas"); //獲取HTML端畫布var context = canvas.getContext("2d"); //獲取畫布2D上下文context.fillStyle = "#565656"; //畫布填充色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 < 5; i++) {rand[i] = nums[Math.floor(Math.random() * nums.length)]x[i] = i * 16 + 10;y[i] = Math.random() * 20 + 20;context.fillText(rand[i], x[i], y[i]);}//畫3條隨機線for (var i = 0; i < 3; 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 = 'rgba(50,50,50,0.3)'; //隨機線描邊屬性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.stroke();}// 繪制圖片function convertCanvasToImage(canvas) {document.getElementById("verifyCanvas").style.display = "none";var image = document.getElementById("code_img");image.src = canvas.toDataURL("image/png");return image;}// 點擊圖片刷新document.getElementById('code_img').onclick = function () {$('#verifyCanvas').remove();$('#verify').after('<canvas width="100" height="40" id="verifyCanvas"></canvas>');drawCode();}

轉載于:https://my.oschina.net/u/2393989/blog/2254786

總結

以上是生活随笔為你收集整理的获取验证码canvas的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。