<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> body { text-align: center; } canvas { background-color: bisque; } </style> </head> <body> <canvas width="500" height="500"></canvas> <script> var canvas = document.getElementsByTagName('canvas')[0]; var ctx = canvas.getContext('2d'); var w = 500; var r = 250; //半径 // 绘制表盘 function fun() { ctx.clearRect(0, 0, w, w); ctx.save(); // 重新定位圆心 ctx.translate(r, r); // 绘制表盘 ctx.beginPath(); ctx.arc(0, 0, r - 5, 0, Math.PI * 2); ctx.lineWidth = '5'; ctx.closePath(); ctx.stroke(); // 绘制表盘数字 ctx.font = '18px 微软雅黑'; ctx.textAlign = 'center'; ctx.textBaseline = "middle"; var hourNum = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2]; for (var i = 0; i < 12; i++) { //获取对应的弧度 var rad = Math.PI * 2 / 12 * i; var x = Math.cos(rad) * (r - 30); var y = Math.sin(rad) * (r - 30); ctx.fillText(hourNum[i], x, y); } // 绘制小点 for (var i = 0; i < 60; i++) { //获取对应的弧度 var rad = Math.PI * 2 / 60 * i; var x = Math.cos(rad) * (r - 15); var y = Math.sin(rad) * (r - 15); ctx.beginPath(); ctx.arc(x, y, 2, 0, Math.PI * 2); ctx.closePath(); if (i % 5 == 0) { ctx.fillStyle = '#000'; } else { ctx.fillStyle = '#ccc' } ctx.fill(); } } // 绘制时针 function drawHour(hour, minu) { ctx.save(); //保存当前环境 var rad = Math.PI * 2 / 12 * hour; // 小时度数 var rad1 = Math.PI * 2 / 12 / 60 * minu; // 分钟度数 ctx.beginPath(); ctx.rotate(rad + rad1); //旋转当前绘图 ctx.moveTo(0, 10); ctx.lineTo(0, -r / 2); ctx.lineWidth = "8"; //时针粗细 ctx.lineCap = "round"; //圆角 ctx.stroke(); ctx.restore(); //还原 } // 绘制分针 function drawMinu(minu) { ctx.save(); var rad = Math.PI * 2 / 60 * minu; ctx.beginPath(); ctx.rotate(rad); ctx.moveTo(0, 20); ctx.lineTo(0, -r + 100); ctx.lineWidth = '5'; ctx.lineCap = 'round'; ctx.stroke(); ctx.restore(); } // 绘制秒针 function drawSecond(second) { ctx.save(); var rad = Math.PI * 2 / 60 * second; ctx.beginPath(); ctx.rotate(rad); ctx.moveTo(0, 20); ctx.lineTo(0, -r + 20); ctx.lineWidth = '4'; ctx.strokeStyle = '#f00'; ctx.stroke(); ctx.restore(); } function dot() { ctx.beginPath(); ctx.arc(0, 0, 5, 0, Math.PI * 2); ctx.closePath(); ctx.fillStyle = "#fff"; ctx.fill(); } setInterval(function() { //获取当前时间 var timeData = new Date(); fun(); var hours = timeData.getHours(); var minu = timeData.getMinutes(); var seconds = timeData.getSeconds(); // 绘制时分秒 drawHour(hours, minu); drawMinu(minu); drawSecond(seconds); dot(); ctx.restore(); }, 1000) // fun(); </script> </body> </html>
图片验证
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> body { text-align: center; margin-top: 100px; } </style> </head> <body> <div class="col-md-3 col-sm-3 col-xs-12 msg-code-box imgCode" id="code-img"> <canvas id="canvas" width="100" height="38"></canvas> <span id="changeImg" class="glyphicon glyphicon-refresh" aria-hidden="true">提交</span> </div> <script> var imgCodeObj = { imgCodeStr: '', /**生成一个随机数**/ randomNum(min, max) { return Math.floor(Math.random() * (max - min) + min); }, /**生成一个随机色**/ randomColor(min, max) { var r = this.randomNum(min, max); var g = this.randomNum(min, max); var b = this.randomNum(min, max); return "rgb(" + r + "," + g + "," + b + ")"; }, /**绘制验证码图片**/ drawPic() { this.imgCodeStr = ''; var canvas = document.getElementById("canvas"); var width = canvas.width; var height = canvas.height; var ctx = canvas.getContext('2d'); ctx.textBaseline = 'bottom'; /**绘制背景色**/ ctx.fillStyle = this.randomColor(180, 240); //颜色若太深可能导致看不清 ctx.fillRect(0, 0, width, height); /**绘制文字**/ var str = 'ABCEFGHJKLMNPQRSTWXY123456789'; for (var i = 0; i < 4; i++) { var txt = str[this.randomNum(0, str.length)]; ctx.fillStyle = this.randomColor(50, 160); //随机生成字体颜色 ctx.font = this.randomNum(25, 40) + 'px SimHei'; //随机生成字体大小 var x = 5 + i * 25; var y = this.randomNum(30, 45); var deg = this.randomNum(-30, 30); //修改坐标原点和旋转角度 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); this.imgCodeStr += txt; } /**绘制干扰线**/ for (var i = 0; i < 8; i++) { ctx.strokeStyle = this.randomColor(100, 180); ctx.beginPath(); ctx.moveTo(this.randomNum(0, width), this.randomNum(0, height)); ctx.lineTo(this.randomNum(0, width), this.randomNum(0, height)); ctx.stroke(); } // <!-- /**绘制干扰点**/ --> for (var i = 0; i < 100; i++) { ctx.fillStyle = this.randomColor(0, 255); ctx.beginPath(); ctx.arc(this.randomNum(0, width), this.randomNum(0, height), 1, 0, 2 * Math.PI); ctx.fill(); } } } // <!-- 调用方法 --> document.getElementById("changeImg").onclick = function(e) { e.preventDefault(); imgCodeObj.drawPic(); } imgCodeObj.drawPic(); </script> </body> </html>