【Canvas与艺术】绘制方形斜纹生化危险Biohazard标志

【关键点】

切角矩形和切角三角形的绘制。

【成果图】

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>方形斜纹生化危险Biohazard标志</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>
            <img id="myImg" src="125.png" style="display:none;"/>
        </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){
        window.requestAnimationFrame(animate);   
    }
}

// 舞台类
function Stage(){

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

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

    // 画背景
    this.paintBg=function(ctx){
        ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏        
    
        // 黑底
        ctx.fillStyle="black";
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);

        // 黄色斜45°条纹
        const step=91;
        for(i=0;i<11;i++){            
            var x=-WIDTH/2+i*step+32;
            var y=-HEIGHT/2+i*step+32;

            ctx.beginPath();
            ctx.moveTo(x,-HEIGHT/2-20);
            ctx.lineTo(-WIDTH/2-20,y);
            ctx.lineWidth=34;
            ctx.strokeStyle="rgb(255,211,63)";
            ctx.stroke();
        }

        // 外圈黑框
        ctx.beginPath();
        ctx.moveTo(-WIDTH/2,-HEIGHT/2);
        ctx.lineTo(-WIDTH/2,HEIGHT/2);
        ctx.lineWidth=60;
        ctx.strokeStyle="black";
        ctx.stroke();

        ctx.beginPath();
        ctx.moveTo(-WIDTH/2,-HEIGHT/2);
        ctx.lineTo(WIDTH/2,-HEIGHT/2);
        ctx.lineWidth=60;
        ctx.strokeStyle="black";
        ctx.stroke();

        ctx.beginPath();
        ctx.moveTo(WIDTH/2,-HEIGHT/2);
        ctx.lineTo(WIDTH/2,HEIGHT/2);
        ctx.lineWidth=60;
        ctx.strokeStyle="black";
        ctx.stroke();

        ctx.beginPath();
        ctx.moveTo(-WIDTH/2,HEIGHT/2);
        ctx.lineTo(WIDTH/2,HEIGHT/2);
        ctx.lineWidth=60;
        ctx.strokeStyle="black";
        ctx.stroke();
        
        // 黑色切角矩形
        drawChamferedRect(ctx,0,170,300,90,18);
        ctx.fillStyle="black";
        ctx.fill();

        // 黄色切角矩形
        drawChamferedRect(ctx,0,170,270,60,12);
        ctx.fillStyle="rgb(255,211,63)";
        ctx.fill();

        // 黑字
        ctx.textBaseline="bottom";
        ctx.textAlign="center";
        ctx.font = "42px Stencil Std";
        ctx.fillStyle="red";
        ctx.fillText("BIOHAZARD",0,170+25); 

        //---下面开始画标志
        ctx.save();
        ctx.translate(0,0);

        // 外圈黄色切角三角形
        var arr=[];
        for(var i=0;i<3;i++){
            var theta=Math.PI*2/3*i+Math.PI/6;
            var x=Math.cos(theta)*230;
            var y=Math.sin(theta)*230;
            arr.push(createPt(x,y));
        }
        drawChamferedTriangle(ctx,arr[0],arr[1],arr[2],12);
        ctx.fillStyle="rgb(255,211,63)";
        ctx.fill();
        ctx.lineWidth=4;
        ctx.strokeStyle="red";
        ctx.stroke();

        // 内圈黑色切角三角形
        var arr=[];
        for(var i=0;i<3;i++){
            var theta=Math.PI*2/3*i+Math.PI/6;
            var x=Math.cos(theta)*210;
            var y=Math.sin(theta)*210;
            arr.push(createPt(x,y));
        }
        drawChamferedTriangle(ctx,arr[0],arr[1],arr[2],9);
        ctx.fillStyle="black";
        ctx.fill();

        // 内圈黄色三角形
        ctx.beginPath();
        for(var i=0;i<3;i++){
            var theta=Math.PI*2/3*i+Math.PI/6;
            var x=Math.cos(theta)*190;
            var y=Math.sin(theta)*190;
            ctx.lineTo(x,y);
        }
        ctx.closePath();
        ctx.fillStyle="rgb(255,211,63)";
        ctx.fill();


        // 下面画多刺爪
        ctx.save();
        
        // 三个黑实心圆
        var arr=createRegTriArr(0,0,50);
        for(var i=0;i<arr.length;i++){
            var pt=arr[i];

            ctx.beginPath();
            ctx.arc(pt.x,pt.y,56,0,Math.PI*2,false);
            ctx.closePath();
            ctx.fillStyle="black";
            ctx.fill();
        }

        // 三个偏心的黄色实心圆
        var arr=createRegTriArr(0,0,62);
        for(var i=0;i<arr.length;i++){
            var pt=arr[i];

            ctx.beginPath();
            ctx.arc(pt.x,pt.y,45,0,Math.PI*2,false);
            ctx.closePath();
            ctx.fillStyle="rgb(255,211,63)";
            ctx.fill();
        }

        // 三段式黑圈
        for(var i=0;i<3;i++){
            var startAngle=i*Math.PI*2/3-Math.PI/28;
            var endAngle=startAngle+Math.PI/5*2;

            ctx.beginPath();
            ctx.arc(0,0,40,startAngle,endAngle,false);
            ctx.lineWidth=12;
            ctx.strokeStyle="black";
            ctx.stroke();
        }

        // 三小黄叉
        var arr=createRegTriArr(0,0,18);
        for(var i=0;i<arr.length;i++){
            var pt=arr[i];

            ctx.beginPath();
            ctx.moveTo(0,0);
            ctx.lineTo(pt.x,pt.y);
            ctx.lineWidth=6;
            ctx.strokeStyle="rgb(255,211,63)";
            ctx.stroke();
        }

        // 中心黄点
        ctx.beginPath();
        ctx.arc(0,0,9,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fillStyle="rgb(255,211,63)";
        ctx.fill();

        ctx.restore();        
        ctx.restore();

        // 版权
        ctx.textBaseline="bottom";
        ctx.textAlign="center";
        ctx.font = "8px consolas";
        ctx.fillStyle="black";
        ctx.fillText("逆火原创",WIDTH/2-40,HEIGHT/2-10);
    }

    // 画前景
    this.paintFg=function(ctx){
        
        
    }
}

