【Canvas与艺术】绘制黄色三角辐射警示标志

【成果图】

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>使用Html5/Canvas绘制黄色三角电离辐射警示标志</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="145.jpg" 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.beginPath();
        ctx.moveTo(0,0);
        ctx.lineTo(256,150);
        ctx.lineTo(256,256);
        ctx.lineTo(-256,256);
        ctx.lineTo(-256,150);
        ctx.closePath();
        ctx.fillStyle="rgb(156,156,156)";
        ctx.fill();
        ctx.lineWidth=0.2;
        ctx.strokeStyle="black";
        ctx.stroke();

        // 右方灰色块
        ctx.beginPath();
        ctx.moveTo(0,0);
        ctx.lineTo(0,-256);
        ctx.lineTo(256,-256);
        ctx.lineTo(256,150);
        ctx.closePath();
        ctx.fillStyle="rgb(213,213,213)";
        ctx.fill();
        ctx.stroke();

        // 左方灰色块
        ctx.beginPath();
        ctx.moveTo(0,0);
        ctx.lineTo(0,-256);
        ctx.lineTo(-256,-256);
        ctx.lineTo(-256,150);
        ctx.closePath();
        ctx.fillStyle="rgb(242,242,242)";
        ctx.fill();
        ctx.stroke();

        // 圆角三角形
        drawRoundTriangle(ctx,0,0,200,10);
        ctx.fillStyle="yellow";
        ctx.fill();
        ctx.lineWidth=10;
        ctx.strokeStyle="black";
        ctx.stroke();

        // 三瓣
        var R=75,r=30;
        for(var i=0;i<3;i++){
            var start=Math.PI*2/3*i-Math.PI/3;
            var end=start+Math.PI/3;

            var s1=createPt(r*Math.cos(start),r*Math.sin(start));
            var s2=createPt(R*Math.cos(start),R*Math.sin(start));
            var e2=createPt(R*Math.cos(end),R*Math.sin(end));
            var e1=createPt(r*Math.cos(end),r*Math.sin(end));

            ctx.beginPath();
            ctx.moveTo(s1.x,s1.y);
            ctx.lineTo(s2.x,s2.y);
            ctx.arc(0,0,R,start,end,false);
            ctx.lineTo(e2.x,e2.y);
            ctx.lineTo(e1.x,e1.y);
            ctx.arc(0,0,r,end,start,true);
            ctx.closePath();
            ctx.fillStyle="rgb(51,51,51)";
            ctx.fill();
        }

        // 中心圆点
        ctx.beginPath();
        ctx.arc(0,0,20,0,Math.PI*2,true);
        ctx.closePath();
        ctx.fillStyle="rgb(51,51,51)";
        ctx.fill();

        // 圆角矩形
        drawRoundRect(ctx,0,160,280,80,8);
        ctx.fillStyle="white";
        ctx.fill();
        ctx.lineWidth=2;
        ctx.strokeStyle="black";
        ctx.stroke();

        // 当心电离辐射
        ctx.textBaseline="bottom";
        ctx.textAlign="center";
        ctx.font = "42px 黑体";
        ctx.fillStyle="black";
        ctx.fillText("当心电离辐射",0,180);
        
        // 版权
        ctx.textBaseline="bottom";
        ctx.textAlign="center";
        ctx.font = "8px consolas";
        ctx.fillStyle="black";
        ctx.fillText("逆火原创",WIDTH/2-30,HEIGHT/2-10);
    }

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

/*----------------------------------------------------------
函数:创建一个二维坐标点
x:横坐标
y:纵坐标
Pt即Point
----------------------------------------------------------*/
function createPt(x,y){
    var retval={};
    retval.x=x;
    retval.y=y;
    return retval;
}

/*----------------------------------------------------------
函数:创建一个以x,y为中心,r为半径的正三角形数组
ctx:绘图上下文
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:三角形中心纵坐标
r:三角形中心到顶点的长度
filletRadius:圆角半径
----------------------------------------------------------*/
function drawRoundTriangle(ctx,x,y,r,filletRadius){
    var arr=createRegTriArr(x,y,r);
    var arrLeft=createRegTriArr(arr[0].x,arr[0].y,filletRadius);
    var arrRight=createRegTriArr(arr[1].x,arr[1].y,filletRadius);
    var arrTop=createRegTriArr(arr[2].x,arr[2].y,filletRadius);

    ctx.beginPath();
    ctx.moveTo(arrTop[0].x,arrTop[0].y);
    ctx.lineTo(arrLeft[2].x,arrLeft[2].y);
    ctx.arcTo(arrLeft[0].x,arrLeft[0].y,arrLeft[1].x,arrLeft[1].y,filletRadius);
    ctx.lineTo(arrRight[0].x,arrRight[0].y);
    ctx.arcTo(arrRight[1].x,arrRight[1].y,arrRight[2].x,arrRight[2].y,filletRadius);
    ctx.lineTo(arrTop[1].x,arrTop[1].y);
    ctx.arcTo(arrTop[2].x,arrTop[2].y,arrTop[0].x,arrTop[0].y,filletRadius);
    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();
}

/*---------------------------------------------
这个病态的社会,
默默忍受是常态,
据理力争是异类,
并不执着于追求梦想,
反而热衷于维持现状,
是许多中青年人的现实。
----------------------------------------------*/
//-->
</script>

END

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