三种思路
MDN 的案例作参考
https://developer.mozilla.org/zh-CN/docs/Web/API/Canvas_API/Tutorial/Basic_animations
我实现的效果,
1.使用 save restore rotate translate
代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <canvas width="800" height="600" id="canvas"></canvas> </body> <script> var canvas = document.getElementById("canvas"); ctx = canvas.getContext("2d"); ctx.translate(350, 300); function setTime() { //创建图片对象 var img = new Image(); img.src = "back.png"; ctx.drawImage(img, -350, -300); function hour(angle) { ctx.save(); ctx.rotate(Math.PI / 180 * angle); //时针 ctx.beginPath(); ctx.lineWidth = "10"; ctx.strokeStyle = "black"; ctx.moveTo(0, 18); ctx.lineTo(0, -200); ctx.lineCap = "round"; //秃一点 ctx.stroke(); ctx.closePath(); ctx.restore(); } function minite(angle) { ctx.save(); ctx.rotate(Math.PI / 180 * angle); //分针 ctx.beginPath(); ctx.lineWidth = "5"; ctx.strokeStyle = "black"; ctx.moveTo(0, 22); ctx.lineTo(0, -220); ctx.stroke(); ctx.closePath(); ctx.restore(); } function second(angle) { ctx.save(); ctx.rotate(Math.PI / 180 * angle); //秒针 ctx.beginPath(); //试试阴影 ctx.shadowOffsetX = 2; ctx.shadowOffsetY = 5; ctx.shadowColor = "red"; ctx.shadowBlur = 10; /*ctx.lineJoin="round"; //想设置交点,没作用 miterLimit=1;*/ ctx.strokeStyle = "red"; ctx.moveTo(0, 30); ctx.lineTo(0, -240); ctx.stroke(); ctx.closePath(); //中间设置一个圆吧,留着阴影吧 ctx.beginPath(); ctx.arc(0, 0, 7, 0, Math.PI * 2); ctx.fillStyle = "red"; ctx.fill(); ctx.closePath(); ctx.restore(); } img.onload = function () { //调用函数 var now = new Date(); var h; (h = now.getHours()) > 12 ? (h = h - 12) : ""; var s = now.getSeconds(); var m = now.getMinutes(); hour(((h * 60 * 60 + m * 60 + s) / (12 * 60 * 60) - 90) * 360); console.log((h * 60 * 60 + m * 60 + s) / (12 * 60 * 60) * 360); minite((m * 60 + s) / (60 * 60) * 360); second(s / 60 * 360); }; } var t = setInterval(function () { ctx.clearRect(0, 0, canvas.width, canvas.height); setTime(); }, 1000); </script> </html>
2.使用 Math.sin() Math.cos()
<!DOCTYPE html> <html lang="en"> <head> <!--translate() 方法为画布的变换矩阵添加水平的和垂直的偏移。参数 dx 和 dy 添加给后续定义路径中的所有点。--> <meta charset="UTF-8"> <title>Title</title> </head> <body> <canvas width="800" height="600" id="canvas"></canvas> </body> <script> var canvas = document.getElementById("canvas"); ctx = canvas.getContext("2d"); ctx.translate(350, 300); //这句话他妈别写在函数里面 function setTime() { //创建图片对象 var img = new Image(); img.src = "back.png"; ctx.drawImage(img, -350, -300); //加载图片之后 function hour(angle) { //时针 ctx.beginPath(); ctx.lineWidth = "10"; ctx.strokeStyle = "black"; ctx.moveTo(0,0); ctx.lineTo(200*Math.cos(angle*Math.PI/180),200*Math.sin(angle*Math.PI/180)); ctx.lineCap = "round"; //秃一点 ctx.stroke(); ctx.closePath(); ctx.restore(); } function minite(angle) { console.log(angle+"分的"); //分针 ctx.beginPath(); ctx.lineWidth = "5"; ctx.strokeStyle = "black"; ctx.moveTo(0,0); ctx.lineTo(220*Math.cos(angle*Math.PI/180),220*Math.sin(angle*Math.PI/180)); ctx.stroke(); ctx.closePath(); } function second(angle) { //秒针 ctx.beginPath(); /* //试试阴影 但是不知道怎么清除.... ctx.shadowOffsetX = 2; ctx.shadowOffsetY = 5; ctx.shadowColor = "red"; ctx.shadowBlur = 10; /!* ctx.lineJoin="round"; //想设置交点,没作用*!/ miterLimit=1;*/ ctx.strokeStyle = "red"; ctx.moveTo(-18 * (Math.cos(angle * (Math.PI / 180))), -18 * (Math.sin(angle * (Math.PI / 180)))); ctx.lineTo(250*Math.cos(angle*Math.PI/180),250*Math.sin(angle*Math.PI/180)); ctx.stroke(); ctx.closePath(); //中间设置一个圆吧,留着阴影吧 ctx.beginPath(); ctx.arc(0, 0, 7, 0, Math.PI * 2); ctx.fillStyle = "red"; ctx.fill(); ctx.closePath(); } //调用函数 var now = new Date(); var h; (h = now.getHours()) > 12 ? (h = h - 12) : ""; var s = now.getSeconds(); var m = now.getMinutes(); img.onload=function () { console.log((h*60*60+m*60+s)/(12 * 60 * 60)*360); hour((h*60*60+m*60+s)/(12 * 60 * 60)*360-90); minite((m * 60 + s) / (60 * 60) * 360-90); second(s / 60 * 360-90); }; } /* setTime();*/ var t = setInterval(function () { ctx.clearRect(0,0,canvas.width,canvas.height); setTime(); }, 1000); </script> </html>
3.使用 arc 画扇形 然后弧度为0
ctx.arc(350,300,200,angle*Math.PI/180,angle*Math.PI/180,false);
实现代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <canvas width="800" height="600" id="canvas"></canvas> </body> <script> var canvas = document.getElementById("canvas"); ctx = canvas.getContext("2d"); function setTime() { //创建图片对象 var img = new Image(); img.src = "back.png"; ctx.drawImage(img, 0, 0); //加载图片之后 function hour(angle) { //时针 ctx.beginPath(); ctx.lineWidth = "8"; ctx.strokeStyle = "black"; ctx.moveTo(350,300); ctx.arc(350,300,200,angle*Math.PI/180,angle*Math.PI/180,false); ctx.closePath(); /* ctx.lineCap = "round";*/ //秃一点,只能给折线设置 ctx.stroke(); } function minite(angle) { ctx.beginPath(); ctx.lineWidth = "4"; ctx.strokeStyle = "black"; ctx.moveTo(350,300); ctx.arc(350,300,230,angle*Math.PI/180,angle*Math.PI/180,false); ctx.closePath(); ctx.stroke(); } function second(angle) { ctx.beginPath(); /* ctx.shadowOffsetX = 2; ctx.shadowOffsetY = 5; ctx.shadowColor = "red"; ctx.shadowBlur = 10;*/ /*ctx.lineJoin="round"; //想设置交点,没作用 miterLimit=1;*/ ctx.strokeStyle = "red"; ctx.lineWidth = "2"; ctx.moveTo(350,300); ctx.arc(350,300,250,angle*Math.PI/180,angle*Math.PI/180,false); ctx.closePath(); ctx.stroke(); } //调用函数 var now = new Date(); var h; (h = now.getHours()) > 12 ? (h = h - 12) : ""; var s = now.getSeconds(); var m = now.getMinutes(); img.onload=function () { console.log((h*60*60+m*60+s)/(12 * 60 * 60)*360); hour((h*60*60+m*60+s)/(12 * 60 * 60)*360-90); minite((m * 60 + s) / (60 * 60) * 360-90); second(s / 60 * 360-90); }; } setTime(); var t = setInterval(function () { ctx.clearRect(0,0,canvas.width,canvas.height); setTime(); }, 1000); </script> </html>