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 相关文章推荐
CCPry JS类库 代码
Oct 30 Javascript
jquery实现table鼠标经过变色代码
Sep 25 Javascript
js jquery分别实现动态的文件上传操作按钮的添加和删除
Jan 13 Javascript
js实现局部页面打印预览原理及示例代码
Jul 03 Javascript
JS+DIV实现鼠标划过切换层效果的方法
May 25 Javascript
JavaScript程序中实现继承特性的方式总结
Jun 24 Javascript
详解JavaScript中数组的reduce方法
Dec 02 Javascript
vue.js开发环境安装教程
Mar 17 Javascript
Vue 引入AMap高德地图的实现代码
Apr 29 Javascript
基于layui table返回的值的多级嵌套的解决方法
Sep 19 Javascript
js实现随机点名功能
Dec 23 Javascript
pnpm对npm及yarn降维打击详解
Aug 05 Javascript
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制作静态网站的模板框架(二)
2006/10/09 PHP
Laravel 错误提示本地化的实现
2019/10/22 PHP
使用onbeforeunload属性后的副作用
2007/03/08 Javascript
网页中可关闭的漂浮窗口实现可自行调节
2013/08/20 Javascript
iframe调用父页面函数示例详解
2014/07/17 Javascript
JavaScript中创建字典对象(dictionary)实例
2015/03/31 Javascript
JavaScript实现拖拽网页内元素的方法
2015/04/15 Javascript
深入理解JavaScript中的箭头函数
2015/07/28 Javascript
js格式化输入框内金额、银行卡号
2016/02/01 Javascript
jQuery选择器及jquery案例详解(必看)
2016/05/20 Javascript
Javascript使用uploadify来实现多文件上传
2016/11/16 Javascript
js鼠标经过tab选项卡时实现切换延迟
2017/03/24 Javascript
windows系统下更新nodejs版本的方案
2017/11/24 NodeJs
微信小程序实时聊天WebSocket
2018/07/05 Javascript
CSS3 动画卡顿性能优化的完美解决方案
2018/09/20 Javascript
解决layui使用layui-icon出现默认图标的问题
2019/09/11 Javascript
element中el-container容器与div布局区分详解
2020/05/13 Javascript
vue中实现图片压缩 file文件的方法
2020/05/28 Javascript
跟老齐学Python之不要红头文件(2)
2014/09/28 Python
python提取内容关键词的方法
2015/03/16 Python
使用Python的Zato发送AMQP消息的教程
2015/04/16 Python
python斐波那契数列的计算方法
2018/09/27 Python
如何将PySpark导入Python的放实现(2种)
2020/04/26 Python
Keras 切换后端方式(Theano和TensorFlow)
2020/06/19 Python
python自动提取文本中的时间(包含中文日期)
2020/08/31 Python
Python私有属性私有方法应用实例解析
2020/09/15 Python
REISS英国官网:伦敦High Street最受欢迎品牌
2016/12/21 全球购物
MIXIT官网:俄罗斯最大的化妆品公司之一
2020/01/25 全球购物
皇家阿尔伯特瓷器美国官网:Royal Albert美国
2020/02/16 全球购物
Java语言程序设计测试题选择题部分
2014/04/03 面试题
普通员工辞职信
2014/01/17 职场文书
小学家长学校培训材料
2014/08/24 职场文书
试用期自我评价范文
2015/03/10 职场文书
家访教师心得体会
2016/01/23 职场文书
五年级作文之成长
2019/09/16 职场文书
2019员工保密协议书(3篇)
2019/09/23 职场文书