【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="10px" height="10px" style="border:1px dotted black;">
                如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.
            </canvas>
        </div>
     </body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(ctrl+A)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/

// canvas的绘图环境
var ctx;

// 边长
const LENGTH=512;

// 舞台对象
var stage;

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

    // 初始化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.beginPath();
        ctx.arc(0,0,250,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=2;
        ctx.strokeStyle="rgb(1,48,83)";
        ctx.stroke();

        // 第二层暗黑圈
        ctx.beginPath();
        ctx.arc(0,0,248,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=2;
        ctx.strokeStyle="rgb(1,14,25)";
        ctx.stroke();

        // 第三层暗蓝圈
        ctx.beginPath();
        ctx.arc(0,0,248,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fillStyle="rgb(2,30,65)";
        ctx.fill();

        // 第四层亮蓝圈
        ctx.beginPath();
        ctx.arc(0,0,246,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=2;
        ctx.strokeStyle="rgb(6,94,149)";
        ctx.stroke();

        // 渐变色圈
        var rgnt=ctx.createRadialGradient(0,0,160,0,0,240);         
        rgnt.addColorStop(0,"rgb(9,114,161)");
        rgnt.addColorStop(0.2,"rgba(2,32,67,0.5)");
        ctx.fillStyle=rgnt;
        ctx.beginPath();
        ctx.arc(0,0,240,0,2*Math.PI,true);
        ctx.closePath();
        ctx.fill();

        // 细绿圈
        ctx.beginPath();
        ctx.arc(0,0,160,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=2;
        ctx.strokeStyle="rgb(4,150,141)";
        ctx.stroke();

        // 暗蓝底
        ctx.beginPath();
        ctx.arc(0,0,158,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fillStyle="rgb(1,14,44)";
        ctx.fill();

        // 粗绿圈
        ctx.beginPath();
        ctx.arc(0,0,150,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=6;
        ctx.strokeStyle="rgb(4,150,141)";
        ctx.stroke();

        // 暗蓝断续三圈
        ctx.beginPath();
        ctx.arc(0,0,120,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=2;
        ctx.strokeStyle="rgb(87,105,204)";
        ctx.stroke();

        for(var i=0;i<3;i++){
            var alpha=Math.PI/6+i*Math.PI*2/3;

            const r=120;
            var x=r*Math.cos(alpha);
            var y=r*Math.sin(alpha);

            ctx.save();

            ctx.translate(x,y);
            ctx.rotate(alpha);            
            ctx.beginPath();
            ctx.fillStyle="rgb(1,14,44)";
            ctx.fillRect(-4,-10,8,20);
            ctx.closePath();

            ctx.beginPath();
            ctx.moveTo(-12,0);
            ctx.lineTo(6,6);
            ctx.lineTo(6,-6);
            ctx.closePath();
            ctx.fillStyle="white";
            ctx.fill();

            ctx.restore();
        }

        // 写速度
        ctx.fillStyle="white";
        ctx.font="90px Bahnschrift SemiBold Condensed";
        ctx.textAlign="center";
        ctx.textBaseLine="Middle";
        ctx.fillText("70",0,30);

        // 写速度单位
        ctx.fillStyle="white";
        ctx.font="30px Bahnschrift Condensed";
        ctx.textAlign="center";
        ctx.textBaseLine="Middle";
        ctx.fillText("km/h",0,80);
    }

    // 画前景
    this.paintFg=function(ctx){    
        var angle=Math.PI*3/2+Math.random()*Math.PI/100+Math.PI/8;

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

            // 刻度
            ctx.save();
            ctx.translate(x,y);
            ctx.rotate(theta);
            ctx.beginPath();
            ctx.moveTo(0,0-3);
            ctx.lineTo(0-6,0-3);
            ctx.lineTo(0-6,0+3);
            ctx.lineTo(0,0+3);
            ctx.closePath();
            if(theta<angle){
                ctx.fillStyle="rgb(254,98,54)";
                ctx.fill();
            }else{
                ctx.fillStyle="rgb(187,234,250)";
                ctx.fill();
            }
            ctx.restore();

            // 文字
            if(i%20==0){
                var x3=210*Math.cos(theta);
                var y3=210*Math.sin(theta);
                ctx.fillStyle="white";
                ctx.font="30px Microsoft YaHei UI";
                ctx.textAlign="center";
                ctx.textBaseLine="Middle";
                ctx.fillText(i/10,x3,y3+8);
            }
        }

        // 画指针
        var x1=236*Math.cos(angle);
        var y1=236*Math.sin(angle);
        ctx.save();        
        ctx.translate(x1,y1);
        ctx.rotate(angle);
        // 指针上片
        ctx.beginPath();
        ctx.moveTo(0,0);
        ctx.lineTo(-18,4);
        ctx.lineTo(-83,4);
        ctx.lineTo(-83,0);        
        ctx.closePath();        
        ctx.fillStyle="red";
        ctx.fill();
        // 指针下片
        ctx.beginPath();
        ctx.moveTo(0,0);
        ctx.lineTo(-18,-4);
        ctx.lineTo(-83,-4);
        ctx.lineTo(-83,0);        
        ctx.closePath();        
        ctx.fillStyle="rgb(245,0,14)";
        ctx.fill();

        ctx.restore();
    }
}

/*-------------------------------------
《面试暗语》

工资6-10K--就是6K,可能再稍往上加加,但别想多了
抗压能力强--压力很大,准备爬屎山,各种坑要填,各种锅要背
要踏实肯干--工作很累,任务很杂,从编码测试美工到墩地打扫厕所都有,要少点心思,老老实实干活。
回家等通知--大概率没戏了
两周通知你--你非首选
有消息我第一时间通知你,领导在出差还没答复-去面别家吧
我们这边没有那么快,你可以先去看其它机会--别等了,直接去别家
什么时候来上班--大概率稳了,但也有放鸽子的。
-------------------------------------*/
//-->
</script>

【原型】

END

相关推荐
DarkBule_14 小时前
0成本get可信域名:dpdns.org公益域名获取全攻略
css·学习·html·github·html5
合作小小程序员小小店16 小时前
web网页开发,旧版在线%考试,判题%系统demo,基于python+flask+随机分配考试题目,基于开发语言python,数据库mysql
开发语言·后端·python·mysql·flask·html5
深圳南柯电子17 小时前
纯电汽车EMC整改:预防性设计节省47%预算|深圳南柯电子
网络·人工智能·汽车·互联网·实验室·emc
SelectDB技术团队19 小时前
货拉拉用户画像基于 Apache Doris 的数据模型设计与实践
数据分析·汽车·apache·用户画像·货拉拉
.生产的驴20 小时前
React 页面路由ReactRouter 路由跳转 参数传递 路由配置 嵌套路由
前端·javascript·react.js·前端框架·json·ecmascript·html5
永霖光电_UVLED21 小时前
Lumileds推出新的汽车前灯封装
汽车
Black蜡笔小新1 天前
赋能智慧货运:视频汇聚平台EasyCVR打造货运汽车安全互联网视频监控与管理方案
网络·汽车·音视频
易晨 微盛·企微管家1 天前
汽车行业SCRM:企业微信+服务商模式破解汽车服务行业痛点的案例分析
大数据·人工智能·汽车·产品运营·企业微信
紧固件研究社1 天前
汽车紧固技术加速进化,推动汽车产业迈向高质量制造新阶段
汽车·制造·紧固件
AcrelGHP1 天前
某汽车公司4S店携手Acrel-5000建筑能耗管理系统,实现连锁门店能源精细化管理新突破
汽车·能源