【Canvas与雷达】铝圈黑底绿环雷达扫描屏

【成图】

120*120的png图标:

大小图:

【代码】

复制代码
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>铝圈绿底雷达 Draft1</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>
        </div>
     </body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/

// canvas的绘图环境
var ctx;

// 高宽
const WIDTH=512;
const HEIGHT=512;

// 舞台对象
var stage;

// 暂停标志
var paused=false;

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

    // 设置点击鼠标暂停
    canvas.onclick=function(e){
        paused=! paused;
    }

    // 初始化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.theta=0;

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

    // 更新
    this.update=function(){  
        if(!paused){
            this.theta+=0.02;
        }
    }

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

    // 画前景
    this.paintFg=function(ctx){
        // 底色
        ctx.save();
        ctx.fillStyle = "white";
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
        ctx.restore();
        
        const R=210;//基准尺寸

        // 第1圈
        ctx.save();
        var r=1.00*R;
        var gnt=ctx.createRadialGradient(0,0,0,0,0,r);
        gnt.addColorStop(0,"rgb(152,152,152)");
        gnt.addColorStop(0.9,"rgb(152,152,152)");
        gnt.addColorStop(0.95,"rgb(251,251,251)");
        gnt.addColorStop(1,"rgb(255,255,255)");
        drawSolidCircle(ctx,0,0,r,gnt);
        ctx.restore();
        
        // 第2圈
        ctx.save();
        var r=0.91*R;
        drawSolidCircle(ctx,0,R/105*2,r,"rgb(154,154,154)");
        ctx.restore();    

        // 第3圈
        ctx.save();
        var r=0.90*R;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,"rgb(255,255,255)");
        gnt.addColorStop(0.25,"rgb(196,188,185)");
        gnt.addColorStop(0.5,"rgb(113,105,103)");
        gnt.addColorStop(0.75,"rgb(44,40,37)");
        gnt.addColorStop(1,"rgb(5,5,5)");
        drawSolidCircle(ctx,0,0,r,gnt);
        ctx.restore();
        
        // 第4圈
        ctx.save();
        var r=0.80*R;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,"rgb(87,83,80)");
        gnt.addColorStop(0.25,"rgb(123,123,123)");
        gnt.addColorStop(0.5,"rgb(206,204,205)");
        gnt.addColorStop(0.75,"rgb(246,246,246)");
        gnt.addColorStop(1,"rgb(255,255,255)");
        drawSolidCircle(ctx,0,0,r,gnt);
        ctx.restore();    

        // 第5圈
        ctx.save();
        var r=0.76*R;
        drawSolidCircle(ctx,0,0,r,"black");
        ctx.restore();

        // 第5圈 雷达屏开始
        ctx.save();
        var r=0.72*R;
        ctx.strokeStyle="green";
        ctx.lineWidth=R/210*2;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.stroke();
        ctx.restore();

        // 短刻度
        ctx.save();
        var r=R*0.68;
        var N=60;
        for(var i=0;i<N;i++){
            var theta=Math.PI*2/N*i-Math.PI/2;            

            var rOut=r/20*20;
            var rIn=r/20*18.5;
            var pt=createPt(rOut*Math.cos(theta),rOut*Math.sin(theta));
            var pt2=createPt(rIn*Math.cos(theta),rIn*Math.sin(theta));

            ctx.lineWidth=R/210*2;
            ctx.strokeStyle="green";
            ctx.beginPath();
            ctx.moveTo(pt.x,pt.y);
            ctx.lineTo(pt2.x,pt2.y);
            ctx.stroke();                
        }
        ctx.restore();

        // 长刻度
        ctx.save();
        var r=R*0.70;
        var N=10;
        for(var i=0;i<N;i++){
            var theta=Math.PI*2/N*i-Math.PI/2;            

            var rOut=r/20*20;
            var rIn=r/20*17.5;
            var pt=createPt(rOut*Math.cos(theta),rOut*Math.sin(theta));
            var pt2=createPt(rIn*Math.cos(theta),rIn*Math.sin(theta));

            ctx.lineWidth=R/210*2.5;
            ctx.strokeStyle="green";
            ctx.beginPath();
            ctx.moveTo(pt.x,pt.y);
            ctx.lineTo(pt2.x,pt2.y);
            ctx.stroke();                
        }
        ctx.restore();    

        // 五个同心圆(纬线)
        ctx.save();
        var r=R*0.58;
        ctx.strokeStyle="green";
        ctx.lineWidth=R/210*1;
        var N=5;
        for(var i=1;i<=N;i++){
            var radius=r/N*i;
            ctx.beginPath();
            ctx.arc(0,0,radius,0,Math.PI*2,false);
            ctx.closePath();
            ctx.stroke();
        }
        ctx.restore();

        // 10瓣(经线)
        ctx.save();
        var r=R*0.58;
        ctx.strokeStyle="green";
        ctx.lineWidth=R/210*1;

        var N=10;
        for(var i=1;i<=N;i++){
            var theta=Math.PI*2/N*i-Math.PI/N;
            var a=createPt(r*Math.cos(theta),r*Math.sin(theta));
            var b=createPt(-a.x,-a.y);

            ctx.beginPath();
            ctx.moveTo(a.x,a.y);
            ctx.lineTo(b.x,b.y);
            ctx.stroke();
        }
        ctx.restore();

        // 扫描扇形
        ctx.save();
        const ANGLE=Math.PI/3*2;
        var r=R*0.72;
        var a=createPt(r*Math.cos(this.theta),r*Math.sin(this.theta));
        var b=createPt(r*Math.cos(this.theta+ANGLE),r*Math.sin(this.theta+ANGLE));
        var c=createPt(a.x*Math.cos(ANGLE),a.y*Math.cos(ANGLE));

        ctx.strokeStyle="rgb(152,255,154)";// 牵引棒
        ctx.lineWidth=R/210*3;
        ctx.beginPath();
        ctx.moveTo(0,0);
        ctx.lineTo(b.x,b.y);
        ctx.stroke();

        var gnt=ctx.createLinearGradient(b.x,b.y,c.x,c.y);// 扇形
        gnt.addColorStop(0,"rgba(8,139,1,0.6)");
        gnt.addColorStop(0.5,"rgba(6,97,1,0.5)");
        gnt.addColorStop(1,"rgba(3,60,1,0.3)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.moveTo(0,0);
        ctx.lineTo(a.x,a.y);
        ctx.arc(0,0,r,this.theta,this.theta+ANGLE,false);
        ctx.closePath();
        ctx.fill();

        ctx.restore();

        // 三个模拟目标点
        ctx.save();
        var r=R*0.58;
        var l=r*0.55;

        var angle=Math.PI*0.33;
        var a=createPt(l*Math.cos(angle),l*Math.sin(angle));                  
        var gnt=ctx.createRadialGradient(a.x,a.y,0,a.x,a.y,r*0.04);
        gnt.addColorStop(0,"rgb(149,246,153)");
        gnt.addColorStop(0.5,"rgb(52,219,53)");
        gnt.addColorStop(1,"rgb(16,176,14)");
        drawSolidCircle(ctx,a.x,a.y,r*0.04,gnt);

        var angle=Math.PI*1.53;
        var a=createPt(l*Math.cos(angle),l*Math.sin(angle));                  
        var gnt=ctx.createRadialGradient(a.x,a.y,0,a.x,a.y,r*0.045);
        gnt.addColorStop(0,"rgb(149,246,153)");
        gnt.addColorStop(0.5,"rgb(52,219,53)");
        gnt.addColorStop(1,"rgb(16,176,14)");
        drawSolidCircle(ctx,a.x,a.y,r*0.045,gnt);

        l=r*0.75;
        angle=Math.PI*0.8;
        var a=createPt(l*Math.cos(angle),l*Math.sin(angle));      
        var gnt=ctx.createRadialGradient(a.x,a.y,0,a.x,a.y,r*0.04);// 辐射渐变
        gnt.addColorStop(0,"rgba(190,199,190,0.8)");
        gnt.addColorStop(0.5,"rgba(190,199,190,0.4");
        gnt.addColorStop(1,"rgba(190,199,190,0.1");
        drawSolidCircle(ctx,a.x,a.y,r*0.04,gnt);

        var l=r*0.65;
        var angle=Math.PI*1.4;
        var a=createPt(l*Math.cos(angle),l*Math.sin(angle));          
        var gnt=ctx.createRadialGradient(a.x,a.y,0,a.x,a.y,r*0.045);// 辐射渐变
        gnt.addColorStop(0,"rgba(190,199,190,0.8)");
        gnt.addColorStop(0.5,"rgba(190,199,190,0.4");
        gnt.addColorStop(1,"rgba(190,199,190,0.1");
        drawSolidCircle(ctx,a.x,a.y,r*0.045,gnt);

        l=r*0.85;
        angle=Math.PI*3.8;
        var a=createPt(l*Math.cos(angle),l*Math.sin(angle));      
        var gnt=ctx.createRadialGradient(a.x,a.y,0,a.x,a.y,r*0.05);// 辐射渐变
        gnt.addColorStop(0,"rgba(190,199,190,0.8)");
        gnt.addColorStop(0.5,"rgba(190,199,190,0.4");
        gnt.addColorStop(1,"rgba(190,199,190,0.1");
        drawSolidCircle(ctx,a.x,a.y,r*0.05,gnt);

        ctx.restore();
                
        writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权
    }
}

/*----------------------------------------------------------
函数:用于绘制矩形(基础函数)
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
width:矩形宽
height:矩形高
----------------------------------------------------------*/
function drawRect(ctx,x,y,width,height){
    ctx.beginPath();
    ctx.moveTo(x-width/2,y-height/2);
    ctx.lineTo(x+width/2,y-height/2);
    ctx.lineTo(x+width/2,y+height/2);
    ctx.lineTo(x-width/2,y+height/2);
    ctx.closePath();
}

/*----------------------------------------------------------
函数:用于绘制实心圆(基础函数)
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
r:圆半径
style:填充圆的方案
----------------------------------------------------------*/
function drawSolidCircle(ctx,x,y,r,style){
    ctx.fillStyle=style;
    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>
复制代码

END

posted @   逆火狂飙  阅读(510)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2014-08-05 【Canvas与诗词】男儿何不带吴钩收取关山五十州
2014-08-05 【Canvas与艺术】绘制灰色橄榄枝环绕“Premium Quality”徽章
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示