【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">
<!--
/*****************************************************************
* 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/

// canvas的绘图环境
var ctx;

// 边长
const LENGTH=540;

// 舞台对象
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.angle=0;
    this.points=[];

    // 初始化
    this.init=function(){
        for(let i=0;i<5;i++){
            let x=Math.random()*220-110;
            let y=Math.random()*220-110;
            let point={};
            point.x=x;
            point.y=y;
            this.points.push(point);
        }

        // 测试点
        /*this.points.push({x:50,y:50});
        this.points.push({x:50,y:-50});
        this.points.push({x:-50,y:50});
        this.points.push({x:-50,y:-50});*/
    }

    // 更新
    this.update=function(){
        // 旋转棒角度累加
        this.angle+=Math.PI/720;

        // 旋转棒角度变换
        if(this.angle>=2*Math.PI){
            this.angle=0;
        }

    }

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

        // 黑底
        ctx.fillStyle="black";
        ctx.fillRect(-LENGTH/2,-LENGTH/2,LENGTH,LENGTH);

        // 四角的四方块
        ctx.fillStyle="cyan";
        ctx.fillRect(-LENGTH/2+4,-LENGTH/2+4,8,8);
        ctx.fillRect(LENGTH/2-12,-LENGTH/2+4,8,8);
        ctx.fillRect(LENGTH/2-12,LENGTH/2-12,8,8);
        ctx.fillRect(-LENGTH/2+4,LENGTH/2-12,8,8);

        // 上边
        for(var i=-250;i<=250;i+=4){
            ctx.beginPath();
            ctx.moveTo(i,-LENGTH/2+4);
            let len=(i%5==0)?12:8;
            ctx.lineTo(i,-LENGTH/2+len);
            ctx.closePath();
            ctx.lineWidth=1;
            ctx.strokeStyle="cyan";
            ctx.stroke();
        }

        // 下边
        for(var i=-250;i<=250;i+=4){
            ctx.beginPath();
            ctx.moveTo(i,LENGTH/2-4);
            let len=(i%5==0)?12:8;
            ctx.lineTo(i,LENGTH/2-len);
            ctx.closePath();
            ctx.lineWidth=1;
            ctx.strokeStyle="cyan";
            ctx.stroke();
        }

        // 左边
        for(var i=-250;i<=250;i+=4){
            ctx.beginPath();
            ctx.moveTo(-LENGTH/2+4,i);
            let len=(i%5==0)?12:8;
            ctx.lineTo(-LENGTH/2+len,i);
            ctx.closePath();
            ctx.lineWidth=1;
            ctx.strokeStyle="cyan";
            ctx.stroke();
        }

        // 右边
        for(var i=-250;i<=250;i+=4){
            ctx.beginPath();
            ctx.moveTo(LENGTH/2-4,i);
            let len=(i%5==0)?12:8;
            ctx.lineTo(LENGTH/2-len,i);
            ctx.closePath();
            ctx.lineWidth=1;
            ctx.strokeStyle="cyan";
            ctx.stroke();
        }

        // 外缘
        ctx.beginPath();
        ctx.arc(0,0,240,0,2*Math.PI,false);
        ctx.closePath();
        ctx.lineWidth=2;
        ctx.strokeStyle="cyan";
        ctx.stroke();

        // 刻度
        for(var i=0;i<360;i++){
            let theta=Math.PI/180*i-Math.PI/2;
            let x1=240*Math.cos(theta);
            let y1=240*Math.sin(theta);

            let x2=0;
            let y2=0;

            if(i%90==0){
                x2=228*Math.cos(theta);
                y2=228*Math.sin(theta);        
            }else if(i%5==0){
                x2=232*Math.cos(theta);
                y2=232*Math.sin(theta);
            }else{
                x2=236*Math.cos(theta);
                y2=236*Math.sin(theta);
            }

            ctx.beginPath();
            ctx.moveTo(x1,y1);
            ctx.lineTo(x2,y2);
            ctx.closePath();
            ctx.lineWidth=1;
            ctx.strokeStyle="cyan";
            ctx.stroke();
        }

        // 文字
        for(var i=0;i<36;i++){
            let theta=Math.PI/18*i-Math.PI/2;
            let x=250*Math.cos(theta);
            let y=250*Math.sin(theta);
            ctx.font="8px Verdana";
            ctx.textAlign="center";
            ctx.textBaseLine="Middle";
            ctx.fillStyle="cyan";            
            ctx.fillText((i*10).toString().padStart(3, '0'),x,y+3);
        }

        // 竖向网格
        for(i=-240;i<270;i+=12){
            let x=i-30;
            let y=Math.sqrt(240*240-x*x);

            ctx.beginPath();
            ctx.moveTo(x,y);
            ctx.lineTo(x,-y);
            ctx.closePath();
            let lwd=(i%5==0)?0.5:0.25;
            ctx.lineWidth=lwd;
            ctx.strokeStyle="cyan";
            ctx.stroke();
        }

        // 横向网格
        for(i=-240;i<270;i+=12){
            let y=i-30;
            let x=Math.sqrt(240*240-y*y);

            ctx.beginPath();
            ctx.moveTo(x,y);
            ctx.lineTo(-x,y);
            ctx.closePath();
            let lwd=(i%5==0)?0.5:0.25;
            ctx.lineWidth=lwd;
            ctx.strokeStyle="cyan";
            ctx.stroke();
        }
    }

    // 画前景
    this.paintFg=function(ctx){    
        
        let x=Math.cos(this.angle)*240;
        let y=Math.sin(this.angle)*240;
 
        // 60°雷达扫描角
        // 渐变的效果是一度画一个小扇形,扇形的逐渐透明化,用rgba中的第四个量alpha值控制
        for(var i=0;i<60;i++){
            var startAngle=this.angle-Math.PI/3/60*i;// 起始角度
            var endAngle=startAngle-Math.PI/3/60;// 中止角度
            var alpha=0.9-0.7/60*i;// 透明度
            ctx.beginPath();
            ctx.moveTo(0, 0);
            ctx.arc(0, 0, 240, startAngle, endAngle,true);
            ctx.closePath();
            ctx.fillStyle = "rgba(186,248,255,"+alpha+")";
            ctx.fill();
        }

        this.points.forEach(function (pt, i) {
            
            let anglePt=Math.atan2(pt.y, pt.x);
            // Math.atan2出来的值需要修正
            anglePt=(anglePt + Math.PI) % (2 * Math.PI) - Math.PI;
            if(pt.y<0){
                anglePt+=Math.PI*2;
            }
            
            let start=stage.angle-Math.PI/3;// 起始角
            let end=stage.angle;// 中止角
            //console.log("Start:"+start+" Curr:"+anglePt+" end:"+end);

            if(anglePt>start && anglePt<end){
                // 在雷达扫描角度内是绿圈
                ctx.beginPath();
                ctx.arc(pt.x, pt.y, 3, 0, 2*Math.PI,true);
                ctx.closePath();
                ctx.fillStyle="yellow";
                ctx.fill();
                
                ctx.beginPath();
                ctx.moveTo(pt.x-8,pt.y-4);
                ctx.lineTo(pt.x-8,pt.y-8);
                ctx.lineTo(pt.x+8,pt.y-8);
                ctx.lineTo(pt.x+8,pt.y-4);
                ctx.lineWidth=1;
                ctx.strokeStyle="yellow";
                ctx.stroke();

                ctx.beginPath();
                ctx.moveTo(pt.x-8,pt.y+4);
                ctx.lineTo(pt.x-8,pt.y+8);
                ctx.lineTo(pt.x+8,pt.y+8);
                ctx.lineTo(pt.x+8,pt.y+4);
                ctx.lineWidth=1;
                ctx.strokeStyle="yellow";
                ctx.stroke();
            }else{
                // 在三坐标雷达扫描角度外是点
                ctx.beginPath();
                ctx.arc(pt.x, pt.y, 2, 0, 2*Math.PI,true);
                ctx.closePath();
                ctx.fillStyle="cyan";
                ctx.fill();
            }
        });        
    }
}

/*--------------------------------------------------------------------------
千军万马过独木桥的全是百姓的儿女,
工厂里加班加点的全是百姓的儿女,
工地上不分寒暑汗如雨下的全是百姓的儿女,
写字楼里996/007没日没夜干的全是百姓的儿女,
战场上牺牲的战士全是百姓的儿女......
不要义愤填膺,这都是事实。
--------------------------------------------------------------------------*/
//-->
</script>

【参考的原图】

END

  • 14
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值