【Canvas与艺术】绘制黑白山间野营Camping徽章

【说明】

中间的山月图是借用的网上的成图,不是用Canvas绘制的。

【成果图】

 

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>使用Html5/Canvas绘制黑白山间野营Camping徽章</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.arc(0,0,240,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=12;
        ctx.strokeStyle="black";
        ctx.stroke();
        
        // 绳纹
        var arr=[];
        for(var i=0;i<360;i++){
            var theta=i*Math.PI*2/360;
            arr.push(theta);
        }
        const offset=3;
        var r=240-offset,R=240+offset;
        for(var i=0;i<arr.length;i+=4){
            var start=arr[i];
            var end=arr[(i+2)%arr.length];
            var bias=Math.PI*2/360*2.5;

            var a=createPt(r*Math.cos(start),r*Math.sin(start));
            var b=createPt(R*Math.cos(end),R*Math.sin(end));
            var c=createPt(R*Math.cos(end+bias),R*Math.sin(end+bias));
            var d=createPt(r*Math.cos(start+bias),r*Math.sin(start+bias));

            ctx.beginPath();
            ctx.moveTo(a.x,a.y);
            ctx.arcTo(b.x,b.y,c.x,c.y,40);
            ctx.arcTo(d.x,d.y,a.x,a.y,40);
            ctx.closePath();
            ctx.fillStyle="white";
            ctx.fill();
        }

        // 内黑圈
        ctx.beginPath();
        ctx.arc(0,0,230,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=2;
        ctx.strokeStyle="black";
        ctx.stroke();
        ctx.fillStyle="white";
        ctx.fill();

        // 上半圈文字
        var words="GO CAMPING";
        var arr=words.split("");
        for(var i=0;i<arr.length;i++){
            var letter=arr[i];
            var theta=i*Math.PI/180*8.5+Math.PI+Math.PI/180*50;
            var x=190*Math.cos(theta);
            var y=190*Math.sin(theta);

            ctx.save();
            ctx.translate(x,y);
            ctx.rotate(theta+Math.PI/2);
            ctx.scale(1,1.2);
            ctx.textBaseline="bottom";
            ctx.textAlign="center";
            ctx.font = "24px Stencil Std";
            ctx.fillStyle="rgb(19,28,57)";
            ctx.fillText(letter,0,0);
            ctx.restore();
        }

        // 下半圈文字
        var words="in the mountains";
        var arr=words.split("");
        for(var i=0;i<arr.length;i++){
            var letter=arr[i];
            var theta=-i*Math.PI/180*8.5+Math.PI-Math.PI/180*26;
            var x=225*Math.cos(theta);
            var y=225*Math.sin(theta);

            ctx.save();
            ctx.translate(x,y);
            ctx.rotate(theta-Math.PI/2);
            ctx.scale(1,1.2);
            ctx.textBaseline="bottom";
            ctx.textAlign="center";
            ctx.font = "24px Stencil Std";
            ctx.fillStyle="rgb(19,28,57)";
            ctx.fillText(letter,0,0);
            ctx.restore();
        }

        // 两侧五星
        draw5Star(ctx,206,0,10);
        ctx.fillStyle="rgb(19,28,57)";
        ctx.fill();
        draw5Star(ctx,-206,0,10);
        ctx.fillStyle="rgb(19,28,57)";
        ctx.fill();

        // 内小点圈
        ctx.beginPath();
        ctx.arc(0,0,180,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=6;
        ctx.strokeStyle="black";
        ctx.stroke();
        for(var i=0;i<180;i++){
            var theta=Math.PI*2/180*i;
            var x=180*Math.cos(theta);
            var y=180*Math.sin(theta);

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

        //
        ctx.save();
        ctx.beginPath();
        ctx.arc(0,0,177,0,Math.PI*2,false);
        ctx.closePath();
        ctx.clip();
        var img=document.getElementById("myImg");
        ctx.drawImage(img,0,0,500,350,-250,-175,500,350);
        ctx.restore();
        
        // 版权
        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;
}

/*--------------------------------------------------
函数:绘制正五角星的推荐画法
ctx:绘图上下文
x:五角星中心横坐标
y:五角星中心纵坐标
R:五角星中心到顶点的距离
---------------------------------------------------*/
function draw5Star(ctx,x,y,R){
    var r=R*Math.sin(Math.PI/10)/Math.sin(Math.PI/10*7);
    
    var arr=[0,0,0,0,0,0,0,0,0,0];

    // 顶五点
    for(var i=0;i<5;i++){
        var theta=i*Math.PI/5*2-Math.PI/10;
        var x1=R*Math.cos(theta)+x;
        var y1=R*Math.sin(theta)+y;
        arr[i*2]=createPt(x1,y1);
    }

    // 内五点
    for(var i=0;i<5;i++){
        var theta=i*Math.PI/5*2+Math.PI/10;
        var x1=r*Math.cos(theta)+x;
        var y1=r*Math.sin(theta)+y;
        arr[i*2+1]=createPt(x1,y1);
    }

    ctx.beginPath();
    for(var i=0;i<arr.length;i++){
        ctx.lineTo(arr[i].x,arr[i].y);
    }
    ctx.closePath();
}

/*---------------------------------------------
读诗使人灵秀,
然而诗歌的作用远不止于此,
它还能令人茅塞顿开,使人恍然大我。
生活中的曲折与困顿,
古人在遥远的几千年前就已传授了解决之道:
冬天不会持续寒冷,春天总会到来。
----------------------------------------------*/
//-->
</script>

【借用的底图】

【原图】

END

 

posted @ 2014-12-14 16:20  逆火狂飙  阅读(192)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东