【Canvas与艺术】绘制斜置黄色三角biohazard标志

【关键点】

径向渐变色和文字按角度偏转。

【成果图】

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>使用Html5/Canvas绘制斜置黄色三角biohazard标志</title>
     <style type="text/css">
     .centerlize{
        margin:0 auto;
        width:1200px;
     }
     </style>
     </head>

     <body οnlοad="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){
        window.requestAnimationFrame(animate);   
    }
}

// 舞台类
function Stage(){

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

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

    // 画背景
    this.paintBg=function(ctx){
        ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏        

        // 中心渐变背景
        var rgnt=ctx.createRadialGradient(0,0,0,0,0,300);
        rgnt.addColorStop(0,"rgb(130,130,130)");
        rgnt.addColorStop(1,"rgb(40,40,40)");
        ctx.fillStyle=rgnt;
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);

        // 整体偏置
        ctx.save();
        ctx.translate(0,20);
        ctx.rotate(-Math.PI/10);

        // 白色切角三角形
        var arr=createRegTriArr(0,0,250);
        drawChamferedTriangle(ctx,arr[0],arr[1],arr[2],36);
        ctx.fillStyle="white";
        ctx.fill();

        // 黑色切角三角形
        var arr=createRegTriArr(0,0,240);
        drawChamferedTriangle(ctx,arr[0],arr[1],arr[2],30);
        ctx.fillStyle="rgb(32,26,26)";
        ctx.fill();

        // 黄色切角三角形
        var arr=createRegTriArr(0,0,160);
        drawChamferedTriangle(ctx,arr[0],arr[1],arr[2],24);
        ctx.fillStyle="rgb(255,240,1)";
        ctx.fill();

        // 文字
        var words=["SP-058-A","BIOHARZARD","DANGER"];
        for(i=0;i<3;i++){
            var theta=i*Math.PI*2/3-Math.PI/6;
            var pt=createPt(84*Math.cos(theta),84*Math.sin(theta));

            ctx.save();            
            if(i==1){
                ctx.translate(pt.x,pt.y+33);
                ctx.rotate(theta-Math.PI/2);
                
            }else{
                ctx.translate(pt.x,pt.y);
                ctx.rotate(theta+Math.PI/2);                
            }
            ctx.textBaseline="bottom";
            ctx.textAlign="center";
            ctx.font = "27px Stencil Std";
            ctx.fillStyle="rgb(255,240,1)";
            ctx.fillText(words[i],0,0);
            ctx.restore();
        }

        //--------------下面开始绘制多刺爪
        ctx.save();
        ctx.scale(0.85,0.85);
        
        // 三个黑实心圆
        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,240,1)";
            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,240,1)";
            ctx.stroke();
        }

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

        ctx.restore();

        ctx.restore();

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

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

/*----------------------------------------------------------
函数:创建一个二维坐标点
x:坐标点的横坐标
y:坐标点的纵坐标
返回:坐标点对象
----------------------------------------------------------*/
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:绘图上下文
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

  • 30
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值