【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

相关推荐
在所不辞兄1 天前
为什么在算力受限的车载芯片上部署独立 Neck 架构时,需要专门优化显存占用,而不能直接沿用训练阶段的显存策略?
人工智能·机器学习·自动驾驶·汽车·能源
zhanlang02177 天前
HTML 零基础入门到企业实战【第八章 8.6】HTML5 本地存储、拖拽 API、视频音频新特性
html·音视频·html5
zhanlang02178 天前
HTML 零基础入门到企业实战全套教程【连载 8.4】HTML5 新增表单属性与表单控件
前端·html·html5
zhanlang02179 天前
HTML 零基础入门到企业实战全套教程【第八章 8.3】HTML5 语义化标签|SEO 优化、页面布局实战应用
前端·html·html5
YH行业报告分析10 天前
2026信号校准器市场洞察:热电偶与RTD校准器如何支撑汽车、航空现场计量?
汽车
manduic11 天前
汽车座舱Buck选型实战:AWK6613 对比 SCT2432,参数逐项对齐(附选型清单)
网络·汽车
矩子01X11 天前
11 月 1 日已实施:4 项汽车新标准要点清单,台架定制、环境搭建与测试软件找谁做
自动化测试·汽车·汽车测试·硬件在环测试
屿华AI13 天前
AUTOSAR 实战-诊断工具 | UDS 0x27 安全访问的配置化实现:种子密钥算法插件化设计
python·测试工具·汽车
屿华AI13 天前
AUTOSAR 实战-诊断工具 | DTC/Event/FiM 故障报告自动化:DEM 诊断数据采集与解析
python·测试工具·自动化·汽车
稳联技术老娜16 天前
汽车轮罩产线 PROFINET 转 DeviceNet 通信落地方案
汽车