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 相关文章推荐
window.addeventjs事件驱动函数集合addEvent等
Feb 19 Javascript
JS DOM 操作实现代码
Aug 01 Javascript
使用javascipt---实现二分查找法
Apr 10 Javascript
JavaScript使用slice函数获取数组部分元素的方法
Apr 06 Javascript
JQuery判断radio(单选框)是否选中和获取选中值方法总结
Apr 15 Javascript
jquery中的工具使用方法$.isFunction, $.isArray(), $.isWindow()
Aug 09 Javascript
Bootstrap每天必学之按钮
Nov 26 Javascript
纯js实现倒计时功能
Jan 06 Javascript
vue元素实现动画过渡效果
Jul 01 Javascript
ant design实现圈选功能
Dec 17 Javascript
JS删除对象中某一属性案例详解
Sep 08 Javascript
小程序wx.getUserProfile接口的具体使用
Jun 02 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
150kHz到30Mhz完全冲浪手册
2020/03/20 无线电
JavaScript 判断浏览器类型及版本
2009/02/21 Javascript
Jquery Autocomplete 结合asp.net使用要点
2010/10/29 Javascript
jqgrid 表格数据导出实例
2013/11/21 Javascript
js报$ is not a function 的问题的解决方法
2014/01/20 Javascript
js二维数组排序的简单示例代码
2014/01/24 Javascript
简介AngularJS中使用factory和service的方法
2015/06/17 Javascript
JQuery zClip插件实现复制页面内容到剪贴板
2015/11/02 Javascript
SpringMVC返回json数据的三种方式
2015/12/10 Javascript
javaScript如何跳出多重循环break、continue
2016/09/01 Javascript
JavaScript使用readAsDataUrl方法预览图片
2017/05/10 Javascript
JavaScript中错误正确处理方式小结你用对了吗
2017/10/10 Javascript
解决Vue.js 2.0 有时双向绑定img src属性失败的问题
2018/03/14 Javascript
微信小程序onLaunch异步,首页onLoad先执行?
2018/09/20 Javascript
微信小程序实现人脸识别登陆的示例代码
2019/04/02 Javascript
layui表格内放置图片,并点击放大的实例
2019/09/10 Javascript
VUE单页面切换动画代码(全网最好的切换效果)
2019/10/31 Javascript
vue v-for直接循环数字实例
2019/11/07 Javascript
使用webpack/gulp构建TypeScript项目的方法示例
2019/12/18 Javascript
python实现根据用户输入从电影网站获取影片信息的方法
2015/04/07 Python
Python完成毫秒级抢淘宝大单功能
2019/06/06 Python
使用Tensorboard工具查看Loss损失率
2020/02/15 Python
推荐8款常用的Python GUI图形界面开发框架
2020/02/23 Python
如何理解python对象
2020/06/21 Python
css3动画效果小结(推荐)
2016/07/25 HTML / CSS
Canvas系列之滤镜效果
2019/02/12 HTML / CSS
Clarks英国官方网站:全球领军鞋履品牌
2016/11/26 全球购物
计算机专业推荐信范文
2013/11/20 职场文书
积极分子思想汇报
2014/01/04 职场文书
大学生学期自我鉴定
2014/03/19 职场文书
教师节倡议书
2014/08/30 职场文书
2014年村委会工作总结
2014/11/24 职场文书
公司财务经理岗位职责
2015/04/08 职场文书
劳动仲裁代理词范文
2015/05/25 职场文书
西游降魔篇观后感
2015/06/15 职场文书
pytorch fine-tune 预训练的模型操作
2021/06/03 Python