【Canvas与艺术】简约式胡萝卜配色汽车速度表

【效果图】

【代码】

复制代码
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>胡萝卜色汽车速度仪表盘简化版</title>
     <style type="text/css">
     .centerlize{
        margin:0 auto;
        width:1200px;
     }
     </style>
     </head>

     <body οnlοad="init();">
        <div class="centerlize">
            <canvas id="myCanvas" width="512px" height="512px" style="border:1px dotted black;">
                如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.
            </canvas>
        </div>
     </body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/

// canvas的绘图环境
var ctx;

// 边长
const LENGTH=512;

// 舞台对象
var stage;

//-------------------------------
// 初始化
//-------------------------------
function init(){
    // 获得canvas对象
    var canvas=document.getElementById('myCanvas');  

    // 初始化canvas的绘图环境
    ctx=canvas.getContext('2d');  
    ctx.translate(LENGTH/2,LENGTH/2);// 原点平移到画布中央

    // 准备
    stage=new Stage();    
    stage.init();

    // 开幕
    animate();
}

// 播放动画
function animate(){    
    stage.update();    
    stage.paintBg(ctx);
    stage.paintFg(ctx);     

    // 循环
    if(true){
        window.requestAnimationFrame(animate);   
    }
}

// 舞台类
function Stage(){

    // 初始化
    this.init=function(){

    }

    // 更新
    this.update=function(){

    }

    // 画背景
    this.paintBg=function(ctx){
        ctx.clearRect(-LENGTH/2,-LENGTH/2,LENGTH,LENGTH);// 清屏

        // 灰外圈
        ctx.strokeStyle="rgb(49,49,49)";
        ctx.lineWidth=2;
        ctx.beginPath();
        ctx.arc(0,0,250,0,2*Math.PI,true);
        ctx.closePath();
        ctx.stroke();

        // 黑凸起圈
        ctx.strokeStyle="rgb(12,12,12)";
        ctx.lineWidth=4;
        ctx.beginPath();
        ctx.arc(0,0,246,0,2*Math.PI,true);
        ctx.closePath();
        ctx.stroke();

        // 灰内圈
        ctx.strokeStyle="rgb(102,100,101)";
        ctx.lineWidth=2;
        ctx.beginPath();
        ctx.arc(0,0,242,0,2*Math.PI,true);
        ctx.closePath();
        ctx.stroke();

        // 灰底
        ctx.fillStyle="rgb(53,51,54)";
        ctx.beginPath();
        ctx.arc(0,0,242,0,2*Math.PI,true);
        ctx.closePath();
        ctx.fill();

        // 画刻度
        for(var i=0;i<=120;i++){
            var theta=Math.PI/80*i+Math.PI*3/4;
            var x=230*Math.cos(theta);
            var y=230*Math.sin(theta);

            if((i % 4)==0){
                // 画刻度
                var x1=240*Math.cos(theta);
                var y1=240*Math.sin(theta);
                
                if((i % 20)==0){
                    var x4=190*Math.cos(theta);
                    var y4=190*Math.sin(theta);

                    ctx.lineWidth=6;
                    ctx.strokeStyle="rgb(242,178,55)";
                    ctx.beginPath();
                    ctx.moveTo(x1,y1);
                    ctx.lineTo(x4,y4);
                    ctx.closePath();
                    ctx.stroke();

                     // 写数字
                    var x3=170*Math.cos(theta);
                    var y3=170*Math.sin(theta);
                    ctx.fillStyle="white";
                    ctx.font="30px Bahnschrift Condensed";
                    ctx.textAlign="center";
                    ctx.textBaseLine="Middle";
                    ctx.fillText(i,x3-4,y3+8);

                }else{
                    var x2=200*Math.cos(theta);
                    var y2=200*Math.sin(theta);

                    ctx.lineWidth=2;
                    ctx.strokeStyle="rgb(235,98,17)";
                    ctx.beginPath();
                    ctx.moveTo(x1,y1);
                    ctx.lineTo(x2,y2);
                    ctx.closePath();
                    ctx.stroke();
                }               
            }
        }
    }

    // 画前景
    this.paintFg=function(ctx){    
        
        // 灰底
        ctx.fillStyle="rgb(26,26,26)";
        ctx.beginPath();
        ctx.arc(0,0,20,0,2*Math.PI,true);
        ctx.closePath();
        ctx.fill();
        
        var angle=Math.PI*7/6+Math.random()*Math.PI/120;

        // 
        ctx.save();
        ctx.lineWidth=0.5; 
        ctx.fillStyle = "rgb(227,90,48)";
        ctx.beginPath();
        ctx.rotate(angle);
        ctx.moveTo(200,0);
        ctx.lineTo(-50,0);
        ctx.lineTo(-40,5);
        ctx.lineTo(190,2);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        ctx.save();
        ctx.lineWidth=0.5; 
        ctx.fillStyle = "rgb(243,126,25)";
        ctx.beginPath();
        ctx.rotate(angle);
        ctx.moveTo(200,0);
        ctx.lineTo(-50,0);
        ctx.lineTo(-40,-5);
        ctx.lineTo(190,-2);
        ctx.closePath();
        ctx.fill();
        ctx.restore();
        
        ctx.fillStyle="rgb(39,172,231)";
        ctx.font="30px Bahnschrift Condensed";
        ctx.textAlign="center";
        ctx.textBaseLine="Middle";
        ctx.fillText("33.5 km/h",0,150);
    }
}

/*-------------------------------------
《万家灯火》
他被炒了两周了,一直瞒着老婆在找工作。
一天回到家,他正想把简历藏到床底下,
却未曾想,妻子的乳腺癌诊断书也在。
她叫他吃饭,问老公今天工作一天挺累的吧?
他说就那样,那个...你的诊断结果咋样?
她笑着说放心,没事的良性.
两个人都忍住泪坐着,窗外正是万家灯火...
-------------------------------------*/
//-->
</script>

END

相关推荐
yijunpinggang2 小时前
膜结构汽车棚的造价与哪些因素有关?
汽车
ranjun102315 小时前
全周期服务重塑:汉中路峰冠打破“一锤子卖车”模式
汽车
Dotrust东信创智2 天前
PREEvision S2S方案破解汽车SOA转型难题
汽车
云飞云共享云桌面2 天前
SolidWorks—天津智能装备工厂1台高配主机共享给10个研发用
运维·服务器·自动化·汽车·制造
威联通安全存储3 天前
TS-h1887XU-RP在汽车发动机热试与NVH在线质检网中的部署
python·汽车
总线通信小百科3 天前
汽车零部件LIN总线测试三大痛点
经验分享·自动驾驶·汽车·数据处理
汽车网络安全爱好者3 天前
Public Key Infrastructure(二)— 深入理解 X.509 证书:从 RFC 5280 到 OpenSSL 实践
运维·服务器·算法·网络安全·汽车·密码学·可信计算技术
a1117763 天前
汽车3D配置器 THreeJS 开源项目
前端·3d·html·汽车
ranjun10233 天前
校企协同培育汽车专业人才|破解汉中汽修行业用工缺口
汽车
南山电子nscn3 天前
新洁能为汽车电池管理系统BMS提供从充放电控制到电池均衡的全方位MOSFET解决方案
汽车·mosfet·电池管理系统bms