当前位置: 首页 > news >正文

【Canvas与钟表】干支表盘

【成图】

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head><title>387.干支表盘</title><style type="text/css">.centerlize{margin:0 auto;width:1200px;}</style></head><body οnlοad="init();"><div class="centerlize"><canvas id="myCanvas" width="12px" height="12px" style="border:1px dotted black;">如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.</canvas><img id="myImg" src="387.jpg" style="display:none;"/></div></body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/// canvas的绘图环境
var ctx;// 高宽
const WIDTH=512;
const HEIGHT=512;// 舞台对象
var stage;//-------------------------------
// 初始化
//-------------------------------
function init(){// 获得canvas对象var canvas=document.getElementById('myCanvas');  canvas.width=WIDTH;canvas.height=HEIGHT;// 初始化canvas的绘图环境ctx=canvas.getContext('2d');  ctx.translate(WIDTH/2,HEIGHT/2);// 原点平移// 准备stage=new Stage();    stage.init();// 开幕animate();
}// 播放动画
function animate(){    stage.update();    stage.paintBg(ctx);stage.paintFg(ctx);     // 循环if(true){//sleep(100);window.requestAnimationFrame(animate);   }
}// 舞台类
function Stage(){// 初始化this.init=function(){}// 更新this.update=function(){    }// 画背景this.paintBg=function(ctx){ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏    }// 画前景this.paintFg=function(ctx){// 底色ctx.fillStyle = "white";ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);        // 表盘半径,基准尺寸const R=220;// 外凸圈var r=R+12;ctx.save();ctx.beginPath();ctx.arc(0,0,r,0,Math.PI*2,false);ctx.closePath();var lgrd=ctx.createLinearGradient(0,-r,0,r);lgrd.addColorStop(0,"rgb(209,214,217)");lgrd.addColorStop(1,"rgb(83,100,108)");        ctx.fillStyle=lgrd;ctx.fill();// 中圈ctx.save();ctx.rotate(Math.PI*5/4);r=R+10;ctx.beginPath();ctx.arc(0,0,r,0,Math.PI*2,false);ctx.closePath();ctx.clip();var img=document.getElementById("myImg");ctx.drawImage(img,0,0,300,300,-r,-r,2*r,2*r);ctx.restore();// 内凹圈r=R+2;ctx.beginPath();ctx.arc(0,0,r,0,Math.PI*2,false);ctx.closePath();var lgrd=ctx.createLinearGradient(0,-r,0,r);lgrd.addColorStop(0,"rgb(83,100,108)");    lgrd.addColorStop(1,"rgb(209,214,217)");        ctx.fillStyle=lgrd;ctx.fill();// 表盘开始ctx.fillStyle="rgb(76,97,120)";r=R;ctx.beginPath();ctx.arc(0,0,r,0,Math.PI*2,false);ctx.closePath();        ctx.fill();// 刻度r=R/20*19;ctx.strokeStyle="lightgrey";ctx.beginPath();ctx.arc(0,0,r,0,Math.PI*2,false);ctx.closePath();ctx.stroke();for(var i=0;i<60;i++){var theta=i*Math.PI/30;var pt=createPt(r*Math.cos(theta),r*Math.sin(theta));var rad=r/19*19.5;var pt2=createPt(rad*Math.cos(theta),rad*Math.sin(theta));//drawSolidCircle(ctx,pt.x,pt.y,(i%5==0)?3:1,"black");if(i%5==0){// 画三角形ctx.save();ctx.translate(pt2.x,pt2.y);ctx.rotate(theta+Math.PI/2*3);drawSolidRegTri(ctx,0,0,4,"lightgrey");ctx.restore();}else{// 画刻度                ctx.strokeStyle="lightgrey";ctx.beginPath();ctx.moveTo(pt.x,pt.y);ctx.lineTo(pt2.x,pt2.y);ctx.stroke();}}// 数字底圈r=R/20*18.8;ctx.fillStyle="rgb(177,211,184)";ctx.beginPath();ctx.arc(0,0,r,0,Math.PI*2,false);ctx.closePath();        ctx.fill();// 数字r=R/20*17;var hours=["Ⅲ","Ⅳ","Ⅴ","Ⅵ","Ⅶ","Ⅷ","Ⅸ","Ⅹ","Ⅺ","Ⅻ","Ⅰ","Ⅱ"]; for(var i=0;i<12;i++){var theta=i*Math.PI/6;var pt=createPt(r*Math.cos(theta),r*Math.sin(theta));ctx.save();ctx.translate(pt.x,pt.y);ctx.rotate(theta+Math.PI/2);ctx.font="16px Microsoft YaHei UI";ctx.textAlign="center";ctx.textBaseLine="Middle";ctx.fillStyle="rgb(70,95,72)";ctx.fillText(hours[i],0,0);ctx.restore();}// 月份底圈r=R/20*16.5;ctx.fillStyle="rgb(76,97,120)";ctx.beginPath();ctx.arc(0,0,r,0,Math.PI*2,false);ctx.closePath();        ctx.fill();// 社会主义核心价值观24字r=R/20*14.8;var hours=["富强","民主","文明","和谐","自由","平等","公正","法治","爱国","敬业","诚信","友善",]; for(var i=0;i<12;i++){var theta=i*Math.PI/6-Math.PI/2;var pt=createPt(r*Math.cos(theta),r*Math.sin(theta));ctx.save();ctx.translate(pt.x,pt.y);ctx.rotate(theta+Math.PI/2);ctx.font="16px 仿宋";ctx.textAlign="center";ctx.textBaseLine="Middle";ctx.fillStyle="lightgrey";ctx.fillText(hours[i],0,0);ctx.restore();}// 地支底圈r=R/20*14.2;ctx.fillStyle="rgb(177,211,184)";ctx.beginPath();ctx.arc(0,0,r,0,Math.PI*2,false);ctx.closePath();        ctx.fill();// 地支文字r=R/20*12.5;var hours=["午","未","申","酉","戌","亥","子","丑","寅","卯","辰","巳",]; for(var i=0;i<12;i++){var theta=i*Math.PI/6-Math.PI/2;var pt=createPt(r*Math.cos(theta),r*Math.sin(theta));ctx.save();ctx.translate(pt.x,pt.y);ctx.rotate(theta+Math.PI/2);ctx.font="18px Microsoft YaHei UI";ctx.textAlign="center";ctx.textBaseLine="Middle";ctx.fillStyle="rgb(70,95,72";ctx.fillText(hours[i],0,0);ctx.restore();}// 分度辐射r=R/20*16.5;for(var i=0;i<12;i++){var theta=Math.PI*2/12*i+Math.PI/12;var pt=createPt(r*Math.cos(theta),r*Math.sin(theta));// 画刻度                ctx.strokeStyle="grey";ctx.beginPath();ctx.moveTo(0,0);ctx.lineTo(pt.x,pt.y);ctx.stroke();}// 渐变圈r=R/20*11.9;var gnt2=ctx.createRadialGradient(0,0,0,0,0,r);gnt2.addColorStop(0,"rgb(76,97,120)");gnt2.addColorStop(0.9,"rgb(76,97,120)");gnt2.addColorStop(1,"rgb(47,59,71)");ctx.fillStyle=gnt2;ctx.beginPath();ctx.arc(0,0,r,0,Math.PI*2,false);ctx.closePath();        ctx.fill();// 中间的螺旋drawSpiral(ctx,0,0,R/1.8);// 得到当前时间var now=new Date();var s=now.getSeconds();var m=now.getMinutes();var h=now.getHours()+m/60;// 画表针drawHourPointer(ctx,h,R);drawMinutePointer(ctx,m,R);drawSecondPointer(ctx,s,R);// 画中心小圆点ctx.beginPath();ctx.arc(0,0,6,0,Math.PI*2,true);ctx.closePath();ctx.fillStyle="rgb(177,211,184)";ctx.fill();ctx.beginPath();ctx.arc(0,0,2,0,Math.PI*2,true);ctx.closePath();ctx.fillStyle="rgb(76,97,120)";ctx.fill();writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火原创","8px consolas","lightgrey");// 版权}
}// 画中间的螺旋
function drawSpiral(ctx,x,y,radius){ctx.save();ctx.translate(x,y);var r=radius;// 初始半径,多边形顶点到屏幕中心的距离var gnt1=ctx.createRadialGradient(0,0,0,0,0,r); // 渐变色gnt1.addColorStop(0,"rgb(177,211,184)");gnt1.addColorStop(1,"rgb(76,97,120)");        const DELTA=Math.PI/40;// 每次转动的角度const N=5;// 边数const ANGLE=(N-2)*Math.PI/2/N;const RATIO=Math.sin(ANGLE)/Math.sin(Math.PI-ANGLE-DELTA);// 根据正弦定理计算半径缩小比例    const TIMES=30;// 往里画的次数ctx.lineWidth=1;        ctx.strokeStyle=gnt1;ctx.beginPath();for(var i=0;i<TIMES;i++){            r=r*RATIO;for(var j=0;j<=N;j++){var theta=-DELTA*i+ANGLE+Math.PI*2/N*j;var pt=createPt(r*Math.cos(theta),r*Math.sin(theta));ctx.lineTo(pt.x,pt.y);}}ctx.stroke();ctx.restore();
}// 画时针
function drawHourPointer(ctx,h,radius){const R=radius;ctx.save();ctx.rotate(h*Math.PI/6-Math.PI/2);ctx.beginPath();ctx.moveTo(0,0);ctx.lineTo(-R/11,0);ctx.lineTo(-R/11,R/55);ctx.lineTo(R-100,R/55);ctx.lineTo(R-90,0);ctx.closePath();ctx.fillStyle="rgb(171,172,166)";ctx.fill();ctx.fillStyle="rgb(171,172,166)";ctx.beginPath();ctx.arc(0,0,R/25,0,Math.PI,false);ctx.closePath();        ctx.fill();ctx.beginPath();ctx.moveTo(0,0);ctx.lineTo(-R/11,0);ctx.lineTo(-R/11,-R/55);ctx.lineTo(R-100,-R/55);ctx.lineTo(R-90,0);ctx.closePath();ctx.fillStyle="rgb(252,252,250)";ctx.fill();ctx.fillStyle="rgb(252,252,250)";ctx.beginPath();ctx.arc(0,0,R/25,Math.PI,Math.PI*2,false);ctx.closePath();        ctx.fill();ctx.restore();
}// 画分针
function drawMinutePointer(ctx,m,radius){const R=radius;ctx.save();ctx.rotate(m*Math.PI/30-Math.PI/2);ctx.beginPath();ctx.moveTo(0,0);ctx.lineTo(-R/10,0);ctx.lineTo(-R/10,R/55);ctx.lineTo(R-47,R/55);ctx.lineTo(R-37,0);ctx.closePath();ctx.fillStyle="rgb(171,172,166)";ctx.fill();ctx.fillStyle="rgb(171,172,166)";ctx.beginPath();ctx.arc(0,0,R/27,0,Math.PI,false);ctx.closePath();        ctx.fill();ctx.beginPath();ctx.moveTo(0,0);ctx.lineTo(-R/10,0);ctx.lineTo(-R/10,-R/55);ctx.lineTo(R-47,-R/55);ctx.lineTo(R-37,0);ctx.closePath();ctx.fillStyle="rgb(252,252,250)";ctx.fill();ctx.fillStyle="rgb(252,252,250)";ctx.beginPath();ctx.arc(0,0,R/27,Math.PI,Math.PI*2,false);ctx.closePath();        ctx.fill();ctx.restore();
}// 画秒针
function drawSecondPointer(ctx,s,radius){const R=radius;ctx.save();ctx.rotate(s*Math.PI/30-Math.PI/2);ctx.beginPath();ctx.moveTo(0,0);ctx.lineTo(-R/9,0);ctx.lineTo(-R/9,R/110);ctx.lineTo(R-40,R/110);ctx.lineTo(R-40,R/110*3);ctx.lineTo(R-10,0);ctx.closePath();ctx.fillStyle="rgb(171,172,166)";ctx.fill();ctx.beginPath();ctx.moveTo(0,0);ctx.lineTo(-R/9,0);ctx.lineTo(-R/9,-R/110);ctx.lineTo(R-40,-R/110);ctx.lineTo(R-40,-R/110*3);ctx.lineTo(R-10,0);ctx.closePath();ctx.fillStyle="rgb(252,252,250)";ctx.fill();ctx.restore();
}/*----------------------------------------------------------
函数:绘制实心三角形
ctx:绘图上下文
x:三角形中心横坐标
y:三角形中心纵坐标
r:三角形中心到顶点的长度
color:填充色
----------------------------------------------------------*/
function drawSolidRegTri(ctx,x,y,r,color){ctx.fillStyle=color;// 画三角形var arr=createRegTriArr(x,y,r);ctx.beginPath();for(var j=0;j<arr.length;j++){ctx.lineTo(arr[j].x,arr[j].y);}ctx.closePath();ctx.fill();
}/*----------------------------------------------------------
函数:创建一个以x,y为中心,r为半径的正三角形数组
ctx:绘图上下文
x:三角形中心横坐标
y:三角形中心纵坐标
r:三角形中心到顶点的长度
arr[0]为右下,arr[1]为左下,arr[2]为正上。
----------------------------------------------------------*/
function createRegTriArr(x,y,r){var arr=new Array();for(var i=0;i<3;i++){var theta=Math.PI*2/3*i+Math.PI/6;var pt=createPt(r*Math.cos(theta)+x,r*Math.sin(theta)+y);arr.push(pt);}return arr;
}/*----------------------------------------------------------
函数:由坐标得到弧度
x:点横坐标
y:点纵坐标
----------------------------------------------------------*/
function getRad(x,y){var r=Math.sqrt(x*x+y*y);var theta=Math.asin(Math.abs(y)/r);if(x>=0 && y>=0){return theta;}else if(x<0 && y>=0){return Math.PI-theta;}else if(x<0 && y<0){return Math.PI+theta;}else if(x>=0 && y<0){return -theta;}return null;
}/*----------------------------------------------------------
函数:用于绘制实心圆,用途是标记点以辅助作图
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
r:圆半径
color:填充圆的颜色
----------------------------------------------------------*/
function drawSolidCircle(ctx,x,y,r,color){ctx.fillStyle=color;ctx.beginPath();ctx.arc(x,y,r,0,Math.PI*2,false);ctx.closePath();ctx.fill();
}/*----------------------------------------------------------
函数:创建一个二维坐标点
x:横坐标
y:纵坐标
Pt即Point
----------------------------------------------------------*/
function createPt(x,y){var retval={};retval.x=x;retval.y=y;return retval;
}/*----------------------------------------------------------
函数:延时若干毫秒
milliseconds:毫秒数
----------------------------------------------------------*/
function sleep(milliSeconds) {const date = Date.now();let currDate = null;while (currDate - date < milliSeconds) {currDate = Date.now();} 
}/*----------------------------------------------------------
函数:书写文字
ctx:绘图上下文
x:横坐标
y:纵坐标
text:文字
font:字体
color:颜色
----------------------------------------------------------*/
function writeText(ctx,x,y,text,font,color){ctx.save();ctx.textBaseline="bottom";ctx.textAlign="center";ctx.font = font;ctx.fillStyle=color;ctx.fillText(text,x,y);ctx.restore();
}/*-------------------------------------------------------------
有些人,活了一辈子,
其实不过是认真过了一天,其余时间都在重复这一天而已,
也有人每天不重样,看似折腾,却活出了滋味。
--------------------------------------------------------------*/
//-->
</script>

