最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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>

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

相關(guān)文章

  • 使用apply方法處理數(shù)組的三個(gè)技巧[譯]

    使用apply方法處理數(shù)組的三個(gè)技巧[譯]

    本文要講的是使用apply方法處理數(shù)組的三個(gè)技巧,需要的朋友可以參考下
    2012-09-09
  • mustache.js實(shí)現(xiàn)首頁元件動(dòng)態(tài)渲染的示例代碼

    mustache.js實(shí)現(xiàn)首頁元件動(dòng)態(tài)渲染的示例代碼

    這篇文章主要介紹了mustache.js實(shí)現(xiàn)首頁元件動(dòng)態(tài)渲染的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • webpack中使用zepto步驟方法

    webpack中使用zepto步驟方法

    這篇文章主要為大家介紹了webpack中使用zepto步驟方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • WebGL 顏色與紋理使用介紹

    WebGL 顏色與紋理使用介紹

    這篇文章主要為大家介紹了WebGL 顏色與紋理使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • 詳解JavaScript的表達(dá)式與運(yùn)算符

    詳解JavaScript的表達(dá)式與運(yùn)算符

    這篇文章主要介紹了JavaScript的表達(dá)式與運(yùn)算符,需要的朋友可以參考下
    2015-11-11
  • 使用mpvue搭建一個(gè)初始小程序及項(xiàng)目配置方法

    使用mpvue搭建一個(gè)初始小程序及項(xiàng)目配置方法

    這篇文章主要介紹了使用mpvue搭建一個(gè)初始小程序及項(xiàng)目配置方法,需要的朋友可以參考下
    2018-12-12
  • bootstrap datetimepicker控件位置異常的解決方法

    bootstrap datetimepicker控件位置異常的解決方法

    這篇文章主要為大家詳細(xì)介紹了bootstrap datetimepicker控件位置異常的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • ES6 Set結(jié)構(gòu)的應(yīng)用實(shí)例分析

    ES6 Set結(jié)構(gòu)的應(yīng)用實(shí)例分析

    這篇文章主要介紹了ES6 Set結(jié)構(gòu)的應(yīng)用,結(jié)合實(shí)例形式分析了ES6 set結(jié)構(gòu)的功能、特點(diǎn)、常見用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-06-06
  • 最新評(píng)論

    玛曲县| 恩平市| 陆良县| 盐亭县| 大庆市| 穆棱市| 视频| 安新县| 保亭| 曲周县| 玉门市| 张掖市| 新源县| 侯马市| 贞丰县| 偃师市| 漯河市| 湖北省| 遂川县| 江北区| 资阳市| 阿拉善右旗| 宁河县| 鹤山市| 张家口市| 渭源县| 新泰市| 尼勒克县| 新野县| 横山县| 会理县| 阳原县| 繁昌县| 大渡口区| 清徐县| 德州市| 什邡市| 左云县| 建湖县| 台山市| 安多县|