js 類比氣泡屏保效果代碼

來源:互聯網
上載者:User

核心代碼: 複製代碼 代碼如下:var T$ = function(id) { return document.getElementById(id); }
var $extend = function(des, src) { for (var p in src) { des[p] = src[p]} return des; }
var Bubble = function() {
// 小球隨機樣式
var clss = ['ball_one', 'ball_two', 'ball_three', 'ball_four', 'ball_five', 'ball_six'];
var Ball = function(radius, clsname) {
var ball = document.createElement('div');
ball.className = clsname;
with(ball.style) {
width = height = (radius || 10) + 'px'; position = 'absolute';
}
return ball;
};
// 屏保主類
var Screen = function(cid, config) {
var self = this;
if (!(self instanceof Screen)) {
return new Screen(cid, config);
}
self.container = T$(cid);
if (!self.container) return;
config = $extend(Screen.Config, config || {});
// 配置屬性
self.ballsnum = config.ballsnum;
self.diameter = 55;
self.radius = self.diameter / 2;
self.bounce = config.bounce;
self.spring = config.spring;
self.gravity = config.gravity;
self.balls = [];
self.timer = null;
// 上下左右邊界
self.T_bound = 0;
self.B_bound = self.container.clientHeight;
self.L_bound = 0;
self.R_bound = self.container.clientWidth;
};
// 靜態屬性
Screen.Config = {
ballsnum: 5, // 小球數目
spring: 0.8, // 彈力加速度
bounce: -0.95, // 反彈
gravity: 0.1 // 重力
};
Screen.prototype = {
initialize: function() {
var self = this;
// 產生小球
self.createBalls();
// 偵聽碰撞
self.timer = setInterval(function() {
self.hitTest();
}, 32);
},
createBalls: function() {
var self = this, num = self.ballsnum, i = 0;
var frag = document.createDocumentFragment();
for (; i < num; i++) {
var ball = new Ball(self.diameter, clss[Math.floor(Math.random() * (clss.length - 1))]);
ball.radius = self.radius;
ball.diameter = self.diameter;
ball.style.left = (Math.random() * self.B_bound) + 'px';
ball.style.top = (Math.random() * self.R_bound) + 'px';
ball.vx = Math.random() * 6 - 3;
ball.vy = Math.random() * 6 - 3;
frag.appendChild(ball);
self.balls[i] = ball;
}
self.container.appendChild(frag);
},
// 碰撞檢測
hitTest: function() {
var self = this, num = self.ballsnum, balls = self.balls;
for (var i = 0; i < num - 1; i++) {
var ball0 = balls[i];
ball0.x = ball0.offsetLeft + ball0.radius;
ball0.y = ball0.offsetTop + ball0.radius;
for (var j = i + 1; j < num; j++) {
var ball1 = balls[j];
ball1.x = ball1.offsetLeft + ball1.radius;
ball1.y = ball1.offsetTop + ball1.radius;
var dx = ball1.x - ball0.x;
var dy = ball1.y - ball0.y;
var dist = Math.sqrt(dx * dx + dy * dy);
var misDist = ball0.radius + ball1.radius;
if (dist < misDist) {
var angle = Math.atan2(dy, dx);
var tx = ball0.x + Math.cos(angle) * misDist;
var ty = ball0.y + Math.sin(angle) * misDist;
var ax = (tx - ball1.x) * self.spring;
var ay = (ty - ball1.y) * self.spring;
ball0.vx -= ax;
ball0.vy -= ay;
ball1.vx += ax;
ball1.vy += ay;
}
}
}
for (var i = 0; i < num; i++) {
self.move(balls[i]);
}
},
// 氣泡運動
move: function(ball) {
var self = this;
ball.vy += self.gravity;
ball.style.left = (ball.offsetLeft + ball.vx) + 'px';
ball.style.top = (ball.offsetTop + ball.vy) + 'px';
// 邊界檢測
var T = self.T_bound, B = self.B_bound, L = self.L_bound, R = self.R_bound, BC = self.bounce;
if (ball.offsetLeft + ball.diameter > R) {
ball.style.left = R - ball.diameter + 'px';
ball.vx *= BC;
} else if (ball.offsetLeft < L) {
ball.style.left = L + 'px';
ball.vx *= BC;
}
if (ball.offsetTop + ball.diameter > B) {
ball.style.top = B - ball.diameter + 'px';
ball.vy *= BC;
} else if (ball.offsetTop < T) {
ball.style.top = T + 'px';
ball.vy *= BC;
}
}
};
return { Screen: Screen }
}();
window.onload = function() {
var sc = null;
T$('start').onclick = function() {
document.getElementById('inner').innerHTML = '';
sc = Bubble.Screen('inner', { ballsnum: 5, spring: 0.8, bounce: -0.95, gravity: 0.1});
sc.initialize();
};
T$('stop').onclick = function() { clearInterval(sc.timer); }
var bound = false
T$('change').onclick = function() {
if (!bound) {
T$('screen').style.backgroundImage = 'url("http://demo.jb51.net/js/bubbling/o_bg1.jpg")';
bound = true;
} else {
T$('screen').style.backgroundImage = 'url("http://demo.jb51.net/js/bubbling/o_bg2.jpg")';
bound = false;
}
}
}

【說明】
程式效率出現了很大瓶頸。需要做的最佳化還有很多。有時間繼續完善。
另:感謝羅浮宮群友逍遙君武和豪情對圖片的支援。
【源碼下載】
http://www.jb51.net/jiaoben/28295.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.