Browse Source

Some changes

master
Joshua Moerman 12 years ago
parent
commit
e2b78d1e22
  1. 2
      beats.sublime-project
  2. 28
      index.js

2
beats.sublime-project

@ -2,7 +2,7 @@
"folders":
[
{
"path": "/Users/joshua/Documents/www/vadovas/beats"
"path": "./"
}
]
}

28
index.js

@ -1,10 +1,14 @@
var myName;
var websocket;
var statusbar
var ballscreen;
var svgscreen;
var current_svgline;
var balls_online = new Array();
var svglines_online = new Array();
var speed = 100;
var line_kind = 1;
@ -14,7 +18,7 @@ function init() {
var login = document.getElementById('login');
login.parentNode.removeChild(login);
var statusbar = document.getElementById('status');
statusbar = document.getElementById('status');
ballscreen = document.getElementById("ballscreen");
svgscreen = document.getElementById("svgscreen");
@ -31,40 +35,45 @@ function init() {
function onOpen(evt) {
updateUser();
setInterval(poll, speed);
}
function onMessage(evt) {
console.log(evt.data);
var ret = JSON.parse(evt.data);
var balls = ret.balls;
var lines = ret.lines;
statusbar.innerHTML = balls.length;
if(balls){
console.log('updating balls');
updateBalls(balls);
}
if(lines){
console.log('updating lines');
updateLines(lines);
//updateLines(lines);
}
setTimeout(function(){
poll();
}, speed);
}
function updateBalls(balls){
// Because we use transitions for the balls
// we really update them, and only delete the ones which are absent
var updates = 0;
var news = 0;
var removed = 0;
for(i in balls_online){
balls_online[i].should_remove = true;
}
for (var i = balls.length - 1; i >= 0; i--) {
var ball = document.getElementById(balls[i].information);
if(ball){
++updates;
ball.style.left = balls[i].position.x + "px";
ball.style.top = balls[i].position.y + "px";
ball.should_remove = false;
} else {
++news;
var ball = document.createElement('div');
ball.should_remove = false;
ball.id = balls[i].information;
@ -79,10 +88,13 @@ function updateBalls(balls){
}
for (var i = balls_online.length - 1; i >= 0; i--) {
if(balls_online[i].should_remove){
++removed
ballscreen.removeChild(balls_online[i]);
balls_online.splice(i, 1);
}
};
statusbar.innerHTML = updates + ', ' + news + ', ' + removed;
}
function updateLines(lines){