html5API

tech2024-07-20  60

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>clock</title> <style> img{ width: 50px; height: 50px; } </style> <script> window.onload=function(){ //1.获取画布 var canvas=document.getElementById("canvas"); //2.获取画笔(获取上下文对象) var cx=canvas.getContext("2d"); //超时/间歇调用/超时模拟间歇 function clock(){ //3.设置画笔样式 cx.fillStyle='white'; //实心画笔样式 // cx.strokeStyle='blue'; //空心画笔样式 //4.绘制图形 //1.绘制表盘 cx.beginPath(); cx.arc(300,300,200,0,Math.PI*2); cx.closePath(); cx.fill(); //2.绘制时刻度 循环12次 cx.lineWidth=2; cx.strokeStyle='black'; for(var i=0;i<12;i++){ //平移坐标原点-》最好保存 cx.save(); cx.translate(300,300); cx.rotate(i*(Math.PI/6)); //旋转 cx.beginPath(); cx.moveTo(0,-180); cx.lineTo(0,-200); cx.closePath(); cx.stroke(); cx.fillStyle='black'; cx.font='16px blod'; cx.rotate(Math.PI/6); //旋转 cx.fillText(i+1,-6,-220); //文字 cx.restore(); } //3.绘制分刻度 for(var i=0;i<60;i++){ cx.save(); cx.translate(300,300); cx.rotate(i*(Math.PI/30)); //旋转 cx.beginPath(); cx.moveTo(0,-190); cx.lineTo(0,-200); cx.closePath(); cx.stroke(); cx.restore(); } //4.获取当前时间 var today=new Date(); var hour=today.getHours(); var min=today.getMinutes(); var sec=today.getSeconds(); hour=hour+min/60; //分钟转小时 //绘制时针 cx.lineWidth=5; cx.save(); cx.translate(300,300); cx.rotate(hour*(Math.PI/6)); //旋转 cx.beginPath(); cx.moveTo(0,10); cx.lineTo(0,-120); cx.closePath(); cx.stroke(); cx.restore(); //绘制分针 cx.lineWidth=3; cx.save(); cx.translate(300,300); cx.rotate(min*(Math.PI/30)); //旋转 cx.beginPath(); cx.moveTo(0,10); cx.lineTo(0,-160); cx.closePath(); cx.stroke(); cx.restore(); //绘制秒针 cx.lineWidth=1; cx.strokeStyle="red"; cx.save(); cx.translate(300,300); cx.rotate(sec*(Math.PI/30)); //旋转 cx.beginPath(); cx.moveTo(0,10); cx.lineTo(0,-180); cx.closePath(); cx.stroke(); cx.restore(); //绘制交叉处 cx.fillStyle='#ccc'; cx.strokeStyle="red"; cx.save(); cx.translate(300,300); cx.beginPath(); cx.arc(0,0,5,0,Math.PI*2); cx.closePath(); cx.fill(); cx.stroke(); cx.restore(); var clockImg = new Image(); clockImg.src = '1.png'; clockImg.onload = function(){ cx.drawImage(this, 280, 280,50,50) // context.drawImage(this, 0, 0, 1080, 980)改变图片大小到1080*980 cx.save(); cx.translate(300,300); cx.restore(); } setTimeout(clock,1000); } // setInterval(clock,1000); clock(); } </script> </head> <body> <canvas id="canvas" width="600px" height="600px" style="background-color:#ccc"></canvas> <!-- <img src="./1.png" id="img"> --> </body> </html>
最新回复(0)