// 函数,创建一个二维坐标点
function createPt(x,y){
    var retval={};
    retval.x=x;
    retval.y=y;
    return retval;
}

// function:create regular triangular array
// 函数,创建一个以x,y为中心,r为半径的正三角形数组
// arr[0]为右下,arr[1]为左下,arr[2]为正上。
function createRegTriArr(x,y,r){
    var arr=new Array();

    for(var i=0;i<3;i++){
        var theta=Math.PI*2/3*i+Math.PI/6;
        var pt=createPt(r*Math.cos(theta)+x,r*Math.sin(theta)+y);
        arr.push(pt);
    }

    return arr;
}

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

/*----------------------------------------------------------
函数:用于绘制倒角三角形
ctx:绘图上下文
ptRight:右顶点
ptLeft:左顶点
ptTop:上顶点
chamferLength:倒角长度
----------------------------------------------------------*/
function drawChamferedTriangle(ctx,ptRight,ptLeft,ptTop,chamferLength){
    ctx.beginPath();
    ctx.moveTo(ptRight.x-chamferLength,ptRight.y);
    ctx.lineTo(ptLeft.x+chamferLength,ptLeft.y);
    ctx.lineTo(ptLeft.x+chamferLength/2,ptLeft.y-chamferLength*1.732/2);
    ctx.lineTo(ptTop.x-chamferLength/2,ptTop.y+chamferLength*1.732/2);
    ctx.lineTo(ptTop.x+chamferLength/2,ptTop.y+chamferLength*1.732/2);
    ctx.lineTo(ptRight.x-chamferLength/2,ptRight.y-chamferLength*1.732/2);
    ctx.closePath();
}

/*--------------------------------------------------
“我大清”的故事
与曾国藩、李鸿章、左宗棠并称晚清中兴四大名臣
的张之洞,他有个口头禅就是时常把“我大清”挂在嘴边。
有一次就在朝堂上,张之洞又在说“我大清”的时候,
素来与之不和的醇亲王载沣一本正经地问他:“张大人,
你什么时候被抬的旗啊?我怎么不知道?”
张之洞诧异道:“老夫没有入旗。”
载沣得意地道:“大清是我爱新觉罗家的,你等只是
我大清养的狗,连奴才都不算。你以后不要张口我大清,
闭口我大清的,省得让人误会了,还以为大清是你们的了!”
张之洞被噎得哑口无言,慈禧太后在一旁哈哈大笑...
---------------------------------------------------*/
//-->
</script>

【原图】

END

posted @ 2016-03-22 00:33  逆火狂飙  阅读(148)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东