【成图】





【代码】
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<title>金波浪圈墨西哥旗 Draft1</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;//基准尺寸
// #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 gnt=ctx.createLinearGradient(r,-r,-r,r);
gnt.addColorStop(0,"rgb(254,251,220)");
gnt.addColorStop(0.25,"rgb(247,232,177)");
gnt.addColorStop(0.5,"rgb(199,176,112)");
gnt.addColorStop(0.75,"rgb(145,77,26)");
gnt.addColorStop(1,"rgb(85,45,15)");
ctx.fillStyle=gnt;
drawWavedCircle(ctx,0,0,r,r*0.1,48);
ctx.fill();
ctx.restore();
// #3 金圆
ctx.save();
var r=R*0.84;
var gnt=ctx.createLinearGradient(r,-r,-r,r);
gnt.addColorStop(0,"rgb(255,254,172)");
gnt.addColorStop(1,"rgb(220,139,42)");
drawSolidCircle(ctx,0,0,r,gnt);
ctx.restore();
// #4 凸金圆
ctx.save();
var r=R*0.83;
var gnt=ctx.createLinearGradient(r,-r,-r,r);
gnt.addColorStop(0,"rgb(229,179,63)");
gnt.addColorStop(1,"rgb(85,45,15)");
drawSolidCircle(ctx,0,0,r,gnt);
ctx.restore();
// #4 平金圆
ctx.save();
var r=R*0.81;
var gnt=ctx.createLinearGradient(r,-r,-r,r);
gnt.addColorStop(0,"rgb(252,248,214)");
gnt.addColorStop(1,"rgb(198,141,48)");
drawSolidCircle(ctx,0,0,r,gnt);
ctx.restore();
// #5 凹金圆
ctx.save();
var r=R*0.62;
var gnt=ctx.createLinearGradient(r,-r,-r,r);
gnt.addColorStop(0,"rgb(95,56,22)");
gnt.addColorStop(1,"rgb(215,166,58)");
drawSolidCircle(ctx,0,0,r,gnt);
ctx.restore();
// #6 内金圆
ctx.save();
var r=R*0.60;
var gnt=ctx.createLinearGradient(r,-r,-r,r);
gnt.addColorStop(0,"rgb(251,249,182)");
gnt.addColorStop(1,"rgb(72,30,15)");
drawSolidCircle(ctx,0,0,r,gnt);
ctx.restore();
// #7 三色
ctx.save();
var r=R*0.59;
ctx.beginPath();
ctx.arc(0,0,r,0,Math.PI*2,false);
ctx.clip();
var w=2*r;
var h=w;
var colors=["rgb(3,114,62)","white","rgb(214,31,43)"];
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();
// #8 玻璃光
ctx.save();
var r=R*0.59;
var center=createPt(0,0);
var a=createPt2(center.x,center.y,r,Math.PI/4);
var b=createPt2(center.x,center.y,r,Math.PI/4*5);
var c=createPt2(center.x,center.y,r,-Math.PI/4);
var gnt=ctx.createLinearGradient(c.x,c.y,center.x,center.y);
gnt.addColorStop(0,"rgba(255,255,255,0.9)");
gnt.addColorStop(0.5,"rgba(255,255,255,0.4)");
gnt.addColorStop(1,"rgba(255,255,255,0)");
ctx.fillStyle=gnt;
ctx.beginPath();
ctx.moveTo(a.x,a.y);
ctx.arc(center.x,center.y,r,Math.PI/4,Math.PI/4*5,true);
ctx.closePath();
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:矩形中心纵坐标
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();
}
/*----------------------------------------------------------
函数:用于绘制实心圆
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();
}
/*----------------------------------------------------------
函数:创建一个二维坐标点
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