JS实现随机乱撞彩色圆球特效的方法


Posted in Javascript onMay 05, 2015

本文实例讲述了JS实现随机乱撞彩色圆球特效的方法。分享给大家供大家参考。具体实现方法如下:

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>JS实现的随机乱撞的彩色圆球特效代码</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实现的随机乱撞的彩色圆球特效代码</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){//生产随机数
  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程序设计有所帮助。

Javascript 相关文章推荐
基于JQuery实现的Select级联
Jan 27 Javascript
JavaScript中的lastIndexOf()方法使用详解
Jun 06 Javascript
js实现的黑背景灰色二级导航菜单效果代码
Aug 24 Javascript
jquery图片倾斜层叠切换特效代码分享
Aug 27 Javascript
谈谈js中的prototype及prototype属性解释和常用方法
Nov 25 Javascript
详解JavaScript中localStorage使用要点
Jan 13 Javascript
jQuery ready()和onload的加载耗时分析
Sep 08 Javascript
IE9 elementUI文件上传的问题解决
Oct 17 Javascript
基于jquery实现的tab选项卡功能示例【附源码下载】
Jun 10 jQuery
Vue-cli打包后部署到子目录下的路径问题说明
Sep 02 Javascript
一行JavaScript代码如何实现瀑布流布局
Dec 11 Javascript
vue项目如何打包之项目打包优化(让打包的js文件变小)
Apr 30 Vue.js
jquery实现图片随机排列的方法
May 04 #Javascript
jquery实现的美女拼图游戏实例
May 04 #Javascript
jQuery实现仿Alipay支付宝首页全屏焦点图切换特效
May 04 #Javascript
jQuery插件kinMaxShow扩展效果用法实例
May 04 #Javascript
jQuery消息提示框插件Tipso
May 04 #Javascript
jquery实现多屏多图焦点图切换特效的方法
May 04 #Javascript
jquery实现鼠标拖拽滑动效果来选择数字的方法
May 04 #Javascript
You might like
PHP对文件夹递归执行chmod命令的方法
2015/06/19 PHP
编写PHP程序检查字符串中的中文字符个数的实例分享
2016/03/17 PHP
仿163填写邮件地址自动显示下拉(无优化)
2008/11/05 Javascript
js 数组操作代码集锦
2009/04/28 Javascript
js+css在交互上的应用
2010/07/18 Javascript
推荐11款jQuery开发的复选框和单选框美化插件
2011/08/02 Javascript
jquery next nextAll nextUntil siblings的区别介绍
2013/10/05 Javascript
JavaScript代码判断点击第几个按钮
2015/12/13 Javascript
JavaScript的字符串方法汇总
2016/07/31 Javascript
js事件委托和事件代理案例分享
2017/07/25 Javascript
微信小程序之发送短信倒计时功能
2017/08/30 Javascript
小程序显示弹窗时禁止下层的内容滚动实现方法
2019/03/20 Javascript
Vue配置marked链接添加target=&quot;_blank&quot;的方法
2019/07/19 Javascript
Vue3 响应式侦听与计算的实现
2020/11/11 Javascript
[01:22:10]Ti4 循环赛第二日 DK vs Empire
2014/07/11 DOTA
python运行时间的几种方法
2016/06/17 Python
python实现随机森林random forest的原理及方法
2017/12/21 Python
使用NumPy和pandas对CSV文件进行写操作的实例
2018/06/14 Python
pandas使用get_dummies进行one-hot编码的方法
2018/07/10 Python
Python小白必备的8个最常用的内置函数(推荐)
2019/04/03 Python
python+selenium实现简历自动刷新的示例代码
2019/05/20 Python
Python正则表达式匹配和提取IP地址
2019/06/06 Python
python 求某条线上特定x值或y值的点坐标方法
2019/07/09 Python
tensorflow没有output结点,存储成pb文件的例子
2020/01/04 Python
python-OpenCV 实现将数组转换成灰度图和彩图
2020/01/09 Python
详解用Python调用百度地图正/逆地理编码API
2020/07/02 Python
css3实现input输入框颜色渐变发光效果代码
2014/04/02 HTML / CSS
顶丰TOPPIK台湾官网:增发纤维假发,告别秃发困扰
2018/06/13 全球购物
New delete 与malloc free 的联系与区别
2013/02/04 面试题
成功的酒店创业计划书
2013/12/27 职场文书
国际贸易毕业生求职信范文
2014/02/21 职场文书
品德评语大全
2014/05/05 职场文书
知识竞赛拉拉队口号
2014/06/16 职场文书
乡镇食品安全责任书
2014/07/28 职场文书
搞笑的获奖感言
2014/08/16 职场文书
PHP策略模式写法
2021/04/01 PHP