【Canvas与盾牌】黑白灰盾

【成图】

1

2

3

4

5

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 = "black";
       // ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
        ctx.restore();
        
        const R=220;//基准尺寸
        var ct=createPt(0,0);// ct=center

        // #1 
        ctx.save();    
        var r=R*1.00;
        var h=r*2;
        var w=h*0.8;        
        ctx.fillStyle="rgb(90,90,90)";
        drawShield(ctx,0,0,w,h,0);
        ctx.fill();
        ctx.restore();

        // #2
        ctx.save();    
        var r=R*1.00;
        h-=r*0.2;
        w-=r*0.2*0.8;
        ctx.fillStyle="white";
        drawShield(ctx,0,0,w,h,0);
        ctx.fill();
        ctx.restore();

        // #3
        ctx.save();    
        var r=R*1.00;
        h-=r*0.2;
        w-=r*0.2*0.8;
        ctx.fillStyle="rgb(206,206,206)";
        drawShield(ctx,0,0,w,h,0);
        ctx.fill();
        ctx.restore();

        // #4
        ctx.save();    
        ctx.fillStyle="rgb(104,104,104)";
        drawHalfShield(ctx,0,0,w,h,0);
        ctx.fill();
        ctx.restore();
        
        writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权
    }
}

/*----------------------------------------------------------
函数:用于绘制右半个盾牌
ctx:绘图上下文
x:盾牌中心横坐标
y:盾牌中心纵坐标
w:盾牌宽
h:盾牌高
roundRadius:圆角半径
----------------------------------------------------------*/
function drawHalfShield(ctx,x,y,w,h,roundRadius){
    var ct=createPt(x,y);    

    // 定点
    var a=createPt(ct.x,-h/2);
    var e=createPt(ct.x,h/2);
    var c=createPt(a.x-w/2,a.y+w/5);
    var c1=createPt2(c.x,c.y,roundRadius,0);
    var c2=createPt2(c.x,c.y,roundRadius,Math.PI/2);
    var g=createPt(a.x+w/2,a.y+w/5);
    var g1=createPt2(g.x,g.y,roundRadius,Math.PI/2);
    var g2=createPt2(g.x,g.y,roundRadius,Math.PI);
    var b=createPt(a.x/2+c.x/2,c.y);
    var hh=createPt(a.x/2+g.x/2,g.y);
    var d=createPt2(e.x,e.y,w/2/Math.cos(Math.PI/6),Math.PI+Math.PI/6);
    var f=createPt2(e.x,e.y,w/2/Math.cos(Math.PI/6),-Math.PI/6);    

    // 勾线
    ctx.beginPath();
    ctx.moveTo(a.x,a.y);
    ctx.lineTo(e.x,e.y);
    ctx.quadraticCurveTo(f.x,f.y,g1.x,g1.y);
    ctx.quadraticCurveTo(g.x,g.y,g2.x,g2.y);
    ctx.quadraticCurveTo(hh.x,hh.y,a.x,a.y);
    ctx.closePath();
}

/*----------------------------------------------------------
函数:用于绘制盾牌外轮廓,可控制圆角大小,使转折处厚边可调
ctx:绘图上下文
x:盾牌中心横坐标
y:盾牌中心纵坐标
w:盾牌宽
h:盾牌高
roundRadius:圆角半径
----------------------------------------------------------*/
function drawShield(ctx,x,y,w,h,roundRadius){
    var ct=createPt(x,y);    

    // 定点
    var a=createPt(ct.x,-h/2);
    var e=createPt(ct.x,h/2);
    var c=createPt(a.x-w/2,a.y+w/5);
    var c1=createPt2(c.x,c.y,roundRadius,0);
    var c2=createPt2(c.x,c.y,roundRadius,Math.PI/2);
    var g=createPt(a.x+w/2,a.y+w/5);
    var g1=createPt2(g.x,g.y,roundRadius,Math.PI/2);
    var g2=createPt2(g.x,g.y,roundRadius,Math.PI);
    var b=createPt(a.x/2+c.x/2,c.y);
    var hh=createPt(a.x/2+g.x/2,g.y);
    var d=createPt2(e.x,e.y,w/2/Math.cos(Math.PI/6),Math.PI+Math.PI/6);
    var f=createPt2(e.x,e.y,w/2/Math.cos(Math.PI/6),-Math.PI/6);    

    // 勾线
    ctx.beginPath();
    ctx.moveTo(a.x,a.y);
    ctx.quadraticCurveTo(b.x,b.y,c1.x,c1.y);
    ctx.quadraticCurveTo(c.x,c.y,c2.x,c2.y);
    ctx.quadraticCurveTo(d.x,d.y,e.x,e.y);
    ctx.quadraticCurveTo(f.x,f.y,g1.x,g1.y);
    ctx.quadraticCurveTo(g.x,g.y,g2.x,g2.y);
    ctx.quadraticCurveTo(hh.x,hh.y,a.x,a.y);
    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();
}

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