JavaScript图形实例:正弦曲线
<p> 正弦曲线的坐标方程为:</p><p> Y=A*SIN(X) (A为振幅)</p>
<h3>1.正弦曲线</h3>
<p> 在弧度为0~4π的正弦曲线上取360个点,将这些点用线连接起来,可以绘制出正弦曲线。编写如下的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 dig=Math.PI/90;</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> for (var x=0;x<360;x++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> y=150-120*Math.sin(x*dig);</p>
<p style="margin-left: 30px"> if (x==0)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.moveTo(x,y);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> else</p>
<p style="margin-left: 30px"> context.lineTo(x,y);</p>
<p style="margin-left: 30px"> }</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="400" height="300"></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/common/1485495/201912/1485495-20191224164247440-1744624307.png"> </p>
<p align="center">图1 正弦曲线</p>
<p> 若将上述文件中的语句“y=150-120*Math.sin(x*dig);”改为“y=150-120*Math.cos(x*dig);”,可以绘制出如图2所示的余弦曲线。</p>
<p align="center"> <img src="https://img2018.cnblogs.com/common/1485495/201912/1485495-20191224164310936-1184990231.png"></p>
<p align="center">图2 余弦曲线</p>
<h3>2.正弦波</h3>
<p> 适当减小图1中正弦函数的振幅,并且用循环绘制多条在Y轴方向上向下平移若干单位的正弦曲线,可以绘制出正弦波形图案。编写如下的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 dig=Math.PI/90;</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> for (py=40;py<=200;py+=5)</p>
<p style="margin-left: 30px"> for (var x=0;x<360;x++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> y=py-15*Math.sin(x*dig-dig*(py-40)/2); // 正弦函数的相位进行变化</p>
<p style="margin-left: 30px"> if (x==0)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.moveTo(x,y);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> else</p>
<p style="margin-left: 30px"> context.lineTo(x,y);</p>
<p style="margin-left: 30px"> }</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="400" height="300"></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/common/1485495/201912/1485495-20191224164357635-327706725.png"></p>
<p align="center">图3 正弦波形</p>
<h3>3.合成正弦波形</h3>
<p> 若将正弦函数进行合成,例如取 Y=A*SIN(3X)*SIN(X),可以绘制出合成正弦波图案。</p>
<p style="margin-left: 30px">编写的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 dig=Math.PI/90;</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> for (py=40;py<=200;py+=5)</p>
<p style="margin-left: 30px"> for (var x=0;x<360;x++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> y=py-15*Math.sin(3*x*dig)*Math.sin(x*dig-dig*(py-40)/2);</p>
<p style="margin-left: 30px"> if (x==0)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.moveTo(x,y);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> else</p>
<p style="margin-left: 30px"> context.lineTo(x,y);</p>
<p style="margin-left: 30px"> }</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="400" height="300"></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/common/1485495/201912/1485495-20191224164435969-378509025.png"></p>
<p align="center">图4 合成正弦波</p>
<h3>4.端点按三角函数规律变化的线段</h3>
<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="#EEEEFF";</p>
<p style="margin-left: 30px"> context.fillRect(0,0,400,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/90;</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.moveTo(0,150);</p>
<p style="margin-left: 30px"> for (var i=0;i<360;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> y=150-120*Math.sin(i*dig);</p>
<p style="margin-left: 30px"> context.lineTo(i,y);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> context.stroke();</p>
<p style="margin-left: 30px"> context.strokeStyle="red";</p>
<p style="margin-left: 30px"> context.lineWidth=2;</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> for (var i=0;i<360;i+=5)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x=150-120*Math.sin(i*dig);</p>
<p style="margin-left: 30px"> y=150-120*Math.cos(i*dig);</p>
<p style="margin-left: 30px"> context.moveTo(i,x);</p>
<p style="margin-left: 30px"> context.lineTo(i,y);</p>
<p style="margin-left: 30px"> }</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="400" height="300"></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/common/1485495/201912/1485495-20191224164519324-1067778154.png"> </p>
<p align="center">图5 端点按三角函数规律变化的线段</p>
<p> 将图5中绘制的线段按规律进行着色,并适当改变线段端点的三角函数计算方法。编写如下的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 colors = ['red','orange', 'yellow', 'green', 'cyan','blue', 'purple' ];</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,600,200);</p>
<p style="margin-left: 30px"> context.lineWidth=3;</p>
<p style="margin-left: 30px"> var dig=Math.PI/150</p>
<p style="margin-left: 30px"> for (var i=0;i<600;i+=4)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> y1=100-80*Math.cos(i*dig);</p>
<p style="margin-left: 30px"> y2=120-75*Math.sin(i*dig-Math.PI/2);</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.moveTo(i,y1);</p>
<p style="margin-left: 30px"> context.lineTo(i+20,y2);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.strokeStyle=colors[(i/4)%7];</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="600" height="200"></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/common/1485495/201912/1485495-20191224164548653-197930614.png"> </p>
<p align="center">图6 彩带</p>
<p> 继续仿照图5线段图形绘制的方法,我们适当构造线段端点位置计算的三角函数,可以绘制出飘逸的丝带图案。编写如下的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=1;</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> for (i=0;i<=180;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> a=i*Math.PI/360;</p>
<p style="margin-left: 30px"> x1=200+180*Math.cos(1.5*a);</p>
<p style="margin-left: 30px"> x2=200+180*Math.cos(2*a);</p>
<p style="margin-left: 30px"> y1=150+120*Math.sin(7*a)*Math.cos(a/2.5);</p>
<p style="margin-left: 30px"> y2=y1;</p>
<p style="margin-left: 30px"> context.moveTo(x1,y1);</p>
<p style="margin-left: 30px"> context.lineTo(x2,y2);</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="400" height="300"></canvas></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出如图7所示的丝带图案1。</p>
<p align="center"><img src="https://img2018.cnblogs.com/common/1485495/201912/1485495-20191224164622686-58300848.png"> </p>
<p align="center">图7 丝带图案1</p>
<p> 改变绘制图7的HTML文件中的线段端点计算函数,编写如下的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=1;</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> for (i=0;i<=300;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> a=i*Math.PI/120;</p>
<p style="margin-left: 30px"> x1=200+180*Math.cos(0.5*a);</p>
<p style="margin-left: 30px"> x2=200+180*Math.cos(0.5*a+Math.PI/4);</p>
<p style="margin-left: 30px"> y1=150+(50-80*Math.sin(2.5*a))*Math.cos(a/2.5);</p>
<p style="margin-left: 30px"> y2=150+(50-80*Math.sin(2.5*a-Math.PI/4))*Math.cos(a/2.5);</p>
<p style="margin-left: 30px"> context.moveTo(x1,y1);</p>
<p style="margin-left: 30px"> context.lineTo(x2,y2);</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="400" height="300"></canvas></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出如图8所示的丝带图案2。</p>
<p align="center"><img src="https://img2018.cnblogs.com/common/1485495/201912/1485495-20191224164654978-1955538162.png"> </p>
<p align="center">图8 丝带图案2 </p><br><br>
来源:https://www.cnblogs.com/cs-whut/p/12092348.html
頁:
[1]