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 相关文章推荐
让浏览器非阻塞加载javascript的几种方法小结
Apr 25 Javascript
原生js和jquery中有关透明度设置的相关问题
Jan 08 Javascript
javascript创建createXmlHttpRequest对象示例代码
Feb 10 Javascript
js事件绑定快捷键以ctrl+k为例
Sep 30 Javascript
跟我学习javascript的作用域与作用域链
Nov 19 Javascript
关于JavaScript限制字数的输入框的那些事
Aug 14 Javascript
微信小程序中使用ECharts 异步加载数据的方法
Jun 27 Javascript
JS实现集合的交集、补集、差集、去重运算示例【ES5与ES6写法】
Feb 18 Javascript
从零到一详聊创建Vue工程及遇到的常见问题
Apr 25 Javascript
Vue 中获取当前时间并实时刷新的实现代码
May 12 Javascript
jQuery加PHP实现图片上传并提交的示例代码
Jul 16 jQuery
Nest.js参数校验和自定义返回数据格式详解
Mar 29 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
smarty+adodb+部分自定义类的php开发模式
2006/12/31 PHP
HTML中事件触发列表与解说
2007/07/09 Javascript
WordPress 照片lightbox效果的运用几点
2009/06/22 Javascript
js 省地市级联选择
2010/02/07 Javascript
jQuery ui插件的使用方法代码实例
2013/05/08 Javascript
jquery实现按Enter键触发事件示例
2013/09/10 Javascript
js取消单选按钮选中并判断对象是否为空
2013/11/14 Javascript
jQuery的选择器中的通配符使用介绍
2014/03/20 Javascript
对于jQuery性能的一些优化建议
2015/08/13 Javascript
基于jQuery实现选取月份插件附源码下载
2015/12/28 Javascript
js前端实现多图图片上传预览的两个方法(推荐)
2016/11/18 Javascript
基于jquery实现的鼠标悬停提示案例
2016/12/11 Javascript
原生js的ajax和解决跨域的jsonp(实例讲解)
2017/10/16 Javascript
Vue按需加载的具体实现
2017/12/02 Javascript
Vuex 单状态库与多模块状态库详解
2018/12/11 Javascript
Vue动画事件详解及过渡动画实例
2019/02/09 Javascript
详解vue项目打包步骤
2019/03/29 Javascript
ES6中的迭代器、Generator函数及Generator函数的异步操作方法
2019/05/12 Javascript
Vue.js@2.6.10更新内置错误处机制Fundebug同步支持相应错误监控
2019/05/13 Javascript
微信小程序 button样式设置为图片的方法
2020/06/19 Javascript
Vue包大小优化的实现(从1.72M到94K)
2021/02/18 Vue.js
[59:26]DOTA2上海特级锦标赛D组资格赛#1 EG VS VP第二局
2016/02/28 DOTA
python解析json实例方法
2013/11/19 Python
Python上传package到Pypi(代码简单)
2016/02/06 Python
在Python中表示一个对象的方法
2019/06/25 Python
使用 Python 处理3万多条数据只要几秒钟
2020/01/19 Python
解析浏览器的一些“滚动”行为鉴赏
2019/09/16 HTML / CSS
意大利简约的休闲品牌:Aspesi
2018/02/08 全球购物
美国价格实惠的在线眼镜网站:Zeelool
2020/12/25 全球购物
一道输出判断型Java面试题
2014/10/01 面试题
文科毕业生自荐书范文
2014/04/17 职场文书
竞选生活委员演讲稿
2014/04/28 职场文书
财务审计整改报告
2014/11/06 职场文书
自我检讨书范文
2015/01/28 职场文书
青年志愿者服务活动总结
2015/05/06 职场文书
Python操作CSV格式文件的方法大全
2021/07/15 Python