Berikut ialah kod sumber: Salin kod Kod adalah seperti berikut: HTML5 随机弹跳的小球 <br> body{<br> font-family: 微软雅黑; <br> }<br> body,h1{<br> margin:0;<br> }<br> canvas{<br> display:block;margin-left: auto;margin-right: auto;<br> border:1px solid #DDD; <br> background: -webkit-linear-gradient(top, #222,#111);<br> } <br> HTML5特效 随机弹跳的小球 请使用支持HTML5的浏览器开打本页。 暂停 继续 增加小球 刷新 每次刷新页面的小球大小,颜色,运动路线,都是新的,可以点击上面各个按钮看看效果。 <br> var jika = {<br> diberikan: null,<br> kandungan: null, <br> data: {<br> Julat jejari : [ 5 , 20 ] , <br> Julat kelajuan: [-5, 5],<br> scrollHeight : null , <br> scrollWdith : null <br> },<br> bola: [],<br> iaitu: {<br> kanvas: null<br> },<br> fn: {<br> createRandom : function ( startInt , endInt ) { // CreateRandom <br> var iResult;<br> iResult = startInt ( Math . floor ( Math . rawak () * ( endInt - startInt 1 )) ); kembalikan iResult<br> },<br> init : fungsi ( ) { <br> data scrollWidth = badan tatal data scrollHeight = scrollHeight nimo.ele.canvas = document.getElementById('canvas-that' 'this-com');<br> nimo.content = nimo.ele.canvas.getContext('2d');<br> nimo.ele.canvas.width = nimo.data.scrollWdith - 50;<br> nimo.ele.canvas.height = nimo.data.scrollHeight - 100;<br> },<br> addBall : fungsi ( ) { <br> var aRandomColor = [];<br> aRandomColor tolak ( nimo . fn . createRandom ( 0 , 255 ) ); aRandomColor tolak ( nimo . fn . createRandom ( 0 , 255 ) ); aRandomColor tolak ( nimo . fn . createRandom ( 0 , 255 ) ); var iRandomRadius = nimo.fn.creatRandom(nimo.data.radiusRange[0], nimo.data.radiusRange[1]);<br> var oTempBall = {<br> coordsX: nimo.fn.createRandom(iRandomRadius, nimo.ele.canvas.width - iRandomRadius),<br> coordsY: nimo.fn.createRandom(iRandomRadius, nimo.ele.canvas.height - iRandomRadius),<br> jejari: iRandomRadius,<br> bgColor: 'rgba(' aRandomColor[0] ',' aRandomColor[1] ',' aRandomColor[2] ',0.5)'<br> };<br> oTempBall.speedX = nimo.fn.createRandom(nimo.data.speedRange[0], nimo.data.speedRange[1]);<br> jika ( oTempBall . speedX === 0 ) { <br> oTempBall.speedX = 1<br> }<br> jika ( oTempBall . speedY === 0 ) { <br> oTempBall.speedY = 1<br> }<br> oTempBall.speedY = nimo.fn.createRandom(nimo.data.speedRange[0], nimo.data.speedRange[1]);<br> nimo.balls.push(atauTempBall)<br> },<br> drawBall : fungsi ( bStatic ) { <br> var i, iSize;<br> kandungan nimo. clearRect ( 0, 0, nimo. ele. kanvas. lebar, nimo. ele. kanvas. ketinggian ) <br> untuk ( var i = 0 , iSize = nimo . bola . panjang ; i < ; iSize ; i ) { <br> var oTarger = nimo.balls[i];<br> kandungan bermulaPath ( ); kandungan nimo . nimo.content.fillStyle = oTarger.bgColor;<br> nimo.content.fill();<br> jika ( ! bStatik ) { <br> jika ( oTarger . coordsX oTarger . jejari > ;= nimo . ele . kanvas . lebar ) { <br> oTarget kelajuanX = - ( Matematik . abs ( oTager . kelajuanX ) ) <br> }<br> jika ( oTarger . coordsX -- oTarger . radius < ;= 0 ) { <br> oTarger.speedX = Math.abs(oTarger.speedX)<br> }<br> jika ( oTarger . coordsY -- oTager . radius < ;= 0 ) { <br> oTarger.speedY = Math.abs(oTarger.speedY)<br> }<br> jika ( oTarger . coordsY oTarger . radius > ;= nimo . ele . kanvas . ketinggian ) { <br> oTarget lajuY = -( Matematik . abs ( oTager . speedY ) ) <br> }<br> oTarger.coordsX = oTarger.coordsX oTarger.speedX;<br> oTarger.coordsY = oTarger.coordsY oTarger.speedY;<br> }<br> }<br> },<br> jalankan : fungsi ( ) { <br> if.fn.drawBall();<br> nimo diberikan = setTimeout ( function ( ) { <br> if.fn.drawBall();<br> nimo . diberi = setTimeout ( hujah . panggilan , 10 ) <br> }, 10)<br> },<br> berhenti : fungsi ( ) { <br> clearTimeout ( nimo . whatyougave ) <br> }<br> }<br> }<br> tetingkap onload = fungsi ( ) { <br> nimo.fn.init();<br> var i;<br> untuk ( var i = 0 ; i < ; 10; i ) { <br> if.fn.addBall();<br> }<br> nimo.fn.run();<br> document.getElementById('stop-only' 'this-com').onclick = function() {<br> nimo.fn.stop()<br> }<br> document.getElementById('run-keley' 'i-com').onclick = function() {<br> nimo.fn.stop()<br> nimo.fn.run()<br> }<br> document.getElementById('addBall-this-com').onclick = function() {<br> var i;<br> untuk ( var i = 0 ; i < ; 10; i ) { <br> if.fn.addBall();<br> }<br> nimo fn ( benar ); }<br> }<br> </skrip><br> </body><br> </html><br> <br><br> </div>