【关键点】
使用贝塞尔二次曲线生成环状波纹轮廓。
【成果图】
【代码】
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<title>蓝色波纹铜质Best Product Guaranteed徽章</title>
<style type="text/css">
.centerlize{
margin:0 auto;
width:1200px;
}
</style>
</head>
<body onload="init();">
<div class="centerlize">
<canvas id="myCanvas" width="512px" height="512px" style="border:1px dotted black;">
如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.
</canvas>
<img id="myImg" src="115.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);// 清屏
// 波纹形外圈
arr=createWaveCircleArray(72,240,10);
ctx.beginPath();
for(var i=0;i<arr.length-2;i+=2){
var pt1=arr[i];
var pt2=arr[i+1];// 控制点
var pt3=arr[i+2];
ctx.lineTo(pt1.x,pt1.y);
ctx.quadraticCurveTo(pt2.x,pt2.y,pt3.x,pt3.y);
}
ctx.closePath();
ctx.lineWidth=4;
ctx.strokeStyle="rgb(201,195,183)";
ctx.stroke();
ctx.fillStyle="rgb(14,86,108)";
ctx.fill();
// 用圆去切取背景图成黄铜底
ctx.save();
ctx.beginPath();
ctx.arc(0,0,210,0,Math.PI*2,true);
ctx.closePath();
ctx.lineWidth=4;
var gnt=ctx.createLinearGradient(0,-170,0,170);
gnt.addColorStop(0,"rgb(224,174,77)");
gnt.addColorStop(1,"rgb(85,43,5)");
ctx.strokeStyle=gnt;
ctx.stroke();
ctx.clip();
var img=document.getElementById("myImg");
ctx.drawImage(img,0,0,625,625,-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
ctx.restore();
// 一圈五角星
var radius=190;
for(var i=0;i<36;i++){
var theta=Math.PI/18*i;
var x=radius*Math.cos(theta);
var y=radius*Math.sin(theta);
ctx.save();
ctx.translate(x,y);
ctx.rotate(theta-Math.PI/2);
draw5Star(ctx,0,0,8,"rgb(14,86,108)");
ctx.restore();
}
// 渐变内圈
ctx.beginPath();
ctx.arc(0,0,170,0,Math.PI*2,true);
ctx.closePath();
ctx.lineWidth=4;
var gnt=ctx.createLinearGradient(0,-170,0,170);
gnt.addColorStop(0,"rgb(85,43,5)");
gnt.addColorStop(1,"rgb(224,174,77)");
ctx.strokeStyle=gnt;
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();
// Best Product Guaranteed
ctx.textBaseline="bottom";
ctx.textAlign="center";
ctx.font = "60px DejaVu Math TeX Gyre";
ctx.fillStyle="black";
ctx.fillText("BEST",0,-40);
ctx.fillText("PRODUCT",0,35);
ctx.font = "50px 方正宋刻本秀楷简体";
ctx.fillText("guaranteed",0,90);
// 作者
ctx.font = "8px consolas";
ctx.fillStyle="black";
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>
【底图115.jpg】
END