JavaScript图形实例:圆形图案
<p> 在HTML5的Canvas 2D API中,可以调用arc方法绘制圆或圆弧。该方法调用格式为:</p><p> context . arc(x, y, radius, startAngle, endAngle, anticlockwise)</p>
<p> 其中:(x,y)为圆心坐标,radius为半径,startAngle和endAngle给定圆弧的开始角度和结束角度,anticlockwise给定方向,为布尔类型,规定应该逆时针还是顺时针绘图。False = 顺时针,true = 逆时针。</p>
<p> 例如,编写如下的HTML文件。</p>
<p style="margin-left: 30px"><!DOCTYPE html></p>
<p style="margin-left: 30px"><html></p>
<p style="margin-left: 30px"><head></p>
<p style="margin-left: 30px"><title>笑脸</title></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> function draw(id)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> var canvas=document.getElementById(id);</p>
<p style="margin-left: 30px"> if (canvas==null)</p>
<p style="margin-left: 30px"> return false;</p>
<p style="margin-left: 30px"> var context=canvas.getContext('2d');</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(75,75,50,0,Math.PI*2,true); // 圆脸</p>
<p style="margin-left: 30px"> context.moveTo(110,75);</p>
<p style="margin-left: 30px"> context.arc(75,75,35,0,Math.PI,false); // 口(顺时针)</p>
<p style="margin-left: 30px"> context.moveTo(65,65);</p>
<p style="margin-left: 30px"> context.arc(60,65,5,0,Math.PI*2,true); // 左眼</p>
<p style="margin-left: 30px"> context.moveTo(95,65);</p>
<p style="margin-left: 30px"> context.arc(90,65,5,0,Math.PI*2,true); // 右眼</p>
<p style="margin-left: 30px"> context.stroke();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></head></p>
<p style="margin-left: 30px"><body onload="draw('myCanvas');"></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="300" height="200" style="border:3px double #996633;"></canvas></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 将上述HTML代码保存到一个html文本文件中,再在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出笑脸图案,如图1所示。</p>
<p align="center"><img src="https://img2018.cnblogs.com/i-beta/1485495/201912/1485495-20191220164114236-193029766.png"></p>
<p align="center">图1 笑脸图案</p>
<p> 再例如,编写如下的HTML文件。</p>
<p style="margin-left: 30px"><!DOCTYPE html></p>
<p style="margin-left: 30px"><head></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> function draw(id)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> var canvas=document.getElementById(id);</p>
<p style="margin-left: 30px"> if (canvas==null)</p>
<p style="margin-left: 30px"> return false;</p>
<p style="margin-left: 30px"> var context=canvas.getContext('2d');</p>
<p style="margin-left: 30px"> context.fillStyle="#EEEEFF";</p>
<p style="margin-left: 30px"> context.fillRect(0,0,400,300);</p>
<p style="margin-left: 30px"> context.lineWidth=2;</p>
<p style="margin-left: 30px"> for (var i=1;i<10;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(i*20,i*20,i*10,0,Math.PI*2,true);</p>
<p style="margin-left: 30px"> context.strokeStyle = 'rgb('+(25*i)+','+(255-25*i)+',255)';</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.stroke();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></head></p>
<p style="margin-left: 30px"><body onload="draw('myCanvas');"></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="400" height="300">您的浏览器不支持canvas!</p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 将上述HTML代码保存到一个html文本文件中,再在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出9个圆形沿直线从小到大抛出的图案,如图2所示。</p>
<p> <img src="https://img2018.cnblogs.com/i-beta/1485495/201912/1485495-20191220164156869-155627222.png"></p>
<p align="center">图2 沿直线从小到大抛出的9个圆</p>
<h3>1.圆周上的圆图案</h3>
<p> 在半径为60的圆周上取36个点作为圆心,绘制36个半径为60的圆。</p>
<p> 可编写如下的HTML代码。</p>
<p style="margin-left: 30px"><!DOCTYPE html></p>
<p style="margin-left: 30px"><head></p>
<p style="margin-left: 30px"><title>圆周上的圆</title></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> function draw(id)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> var canvas=document.getElementById(id);</p>
<p style="margin-left: 30px"> if (canvas==null)</p>
<p style="margin-left: 30px"> return false;</p>
<p style="margin-left: 30px"> var context=canvas.getContext('2d');</p>
<p style="margin-left: 30px"> context.fillStyle="#EEEEDD";</p>
<p style="margin-left: 30px"> context.fillRect(0,0,300,300);</p>
<p style="margin-left: 30px"> context.strokeStyle="blue";</p>
<p style="margin-left: 30px"> context.lineWidth=1;</p>
<p style="margin-left: 30px"> var dig=Math.PI/18;</p>
<p style="margin-left: 30px"> var radius=60;</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> for (var i=0;i<36;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> var x=radius*Math.cos(i*dig)+150;</p>
<p style="margin-left: 30px"> var y=radius*Math.sin(i*dig)+150;</p>
<p style="margin-left: 30px"> context.arc(x,y,radius,0,Math.PI*2,true);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.stroke();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></head></p>
<p style="margin-left: 30px"><body onload="draw('myCanvas');"></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="300" height="300">您的浏览器不支持canvas!</p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 将上述HTML代码保存到一个html文本文件中,再在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出圆周上的圆图案,如图3所示。</p>
<p align="center"><img src="https://img2018.cnblogs.com/i-beta/1485495/201912/1485495-20191220160349982-1485068547.png"> </p>
<p align="center">图3 圆周上的圆图案</p>
<p> 上面绘制的圆的颜色全部采用蓝色,若颜色采用rgb函数计算,可以修改上述HTML文件如下:</p>
<p style="margin-left: 30px"><!DOCTYPE html></p>
<p style="margin-left: 30px"><head></p>
<p style="margin-left: 30px"><title>圆周上的圆(二)</title></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> function draw(id)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> var canvas=document.getElementById(id);</p>
<p style="margin-left: 30px"> if (canvas==null)</p>
<p style="margin-left: 30px"> return false;</p>
<p style="margin-left: 30px"> var context=canvas.getContext('2d');</p>
<p style="margin-left: 30px"> context.fillStyle="#EEEEDD";</p>
<p style="margin-left: 30px"> context.fillRect(0,0,300,300);</p>
<p style="margin-left: 30px"> context.strokeStyle="blue";</p>
<p style="margin-left: 30px"> context.lineWidth=1;</p>
<p style="margin-left: 30px"> var dig=Math.PI/20;</p>
<p style="margin-left: 30px"> var radius=70;</p>
<p style="margin-left: 30px"> for (var i=0;i<40;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> var x=radius*Math.cos(i*dig)+150;</p>
<p style="margin-left: 30px"> var y=radius*Math.sin(i*dig)+150;</p>
<p style="margin-left: 30px"> context.arc(x,y,radius,0,Math.PI*2,true);</p>
<p style="margin-left: 30px"> context.strokeStyle = 'rgb('+(6.25*i)+','+(255-6.25*i)+',255)';</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.stroke();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></head></p>
<p style="margin-left: 30px"><body onload="draw('myCanvas');"></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="300" height="300">您的浏览器不支持canvas!</p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出圆周上的圆图案,如图4所示。</p>
<p align="center"> <img src="https://img2018.cnblogs.com/i-beta/1485495/201912/1485495-20191220160423133-1493715906.png"> </p>
<p align="center">图4 圆周上的圆</p>
<h3>2.圆的叠加</h3>
<p> 随机选定40个圆心坐标,绘制50个半径为40的圆,并用给定颜色进行填充。可编写的HTML代码如下。</p>
<p style="margin-left: 30px"><html></p>
<p style="margin-left: 30px"><head></p>
<p style="margin-left: 30px"><title>圆的叠加</title></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> function draw(id)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> var canvas=document.getElementById(id);</p>
<p style="margin-left: 30px"> var context=canvas.getContext('2d');</p>
<p style="margin-left: 30px"> context.globalCompositeOperation = "lighter";</p>
<p style="margin-left: 30px"> context.fillStyle = "#ff6699";</p>
<p style="margin-left: 30px"> for(var i=0; i<50; i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(Math.random()*400,Math.random()*400, 40, 0,Math.PI*2);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></head></p>
<p style="margin-left: 30px"><body onload="draw('myCanvas');"></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="400" height="400" style="border:3px double #996633;"></p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出圆的叠加图案,如图5所示。</p>
<p align="center"><img src="https://img2018.cnblogs.com/i-beta/1485495/201912/1485495-20191220160507990-759247179.png"> </p>
<p align="center">图5 圆的叠加图案</p>
<h3>3.心脏形图案</h3>
<p> 在半径为60的圆周上选取28个圆心坐标,根据当前圆心坐标的椭圆公式计算出半径,按计算的半径分别绘制28个圆。可编写的HTML代码如下。</p>
<p style="margin-left: 30px"><!DOCTYPE html></p>
<p style="margin-left: 30px"><head></p>
<p style="margin-left: 30px"><title>心脏形图案</title></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> function draw(id)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> var canvas=document.getElementById(id);</p>
<p style="margin-left: 30px"> if (canvas==null)</p>
<p style="margin-left: 30px"> return false;</p>
<p style="margin-left: 30px"> var context=canvas.getContext('2d');</p>
<p style="margin-left: 30px"> context.fillStyle="#EEEEFF";</p>
<p style="margin-left: 30px"> context.fillRect(0,0,400,400);</p>
<p style="margin-left: 30px"> context.strokeStyle="red";</p>
<p style="margin-left: 30px"> context.lineWidth=2;</p>
<p style="margin-left: 30px"> var r1=60;</p>
<p style="margin-left: 30px"> var y1=150-r1;</p>
<p style="margin-left: 30px"> var PI=3.1415926;</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> for(a=0;a<2*PI;a+=PI/27)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x=200+r1*Math.cos(a);</p>
<p style="margin-left: 30px"> y=150+r1*Math.sin(a);</p>
<p style="margin-left: 30px"> rs=Math.sqrt((x-200)*(x-200)+(y-y1)*(y-y1));</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(x,y,rs,0,Math.PI*2,true);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.stroke();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></head></p>
<p style="margin-left: 30px"><body onload="draw('myCanvas');"></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="400" height="400">您的浏览器不支持canvas!</p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出心脏形图案,如图6所示。</p>
<p align="center"> <img src="https://img2018.cnblogs.com/i-beta/1485495/201912/1485495-20191220160550316-341428778.png"> </p>
<p align="center">图6 心脏形图案</p>
<h3>4.肾形图案</h3>
<p> 在半径为80的圆周上选取28个圆心坐标,将当前圆心坐标与画布中心位置的横向距离作为半径分别绘制28个圆。可编写的HTML代码如下。</p>
<p style="margin-left: 30px"><!DOCTYPE html></p>
<p style="margin-left: 30px"><head></p>
<p style="margin-left: 30px"><title>肾形图案</title></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> function draw(id)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> var canvas=document.getElementById(id);</p>
<p style="margin-left: 30px"> if (canvas==null)</p>
<p style="margin-left: 30px"> return false;</p>
<p style="margin-left: 30px"> var context=canvas.getContext('2d');</p>
<p style="margin-left: 30px"> context.fillStyle="#EEEEFF";</p>
<p style="margin-left: 30px"> context.fillRect(0,0,400,300);</p>
<p style="margin-left: 30px"> context.strokeStyle="red";</p>
<p style="margin-left: 30px"> context.lineWidth=2;</p>
<p style="margin-left: 30px"> var r1=80;</p>
<p style="margin-left: 30px"> var PI=3.1415926;</p>
<p style="margin-left: 30px"> for(a=0;a<=2*PI;a+=PI/27)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x=200+r1*Math.cos(a);</p>
<p style="margin-left: 30px"> y=150+r1*Math.sin(a);</p>
<p style="margin-left: 30px"> rs=Math.abs(x-200);</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(x,y,rs,0,Math.PI*2,true);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.stroke();</p>
<p style="margin-left: 30px"> } </p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></head></p>
<p style="margin-left: 30px"><body onload="draw('myCanvas');"></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="400" height="300">您的浏览器不支持canvas!</p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出肾形图案,如图7所示。</p>
<p align="center"><img src="https://img2018.cnblogs.com/i-beta/1485495/201912/1485495-20191220160623609-309793028.png"> </p>
<p align="center">图7 肾形图案 </p><br><br>
来源:https://www.cnblogs.com/cs-whut/p/12073656.html
頁:
[1]