canvas實現的驗證碼效果程式碼例項
驗證碼通常是圖片形式的,canvas恰好就是繪圖的,所以使用canvas實現驗證碼效果也是一種選擇。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</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); 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>
相關文章
- 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
- Web 端 實現 app “輸入驗證碼 ”的效果WebAPP
- canvas繪製機器貓程式碼例項Canvas
- canvas繪製拋物線程式碼例項Canvas線程
- canvas translate()、scale()和rotate()方法程式碼例項Canvas
- 美化滾動條效果程式碼例項
- css3水滴效果程式碼例項CSSS3
- css背景虛化效果程式碼例項CSS
- Canvas實現放大鏡效果完整案例分析(附程式碼)Canvas
- 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
- 【例項】使用GD庫生成圖片驗證碼
- vuejs實現新增tag標籤程式碼例項VueJS