【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="124.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);// 清屏	

		// 底色
        var lgrd=ctx.createLinearGradient(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
        lgrd.addColorStop(0,"rgba(168,159,154,0.8)");
		lgrd.addColorStop(0.28,"rgba(7,12,41,0.8)");
        lgrd.addColorStop(1,"rgba(7,12,41,0.8)");
		ctx.fillStyle=lgrd;
		ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);

		// 斜向条纹
		ctx.save();
		ctx.rotate(Math.PI/6);
		for(var i=0;i<100;i++){
			var y=-HEIGHT+i*10;
			ctx.beginPath();// 不加此句线条粗劣
			ctx.moveTo(-WIDTH,y);
			ctx.lineTo(WIDTH,y);
			ctx.lineWidth=0.2;
			var lgrd=ctx.createLinearGradient(-WIDTH,y,WIDTH,y);
			lgrd.addColorStop(0,"rgb(250,250,250)");
			lgrd.addColorStop(1,"rgb(7,12,41)");
			ctx.strokeStyle=lgrd;
			ctx.stroke();
		}
		ctx.restore();

		// 外缘
		var arr=createWaveCircleArray(48,141,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.strokeStyle="rgb(231,170,79)";
		ctx.stroke();

		// 铜底图
		ctx.save();
		var arr=createWaveCircleArray(48,140,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.clip();
		var img=document.getElementById("myImg");
		ctx.drawImage(img,0,0,320,320,-160,-160,320,320);
		ctx.restore();

		// 暗圈
		ctx.beginPath();
		ctx.arc(0,0,122,0,Math.PI*2,true);
		ctx.closePath();
		ctx.lineWidth=1;
		ctx.strokeStyle="rgb(229,174,94)";
		ctx.stroke();

		// 蓝圈
		ctx.beginPath();
		ctx.arc(0,0,120,0,Math.PI*2,true);
		ctx.closePath();
		ctx.lineWidth=2;
		ctx.strokeStyle="rgb(19,28,57)";
		ctx.stroke();

		// 亮圈
		ctx.beginPath();
		ctx.arc(0,0,118,0,Math.PI*2,true);
		ctx.closePath();
		ctx.lineWidth=1;
		ctx.strokeStyle="rgb(231,170,79)";
		ctx.stroke();

		// 上半圈文字
		var words="PREMIUM QUALITY";
		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*34;
            var x=84*Math.cos(theta);
            var y=84*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 = "20px Stencil Std";
            ctx.fillStyle="rgb(19,28,57)";
            ctx.fillText(letter,0,0);
            ctx.restore();
        }


		// 下半圈文字
		var words="PREMIUM QUALITY";
		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*28;
            var x=113*Math.cos(theta);
            var y=113*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 = "20px Stencil Std";
            ctx.fillStyle="rgb(19,28,57)";
            ctx.fillText(letter,0,0);
            ctx.restore();
        }

		draw5Star(ctx,-100,0,13,"rgb(19,28,57)");
		draw5Star(ctx, 100,0,13,"rgb(19,28,57)");


		// 蓝圈
		ctx.beginPath();
		ctx.arc(0,0,80,0,Math.PI*2,true);
		ctx.closePath();
		ctx.lineWidth=1;
		ctx.strokeStyle="rgb(19,28,57)";
		ctx.stroke();

		// 亮圈
		ctx.beginPath();
		ctx.arc(0,0,80.5,0,Math.PI*2,true);
		ctx.closePath();
		ctx.lineWidth=0.5;
		ctx.strokeStyle="rgb(231,170,79)";
		ctx.stroke();

		// 中心文字
		ctx.textBaseline="bottom";
		ctx.textAlign="center";		
		ctx.fillStyle="rgb(19,28,57)";	
		ctx.font = "36px Ink Free";// 手写字体
		ctx.fillText("BEST",0,-10);

		// 加两根俏皮的横线
		ctx.beginPath();
		ctx.moveTo(-40,-10);
		ctx.lineTo(40,-15);
		ctx.lineWidth=0.8;
		ctx.strokeStyle="rgb(19,28,57)";
		ctx.stroke();
		ctx.beginPath();
		ctx.moveTo(-32,-8);
		ctx.lineTo(38,-11);
		ctx.lineWidth=0.5;
		ctx.strokeStyle="rgb(19,28,57)";
		ctx.stroke();

		// 正规字体
		ctx.font = "36px Bahnschrift SemiBold";
		ctx.fillText("CHOICE",0,40);

		// 作者
		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>

【底图】

上文用到的底图 124.jpg

END

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值