利用canvas實現的驗證碼效果程式碼例項
本章節分享一段程式碼例項,它利用canvas實現了驗證碼效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> #container { width: 960px; margin: 0 auto; } #box1 { left: 500px; position: absolute; top: 300px; background: #177889; padding: 2px; } #box { position: relative; top: 260px; left: 400px; display: none; } #canvas { box-shadow: 10px 10px 10px #ccc; background: #f60; } #itext { height: 20px; width: 180px; } label { font-size: 20px; vertical-align: middle; padding-right: 5px; } #btn { height: 30px; } </style> </head> <body> <div id="container"> <div id="box"> <canvas id="canvas" width="100" height="30"></canvas> </div> <div id="box1"> <form> <label for="itext">驗證碼:</label> <input id="itext" type="text" placeholder="雙擊切換驗證碼"> <button id="btn">確定</button> </form> </div> </div> <script> var ctx = document.getElementById("canvas").getContext("2d"); loading(); function loading() { ctx.fillStyle = "#f60"; ctx.fillRect(0, 0, 100, 30); var box = document.getElementById("box"); var posx = [20, 40, 60, 80]; var posy = [13, 18, 15, 20]; var color = []; num = []; ctx.font = "20px Arial"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; c(color, num); for (var k = 0; k < 4; k++) { // ctx.beginPath(); ctx.fillStyle = color[k]; ctx.fillText(num[k], posx[k], posy[k]); // ctx.close(); } event(); } function event() { var input = document.getElementById("itext"); var btn = document.getElementById("btn"); input.onmouseover = function() { box.style.display = "block"; }; input.onmouseout = function() { box.style.display = "none"; // loading(); }; input.ondblclick = loading; btn.onclick = check; } function c(color, num) { for (var j = 0; j < 4; j++) { var r, g, b; r = Math.floor(Math.random() * 255); g = Math.floor(Math.random() * 255); b = Math.floor(Math.random() * 255); color[j] = "rgb(" + r + "," + g + "," + b + ")"; num[j] = Math.floor(Math.random() * 9); } } function check() { var s = ''; for (var i = 0; i < num.length; i++) { s += num<i>; } var value = document.getElementById("itext").value; if (s == value) { alert("驗證成功"); } else { alert("驗證碼錯誤"); } } </script> </body> </html></i>
相關文章
- canvas載入效果程式碼例項Canvas
- canvas原型鐘錶效果程式碼例項Canvas原型
- canvas氣泡上浮效果程式碼例項Canvas
- canvas繪製箭頭效果程式碼例項Canvas
- html實現簡單ListViews效果的例項程式碼HTMLView
- JavaScript 表單驗證程式碼例項JavaScript
- canvas刮刮樂程式碼例項Canvas
- canvas繪製扇形程式碼例項Canvas
- 影片直播原始碼,前端canvas動態驗證碼實現原始碼前端Canvas
- canvas繪製網格程式碼例項Canvas
- CSS橢圓效果程式碼例項CSS
- jQuery tab選項卡效果程式碼例項jQuery
- soket 利用http實現驗證碼的傳送HTTP
- Web 端 實現 app “輸入驗證碼 ”的效果WebAPP
- canvas繪製機器貓程式碼例項Canvas
- canvas繪製拋物線程式碼例項Canvas線程
- canvas translate()、scale()和rotate()方法程式碼例項Canvas
- 美化滾動條效果程式碼例項
- css3水滴效果程式碼例項CSSS3
- css背景虛化效果程式碼例項CSS
- Canvas實現放大鏡效果完整案例分析(附程式碼)Canvas
- jQuery利用name匹配元素程式碼例項jQuery
- canvas繪製圓形鐘錶程式碼例項Canvas
- jQuery點選滑出層效果程式碼例項jQuery
- css3晃動效果程式碼例項CSSS3
- CSS3心形效果程式碼例項CSSS3
- div前後翻轉效果程式碼例項
- 當前文字框高亮效果程式碼例項
- CSS3旋轉效果程式碼例項CSSS3
- 淡入淡出效果簡單程式碼例項
- JavaScript驗證碼生成和驗證效果JavaScript
- canvas漫天飛雪效果程式碼Canvas
- CSS3滑過光束效果程式碼例項CSSS3
- CSS3小黃人效果程式碼例項CSSS3
- css3折角效果程式碼例項CSSS3
- CSS3梯形效果程式碼例項CSSS3
- CSS3背景漸變效果程式碼例項CSSS3
- CSS3星系運動效果程式碼例項CSSS3
- 5種PHP生成圖片驗證碼例項PHP