【Canvas与艺术】绘制灰白黑鱼鳞纹“Premium Quality”标志

【关键点】

环状鱼鳞纹的制作

【成果图】

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>灰白黑鱼鳞纹Premium Quality标志</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.theta+=Math.PI/180;
    }

    // 画背景
    this.paintBg=function(ctx){
        ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏    
        
        // 径向渐变背景
        var rgnt=ctx.createRadialGradient(0,0,0,0,0,256);
        rgnt.addColorStop(0,"rgb(80,80,80)");
        rgnt.addColorStop(1,"rgb(40,40,40)");
        ctx.fillStyle=rgnt;
        ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);

        // 外缘
        var arr=createWaveCircleArray(96,180,12);
        ctx.beginPath();
        for(var j=0;j<arr.length-2;j+=2){
            var pt1=arr[j];
            var pt2=arr[j+1];// 控制点
            var pt3=arr[j+2];            
            ctx.lineTo(pt1.x,pt1.y);
            ctx.quadraticCurveTo(pt2.x,pt2.y,pt3.x,pt3.y);
        }
        ctx.closePath();
        ctx.fillStyle="white";
        ctx.fill();
    
        // 黑圈
        ctx.beginPath();
        ctx.arc(0,0,164,0,Math.PI*2,false);
        ctx.closePath();
        ctx.lineWidth=2;
        ctx.strokeStyle="black";
        ctx.stroke();

        // 点圈
        for(var i=0;i<180;i++){
            var theta=i*Math.PI/90;
            var x=159*Math.cos(theta);
            var y=159*Math.sin(theta);

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

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

        // 鱼鳞
        for(i=0;i<60;i++){
            var start=i*Math.PI/30;
            var end=start+Math.PI/40;
            var bias=Math.PI/72;
            var r=145;

            var x1=(r+5)*Math.cos(start);
            var y1=(r+5)*Math.sin(start);
            
            var x2=(r+5)*Math.cos(end);
            var y2=(r+5)*Math.sin(end);

            var x3=(r)*Math.cos(end+bias);
            var y3=(r)*Math.sin(end+bias);

            var x4=(r-5)*Math.cos(end);
            var y4=(r-5)*Math.sin(end);
            
            var x5=(r-5)*Math.cos(start);
            var y5=(r-5)*Math.sin(start);

            var x6=(r)*Math.cos(start+bias);
            var y6=(r)*Math.sin(start+bias);

            ctx.beginPath();
            ctx.moveTo(x1,y1);
            ctx.lineTo(x2,y2);
            ctx.lineTo(x3,y3);
            ctx.lineTo(x4,y4);
            ctx.lineTo(x5,y5);
            ctx.lineTo(x6,y6);
            ctx.closePath();
            ctx.fillStyle="black";
            ctx.fill();
        }

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

        // 中心渐变背景
        var rgnt2=ctx.createRadialGradient(0,0,0,0,0,120);
        rgnt2.addColorStop(0,"rgb(130,130,130)");
        rgnt2.addColorStop(1,"rgb(40,40,40)");
        ctx.fillStyle=rgnt;
        ctx.beginPath();
        ctx.arc(0,0,120,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fillStyle=rgnt2;
        ctx.fill();

        // 上方五星
        ctx.beginPath();
        ctx.moveTo(-60,-80);
        ctx.lineTo(-18,-80);
        ctx.closePath();
        ctx.lineWidth=1;
        ctx.strokeStyle="white";
        ctx.stroke();
        draw5Star(ctx,0,-80,10,"white");
        ctx.beginPath();
        ctx.moveTo(60,-80);
        ctx.lineTo(18,-80);
        ctx.closePath();
        ctx.lineWidth=1;
        ctx.strokeStyle="white";
        ctx.stroke();

        // 文字
        ctx.textBaseline="bottom";
        ctx.textAlign="center";
        ctx.font = "42px Bahnschrift SemiBold Condensed";
        ctx.fillStyle="white";
        ctx.fillText("Premium",0,-13);
        ctx.fillText("Quality",0,49);

        // 下方五星
        ctx.beginPath();
        ctx.moveTo(-60,80);
        ctx.lineTo(-18,80);
        ctx.closePath();
        ctx.lineWidth=1;
        ctx.strokeStyle="white";
        ctx.stroke();
        draw5Star(ctx,0,80,10,"white");
        ctx.beginPath();
        ctx.moveTo(60,80);
        ctx.lineTo(18,80);
        ctx.closePath();
        ctx.lineWidth=1;
        ctx.strokeStyle="white";
        ctx.stroke();

        // 版权
        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){
        
        
    }
}

//--------------------------------------------------
// 函数:创建波浪式环形需要的数组
// n:浪头峰谷个数
// radius:环形半径
// waveHeight:浪高
// 返回:包含浪高中低点坐标的数组
//--------------------------------------------------
function createWaveCircleArray(n,radius,waveHeight){
    var arr=[];

    const LEN=n+2;// 数组长度比浪头峰谷数多两个以在绘图时形成闭环    
    for(var i=0;i<LEN;i++){
        var theta=i*Math.PI*2/n;
        var r=radius+Math.sin(Math.PI/2*i)*waveHeight;// 造成涨落
        var pt={};
        pt.x=r*Math.cos(theta);
        pt.y=r*Math.sin(theta);
        arr.push(pt);
    }

    return arr;
}

// 画实心五角星的函数
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

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