【Canvas与标志】黄色背景竖直条纹辐射标志

【成图】

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>黄色背景竖直条纹辐射标志 Draft2</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;

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

    // 初始化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.init=function(){
        
    }

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

    // 画背景
    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;//基准尺寸

        // 背景
        ctx.save();
        r=R*1.00;
        var w=2*r;
        var h=w;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,"rgb(255,191,38)");
        gnt.addColorStop(0.9,"rgb(253,252,1)");
        gnt.addColorStop(1,"rgb(255,255,1)");
        ctx.fillStyle=gnt;
        drawRoundRect(ctx,0,0,w,h,R/10);
        ctx.fill();
        ctx.restore();

        // 细小三角形
        ctx.save();
        drawRoundRect(ctx,0,0,w,h,R/10);
        ctx.clip();

        var arr=[];
        arr.push({"x":-w/2*0.94,"h":h*0.96,"w":w*0.01,"color":"rgb(255,234,77)"});
        arr.push({"x":-w/2*0.87,"h":h*0.98,"w":w*0.02,"color":"rgb(255,244,102)"});
        arr.push({"x":-w/2*0.81,"h":h*0.93,"w":w*0.015,"color":"rgb(251,227,92)"});
        arr.push({"x":-w/2*0.74,"h":h*0.91,"w":w*0.022,"color":"rgb(259,227,92)"});
        arr.push({"x":-w/2*0.72,"h":h*0.92,"w":w*0.013,"color":"rgb(258,227,92)"});
        arr.push({"x":-w/2*0.65,"h":h*0.79,"w":w*0.014,"color":"rgb(253,233,165)"});
        arr.push({"x":-w/2*0.61,"h":h*0.85,"w":w*0.018,"color":"rgb(255,225,92)"});
        arr.push({"x":-w/2*0.59,"h":h*0.99,"w":w*0.021,"color":"rgb(252,235,192)"});
        arr.push({"x":-w/2*0.52,"h":h*0.91,"w":w*0.009,"color":"rgb(255,237,92)"});
        arr.push({"x":-w/2*0.43,"h":h*0.79,"w":w*0.016,"color":"rgb(255,227,91)"});
        arr.push({"x":-w/2*0.35,"h":h*0.85,"w":w*0.007,"color":"rgb(255,227,99)"});
        arr.push({"x":-w/2*0.27,"h":h*0.81,"w":w*0.003,"color":"rgb(255,237,164)"});
        arr.push({"x":-w/2*0.22,"h":h*0.93,"w":w*0.013,"color":"rgb(255,242,177)"});
        arr.push({"x":-w/2*0.13,"h":h*0.98,"w":w*0.022,"color":"rgb(251,227,92)"});
        arr.push({"x":-w/2*0.08,"h":h*0.86,"w":w*0.008,"color":"rgb(255,221,92)"});
        arr.push({"x":-w/2*0.04,"h":h*0.94,"w":w*0.001,"color":"rgb(255,227,91)"});
        arr.push({"x":0,"h":h,"w":w*0.02,"color":"rgb(255,235,182)"});
        arr.push({"x":w/2*0.94,"h":h*0.93,"w":w*0.016,"color":"rgb(255,234,77)"});
        arr.push({"x":w/2*0.89,"h":h*0.98,"w":w*0.021,"color":"rgb(255,247,232)"});
        arr.push({"x":w/2*0.81,"h":h*0.91,"w":w*0.015,"color":"rgb(251,227,93)"});
        arr.push({"x":w/2*0.72,"h":h*0.91,"w":w*0.022,"color":"rgb(259,237,92)"});
        arr.push({"x":w/2*0.72,"h":h*0.92,"w":w*0.023,"color":"rgb(258,227,192)"});
        arr.push({"x":w/2*0.65,"h":h*0.99,"w":w*0.014,"color":"rgb(255,235,131)"});
        arr.push({"x":w/2*0.61,"h":h*0.89,"w":w*0.008,"color":"rgb(245,235,92)"});
        arr.push({"x":w/2*0.59,"h":h*0.99,"w":w*0.021,"color":"rgb(255,228,195)"});
        arr.push({"x":w/2*0.51,"h":h*0.91,"w":w*0.019,"color":"rgb(255,237,92)"});
        arr.push({"x":w/2*0.53,"h":h*0.79,"w":w*0.016,"color":"rgb(245,227,91)"});
        arr.push({"x":w/2*0.35,"h":h*0.85,"w":w*0.017,"color":"rgb(253,248,189)"});
        arr.push({"x":w/2*0.27,"h":h*0.81,"w":w*0.023,"color":"rgb(255,248,131)"});
        arr.push({"x":w/2*0.29,"h":h*0.99,"w":w*0.013,"color":"rgb(253,227,92)"});
        arr.push({"x":w/2*0.13,"h":h*0.92,"w":w*0.012,"color":"rgb(255,244,227)"});
        arr.push({"x":w/2*0.08,"h":h*0.96,"w":w*0.018,"color":"rgb(235,221,92)"});
        arr.push({"x":w/2*0.06,"h":h*0.98,"w":w*0.011,"color":"rgb(255,227,91)"});

        for(var i=0;i<arr.length;i++){
            var tri=arr[i];

            var a=createPt(tri.x-tri.w/2,-h/2);
            var b=createPt(tri.x+tri.w/2,-h/2);
            var c=createPt(tri.x+tri.w/5,-h/2+tri.h);
            var d=createPt(tri.x-tri.w/5,-h/2+tri.h);

            var gnt=ctx.createLinearGradient(0,a.y,0,c.y);
            gnt.addColorStop(0,tri.color);
            gnt.addColorStop(1,"rgb(255,255,1)");
            ctx.fillStyle=gnt;
            ctx.beginPath();
            ctx.moveTo(a.x,a.y);
            ctx.lineTo(b.x,b.y);
            ctx.lineTo(c.x,c.y);
            ctx.lineTo(d.x,d.y);
            ctx.closePath();
            ctx.fill();
        }
        ctx.restore();

        // 外环
        ctx.save();
        var rOut=R*0.75;
        var rIn=R*0.70;
        ctx.fillStyle="black";
        drawFan(ctx,0,0,rIn,rOut,0,Math.PI*2);
        ctx.fill();
        ctx.restore();

        // 内环
        ctx.save();
        var rOut=R*0.60;
        var rIn=R*0.58;
        ctx.fillStyle="black";
        drawFan(ctx,0,0,rIn,rOut,0,Math.PI*2);
        ctx.fill();
        ctx.restore();
        
        // 辐射三块
        ctx.save();
        var rOut=R*0.55;
        var rIn=R*0.20;
        var N=3;

        for(var i=0;i<N;i++){
            var theta=Math.PI*2/N*i+Math.PI/2;

            var angle=theta+Math.PI/6;
            var r=rIn;
            var a=createPt(r*Math.cos(angle),r*Math.sin(angle));

            angle=theta+Math.PI/6;
            r=rOut;
            var b=createPt(r*Math.cos(angle),r*Math.sin(angle));

            angle=theta-Math.PI/6;
            r=rOut;
            var c=createPt(r*Math.cos(angle),r*Math.sin(angle));

            angle=theta-Math.PI/6;
            r=rIn;
            var d=createPt(r*Math.cos(angle),r*Math.sin(angle));

            ctx.fillStyle="black";
            ctx.beginPath();
            ctx.moveTo(a.x,a.y);
            ctx.lineTo(b.x,b.y);
            ctx.arc(0,0,rOut,theta+Math.PI/6,theta-Math.PI/6,true);
            ctx.lineTo(c.x,c.y);
            ctx.lineTo(d.x,d.y);
            ctx.arc(0,0,rIn,theta-Math.PI/6,theta+Math.PI/6,false);
            ctx.closePath();
            ctx.fill();
        }
        ctx.restore();

        // 内圆
        ctx.save();
        var r=R*0.17;
        drawSolidCircle(ctx,0,0,r,"black")
        ctx.fill();
        ctx.restore();

        // 文字
        ctx.save();
        var r=R*1.00;        
        var text="CAUTION";
        writeText(ctx,0,-r*0.78,text,r*0.15+"px Stencil Std","white");
        writeText(ctx,0,-r*0.76,text,r*0.15+"px Stencil Std","black");
        writeText(ctx,0,-r*0.77,text,r*0.15+"px Stencil Std","rgb(255,191,38)");

        var text="NUCLEAR LAUNTCH DETETED";
        writeText(ctx,0,r*0.91,text,r*0.12+"px Noto Sans SC Black","white");        
        writeText(ctx,0,r*0.93,text,r*0.12+"px Noto Sans SC Black","black");
        writeText(ctx,0,r*0.92,text,r*0.12+"px Noto Sans SC Black","yellow");
        ctx.restore();
                    
        writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权
    }
}

