JavaScript实例:运动的小球
<p> 本篇博文通过制作一个小球运动动画的实例,来学习在HTML5的画布上实现动画制作的方法,同时理解面向对象程序设计的基本思想。</p><h3> 1.绘制小球 </h3>
<p> 先在HTML页面中设置一个画布。</p>
<p> <canvas id="myCanvas" width="400" height="300" style="border:3px double #996633;"></p>
<p> </canvas></p>
<p> 再将小球画在canvas(画布)上面。</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"></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 type="text/javascript"></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 x=100, y=100,radius=25;</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(x, y, radius, 0, Math.PI*2, true);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.fillStyle = "blue";</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 其中,变量x、y、radius分别是小球的圆心坐标(x,y)和半径radius。</p>
<p> 将上述HTML代码保存到一个html文本文件中,再在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中出现一个蓝色的小球。这个小球没有什么特别的,实际上是一个简单的圆形,用arc()函数绘制出来的。</p>
<h3> 2.让小球动起来</h3>
<p> 添加一个animation函数,在函数中先用clearRect 清理掉画布里之前的小球圆形,然后重新绘制小球,之后修改小球的圆心坐标。再用window.requestAnimationFrame()控制动画。</p>
<p> 设小球x坐标轴(水平方向)的移动速度为xspeed、y坐标轴(竖直方向)的移动速度为yspeed,则修改小球圆心坐标的方法为:x += xspeed; y +=yspeed; 。</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"></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 type="text/javascript"></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 x=100, y=100, xspeed=5, yspeed=1, radius=25;</p>
<p style="margin-left: 30px"> function animation()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.clearRect(0,0,canvas.width,canvas.height);</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(x, y, radius, 0, Math.PI*2, true);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.fillStyle = "blue";</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> x += xspeed;</p>
<p style="margin-left: 30px"> y += yspeed;</p>
<p style="margin-left: 30px"> window.requestAnimationFrame(animation);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> window.requestAnimationFrame(animation);</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文件,可以看到在浏览器窗口中出现一个蓝色的小球在运动。</p>
<h3> 3.边界碰撞处理</h3>
<p> 在浏览器中打开添加了animation函数的html文件,会发现小球很快会移出画布。这是由于animation函数中没有进行边界碰撞测试,因此,需要在修改小球圆心坐标后,判断小球按新的圆心坐标绘制时是否会超出画布的尺寸,并控制相应的速度量反转(增量变减量或者减量变增量)。修改后的animation函数如下:</p>
<p style="margin-left: 30px"> function animation()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.clearRect(0,0,canvas.width,canvas.height);</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(x, y, radius, 0, Math.PI*2, true);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.fillStyle = "blue";</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> x += xspeed;</p>
<p style="margin-left: 30px"> y += yspeed;</p>
<p style="margin-left: 30px"> if (y+radius>=canvas.height || y-radius<=0)</p>
<p style="margin-left: 30px"> yspeed = -yspeed;</p>
<p style="margin-left: 30px"> if (x+ radius >= canvas.width || x-radius<= 0)</p>
<p style="margin-left: 30px"> xspeed = -xspeed;</p>
<p style="margin-left: 30px"> window.requestAnimationFrame(animation);</p>
<p style="margin-left: 30px"> }</p>
<p> 修改animation函数后,完整的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"></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 type="text/javascript"></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 x=100, y=100, xspeed=5, yspeed=1, radius=25;</p>
<p style="margin-left: 30px"> function animation()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.clearRect(0,0,canvas.width,canvas.height);</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(x, y, radius, 0, Math.PI*2, true);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.fillStyle = "blue";</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> x += xspeed;</p>
<p style="margin-left: 30px"> y += yspeed;</p>
<p style="margin-left: 30px"> if (y+radius>=canvas.height || y-radius<=0)</p>
<p style="margin-left: 30px"> yspeed = -yspeed;</p>
<p style="margin-left: 30px"> if (x+ radius >= canvas.width || x-radius<= 0)</p>
<p style="margin-left: 30px"> xspeed = -xspeed;</p>
<p style="margin-left: 30px"> window.requestAnimationFrame(animation);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> window.requestAnimationFrame(animation);</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文件,可以看到在浏览器窗口中出现一个蓝色的小球在运动,并且小球在碰撞到边界后,会改变方向,这样小球继续在框中运动,不会出界。</p>
<h3> 4.再添加一个运动的小球</h3>
<p> 分别用 var x1=100, y1=100, xspeed1=5, yspeed1=1, radius1=25;</p>
<p> var x2=50, y2=50, xspeed2=3, yspeed2=1, radius2=15;来表示两个小球的数据。</p>
<p> 然后将animation函数中有关小球的绘制和坐标修改语句复制一次并修改相应的变量名即可。</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"></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 type="text/javascript"></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 x1=100, y1=100, xspeed1=5, yspeed1=1, radius1=25;</p>
<p style="margin-left: 30px"> var x2=50, y2=50, xspeed2=3, yspeed2=1, radius2=15;</p>
<p style="margin-left: 30px"> function animation()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.clearRect(0,0,canvas.width,canvas.height);</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(x1, y1, radius1, 0, Math.PI*2, true);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.fillStyle = "blue";</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(x2, y2, radius2, 0, Math.PI*2, true);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.fillStyle = "red";</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> x1 += xspeed1;</p>
<p style="margin-left: 30px"> y1 += yspeed1;</p>
<p style="margin-left: 30px"> if (y1+radius1>=canvas.height || y1-radius1<=0)</p>
<p style="margin-left: 30px"> yspeed1 = -yspeed1;</p>
<p style="margin-left: 30px"> if (x1+ radius1 >= canvas.width || x1-radius1<= 0)</p>
<p style="margin-left: 30px"> xspeed1 = -xspeed1;</p>
<p style="margin-left: 30px"> x2 += xspeed2;</p>
<p style="margin-left: 30px"> y2 += yspeed2;</p>
<p style="margin-left: 30px"> if (y2+radius2>=canvas.height || y2-radius2<=0)</p>
<p style="margin-left: 30px"> yspeed2 = -yspeed2;</p>
<p style="margin-left: 30px"> if (x2+ radius2 >= canvas.width || x2-radius2<= 0)</p>
<p style="margin-left: 30px"> xspeed2 = -xspeed2;</p>
<p style="margin-left: 30px"> window.requestAnimationFrame(animation);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> window.requestAnimationFrame(animation);</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文件,可以看到在浏览器窗口中出现一个蓝色和一个红色的小球在运动。</p>
<h3> 5.定义小球对象。</h3>
<p> 按上面的方法,如果再增加一个或多个小球,相应的代码会复制多次。实际上,小球是一个对象,它包括圆心坐标(x,y)、半径radius、水平方向的移动速度xspeed、竖直方向的移动速度yspeed和小球颜色等属性,以及绘制小球和小球移动一步(指圆心坐标变化)等操作。显然,将小球可以定义为一个对象更合适。</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"></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;"></canvas></p>
<p style="margin-left: 30px"><script type="text/javascript"></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"> // 定义小球对象</p>
<p style="margin-left: 30px"> function Ball(x,y,xspeed,yspeed,radius,color){</p>
<p style="margin-left: 30px"> this.x=x;</p>
<p style="margin-left: 30px"> this.y=y;</p>
<p style="margin-left: 30px"> this.xspeed=xspeed;</p>
<p style="margin-left: 30px"> this.yspeed=yspeed;</p>
<p style="margin-left: 30px"> this.radius=radius;</p>
<p style="margin-left: 30px"> this.color=color;</p>
<p style="margin-left: 30px"> this.draw=function()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(this.x,this.y,this.radius, 0, Math.PI*2, true);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.fillStyle =this.color;</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> this.step=function()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> this.x += this.xspeed;</p>
<p style="margin-left: 30px"> this.y += this.yspeed;</p>
<p style="margin-left: 30px"> if (this.y+this.radius>=canvas.height || this.y-this.radius<=0)</p>
<p style="margin-left: 30px"> this.yspeed = -this.yspeed;</p>
<p style="margin-left: 30px"> if (this.x+ this.radius >= canvas.width || this.x-this.radius<= 0)</p>
<p style="margin-left: 30px"> this.xspeed = -this.xspeed;</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> // 创建三个小球</p>
<p style="margin-left: 30px"> var ball1=new Ball(100,100,5,3,25,"#0000FF");</p>
<p style="margin-left: 30px"> var ball2=new Ball(50, 40,3,1,20,"#FF0000");</p>
<p style="margin-left: 30px"> var ball3=new Ball(30, 30, 3, 2, 15,"#00FFFF");</p>
<p style="margin-left: 30px"> function animation()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.clearRect(0,0,canvas.width,canvas.height);</p>
<p style="margin-left: 30px"> ball1.draw();</p>
<p style="margin-left: 30px"> ball2.draw();</p>
<p style="margin-left: 30px"> ball3.draw();</p>
<p style="margin-left: 30px"> ball1.step();</p>
<p style="margin-left: 30px"> ball2.step();</p>
<p style="margin-left: 30px"> ball3.step();</p>
<p style="margin-left: 30px"> window.requestAnimationFrame(animation);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> window.requestAnimationFrame(animation);</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文件,可以看到在浏览器窗口中出现一个蓝色、一个红色和一个青色的三个小球在运动。</p>
<p> 采用对象后,增加一个运动小球就很方便了,只需先创建一个小球对象</p>
<p> var ball4=new Ball(10, 20, 3, 2, 15,"#FFFF00");</p>
<p> 再添加两条语句 ball4.draw();和 ball4.step();即可。</p>
<h3> 6.给小球一个长尾效果。</h3>
<p> 如果将animation函数中的语句 context.clearRect(0,0,canvas.width,canvas.height);改写为如下两条语句:</p>
<p> context.fillStyle = 'rgba(255,255,255,0.3)';</p>
<p> context.fillRect(0,0,canvas.width,canvas.height);</p>
<p> 这样在清除前一帧动画时,由于是用一个半透明的fillRect函数填充画布,可以为每个小球增加一个长尾效果。</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"></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;"></canvas></p>
<p style="margin-left: 30px"><script type="text/javascript"></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"> // 定义小球对象</p>
<p style="margin-left: 30px"> function Ball(x,y,xspeed,yspeed,radius,color){</p>
<p style="margin-left: 30px"> this.x=x;</p>
<p style="margin-left: 30px"> this.y=y;</p>
<p style="margin-left: 30px"> this.xspeed=xspeed;</p>
<p style="margin-left: 30px"> this.yspeed=yspeed;</p>
<p style="margin-left: 30px"> this.radius=radius;</p>
<p style="margin-left: 30px"> this.color=color;</p>
<p style="margin-left: 30px"> this.draw=function()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(this.x,this.y,this.radius, 0, Math.PI*2, true);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.fillStyle =this.color;</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> this.step=function()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> this.x += this.xspeed;</p>
<p style="margin-left: 30px"> this.y += this.yspeed;</p>
<p style="margin-left: 30px"> if (this.y+this.radius>=canvas.height || this.y-this.radius<=0)</p>
<p style="margin-left: 30px"> this.yspeed = -this.yspeed;</p>
<p style="margin-left: 30px"> if (this.x+ this.radius >= canvas.width || this.x-this.radius<= 0)</p>
<p style="margin-left: 30px"> this.xspeed = -this.xspeed;</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> // 创建三个小球</p>
<p style="margin-left: 30px"> var ball1=new Ball(100,100,5,3,25,"#0000FF");</p>
<p style="margin-left: 30px"> var ball2=new Ball(50, 40,3,1,20,"#FF0000");</p>
<p style="margin-left: 30px"> var ball3=new Ball(30, 30, 3, 2, 15,"#00FFFF");</p>
<p style="margin-left: 30px"> function animation()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.fillStyle = 'rgba(255,255,255,0.3)';</p>
<p style="margin-left: 30px"> context.fillRect(0,0,canvas.width,canvas.height);</p>
<p style="margin-left: 30px"> ball1.draw();</p>
<p style="margin-left: 30px"> ball2.draw();</p>
<p style="margin-left: 30px"> ball3.draw();</p>
<p style="margin-left: 30px"> ball1.step();</p>
<p style="margin-left: 30px"> ball2.step();</p>
<p style="margin-left: 30px"> ball3.step();</p>
<p style="margin-left: 30px"> window.requestAnimationFrame(animation);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> window.requestAnimationFrame(animation);</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文件,可以看到在浏览器窗口中出现一个蓝色、一个红色和一个青色的三个小球在运动,并且每个小球运动时带有长尾效果,如图1所示。</p>
<p align="center"><img src="https://img2018.cnblogs.com/i-beta/1485495/201912/1485495-20191217121319081-2057723252.png"> </p>
<p align="center">图1 带有长尾效果的运动小球</p>
<h3> 7.为画布中的动画添加开始和结束控制。</h3>
<p> 为了对小球运动的开始和结束进行控制,可以在页面中增加“开始”和“停止”两个按钮,单击相应的按钮产生相应的操作。</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"></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"><br/><input id="start" type="button" value="开始"></p>
<p style="margin-left: 30px"><input id="stop" type="button" value="停止"></p>
<p style="margin-left: 30px"><script type="text/javascript"></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 handle=0;</p>
<p style="margin-left: 30px"> // 定义小球对象</p>
<p style="margin-left: 30px"> function Ball(x,y,xspeed,yspeed,radius,color){</p>
<p style="margin-left: 30px"> this.x=x;</p>
<p style="margin-left: 30px"> this.y=y;</p>
<p style="margin-left: 30px"> this.xspeed=xspeed;</p>
<p style="margin-left: 30px"> this.yspeed=yspeed;</p>
<p style="margin-left: 30px"> this.radius=radius;</p>
<p style="margin-left: 30px"> this.color=color;</p>
<p style="margin-left: 30px"> this.draw=function()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(this.x,this.y,this.radius, 0, Math.PI*2, true);</p>
<p style="margin-left: 30px"> context.closePath();</p>
<p style="margin-left: 30px"> context.fillStyle =this.color;</p>
<p style="margin-left: 30px"> context.fill();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> this.step=function()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> this.x += this.xspeed;</p>
<p style="margin-left: 30px"> this.y += this.yspeed;</p>
<p style="margin-left: 30px"> if (this.y+this.radius>=canvas.height || this.y-this.radius<=0)</p>
<p style="margin-left: 30px"> this.yspeed = -this.yspeed;</p>
<p style="margin-left: 30px"> if (this.x+ this.radius >= canvas.width || this.x-this.radius<= 0)</p>
<p style="margin-left: 30px"> this.xspeed = -this.xspeed;</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> // 创建三个小球</p>
<p style="margin-left: 30px"> var ball1=new Ball(100,100,5,3,25,"#0000FF");</p>
<p style="margin-left: 30px"> var ball2=new Ball(50, 50,3,1,20,"#FF0000");</p>
<p style="margin-left: 30px"> var ball3=new Ball(20, 20, 3, 2, 15,"#00FFFF");</p>
<p style="margin-left: 30px"> ball1.draw();</p>
<p style="margin-left: 30px"> ball2.draw();</p>
<p style="margin-left: 30px"> ball3.draw();</p>
<p style="margin-left: 30px"> function animation()</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> context.fillStyle = 'rgba(255,255,255,0.3)';</p>
<p style="margin-left: 30px"> context.fillRect(0,0,canvas.width,canvas.height);</p>
<p style="margin-left: 30px"> ball1.draw();</p>
<p style="margin-left: 30px"> ball2.draw();</p>
<p style="margin-left: 30px"> ball3.draw();</p>
<p style="margin-left: 30px"> ball1.step();</p>
<p style="margin-left: 30px"> ball2.step();</p>
<p style="margin-left: 30px"> ball3.step();</p>
<p style="margin-left: 30px"> handle=window.requestAnimationFrame(animation);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> var running=false;</p>
<p style="margin-left: 30px"> var startBtn=document.getElementById('start');</p>
<p style="margin-left: 30px"> var stopBtn=document.getElementById('stop');</p>
<p style="margin-left: 30px"> startBtn.addEventListener('click', function () {</p>
<p style="margin-left: 30px"> if (!running)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> handle=requestAnimationFrame(animation);</p>
<p style="margin-left: 30px"> running=true;</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> });</p>
<p style="margin-left: 30px"> stopBtn.addEventListener('click', function () {</p>
<p style="margin-left: 30px"> cancelAnimationFrame(handle);</p>
<p style="margin-left: 30px"> handle=null;</p>
<p style="margin-left: 30px"> running=false;</p>
<p style="margin-left: 30px"> });</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文件,可以看到在浏览器窗口中出现一个蓝色、一个红色和一个青色的三个小球,这三个小球初始时静止。单击“开始”按钮后,小球开始运动,单击“停止”按钮,小球停止运动,在当前位置静止不动。再单击“开始”按钮,小球又开始运动。</p><br><br>
来源:https://www.cnblogs.com/cs-whut/p/12053783.html
頁:
[1]