【成图】

【Canvas与钟表】"社会主义核心价值观"表盘_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 onload="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

【Canvas与钟表】"社会主义核心价值观"表盘_Canvas 表盘 钟表_02

END