【成图】





【代码】
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head><title>1088.金波浪圈法兰西国旗 Draft2</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></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){//sleep(100);window.requestAnimationFrame(animate); }
}// 舞台类
function Stage(){// 初始化this.init=function(){}// 更新this.update=function(){ }// 画背景this.paintBg=function(ctx){ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏 }// 画前景this.paintFg=function(ctx){// 底色/*ctx.save();ctx.fillStyle = "white";ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);ctx.restore();*/const R=220;//基准尺寸var ct=createPt(0,0);// ct=center// #1 波浪形外圈ctx.save(); var r=R*1.00;ctx.fillStyle="rgb(202,147,51)";drawWavedCircle(ctx,0,0,r,r*0.1,48);ctx.fill();ctx.restore();// #2 波浪形内圈ctx.save(); var r=R*0.99;var a=createPt2(ct.x,ct.y,r,-Math.PI/3);var b=createPt2(ct.x,ct.y,r,Math.PI/3*2);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y); gnt.addColorStop(0,"rgb(199,176,112)");gnt.addColorStop(0.25,"rgb(247,232,177)");gnt.addColorStop(0.5,"rgb(254,251,220)");gnt.addColorStop(0.75,"rgb(247,232,177)");gnt.addColorStop(1,"rgb(199,176,112)");ctx.fillStyle=gnt;drawWavedCircle(ctx,0,0,r,r*0.1,48);ctx.fill();ctx.restore();// #3 凸圆内圈ctx.save(); var r=R*0.99;drawWavedCircle(ctx,0,0,r,r*0.1,48);ctx.clip();// 切图var r=R*1.1;// 凸圆var a=createPt2(ct.x,ct.y,r,-Math.PI/3);var b=createPt2(ct.x,ct.y,r,Math.PI/3*2);var c=createPt2(ct.x,ct.y,r,Math.PI/6);var d=createPt2(ct.x,ct.y,(Math.sqrt(2)-1)*r,Math.PI+Math.PI/6);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);gnt.addColorStop(0,"rgb(199,176,112)");gnt.addColorStop(0.25,"rgb(145,77,26)");gnt.addColorStop(0.5,"rgb(85,45,15)");gnt.addColorStop(0.75,"rgb(145,77,26)");gnt.addColorStop(1,"rgb(199,176,112)");ctx.fillStyle=gnt;ctx.beginPath();ctx.arc(ct.x,ct.y,r,-Math.PI/3,Math.PI/3*2,false);ctx.arc(c.x,c.y,r*Math.sqrt(2),Math.PI-Math.PI/12,-Math.PI/2-Math.PI/12,false);ctx.fill();ctx.restore();// #4 金线ctx.save(); var r=R*0.84;var a=createPt2(ct.x,ct.y,r,Math.PI+Math.PI/6);var b=createPt2(ct.x,ct.y,r,Math.PI/6);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y); gnt.addColorStop(0,"rgb(255,246,166)");gnt.addColorStop(1,"rgb(99,60,25)");drawSolidCircle(ctx,0,0,r,gnt);ctx.restore();// #5 凸金圆ctx.save(); var r=R*0.83;var a=createPt2(ct.x,ct.y,r,Math.PI+Math.PI/6);var b=createPt2(ct.x,ct.y,r,Math.PI/6);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y); gnt.addColorStop(0,"rgb(229,179,63)");gnt.addColorStop(1,"rgb(85,45,15)");drawSolidCircle(ctx,0,0,r,gnt);ctx.restore();// #6 平金圆ctx.save(); var r=R*0.80;var a=createPt2(ct.x,ct.y,r,Math.PI+Math.PI/6);var b=createPt2(ct.x,ct.y,r,Math.PI/6);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y); gnt.addColorStop(0,"rgb(253,249,216)");gnt.addColorStop(1,"rgb(198,141,48)");drawSolidCircle(ctx,0,0,r,gnt);ctx.restore();// #7 凹金圆ctx.save(); var r=R*0.70;var a=createPt2(ct.x,ct.y,r,Math.PI+Math.PI/6);var b=createPt2(ct.x,ct.y,r,Math.PI/6);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y); gnt.addColorStop(0,"rgb(88,48,16)");gnt.addColorStop(1,"rgb(225,176,62)");drawSolidCircle(ctx,0,0,r,gnt);ctx.restore();// #8 金线ctx.save(); var r=R*0.67;var a=createPt2(ct.x,ct.y,r,Math.PI+Math.PI/6);var b=createPt2(ct.x,ct.y,r,Math.PI/6);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y); gnt.addColorStop(0,"rgb(99,60,25)");gnt.addColorStop(1,"rgb(255,246,166)");drawSolidCircle(ctx,0,0,r,gnt);ctx.restore();// #9 法国旗ctx.save(); var r=R*0.66;ctx.beginPath();ctx.arc(0,0,r,0,Math.PI*2,false);ctx.clip();var w=2*r;var h=w;var colors=["rgb(4,72,104)","rgb(255,255,255)","rgb(238,6,3)"];// 蓝白红三色var n=colors.length;var w1=w/n;var leftTop=createPt(-w/2,-h/2);for(var i=0;i<n;i++){ctx.fillStyle=colors[i];ctx.fillRect(leftTop.x+i*w1,leftTop.y,w1,h);}ctx.restore();// #10 月形玻璃光ctx.save(); var r=R*0.66;var center=createPt(0,0);var c=createPt2(center.x,center.y,r,Math.PI/6);var b=createPt2(center.x,center.y,r,Math.PI/2+Math.PI/6);var a=createPt2(center.x,center.y,r,-Math.PI/3);var gnt=ctx.createLinearGradient(0,-r,0,b.y);gnt.addColorStop(0,"rgba(255,255,255,0.4)");gnt.addColorStop(0.4,"rgba(255,255,255,0.1)");gnt.addColorStop(1,"rgba(255,255,255,0.0)");ctx.fillStyle=gnt;ctx.beginPath();ctx.moveTo(a.x,a.y);ctx.arc(center.x,center.y,r,-Math.PI/3,Math.PI/2+Math.PI/6,true);ctx.arc(c.x,c.y,r*Math.sqrt(2),Math.PI-Math.PI/12,-Math.PI/2-Math.PI/12,false);ctx.fill();ctx.restore();writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权}
}/*----------------------------------------------------------
函数:用于绘制波浪形圆轮廓
ctx:绘图上下文
x:圆中心横坐标
y:圆中心纵坐标
radius:圆半径
waveHeight:波浪高,一般取半径的几分之一
waveCount:波浪个数,一般取4的整倍数
----------------------------------------------------------*/
function drawWavedCircle(ctx,x,y,radius,waveHeight,waveCount){var arr=[];const n=waveCount;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)+x;pt.y=r*Math.sin(theta)+y;arr.push(pt);}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:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
r:圆半径
style:填充圆的方案
----------------------------------------------------------*/
function drawSolidCircle(ctx,x,y,r,style){ctx.fillStyle=style;ctx.beginPath();ctx.arc(x,y,r,0,Math.PI*2,false);ctx.closePath();ctx.fill();
}/*----------------------------------------------------------
基本函数:用于绘制矩形
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
width:矩形宽
height:矩形高
----------------------------------------------------------*/
function drawRect(ctx,x,y,width,height){ctx.beginPath();ctx.moveTo(x-width/2,y-height/2);ctx.lineTo(x+width/2,y-height/2);ctx.lineTo(x+width/2,y+height/2);ctx.lineTo(x-width/2,y+height/2);ctx.closePath();
}/*----------------------------------------------------------
基本函数:创建一个二维坐标点
baseX:基准点横坐标
baseY:基准点纵坐标
radius:当前点到基准点的距离
theta:当前点到基准点的角度
Pt即Point
----------------------------------------------------------*/
function createPt2(baseX,baseY,radius,theta){var retval={};retval.x=baseX+radius*Math.cos(theta);retval.y=baseY+radius*Math.sin(theta);return retval;
}/*----------------------------------------------------------
基本函数:创建一个二维坐标点
x:横坐标
y:纵坐标
Pt即Point
----------------------------------------------------------*/
function createPt(x,y){var retval={};retval.x=x;retval.y=y;return retval;
}/*----------------------------------------------------------
基本函数:延时若干毫秒
milliseconds:毫秒数
----------------------------------------------------------*/
function sleep(milliSeconds) {const date = Date.now();let currDate = null;while (currDate - date < milliSeconds) {currDate = Date.now();}
}/*----------------------------------------------------------
基本函数:书写文字
ctx:绘图上下文
x:横坐标
y:纵坐标
text:文字
font:字体
color:颜色
----------------------------------------------------------*/
function writeText(ctx,x,y,text,font,color){ctx.save();ctx.textBaseline="bottom";ctx.textAlign="center";ctx.font = font;ctx.fillStyle=color;ctx.fillText(text,x,y);ctx.restore();
}/*-------------------------------------------------------------
用体力赚钱就要老实一点,用脑力赚钱就要灵活一点,
用资金赚钱就要狠心一点,用资源赚钱就要圆滑一点,
用人赚钱就要豁达一点。
--------------------------------------------------------------*/
//-->
</script>
END