<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML5学习第5天[乱撞的球]</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="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.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>

JS实现的随机乱撞的彩色圆球特效代码的更多相关文章

  1. 原生js实现tab选项卡里内嵌图片滚动特效代码

    <!DOCTYPE HTML><html lang="en-US"><head><meta charset="UTF-8&quo ...

  2. 总结下js排序算法和乱序算法

    其实本人最怕的就是算法,大学算法课就感觉老师在讲天书,而且对于前端来说,算法在实际的应用中实在是很有限.毕竟算法要依靠大量的数据为基础才能发挥出算法的效率,就浏览器那性能,......是吧,退一万步说 ...

  3. Gremlins.js – 模拟用户随机操作的 JS 测试库

    Gremlins.js 是基于 JavaScript 编写的 Monkey 测试库,支持 Node.js 平台和浏览器中使用.Gremlins.js 随机模拟用户操作:单击窗口中的任意位置,在表格中输 ...

  4. 18款js和jquery文字特效代码分享

    18款js和jquery文字特效代码分享 jQCloud标签云插件_热门城市文字标签云代码 js 3d标签云特效关键词文字球状标签云代码 原生JS鼠标悬停文字球状放大显示效果代码 原生js文字动画圆形 ...

  5. JS实现转动随机数抽奖的特效代码

    JS实现转动随机数抽奖的特效代码 大家都玩过抽奖游戏,或者梦想抽到大奖吧.可是有没有想过抽奖游戏是怎么实现的呐?今天就给大家分享一款转动随机数抽奖的JS特效代码. 实现代码例如以下 <!Doct ...

  6. JS图片自动和可控的轮播切换特效

    点击这里查看效果:http://hovertree.com/texiao/js/1.htm HTML文件代码如下: <!DOCTYPE html> <html xmlns=" ...

  7. 使用JS或jQuery模拟鼠标点击a标签事件代码

    原文 使用JS或jQuery模拟鼠标点击a标签事件代码 这篇文章主要介绍了使用JS或jQuery模拟鼠标点击a标签事件代码,需要的朋友可以参考下 <a id="alink" ...

  8. 一款基础模型的JS打飞机游戏特效代码

    <!DOCTYPE html> <html lang="en"> <head> <title>一款基础模型的JS打飞机游戏特效代码& ...

  9. 疯狂位图之——位图生成12GB无重复随机乱序大整数集

    上一篇讲述了用位图实现无重复数据的排序,排序算法一下就写好了,想弄个大点数据测试一下,因为小数据在内存中快排已经很快. 一.生成的数据集要求 1.数据为0--2147483647(2^31-1)范围内 ...

随机推荐

  1. JDK源码分析:Short.java

    Short是基本数据类型short的包装类. 1)声明部: public final class Short extends Number implements Comparable<Short ...

  2. 理解 JavaScript 原型 / 原型链

    关于对象 以下代码中 p 的值是一个新对象,里面拥有 name 和 age 属性 function People(name, age){ this.name = name this.age = age ...

  3. java poi技术读取到数据库

    https://www.cnblogs.com/hongten/p/java_poi_excel.html java的poi技术读取Excel数据到MySQL 这篇blog是介绍java中的poi技术 ...

  4. Python3 Tkinter-Label

    1.创建 from tkinter import * root=Tk() root.title('Hello tkinter!') root.mainloop() 2.使用内置位图 from tkin ...

  5. SGU 194 Reactor Cooling(无源无汇上下界可行流)

    Description The terrorist group leaded by a well known international terrorist Ben Bladen is bulidin ...

  6. POJ 1815 Friendship(最大流最小割の字典序割点集)

    Description In modern society, each person has his own friends. Since all the people are very busy, ...

  7. POJ 2104 K-th Number(划分树)

    Description You are working for Macrohard company in data structures department. After failing your ...

  8. Thrift IDL使用方式

    I.背景 众所周知,Thrift是一个RPC的框架,其可用于不同语言之间的服务相互调用.比如最近接触到的一个运用环境: *前端使用Node.Js重构了部分我们的老旧代码(前后端未分离的SpringBo ...

  9. 第三章——供机器读取的数据(CSV与JSON)

    本书使用的文件.代码:https://github.com/huangtao36/data_wrangling 机器可读(machine readable)文件格式: 1.逗号分隔值(Comma-Se ...

  10. JVM(二)GC

    GC简介 Java堆内存 在运行时,java的实例被存放在堆内存区域.当一个对象不在被引用,满足条件就会从堆内存移除并且内存空间被回收.堆内存由三个主要区域 1.新生代     Eden空间(任何实例 ...