【成图】





【代码】
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head><title>圆角梅花五星旗 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 RATIO=4;
const WIDTH=3*64*RATIO;
const HEIGHT=2*64*RATIO;// 舞台对象
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 = "red";//ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);//ctx.restore();var ct=createPt(0,0);// ct=center// #1 旗面ctx.save(); var w=WIDTH;var h=HEIGHT;drawRoundRect(ctx,ct.x,ct.y,w,h,h/8);ctx.clip();// 限制范围ctx.fillStyle = "rgb(235,24,33)";// 底色drawRect(ctx,ct.x,ct.y,w,h);ctx.fill();var r=h/4;ct=createPt(0,r*(1-Math.cos(Math.PI/5))/2);drawMeihuaIcon(ctx,ct.x,ct.y,r,Math.PI/10);// 梅花ctx.restore();// #2 玻璃光ctx.save(); ct=createPt(0,0);w-=2*h/100;h-=2*h/100;drawRoundRect(ctx,ct.x,ct.y,w,h,h*0.118);ctx.clip();var top=createPt2(ct.x,ct.y,h/2,-Math.PI/2);var bottom=createPt2(ct.x,ct.y,h/2,Math.PI/2);var gnt=ctx.createLinearGradient(top.x,top.y,bottom.x,bottom.y);gnt.addColorStop(0,"rgba(255,255,255,0.4)");gnt.addColorStop(0.1,"rgba(255,255,255,0.3)");gnt.addColorStop(0.2,"rgba(255,255,255,0.2)");gnt.addColorStop(0.3,"rgba(255,255,255,0.1)");gnt.addColorStop(0.4,"rgba(255,255,255,0.0)");gnt.addColorStop(1,"rgba(255,255,255,0.0)");ctx.fillStyle=gnt;drawRect(ctx,ct.x,ct.y,w,h);ctx.fill();ctx.restore();writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权}
}/*----------------------------------------------------------
函数:用于绘制完整梅花标志,依赖drawMeihua函数
ctx:绘图上下文
x:轮廓中心横坐标
y:轮廓中心纵坐标
R:梅花中正五边形外接圆半径
initAngle:梅花初始旋转角
----------------------------------------------------------*/
function drawMeihuaIcon(ctx,x,y,R,initAngle){const ct=createPt(x,y);//#1 白边ctx.save();var r=R*1.00;ctx.fillStyle="white";drawMeihua(ctx,ct.x,ct.y,r,Math.PI/10);ctx.fill();ctx.restore();//#2 蓝底ctx.save();var ratio=0.85;var BLUE="rgb(61,71,203)";for(var i=0;i<5;i++){var theta=i*Math.PI*2/5+initAngle; var p=createPt2(ct.x,ct.y,R*Math.cos(Math.PI/5),theta+Math.PI/5);var radius=R*Math.sin(Math.PI/5);drawSolidCircle(ctx,p.x,p.y,radius*ratio,BLUE);}drawSolidCircle(ctx,ct.x,ct.y,radius*ratio,BLUE);ctx.restore();//#3 花蕊ctx.save();var r=R*Math.cos(Math.PI/5);for(var i=0;i<5;i++){var theta=i*Math.PI*2/5-initAngle; var a=createPt2(ct.x,ct.y,r,theta);var b=createPt2(ct.x,ct.y,r,theta+Math.PI*2/5);var xOffset=(b.x-a.x)/4;var yOffset=(b.y-a.y)/4;for(var j=0;j<4;j++){var p=createPt(a.x+j*xOffset,a.y+j*yOffset);ctx.strokeStyle="white";ctx.lineWidth=r*0.02;ctx.beginPath();ctx.moveTo(ct.x,ct.y);ctx.lineTo(p.x,p.y);ctx.stroke();if(j==0){ctx.fillStyle="rgb(250,238,38)";draw5Star(ctx,p.x,p.y,r*0.13);ctx.fill();}else{drawSolidCircle(ctx,p.x,p.y,r*0.05,"white");}}}ctx.restore();//#4 花心ctx.save();var r=R*0.1;drawSolidCircle(ctx,ct.x,ct.y,r,"white");ctx.restore();
}/*----------------------------------------------------------
函数:用于绘制梅花轮廓
ctx:绘图上下文
x:轮廓中心横坐标
y:轮廓中心纵坐标
R:梅花中正五边形外接圆半径
initAngle:梅花初始旋转角
----------------------------------------------------------*/
function drawMeihua(ctx,x,y,R,initAngle){const ct=createPt(x,y);ctx.beginPath();for(var i=0;i<5;i++){var theta=i*Math.PI*2/5+initAngle; var p=createPt2(ct.x,ct.y,R*Math.cos(Math.PI/5),theta+Math.PI/5);var radius=R*Math.sin(Math.PI/5);ctx.arc(p.x,p.y,radius,theta+Math.PI/5-Math.PI/2,theta+Math.PI/5+Math.PI/2,false);}
}/*--------------------------------------------------
基本函数:绘制标准正五角星轮廓,可描边,可填充
ctx:绘图上下文
x:五角星中心横坐标
y:五角星中心纵坐标
R:五角星中心到顶点的距离
---------------------------------------------------*/
function draw5Star(ctx,x,y,R){var r=R*Math.sin(Math.PI/10)/Math.sin(Math.PI/10*7);var arr=[0,0,0,0,0,0,0,0,0,0];// 顶五点for(var i=0;i<5;i++){var theta=i*Math.PI/5*2-Math.PI/10;var x1=R*Math.cos(theta)+x;var y1=R*Math.sin(theta)+y;arr[i*2]=createPt(x1,y1);}// 内五点for(var i=0;i<5;i++){var theta=i*Math.PI/5*2+Math.PI/10;var x1=r*Math.cos(theta)+x;var y1=r*Math.sin(theta)+y;arr[i*2+1]=createPt(x1,y1);}ctx.beginPath();for(var i=0;i<arr.length;i++){ctx.lineTo(arr[i].x,arr[i].y);}ctx.closePath();
}/*----------------------------------------------------------
基本函数:用于绘制圆角矩形
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
width:矩形宽
height:矩形高
radius:圆角半径
----------------------------------------------------------*/
function drawRoundRect(ctx,x,y,width,height,radius){ctx.beginPath();ctx.moveTo(x-width/2+radius,y-height/2);ctx.lineTo(x+width/2-radius,y-height/2);ctx.arcTo(x+width/2,y-height/2,x+width/2,y-height/2+radius,radius);ctx.lineTo(x+width/2,y-height/2+radius);ctx.lineTo(x+width/2,y+height/2-radius);ctx.arcTo(x+width/2,y+height/2,x+width/2-radius,y+height/2,radius);ctx.lineTo(x+width/2-radius,y+height/2);ctx.lineTo(x-width/2+radius,y+height/2);ctx.arcTo(x-width/2,y+height/2,x-width/2,y+height/2-radius,radius);ctx.lineTo(x-width/2,y+height/2-radius);ctx.lineTo(x-width/2,y-height/2+radius);ctx.arcTo(x-width/2,y-height/2,x-width/2+radius,y-height/2,radius);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