利用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
- javascript實現的簡單驗證碼效果程式碼例項JavaScript
- canvas實現的鋸齒效果程式碼例項Canvas
- canvas實現的鐘表效果程式碼例項Canvas
- jquery 驗證碼效果程式碼例項jQuery
- javascript實現的驗證碼程式碼例項JavaScript
- canvas實現的雪花飄落效果程式碼例項Canvas
- canvas實現的藍色雨滴效果程式碼例項Canvas
- canvas實現的彈力小球效果程式碼例項Canvas
- canvas實現煙花燃放效果程式碼例項Canvas
- jQuery加法驗證碼效果程式碼例項jQuery
- canvas實現的七巧板效果程式碼例項Canvas
- canvas實現的動態心形效果程式碼例項Canvas
- canvas實現的圓形鐘錶效果程式碼例項Canvas
- canvas實現的簡單畫板效果程式碼例項Canvas
- canvas火焰效果程式碼例項Canvas
- canvas實現的刮刮樂程式碼例項Canvas
- canvas實現的賽貝爾曲線效果程式碼例項Canvas
- canvas實現的小球四壁碰撞效果程式碼例項Canvas
- canvas實現文字線性漸變效果程式碼例項Canvas
- canvas字母雨效果程式碼例項Canvas
- canvas載入效果程式碼例項Canvas
- canvas擺動效果程式碼例項Canvas
- canvas實現的旋轉的太極圖效果程式碼例項Canvas
- canvas實現的美猴王程式碼例項Canvas
- 利用html5實現的飛雪效果程式碼例項HTML
- canvas水位進度效果程式碼例項Canvas
- canvas原型鐘錶效果程式碼例項Canvas原型
- canvas氣泡上浮效果程式碼例項Canvas
- canvas田字格效果程式碼例項Canvas
- canvas繪製圓環效果程式碼例項Canvas
- canvas螞蟻線效果程式碼例項Canvas
- javascript實現的生成驗證碼隨機數程式碼例項JavaScript隨機
- canvas實現點選產生放射性效果程式碼例項Canvas
- canvas實現的變幻線程式碼例項Canvas線程
- canvas實現的多邊形程式碼例項Canvas
- canvas實現的千輪眼程式碼例項Canvas
- canvas模擬彈幕效果程式碼例項Canvas