JavaScript动画实例:李萨如曲线
<p> 在“JavaScript图形实例:阿基米德螺线<span style="font-size: 14px">”和“</span>JavaScript图形实例:曲线方程<span style="font-size: 14px">”中,我们学习了利用曲线的方程绘制曲线的方法。如果想看看曲线是怎样绘制出来的,怎么办呢?编写简单的动画,就可以展示指定曲线的绘制过程。</span></p><h3>1.李萨如曲线</h3>
<p> 设定李萨如曲线的坐标方程为:</p>
<p> X=SIN(2θ)</p>
<p> Y=SIN(3θ) (0≤θ≤2π)</p>
<p> <span style="font-family: "times new roman", times"> 将0~2π区间等分512段,取θ的初始值π/256,按曲线方程求得坐标值(x,y),并在当前坐标处通过绘制一个实心圆的方式描点。之后每隔0.02秒,将θ的初始值加π/256后,按曲线方程求得新坐标值(x,y),并在求得的新坐标处再通过绘制一个实心圆的方式继续描点,这样,可以得到动态绘制的李萨如曲线。曲线绘制完成(即θ的值为2π),将画布清除,令θ重新从初值π/256开始继续动画过程。</span></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"> var context;</p>
<p style="margin-left: 30px"> var i;</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"> context = canvas.getContext('2d');</p>
<p style="margin-left: 30px"> context.fillStyle="#EEEEFF";</p>
<p style="margin-left: 30px"> context.fillRect(0,0,300,300);</p>
<p style="margin-left: 30px"> i=1;</p>
<p style="margin-left: 30px"> setInterval(go,20); </p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> function go()</p>
<p style="margin-left: 30px"> { </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/256;</p>
<p style="margin-left: 30px"> x=150+100*Math.sin(2*i*dig);</p>
<p style="margin-left: 30px"> y=150-100*Math.sin(3*i*dig);</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(x, y, 3, 0, 2 * Math.PI);</p>
<p style="margin-left: 30px"> context.fillStyle = "red";</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> i=i+1;</p>
<p style="margin-left: 30px"> if (i>512)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> i=1;</p>
<p style="margin-left: 30px"> context.clearRect(0,0,300,300);</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" 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文本文件中,再在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中呈现出李萨如曲线的动态绘制过程,如图1所示。</p>
<p align="center"><img src="https://img2018.cnblogs.com/common/1485495/201912/1485495-20191226075402891-236963613.gif"> </p>
<p align="center">图1 李萨如曲线</p>
<h3>2.阿基米德螺线</h3>
<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"> var context;</p>
<p style="margin-left: 30px"> var i;</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"> context = canvas.getContext('2d');</p>
<p style="margin-left: 30px"> context.fillStyle="#EEEEFF";</p>
<p style="margin-left: 30px"> context.fillRect(0,0,300,300);</p>
<p style="margin-left: 30px"> i=1;</p>
<p style="margin-left: 30px"> setInterval(go,20); </p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> function go()</p>
<p style="margin-left: 30px"> { </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/128;</p>
<p style="margin-left: 30px"> x=150+5*i*dig*Math.sin(i*dig);</p>
<p style="margin-left: 30px"> y=150+5*i*dig*Math.cos(i*dig);</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(x, y, 3, 0, 2 * Math.PI);</p>
<p style="margin-left: 30px"> context.fillStyle = "red";</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> i=i+1;</p>
<p style="margin-left: 30px"> if (i>1024)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> i=1;</p>
<p style="margin-left: 30px"> context.clearRect(0,0,300,300);</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" 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文本文件中,再在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中呈现出阿基米德螺线的动态绘制过程,如图2所示。</p>
<p align="center"><img src="https://img2018.cnblogs.com/common/1485495/201912/1485495-20191226075428155-133373679.gif"> </p>
<p align="center">图2 阿基米德螺线</p>
<h3>3.螺旋线</h3>
<p style="margin-left: 30px">设定螺旋线的坐标方程为:</p>
<p style="margin-left: 30px"> X=30*COS(θ)+70*COS(θ/4)</p>
<p style="margin-left: 30px"> Y=30*SIN(θ) - 70*SIN(θ/4) (0≤θ≤8π)</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"> var context;</p>
<p style="margin-left: 30px"> var i;</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"> context = canvas.getContext('2d');</p>
<p style="margin-left: 30px"> context.fillStyle="#EEEEFF";</p>
<p style="margin-left: 30px"> context.fillRect(0,0,300,300);</p>
<p style="margin-left: 30px"> i=1;</p>
<p style="margin-left: 30px"> setInterval(go,20); </p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> function go()</p>
<p style="margin-left: 30px"> { </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 x = 150+30*Math.cos(i*Math.PI/72) +70*Math.cos(i/4*Math.PI/72);</p>
<p style="margin-left: 30px"> var y = 150+30*Math.sin(i*Math.PI/72) -70*Math.sin(i/4*Math.PI/72);</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(x, y, 3, 0, 2 * Math.PI);</p>
<p style="margin-left: 30px"> context.fillStyle = "red";</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> i=i+1;</p>
<p style="margin-left: 30px"> if (i>576)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> i=1;</p>
<p style="margin-left: 30px"> context.clearRect(0,0,300,300);</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" style="border:3px double #996633;"></canvas></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以在浏览器窗口中看到螺旋线绘制动画,如图3所示。</p>
<p align="center"> <img src="https://img2018.cnblogs.com/common/1485495/201912/1485495-20191226075452721-684876125.gif"></p>
<p align="center">图3 螺旋线绘制</p>
<h3>4.七彩花瓣</h3>
<p style="margin-left: 30px">设定四叶花瓣线的坐标方程为:</p>
<p style="margin-left: 30px"> X=R*COS(2θ)*SIN(θ)</p>
<p style="margin-left: 30px"> Y= R*COS(2θ)*COS(θ) (0≤θ≤2π)</p>
<p style="margin-left: 30px">按这个方程,可以绘制如图4所示的四叶花瓣线。</p>
<p align="center"><img src="https://img2018.cnblogs.com/i-beta/1485495/201912/1485495-20191226075515361-2001237860.png"> </p>
<p align="center">图4 四叶花瓣线</p>
<p> 将四叶花瓣线的绘制过程进行动态展示,并且每绘制一个新点,填充颜色在7中颜色间进行切换,这样绘制出七彩花瓣。编写HTML文件如下。</p>
<p style="margin-left: 30px"><!DOCTYPE></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"></head></p>
<p style="margin-left: 30px"><body ></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="400" height="300" style="border:3px double #996633;"></p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"><script></p>
<p style="margin-left: 30px"> var canvas = document.getElementById('myCanvas');</p>
<p style="margin-left: 30px"> var context = canvas.getContext('2d');</p>
<p style="margin-left: 30px"> var i=0;j=0.05,t=0;</p>
<p style="margin-left: 30px"> var colors=new Array('red','orange','yellow','green','cyan','blue','purple');</p>
<p style="margin-left: 30px"> function timing() {</p>
<p style="margin-left: 30px"> t=t+1;</p>
<p style="margin-left: 30px"> i=i+j;</p>
<p style="margin-left: 30px"> var r=120*Math.cos(2*i);</p>
<p style="margin-left: 30px"> if(t>6){t=0;}</p>
<p style="margin-left: 30px"> var x=200+r*Math.sin(i);</p>
<p style="margin-left: 30px"> var y=150+r*Math.cos(i);</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(x,y,5,0,2*Math.PI);</p>
<p style="margin-left: 30px"> context.fillStyle=colors;</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> if(i>6.3){j=-0.05;context.clearRect(0,0,400,300);}</p>
<p style="margin-left: 30px"> if(i<0){j=0.05;context.clearRect(0,0,400,300);}</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> window.setInterval('timing()',200);</p>
<p style="margin-left: 30px"></script></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-20191226075546303-1853817270.gif"></p>
<p align="center">图5 七彩花瓣</p><br><br>
来源:https://www.cnblogs.com/cs-whut/p/12100116.html
頁:
[1]