【底图】

387.jpg

END

http://www.lryc.cn/news/432287.html

相关文章:

  • 分布式项目中使用雪花算法提前获取对象主键ID
  • 小程序多个set-cookie无法处理
  • Mybatis【分页插件,缓存,一级缓存,二级缓存,常见缓存面试题】
  • 【Qt开发】QT6.5.3安装方法(使用国内源)亲测可行!!!
  • springblade-JWT认证缺陷漏洞CVE-2021-44910
  • Chapter 12 Vue CLI脚手架组件化开发
  • Ubuntu: 配置OpenCV环境
  • 芯片解决方案--SL8541e-OpenHarmony适配方案
  • Spring Boot之数据访问集成入门
  • Learn ComputeShader 09 Night version lenses
  • Java学习第七天
  • 深入剖析 Redis 基础及其在 Java 应用中的实战演练
  • Why I‘m getting 404 Resource Not Found to my newly Azure OpenAI deployment?
  • 【word导出带图片】使用docxtemplater导出word,通知书形式的word
  • 微信小程序路由跳转之间的区别
  • centos安装docker并配置加速器
  • 【软件测试】设计测试用例
  • Kafka【十三】消费者消费消息的偏移量
  • Python 的语法元素(容易忘记的)
  • 找到字符串中所有字母异位词问题
  • QEMU用户模式测试AARCH64程序
  • 机器学习(五) -- 监督学习(8) --神经网络2
  • 物联网之PWM呼吸灯、脉冲、LEDC
  • Python利用pyecharts实现数据可视化
  • 网恋照妖镜源码搭建教程
  • STM32
  • 用手机做抢答器 低预算知识竞赛活动的选择
  • ELK学习笔记(二)——使用K8S部署Kibana8.15.0
  • 报错:CPU指令集的问题
  • Type-C接口诱骗取电快充方案