【Canvas与标志】十字盾牌标志

【成图】

120*120的png图标:

6

 

【代码】

<!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;

//-------------------------------
// 初始化
//-------------------------------
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();    
        var r=R*1.00;
        var w=1.2*r;
        var h=1.3*w;
        ctx.fillStyle="black";
        drawShield(ctx,0,0,w,h);
        ctx.fill();
        ctx.restore();

        // #盾牌第二圈(白圈)外缘
        ctx.save();    
        w-=0.08*R;
        h-=0.08*R*1.3;
        ctx.fillStyle="white";
        drawShield(ctx,0,0,w,h);
        ctx.fill();
        ctx.restore();

        // #3盾牌第三圈(墨线圈)外缘
        ctx.save();    
        w-=0.08*R;
        h-=0.08*R*1.3;
        ctx.fillStyle="rgb(48,80,101)";
        drawShield(ctx,0,0,w,h);
        ctx.fill();
        ctx.restore();

        // #4
        ctx.save();    
        w-=0.02*R;
        h-=0.02*R*1.3;
        ctx.fillStyle="white";
        drawShield(ctx,0,0,w,h);
        ctx.fill();        
        ctx.restore();

        // #5 竖线
        /*ctx.save();    
        w-=0.02*R;
        h-=0.02*R*1.3;
        drawShield(ctx,0,0,w,h);
        ctx.clip();

        var N=16;
        var part=w/N;

        for(var i=0;i<N;i++){
            var x=-w/2+i*part;
            var y=-h/2;

            ctx.strokeStyle="rgb(48,80,101)";
            ctx.beginPath();
            ctx.moveTo(x,y);
            ctx.lineTo(x,y+h);
            ctx.stroke();
        }
        ctx.restore();*/

        // #6 十字架
        ctx.save();    
        var center=createPt2(0,0,h/6,-Math.PI/2);
        drawSolidCircle(ctx,center.x,center.y,3,"blue");
        
        var N=48;// 光环
        var rout=w*0.26;
        var rin=rout*0.85;
        for(var i=0;i<N;i++){
            var theta=Math.PI*2/N*i;
            var a=createPt2(center.x,center.y,rin,theta);
            var b=createPt2(center.x,center.y,rout,theta);

            ctx.strokeStyle="rgb(48,80,101)";
            ctx.beginPath();
            ctx.moveTo(a.x,a.y);
            ctx.lineTo(b.x,b.y);
            ctx.stroke();
        }


        var crossWidth=w/5;// #1
        ctx.fillStyle="rgb(48,80,101)";
        drawRect(ctx,center.x,center.y,w*0.8,crossWidth);
        ctx.fill();    
        ctx.fillStyle="rgb(48,80,101)";
        drawRect(ctx,center.x,center.y+h*0.18,crossWidth,h*0.8);
        ctx.fill();

        crossWidth-=R/210*5;// #2
        ctx.fillStyle="white";
        drawRect(ctx,center.x,center.y,w*0.81,crossWidth);
        ctx.fill();    
        ctx.fillStyle="white";
        drawRect(ctx,center.x,center.y+h*0.18,crossWidth,h*0.81);
        ctx.fill();

        crossWidth-=R/210*8;// #3
        ctx.fillStyle="black";
        drawRect(ctx,center.x,center.y,w*0.81,crossWidth);
        ctx.fill();    
        ctx.fillStyle="black";
        drawRect(ctx,center.x,center.y+h*0.18,crossWidth,h*0.81);
        ctx.fill();

        crossWidth-=R/210*18;// #4
        ctx.fillStyle="white";
        drawRect(ctx,center.x,center.y,w*0.82,crossWidth);
        ctx.fill();    
        ctx.fillStyle="white";
        drawRect(ctx,center.x,center.y+h*0.18,crossWidth,h*0.82);
        ctx.fill();

        ctx.restore();


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

/*----------------------------------------------------------
函数:用于绘制盾牌轮廓
ctx:绘图上下文
x:轮廓中心横坐标
y:轮廓中心纵坐标
w:轮廓宽
h:轮廓高
----------------------------------------------------------*/
function drawShield(ctx,x,y,w,h){
    var a=createPt2(x,y,h/2,-Math.PI/2);
    var c=createPt2(x,y,h/2,+Math.PI/2);
    var e=createPt2(x,y,h/3,-Math.PI/2);
    var b=createPt2(e.x,e.y,w/2,Math.PI);
    var d=createPt2(e.x,e.y,w/2,0);
    var be=createPt((e.x-b.x)*2/3+b.x,(e.y-b.y)*2/3+b.y);
    var de=createPt((d.x-e.x)*1/3+e.x,(d.y-e.y)*1/3+e.y);
    var bc=createPt(b.x,(c.y-b.y)*2/3+b.y);
    var cd=createPt(d.x,(d.y-c.y)*1/3+c.y);

    ctx.beginPath();
    ctx.moveTo(a.x,a.y);
    ctx.quadraticCurveTo(be.x,be.y,b.x,b.y);
    ctx.lineTo(b.x,b.y);
    ctx.quadraticCurveTo(bc.x,bc.y,c.x,c.y);
    ctx.lineTo(c.x,c.y);
    ctx.quadraticCurveTo(cd.x,cd.y,d.x,d.y);
    ctx.lineTo(d.x,d.y);
    ctx.quadraticCurveTo(de.x,de.y,a.x,a.y);
    ctx.lineTo(a.x,a.y);
    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();
}

/*----------------------------------------------------------
函数:创建一个二维坐标点
baseX:基准点横坐标
baseY:基准点纵坐标
radius:当前点到基准点的距离
theta:当前点到基准点的角度
Pt即Point
----------------------------------------------------------*/
function createPt2(baseX,baseY,radius,theta){
    var retval={};
    retval.x=baseX+radius*Math.cos(theta);
    retval.y=baseY+radius*Math.sin(theta);
    return retval;
}

/*----------------------------------------------------------
函数:创建一个二维坐标点
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 @ 2015-05-27 15:30  逆火狂飙  阅读(929)  评论(0)    收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东