【成圖】





【代碼】
<!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;//基準尺寸var center=createPt(0,0);// #1 底圓ctx.save(); var r=R*1.00;drawSolidCircle(ctx,center.x,center.y,r,"rgb(107,77,60)"); ctx.restore();// #2 右上左下漸變圈ctx.save(); var r=R*0.99;var a=createPt2(center.x,center.y,r,-Math.PI/3);var b=createPt2(center.x,center.y,r,Math.PI/3*2);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);gnt.addColorStop(0,"rgb(251,251,222)");gnt.addColorStop(0.25,"rgb(244,217,136)");gnt.addColorStop(0.5,"rgb(190,137,34)");gnt.addColorStop(0.75,"rgb(102,64,20)");gnt.addColorStop(1,"rgb(107,77,60)");drawSolidCircle(ctx,center.x,center.y,r,gnt);ctx.restore();// #3 凸型漸變圈ctx.save(); var r=R*0.99;var a=createPt2(center.x,center.y,r,-Math.PI/3);var b=createPt2(center.x,center.y,r,Math.PI/3*2);var c=createPt2(center.x,center.y,r,Math.PI/6);var d=createPt2(center.x,center.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(145,115,63)");gnt.addColorStop(0.25,"rgb(196,167,102)");gnt.addColorStop(0.5,"rgb(211,161,73)");gnt.addColorStop(0.75,"rgb(102,64,20)");gnt.addColorStop(1,"rgb(107,77,60)");ctx.fillStyle=gnt;ctx.beginPath();ctx.arc(center.x,center.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.93;var a=createPt2(center.x,center.y,r,-Math.PI/2);var b=createPt2(center.x,center.y,r,Math.PI/2);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);gnt.addColorStop(0,"rgb(183,135,40)");gnt.addColorStop(0.25,"rgb(209,165,92)");gnt.addColorStop(0.5,"rgb(249,219,147)");gnt.addColorStop(0.75,"rgb(247,237,193)");gnt.addColorStop(1,"rgb(250,251,220)");drawSolidCircle(ctx,center.x,center.y,r,gnt);ctx.restore();// #5 上下漸變圈ctx.save(); var r=R*0.90;var a=createPt2(center.x,center.y,r,-Math.PI/2);var b=createPt2(center.x,center.y,r,Math.PI/2);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);gnt.addColorStop(0,"rgb(255,250,217)");gnt.addColorStop(0.25,"rgb(245,238,192)");gnt.addColorStop(0.5,"rgb(242,221,140)");gnt.addColorStop(0.75,"rgb(211,170,84)");gnt.addColorStop(1,"rgb(196,139,53)");drawSolidCircle(ctx,center.x,center.y,r,gnt);ctx.restore();// #6 上部文字“MADE IN CHINA”ctx.save(); var r=R*0.70;var words="MADE IN CHINA";var arr=words.split(""); var N=arr.length;var startAngle=Math.PI/2*3-Math.PI/3.7;var endAngle=Math.PI/2*3+Math.PI/3.7;var part=(endAngle-startAngle)/N;for(var i=0;i<arr.length;i++){var theta=startAngle+part*(i+1);var a=createPt(r*Math.cos(theta),r*Math.sin(theta));ctx.save(); ctx.translate(a.x,a.y); ctx.rotate(theta+Math.PI/2);ctx.scale(1,1.3);writeText(ctx,0,0,arr[i],r*0.2+"px Noto Sans SC Black","rgb(43,23,9)");ctx.restore();}ctx.restore();// #7 下圈文字ctx.save();var r=0.87*R; var words="中國制造";var arr=words.split(""); var N=arr.length;var startAngle=Math.PI/3*2+Math.PI/15;var endAngle=Math.PI/3;var part=(endAngle-startAngle)/N;for(var i=0;i<arr.length;i++){var theta=startAngle+part*(i+1);var a=createPt(r*Math.cos(theta),r*Math.sin(theta));ctx.save(); ctx.translate(a.x,a.y); ctx.rotate(theta+Math.PI/2*3);ctx.scale(1,1.15);writeText(ctx,0,0,arr[i],r*0.175+"px 黑體","rgb(43,23,9)");ctx.restore();}ctx.restore();// #8 上下漸變圈ctx.save(); var r=R*0.67;var a=createPt2(center.x,center.y,r,-Math.PI/2);var b=createPt2(center.x,center.y,r,Math.PI/2);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);gnt.addColorStop(0,"rgb(83,44,9)");gnt.addColorStop(1,"rgb(227,176,61)");drawSolidCircle(ctx,center.x,center.y,r,gnt);ctx.restore();// #9 國旗外圈ctx.save(); var r=R*0.64;drawSolidCircle(ctx,center.x,center.y,r,"rgb(107,77,60)");ctx.restore();// #10 國旗ctx.save(); var r=R*0.63;var a=createPt2(center.x,center.y,r,-Math.PI/2);// 旗面var b=createPt2(center.x,center.y,r,Math.PI/2);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);gnt.addColorStop(0,"rgb(224,58,71)");gnt.addColorStop(0.5,"rgb(169,26,35)");gnt.addColorStop(1,"rgb(101,18,22)");drawSolidCircle(ctx,center.x,center.y,r,gnt);var radius=r*3.5;// 五星var w=radius/30;//小格寬度var h=radius/20;// 小格高度// 諸星屬性數組var arr=[{"x":-10*w,"y":-5*h,"r":3*w,"theta":0},// 大星{"x":-5*w,"y":-8*h,"r":1*w,"theta":Math.atan(5*w/3/h)-Math.PI/10*2},// 最上小星{"x":-3*w,"y":-6*h,"r":1*w,"theta":Math.atan(7*w/1/h)-Math.PI/10*2},// 第2小星{"x":-3*w,"y":-3*h,"r":1*w,"theta":0},// 第3小星{"x":-5*w,"y":-1*h,"r":1*w,"theta":Math.atan(4*w/5/h)+Math.PI/2-Math.PI/10*2},// 第4小星];// 繪制五星for(var i=0;i<arr.length;i++){var star=arr[i];ctx.save();ctx.fillStyle="yellow";ctx.translate(r*0.7*1+star.x,r*0.6*1+star.y);ctx.rotate(star.theta);draw5Star(ctx,0,0,star.r);ctx.fill();ctx.restore();}ctx.restore();// #8 月形玻璃光ctx.save(); var r=R*0.63;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:五角星中心縱坐標
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:矩形中心縱坐標
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