欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)隨機(jī)亂撞彩色圓球特效的方法

 更新時(shí)間:2015年05月05日 09:33:45   投稿:shichen2014  
這篇文章主要介紹了JS實(shí)現(xiàn)隨機(jī)亂撞彩色圓球特效的方法,可實(shí)現(xiàn)彩色小球的碰撞效果,涉及隨機(jī)函數(shù)與頁面樣式的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了JS實(shí)現(xiàn)隨機(jī)亂撞彩色圓球特效的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>JS實(shí)現(xiàn)的隨機(jī)亂撞的彩色圓球特效代碼</title>
 <style>
 body{
 font-family: 微軟雅黑; 
 }
 body,h1{
 margin:0;
 }
 canvas{
 display:block;margin-left: auto;margin-right: auto;
 border:1px solid #DDD; 
 background: -webkit-linear-gradient(top, #222,#111);
 } 
 </style>
</head>
<body>
 <h1>JS實(shí)現(xiàn)的隨機(jī)亂撞的彩色圓球特效代碼</h1>
<canvas id="canvas" >
</canvas>
<button id="stop">stop</button>
<button id="run">run</button>
<button id="addBall">addBall</button>
<script src="jquery-1.6.2.min.js"></script>
<script>
var nimo={
 aniamted:null,
 content:null,
 data:{
 radiusRange:[5,20],
 speedRange:[-5,5],
 scrollHeight:null,
 scrollWdith:null
 },
 balls:[],
 ele:{
 canvas:null 
 },
 fn:{
 creatRandom:function(startInt,endInt){//生產(chǎn)隨機(jī)數(shù)
  var iResult; 
  iResult=startInt+(Math.floor(Math.random()*(endInt-startInt+1)));
  return iResult
 },
 init:function(){
  nimo.data.scrollWdith=document.body.scrollWidth;
  nimo.data.scrollHeight=document.body.scrollHeight;
  nimo.ele.canvas=document.getElementById('canvas'); 
  nimo.content=nimo.ele.canvas.getContext('2d');  
  nimo.ele.canvas.width=nimo.data.scrollWdith-50;
  nimo.ele.canvas.height=nimo.data.scrollHeight-100;
 },
 addBall:function(){
  var aRandomColor=[];
  aRandomColor.push(nimo.fn.creatRandom(0,255));
  aRandomColor.push(nimo.fn.creatRandom(0,255));
  aRandomColor.push(nimo.fn.creatRandom(0,255)); 
  var iRandomRadius=nimo.fn.creatRandom(nimo.data.radiusRange[0],nimo.data.radiusRange[1]);
  var oTempBall={
  coordsX:nimo.fn.creatRandom(iRandomRadius,nimo.ele.canvas.width-iRandomRadius),
  coordsY:nimo.fn.creatRandom(iRandomRadius,nimo.ele.canvas.height-iRandomRadius),
  radius:iRandomRadius,  
  bgColor:'rgba('+aRandomColor[0]+','+aRandomColor[1]+','+aRandomColor[2]+',0.5)'  
  }; 
  oTempBall.speedX=nimo.fn.creatRandom(nimo.data.speedRange[0],nimo.data.speedRange[1]);
  if(oTempBall.speedX===0){
  oTempBall.speedX=1
  }
  if(oTempBall.speedY===0){
  oTempBall.speedY=1
  }
  oTempBall.speedY=nimo.fn.creatRandom(nimo.data.speedRange[0],nimo.data.speedRange[1]);
  nimo.balls.push(oTempBall)
 },
 drawBall:function(bStatic){  
  var i,iSize;
  nimo.content.clearRect(0,0,nimo.ele.canvas.width,nimo.ele.canvas.height)
  for(var i=0,iSize=nimo.balls.length;i<iSize;i++){
  var oTarger=nimo.balls[i];  
  nimo.content.beginPath();
  nimo.content.arc(oTarger.coordsX,oTarger.coordsY,oTarger.radius,0,10);
  nimo.content.fillStyle=oTarger.bgColor;  
  nimo.content.fill();
  if(!bStatic){
   if(oTarger.coordsX+oTarger.radius>=nimo.ele.canvas.width){
   oTarger.speedX=-(Math.abs(oTarger.speedX))
   }
   if(oTarger.coordsX-oTarger.radius<=0){
   oTarger.speedX=Math.abs(oTarger.speedX)
   }
   if(oTarger.coordsY-oTarger.radius<=0){
   oTarger.speedY=Math.abs(oTarger.speedY)
   }
   if(oTarger.coordsY+oTarger.radius>=nimo.ele.canvas.height){
   oTarger.speedY=-(Math.abs(oTarger.speedY))
   }
   oTarger.coordsX=oTarger.coordsX+oTarger.speedX;
   oTarger.coordsY=oTarger.coordsY+oTarger.speedY;  
  }  
  }
 },
 run:function(){
  nimo.fn.drawBall();
  nimo.aniamted=setTimeout(function(){
  nimo.fn.drawBall();
  nimo.aniamted=setTimeout(arguments.callee,10)
  },10)
 },
 stop:function(){
  clearTimeout(nimo.aniamted)
 }
 }
}
window.onload=function(){
 nimo.fn.init();
 var i;
 for(var i=0;i<10;i++){
 nimo.fn.addBall();
 }
 nimo.fn.run();
 document.getElementById('stop').onclick=function(){
 nimo.fn.stop()
 }
 document.getElementById('run').onclick=function(){
 nimo.fn.stop()
 nimo.fn.run()
 }
 document.getElementById('addBall').onclick=function(){
 var i;
 for(var i=0;i<10;i++){
  nimo.fn.addBall(); 
 }
 nimo.fn.drawBall(true);
 }
}
</script>
</body>
</html>

希望本文所述對大家的javascript程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 前端跨域的幾種解決方式總結(jié)(推薦)

    前端跨域的幾種解決方式總結(jié)(推薦)

    這篇文章主要介紹了前端跨域的幾種解決方式,詳細(xì)介紹了同源策略并同時(shí)給出了跨域的五種解決方案,具體操作步驟大家可查看下文的詳細(xì)講解,感興趣的小伙伴們可以參考一下。
    2017-08-08
  • JS?中在嚴(yán)格模式下?this?的指向問題

    JS?中在嚴(yán)格模式下?this?的指向問題

    這篇文章主要介紹了JS?中在嚴(yán)格模式下this的指向問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • js控制表單操作的常用代碼小結(jié)

    js控制表單操作的常用代碼小結(jié)

    本文章來給各位同學(xué)收集一些在WEB前臺(tái)開發(fā)中常用的一些控制表單操作函數(shù),有需要的朋友可以參考一下
    2013-08-08
  • JS模式之單例模式基本用法

    JS模式之單例模式基本用法

    這篇文章主要介紹了JS模式之單例模式基本用法,實(shí)例分析了javascript單例模式的基本實(shí)現(xiàn)方法,需要的朋友可以參考下
    2015-06-06
  • JS完成代碼前最好對其做5件事

    JS完成代碼前最好對其做5件事

    我們不得面對這樣一個(gè)事實(shí):許多程序員不會(huì)規(guī)劃他們的JS代碼。我們經(jīng)??焖賹懲甏a、運(yùn)行、提交。但當(dāng)我們繼續(xù)開發(fā)遇到變量和函數(shù)時(shí)不得不再次回頭查看它們代表的含義,麻煩就從這里開始了。
    2013-04-04
  • Sort()函數(shù)的多種用法

    Sort()函數(shù)的多種用法

    sort() 方法用于對數(shù)組的元素進(jìn)行排序。接下來通過本文給大家介紹Sort()函數(shù)的多種用法,對sort函數(shù)的用法相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)
    2016-03-03
  • 常用原生JS兼容性寫法匯總

    常用原生JS兼容性寫法匯總

    這篇文章主要為大家詳細(xì)匯總了常用原生JS兼容性寫法,感興趣的小伙伴們可以參考一下
    2016-04-04
  • js通過地址欄給action傳值(中文亂碼全是問號)

    js通過地址欄給action傳值(中文亂碼全是問號)

    我從js代碼中通過地址欄傳值給了action的相應(yīng)變量,但是,如果變量值為中文的時(shí)候,在action中測試輸出則為問號
    2013-05-05
  • 解讀new?Object()和Object.create()的區(qū)別

    解讀new?Object()和Object.create()的區(qū)別

    這篇文章主要介紹了解讀new?Object()和Object.create()的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • JS異步宏隊(duì)列與微隊(duì)列原理區(qū)別詳解

    JS異步宏隊(duì)列與微隊(duì)列原理區(qū)別詳解

    這篇文章主要介紹了JS異步宏隊(duì)列與微隊(duì)列原理區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07

最新評論