You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
4.2 KiB
172 lines
4.2 KiB
<!DOCTYPE html>
|
|
<meta charset="utf-8" />
|
|
<head>
|
|
<meta name="apple-mobile-web-app-capable" content="yes" />
|
|
<meta name="viewport" content="user-scalable=no, width=device-width" />
|
|
|
|
<title>WebSocket Test</title>
|
|
<style type="text/css">
|
|
body {
|
|
font-size: 16pt;
|
|
margin: 0px;
|
|
overflow: hidden;
|
|
height: 100%;
|
|
}
|
|
#ballscreen {
|
|
position: absolute;
|
|
background-color: rgba(0.0, 0.0, 0.0, 0.0);
|
|
width: 320px;
|
|
height: 480px;
|
|
border: 1px solid black;
|
|
}
|
|
.ball {
|
|
-webkit-transition: left 0.5s linear, top 0.5s linear;
|
|
position: absolute;
|
|
}
|
|
.ball .bullet {
|
|
width: 10px;
|
|
height: 10px;
|
|
position: absolute;
|
|
left: -5px;
|
|
top: -5px;
|
|
border-radius: 5px;
|
|
background-color: #f00;
|
|
}
|
|
#svgscreen {
|
|
background-color: #adf;
|
|
position: absolute;
|
|
width: 320px;
|
|
height: 480px;
|
|
}
|
|
#svgscreen line {
|
|
stroke: rgb(255,0,0);
|
|
stroke-width: 2;
|
|
}
|
|
</style>
|
|
<script type="text/javascript" src="connection.js"></script>
|
|
<script type="text/javascript" src="websocket.js"></script>
|
|
<script type="text/javascript" src="drawing.js"></script>
|
|
<script type="text/javascript" src="rendering.js"></script>
|
|
<script language="javascript" type="text/javascript">
|
|
var myName = Math.random().toString(36).substring(7);
|
|
var websocket;
|
|
var ballscreen;
|
|
var svgscreen;
|
|
var current_svgline;
|
|
var balls_online = new Array();
|
|
var svglines_online = new Array();
|
|
var speed = 100;
|
|
|
|
function init() {
|
|
var statusbar = document.getElementById('status');
|
|
ballscreen = document.getElementById("ballscreen");
|
|
document.getElementById("myName").innerHTML = myName;
|
|
svgscreen = document.getElementById("svgscreen");
|
|
|
|
if ("WebSocket" in window){
|
|
init_drawing(ballscreen, drawStart, drawMove, drawEnd, drawRemove);
|
|
init_websocket(statusbar, onOpen, onMessage);
|
|
} else {
|
|
statusbar.innerHTML = 'No websockets :(. This means that you either use <em>Internet Explorer</em> or <em>Android</em>. In both cases, I suggest that you use <a href="http://www.google.com/chrome/">Chrome</a> or <a href="http://www.getfirefox.net/">Firefox</a>!';
|
|
}
|
|
}
|
|
|
|
function onOpen(evt) {
|
|
doSend();
|
|
}
|
|
|
|
function onMessage(evt) {
|
|
var ret = JSON.parse(evt.data);
|
|
var people = ret.people;
|
|
var balls = ret.balls;
|
|
var lines = ret.lines;
|
|
|
|
for(ball in balls_online){
|
|
ball.should_remove = true;
|
|
}
|
|
for (var i = balls.length - 1; i >= 0; i--) {
|
|
var ball = document.getElementById(balls[i].information);
|
|
if(ball){
|
|
ball.style.left = balls[i].position.x + "px";
|
|
ball.style.top = balls[i].position.y + "px";
|
|
ball.should_remove = false;
|
|
} else {
|
|
var ball = document.createElement('div');
|
|
ball.should_remove = false;
|
|
ball.id = balls[i].information;
|
|
ball.className = 'ball';
|
|
ball.innerHTML = '<div class="bullet"></div>';
|
|
ball.style.left = balls[i].position.x + "px";
|
|
ball.style.top = balls[i].position.y + "px";
|
|
|
|
ballscreen.appendChild(ball);
|
|
balls_online.push(ball);
|
|
}
|
|
}
|
|
for (var i = balls_online.length - 1; i >= 0; i--) {
|
|
if(balls_online[i].should_remove){
|
|
ballscreen.removeChild(balls_online[i]);
|
|
balls_online.splice(i, 1);
|
|
}
|
|
};
|
|
|
|
for (var i = svglines_online.length - 1; i >= 0; i--) {
|
|
svgscreen.removeChild(svglines_online[i]);
|
|
};
|
|
svglines_online = new Array();
|
|
for (var i = lines.length - 1; i >= 0; i--) {
|
|
svglines_online.push(addLine(lines[i], svgscreen));
|
|
};
|
|
|
|
|
|
setTimeout(function(){
|
|
doSend();
|
|
}, speed);
|
|
}
|
|
|
|
function drawStart(line){
|
|
current_svgline = addLine(line, svgscreen);
|
|
}
|
|
|
|
function drawMove(line){
|
|
updateLine(current_svgline, line);
|
|
}
|
|
|
|
function drawEnd(line){
|
|
console.log(JSON.stringify(line));
|
|
drawRemove(line);
|
|
}
|
|
|
|
function drawRemove(line){
|
|
if(current_svgline){
|
|
svgscreen.removeChild(current_svgline);
|
|
current_svgline = null;
|
|
}
|
|
}
|
|
|
|
function doSend(message) {
|
|
var myself = {
|
|
name: myName,
|
|
size: {width: window.innerWidth, height: window.innerHeight}
|
|
};
|
|
|
|
websocket.send(JSON.stringify(myself));
|
|
}
|
|
|
|
window.addEventListener("load", init, false);
|
|
|
|
setTimeout(function (){
|
|
window.scrollTo(0, 1);
|
|
}, 0);
|
|
</script>
|
|
</head>
|
|
<body ontouchmove="prevent_scrolling(event);">
|
|
|
|
<p>My name is: <span id="myName"></span></p>
|
|
<p>Status: <span id="status">CONNECTING...</span></p>
|
|
|
|
<svg id="svgscreen" xmlns="http://www.w3.org/2000/svg" version="1.1"></svg>
|
|
<div id="ballscreen"></div>
|
|
|
|
</body>
|
|
</html>
|
|
|