html5 canvas实现圆形时钟代码分享

2020-04-24 18:53:31易采站长站整理

p.beginPath();
//画圈圈
p.arc(centreX,centreY,r,0,360,false);
//画线操作
p.stroke();
//关闭作图路径,避免和之后在画板上绘制的内容产生干扰
p.closePath();
//在传入的画板对象上覆盖图层
p.restore();
}
function drowPoint(p,width,color,centreX,centreY,r){
p.save();
//设置画笔宽度
p.lineWidth = width;
//设置画笔颜色
p.fillStyle = color;
//新开启作图路径,避免和之前画板上的内容产生干扰
p.beginPath();
//画圈圈
p.arc(centreX,centreY,r,0,360,false);
//画线操作
p.fill();
//关闭作图路径,避免和之后在画板上绘制的内容产生干扰
p.closePath();
//在传入的画板对象上覆盖图层
p.restore();
}
function drowScales(p){
//画小刻度
for(var i = 0;i < 60;i++){
drowHorizontalLine(p,scaleSmallWidth,scaleSmallLength,scaleColor,195-scaleSmallLength,0,i*6*Math.PI/180,250,250);
}
//画大刻度
for(var i = 0;i < 12;i++){
drowHorizontalLine(p,i%3==0?scaleBigWidth*1.2:scaleBigWidth,i%3==0?scaleBigLength*1.2:scaleBigLength,scaleColor,195-scaleBigLength,0,i*30*Math.PI/180,250,250);
//可以添加数字刻度
}
}
function drowHourPoint(p,hour){
drowHorizontalLine(p,hourPointWidth,hourPointLength,hourPointColor,-10,0,(hour-3)*30*Math.PI/180,250,250);
}
function drowMinPoint(p,min){
drowHorizontalLine(p,minPointWidth,minPointLength,minPointColor,-15,0,(min-15)*6*Math.PI/180,250,250);
}
function drowSecPoint(p,sec){
drowHorizontalLine(p,secPointWidth,secPointLength,secPointColor,-15,0,(sec-15)*6*Math.PI/180,250,250);
}
function drowClock(){
panel.clearRect(0,0,500,500);

panel.fillText(“”,10,20);
panel.fillText(“<a href=”https://www.jb51.net”,10,40″>https://www.jb51.net”,10,40</a>);
var date = new Date();
var sec = date.getSeconds();
var min = date.getMinutes();
var hour = date.getHours() + min/60;
drowCircle(panel,7,’blue’,250,250,200);
drowScales(panel);

drowHourPoint(panel,hour);
drowMinPoint(panel,min);
drowSecPoint(panel,sec);

drowPoint(panel,1,centerColor,250,250,7);
//drowHorizontalLine(panel,10,10,’red’,-5,0,0,250,250);
}
//drowHorizontalLine(panel,7,30,’#F00′,0,0,Math.PI,250,250);
drowClock();
setInterval(drowClock,1000);
function save(){
var image = clock.toDataURL(“image/png”).replace(“image/png”, “image/octet-stream”);
location.href=image;
}
</script>
</body>
</html>