/*----------------------------------------------------------
函数:用于绘制扇形
ctx:绘图上下文
x:扇形圆心心横坐标
y:扇形圆心纵坐标
rIn:扇形内径
rOut:扇形外径
startAngle:起始角度
endAngle:中止角度
----------------------------------------------------------*/
function drawFan(ctx,x,y,rIn,rOut,startAngle,endAngle){
    var a=createPt(x+rIn*Math.cos(startAngle),y+rIn*Math.sin(startAngle));
    var b=createPt(x+rOut*Math.cos(startAngle),y+rOut*Math.sin(startAngle));
    var c=createPt(x+rOut*Math.cos(endAngle),y+rOut*Math.sin(endAngle));
    var d=createPt(x+rIn*Math.cos(endAngle),y+rIn*Math.sin(endAngle));

    ctx.beginPath();
    ctx.moveTo(a.x,a.y);
    ctx.lineTo(b.x,b.y);
    ctx.arc(0,0,rOut,startAngle,endAngle,false);
    ctx.lineTo(c.x,c.y);
    ctx.lineTo(d.x,d.y);
    ctx.arc(0,0,rIn,endAngle,startAngle,true);
    ctx.closePath();
}

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

/*----------------------------------------------------------
函数:用于绘制矩形
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 @ 2019-03-15 09:11  逆火狂飙  阅读(1102)  评论(0)    收藏  举报