HTML5 随机弹跳的小球

本文介绍了一个使用 HTML5 实现的随机弹跳小球的特效,包括源码解析、实现原理及操作交互。通过简单的代码示例,展示了如何创建动态、互动性强的网页元素。
原文: HTML5 随机弹跳的小球

查看效果:http://keleyi.com/a/bjad/tc1y11dy.htm

Chrome效果图:


火狐效果图:



以下是源码:

  1 <!doctype html>
  2 <html>
  3 <head>
  4 <title>HTML5 随机弹跳的小球-柯乐义</title>
  5 <style>
  6 body{
  7 font-family: 微软雅黑;    
  8 }
  9 body,h1{
 10 margin:0;
 11 }
 12 canvas{
 13 display:block;margin-left: auto;margin-right: auto;
 14 border:1px solid #DDD;    
 15 background: -webkit-linear-gradient(top, #222,#111);
 16 }    
 17 </style>
 18 </head>
 19 <body>
 20 <h1>柯乐义 HTML5特效 随机弹跳的小球</h1>
 21 <div>请使用支持HTML5的浏览器开打本页。<a href="http://keleyi.com/a/bjad/tc1y11dy.htm" target="_blank">原文</a> <button id="stop-keleyi-com">暂停</button>
 22 <button id="run-keleyi-com">继续</button>
 23 <button id="addBall-keleyi-com">增加小球</button> <button onclick="javascript:window.location.reload();">刷新</button>
 24 <br />每次刷新页面的小球大小,颜色,运动路线,都是新的,可以点击上面各个按钮看看效果。
 25 </div>
 26 <canvas id="canvas-keleyi-com" >
 27 
 28 </canvas>
 29 
 30 <script type="text/javascript" src="http://keleyi.com/keleyi/pmedia/jquery/jquery-1.11.0.min.js"></script>
 31 <script type="text/javascript">
 32 var nimo = {
 33 aniamted: null,
 34 content: null,
 35 data: {
 36 radiusRange: [5, 20],
 37 speedRange: [-5, 5],
 38 scrollHeight: null,
 39 scrollWdith: null
 40 },
 41 balls: [],
 42 ele: {
 43 canvas: null
 44 },
 45 fn: {
 46 creatRandom: function (startInt, endInt) {//生产随机数
 47 var iResult;
 48 iResult = startInt + (Math.floor(Math.random() * (endInt - startInt + 1)));
 49 return iResult
 50 },
 51 init: function () {
 52 nimo.data.scrollWdith = document.body.scrollWidth;
 53 nimo.data.scrollHeight = document.body.scrollHeight;
 54 nimo.ele.canvas = document.getElementById('canvas-ke'+'leyi-com');
 55 nimo.content = nimo.ele.canvas.getContext('2d');
 56 nimo.ele.canvas.width = nimo.data.scrollWdith - 50;
 57 nimo.ele.canvas.height = nimo.data.scrollHeight - 100;
 58 },
 59 addBall: function () {
 60 var aRandomColor = [];
 61 aRandomColor.push(nimo.fn.creatRandom(0, 255));
 62 aRandomColor.push(nimo.fn.creatRandom(0, 255));
 63 aRandomColor.push(nimo.fn.creatRandom(0, 255));
 64 var iRandomRadius = nimo.fn.creatRandom(nimo.data.radiusRange[0], nimo.data.radiusRange[1]);
 65 var oTempBall = {
 66 coordsX: nimo.fn.creatRandom(iRandomRadius, nimo.ele.canvas.width - iRandomRadius),
 67 coordsY: nimo.fn.creatRandom(iRandomRadius, nimo.ele.canvas.height - iRandomRadius),
 68 radius: iRandomRadius,
 69 bgColor: 'rgba(' + aRandomColor[0] + ',' + aRandomColor[1] + ',' + aRandomColor[2] + ',0.5)'
 70 };
 71 oTempBall.speedX = nimo.fn.creatRandom(nimo.data.speedRange[0], nimo.data.speedRange[1]);
 72 if (oTempBall.speedX === 0) {
 73 oTempBall.speedX = 1
 74 }
 75 if (oTempBall.speedY === 0) {
 76 oTempBall.speedY = 1
 77 }
 78 oTempBall.speedY = nimo.fn.creatRandom(nimo.data.speedRange[0], nimo.data.speedRange[1]);
 79 nimo.balls.push(oTempBall)
 80 },
 81 drawBall: function (bStatic) {
 82 var i, iSize;
 83 nimo.content.clearRect(0, 0, nimo.ele.canvas.width, nimo.ele.canvas.height)
 84 for (var i = 0, iSize = nimo.balls.length; i < iSize; i++) {
 85 var oTarger = nimo.balls[i];
 86 nimo.content.beginPath();
 87 nimo.content.arc(oTarger.coordsX, oTarger.coordsY, oTarger.radius, 0, 10);
 88 nimo.content.fillStyle = oTarger.bgColor;
 89 nimo.content.fill();
 90 if (!bStatic) {
 91 if (oTarger.coordsX + oTarger.radius >= nimo.ele.canvas.width) {
 92 oTarger.speedX = -(Math.abs(oTarger.speedX))
 93 }
 94 if (oTarger.coordsX - oTarger.radius <= 0) {
 95 oTarger.speedX = Math.abs(oTarger.speedX)
 96 }
 97 if (oTarger.coordsY - oTarger.radius <= 0) {
 98 oTarger.speedY = Math.abs(oTarger.speedY)
 99 }
100 if (oTarger.coordsY + oTarger.radius >= nimo.ele.canvas.height) {
101 oTarger.speedY = -(Math.abs(oTarger.speedY))
102 }
103 oTarger.coordsX = oTarger.coordsX + oTarger.speedX;
104 oTarger.coordsY = oTarger.coordsY + oTarger.speedY;
105 }
106 }
107 },
108 run: function () {
109 nimo.fn.drawBall();
110 nimo.aniamted = setTimeout(function () {
111 nimo.fn.drawBall();
112 nimo.aniamted = setTimeout(arguments.callee, 10)
113 }, 10)
114 },
115 stop: function () {
116 clearTimeout(nimo.aniamted)
117 }
118 }
119 }
120 window.onload = function () {
121 nimo.fn.init();
122 var i;
123 for (var i = 0; i < 10; i++) {
124 nimo.fn.addBall();
125 }
126 nimo.fn.run();
127 document.getElementById('stop-kel'+'eyi-com').onclick = function () {
128 nimo.fn.stop()
129 }
130 document.getElementById('run-keley'+'i-com').onclick = function () {
131 nimo.fn.stop()
132 nimo.fn.run()
133 }
134 document.getElementById('addBall-k'+'eleyi-com').onclick = function () {
135 var i;
136 for (var i = 0; i < 10; i++) {
137 nimo.fn.addBall();
138 }
139 nimo.fn.drawBall(true);
140 }
141 }
142 </script>
143 </body>
144 </html>

web前端:http://www.cnblogs.com/jihua/p/webfront.html

<think>嗯,用户想用HTML、CSS和JavaScript实现一个小球弹跳动画效果。首先,我需要回忆一下相关的知识,看看之前提供的引用内容有没有可用的信息。根据引用[2]和引用[3],里面提到了小球弹跳的实现,可能需要用到CSS定位和JavaScript控制位置。 首先,结构部分应该用HTML创建一个容器,比如一个div元素作为小球。然后CSS设置小球的样式,比如圆形、颜色,并且需要position属性,这样JavaScript才能通过改变它的位置来实现动画。引用[2]中的例子用了position: relative,可能是一个起点。 接下来是动画逻辑。小球弹跳需要模拟物理效果,比如速度、重力和反弹。根据引用[3],他们提到小球碰到边界会反弹,所以需要检测碰撞。这里可能需要用到requestAnimationFrame来循环更新小球的位置。 然后考虑如何实现重力效果。每次更新时,垂直速度增加一个重力加速度的值,碰到底部时反转速度并应用阻尼,让弹跳逐渐减弱。引用[3]中的例子用到了速度调整和碰撞检测,可以参考这个思路。 用户可能还需要交互,比如点击按钮开始动画,或者调整速度。引用[2]里有一个按钮的例子,可以加入事件监听器。不过用户的需求里可能暂时不需要,但作为扩展功能可以提一下。 测试的时候要注意边界条件,比如小球碰到顶部、左右和底部时的处理。可能需要调整反弹的系数,让动画看起来更自然。此外,性能方面使用requestAnimationFrame会比setInterval更好,避免卡顿。 最后,整合代码,确保HTML结构正确,CSS样式到位,JavaScript逻辑没有错误。可能需要调试位置和速度的变化,确保小球运动流畅。引用[1]提到滚动触发的动画,虽然这里不需要滚动触发,但事件监听和动态更新的方法是类似的。 总结步骤:创建HTML结构,设置CSS样式,用JavaScript处理动画循环、速度变化和碰撞检测。测试并调整参数,比如重力、阻尼,以达到理想的弹跳效果。可能还需要考虑初始位置和速度的随机化,如引用[3]提到的,但用户的基本需求可能不需要,可以作为进阶建议。</think>### 分步实现小球弹跳动画效果 #### 一、HTML结构 ```html <div id="ball"></div> <button onclick="startAnimation()">开始弹跳</button> ``` #### 二、CSS样式设计 ```css #ball { width: 50px; height: 50px; background: #ff4757; border-radius: 50%; position: absolute; /* 关键定位属性 */ left: 0; top: 0; } ``` #### 三、JavaScript运动逻辑 ```javascript let ball = document.getElementById('ball'); let velocityY = 0; // 初始垂直速度 let posY = 0; // 垂直位置 let isAnimating = false; // 动画状态标记 const gravity = 0.5; // 重力加速度 const dampen = 0.7; // 碰撞能量衰减系数 function updatePosition() { velocityY += gravity; // 应用重力 posY += velocityY; // 底部碰撞检测(假设容器高度600px) if (posY > 550) { posY = 550; velocityY *= -dampen; // 反弹并衰减 } ball.style.top = posY + 'px'; if (Math.abs(velocityY) > 0.1) { // 停止条件 requestAnimationFrame(updatePosition); } else { isAnimating = false; } } function startAnimation() { if (!isAnimating) { isAnimating = true; posY = 0; velocityY = 0; requestAnimationFrame(updatePosition); } } ``` #### 四、效果增强建议 1. **水平运动**:添加$velocityX$变量和$posX$坐标,实现抛物线运动 2. **随机初始化**:通过`Math.random()`设置初始速度和位置 3. **拖拽交互**:通过`mousedown/mousemove/mouseup`事件实现拖拽投掷功能 #### 五、关键原理说明 1. **重力模拟**:通过公式$v = v_0 + at$实现速度变化[^2] 2. **能量衰减**:碰撞时速度乘以$0 < dampen < 1$系数实现动能损耗 3. **动画循环**:使用`requestAnimationFrame`保持60fps流畅动画[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值