首页 > 其他分享 >利用canvas实现钟表效果

利用canvas实现钟表效果

时间:2023-02-27 15:32:24浏览次数:36  
标签:canvas 效果 moveTo closePath arc 钟表 var beginPath oGc


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>钟表效果</title>
<style>
body{background:black;}
#c1{background:white; border-radius:50%;}
</style>
</head>
<body>
<canvas id="c1" width="400" height="400">
<span>
该浏览器不支持canvas
</span>
</canvas>
<script>
var oC = document.getElementById('c1');
var oGc = oC.getContext('2d');
var x = 200;
var y = 200;
var r = 150;

clock();
setInterval(clock,1000);
function clock(){
oGc.clearRect(0,0,oC.width,oC.height);//清楚画布中的内容
var oDate = new Date();
var oHour =( -90+oDate.getHours()*30+oDate.getMinutes()/2)*Math.PI/180;
var oMin =( -90+oDate.getMinutes()*6)*Math.PI/180;
var oSec = (-90+oDate.getSeconds()*6)*Math.PI/180;//获取弧度数
oGc.beginPath();
for(var i=0; i<60; i++){//画短线
oGc.moveTo(x,y);
oGc.arc(x,y,r,6*i*Math.PI/180,6*(i+1)*Math.PI/180,false);
}
oGc.closePath();
oGc.stroke();
oGc.fillStyle = "white";//用白色填充的圆覆盖大圆的线条
oGc.beginPath();
oGc.moveTo(x,y);
oGc.arc(x,y,r*19/20,0,360,false);
oGc.closePath();
oGc.fill();
oGc.beginPath();
for(var i=0; i<12; i++){//长一点的线
oGc.moveTo(x,y);
oGc.arc(x,y,r,30*i*Math.PI/180,30*(i+1)*Math.PI/180,false);

}
oGc.closePath();
oGc.stroke();
oGc.fillStyle = "white";//用上面同样的方法覆盖
oGc.beginPath();
oGc.moveTo(x,y);
oGc.arc(x,y,r*18/20,0,360,false);
oGc.closePath();
oGc.fill();



oGc.lineWidth = 4;
oGc.beginPath();
oGc.moveTo(x,y);
oGc.arc(x,y,r*8/20,oHour,oHour,false);//时针
oGc.closePath();
oGc.stroke();
oGc.lineWidth = 2;
oGc.beginPath();
oGc.moveTo(x,y);
oGc.arc(x,y,r*12/20,oMin,oMin,false);//分针
oGc.closePath();
oGc.stroke();
oGc.save();//用save()和restore()方法,防止第二次时所有线条都变为红色的
oGc.lineWidth = 1;
oGc.strokeStyle = "red";
oGc.beginPath();
oGc.moveTo(x,y);
oGc.arc(x,y,r*14/20,oSec,oSec,false);//秒针
oGc.closePath();
oGc.stroke();
oGc.restore();
oGc.lineWidth = 1;
oGc.fillStyle = "black";
oGc.beginPath();
oGc.moveTo(x,y);
oGc.arc(x,y,r*2/50,0,360,false);
oGc.closePath();
oGc.fill();

}


</script>
</body>
</html>


标签:canvas,效果,moveTo,closePath,arc,钟表,var,beginPath,oGc
From: https://blog.51cto.com/u_15983333/6088552

相关文章

  • app直播源码,利用原生JS实现回到顶部以及吸顶效果
    app直播源码,利用原生JS实现回到顶部以及吸顶效果  <style>    .box1{      width:1200px;      height:800px;      ......
  • css动画效果
    缓动动画小球下落效果@keyframesbounce{60%,80%,100%{transform:translateY(80px);animation-timing-function:cubic-bezier(.215,.61,.355,......
  • css视觉效果
    单侧投影box-shadow:x方向偏移y方向偏移模糊半径扩张半径颜色内投影/外投影单侧投影box-shadow:05px4px-4pxblack;//扩张半径=-模糊半径邻边投影b......
  • jQuery插件-fullpage全屏幻灯片展示效果
    fullpage全屏插件全屏滚动效果,原生js也很好实现,主要是用mousewheel鼠标滚轮滚动事件,来判断上滚动还是下滚动,之后设置每次滚动的高度为屏幕的高度即可。但是,虽然效果简......
  • vue3使用canvas实现手写签名
    <div><canvas@mousemove="canvasMove"@mouseup="canvasUp"ref="canvas"width="1000"height="500"@mousedown="mousedown"@mouseleave="canvasLeave"></canvas>......
  • Cesium 使用canvas 自定义 label 样式
    https://blog.csdn.net/tutouxiaoyaonie/article/details/127306197项目需求:车辆按照轨迹移动,模型上方要有文本框显示车辆信息,但是cesium自带的label设置样式有限,不美观,所......
  • 用es6的class类单例模式封装canvas环形进度条
    项目中需要一个请求进度效果,尝试了下自己用canvas来绘制一个环形进度条,动效直接用的休眠函数加随机数来模拟。用到了es6里的class类,用单例模式的懒汉模式来实例化对象......
  • vue中页面加载进度条效果的实现
    我们在vue项目中,为了减少首屏加载的时间,通常会开启路由的懒加载。路由懒加载配合gizp确实能帮助我们大大的加快首屏的加载时间。然而,路由懒加载会使得我们在第一次打开一......
  • 直播软件源码,在vue中使用html2canvas在前端生成图片
    直播软件源码,在vue中使用html2canvas在前端生成图片1、安装 npminstallhtml2canvas​2、用法 importhtml2canvasfrom'html2canvas'; html2canvas(document.......
  • spring 定时任务,单次执行时间超过fixedRate时的实际效果
    1.单线程执行时,fixedRate失效,实际执行频率与实际执行时间一致。2.定时任务加上@Async或者定时任务调用加上@Async注解的service方法时,实际执行频率与fixedRate一致。......