【Canvas与艺术】绘制黑白纹章,内嵌陶渊明南山诗

【效果图】

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>用Canvas绘制黑白纹章</title>
     <style type="text/css">
     .centerlize{
        margin:0 auto;
        width:1200px;
     }
     </style>
     </head>

     <body οnlοad="init();">
        <div class="centerlize">
            <canvas id="myCanvas" width="512px" height="512px" 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 gearArr=getGearArray(32,Math.PI/32,252,230);                
        ctx.beginPath();
        for(var i=0;i<gearArr.length;i++){
            ctx.lineTo(gearArr[i].x,gearArr[i].y);
        }
        ctx.closePath();
        ctx.lineWidth=0.5;
        ctx.strokeStyle="black";
        ctx.stroke();

        // 32齿圈
        gearArr=getGearArray(32,Math.PI/32,250,228);                
        ctx.beginPath();
        for(var i=0;i<gearArr.length;i++){
            ctx.lineTo(gearArr[i].x,gearArr[i].y);
        }
        ctx.closePath();
        ctx.fillStyle="black";
        ctx.fill();

        // 白圈
        ctx.beginPath();
        ctx.arc(0,0,216,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fillStyle="white";
        ctx.fill();

        // 黑圈
        ctx.beginPath();
        ctx.arc(0,0,200,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=20;
        ctx.strokeStyle="black";
        ctx.stroke();

        // 灰破则先圈
        const radius=200;
        for(var i=0;i<36;i++){
            var start=Math.PI/18*i;
            var x1=radius*Math.cos(start);
            var y1=radius*Math.sin(start);
            var end=start+Math.PI/36;
            var x2=radius*Math.cos(end);
            var y2=radius*Math.sin(end);
            
            ctx.beginPath();
            ctx.moveTo(x1,y1);
            ctx.lineTo(x2,y2);
            ctx.closePath();
            ctx.lineWidth=4;
            ctx.strokeStyle="gray";
            ctx.stroke();
        }

        // 内部黑粗圈
        ctx.beginPath();
        ctx.arc(0,0,172,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=6;
        ctx.strokeStyle="black";
        ctx.stroke();

        // 内部黑细圈
        ctx.beginPath();
        ctx.arc(0,0,166,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=1;
        ctx.strokeStyle="black";
        ctx.stroke();

        // 左翼
        ctx.beginPath();
        ctx.moveTo(-90,-130);
        ctx.lineTo(-18,-130);
        ctx.closePath();
        ctx.lineWidth=1;
        ctx.strokeStyle="black";
        ctx.stroke();
        // 上方五星
        draw5Star(ctx,0,-130,14,"black");
        // 右翼
        ctx.beginPath();
        ctx.moveTo(90,-130);
        ctx.lineTo(18,-130);
        ctx.closePath();
        ctx.lineWidth=1;
        ctx.strokeStyle="black";
        ctx.stroke();

        // 左翼
        ctx.beginPath();
        ctx.moveTo(-90,130);
        ctx.lineTo(-18,130);
        ctx.closePath();
        ctx.lineWidth=1;
        ctx.strokeStyle="black";
        ctx.stroke();
        // 下方五星
        draw5Star(ctx,0,130,14,"black");
        // 右翼
        ctx.beginPath();
        ctx.moveTo(90,130);
        ctx.lineTo(18,130);
        ctx.closePath();
        ctx.lineWidth=1;
        ctx.strokeStyle="black";
        ctx.stroke();

        ctx.save();
        ctx.textBaseline="bottom";
        ctx.textAlign="center";
        ctx.font = "40px 方正宋刻本秀楷简体";
        ctx.fillStyle="black";
        ctx.fillText("采菊东篱下",0,-56);
        ctx.fillText("悠然见南山",0,-6);
        ctx.fillText("山气日夕佳",0,44);
        ctx.fillText("飞鸟相与还",0,94);
        ctx.restore();

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

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

//-------------------------------------------------------
// 取得一个齿轮全部控制点的函数
// n:齿轮齿数
// angle:齿斜面倾角
// outerRadius:齿轮外径
// innerRadius:齿轮内径
//-------------------------------------------------------
function getGearArray(n,angle,outerRadius,innerRadius){
    // 准备齿轮数组
    var gearArr=new Array();

    for(var i=0;i<n*2;i++){
        var alpha=Math.PI/n*i;
        var bata=alpha+angle;
        var x1,y1,x2,y2;
        
        if(i%2==1){
            x1=innerRadius*Math.cos(alpha);
            y1=innerRadius*Math.sin(alpha);

            x2=outerRadius*Math.cos(bata);
            y2=outerRadius*Math.sin(bata);                
        }else{                
            x1=outerRadius*Math.cos(alpha);
            y1=outerRadius*Math.sin(alpha);

            x2=innerRadius*Math.cos(bata);
            y2=innerRadius*Math.sin(bata);
        }

        gearArr.push({x:x1,y:y1});
        gearArr.push({x:x2,y:y2});
    }

    return gearArr;
}

// 画实心五角星的函数
function draw5Star(ctx,x,y,r,color){
    ctx.save()
    ctx.translate(x-r,y-r);    
    ctx.beginPath();
    ctx.moveTo(r, 0);
    ctx.lineTo(r+Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
    ctx.lineTo(r-Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
    ctx.lineTo(r+Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
    ctx.lineTo(r-Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
    ctx.lineTo(r, 0);
    ctx.closePath();
    ctx.fillStyle=color;
    ctx.fill();
    ctx.restore();
}


/*---------------------------------------------
内政外交无往不利
经济建设欣欣向荣
社会繁荣一片祥和
科技发展遥遥领先
就业形势供需两旺
全国人民富得流油
以上拒绝一切质疑
----------------------------------------------*/
//-->
</script>

END

  • 24
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值