forked from jmesnil/board-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.js
More file actions
140 lines (121 loc) · 3.5 KB
/
board.js
File metadata and controls
140 lines (121 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
(function(windows) {
var canvas = document.getElementById('board');
var Board = {
WIDTH: 320,
HEIGHT: 460,
CIRCLE_RADIUS: 32,
TIMEOUT: 1000, // in ms
REFRESH: 100 // in ms
};
// the own piece of the client
Board.myPiece = {
center: {
x: Board.WIDTH / 2,
y: Board.HEIGHT / 2,
xShift: 0,
yShift: 0
},
color: '#000',
// myPiece may never be updated if the browser
// does not support the DeviceOrientation Event API
updated: false
};
// the pieces of the other clients which are received from the server
var pieces = {};
var randomColor = function() {
var red = Math.floor(Math.random() * 255);
var green = Math.floor(Math.random() * 255);
var blue = Math.floor(Math.random() * 255);
return 'rgb(' + red + ',' + green + ',' + blue + ')';
}
Board.put = function(id, piece) {
if (pieces[id]) {
piece.color = pieces[id].color;
} else {
piece.color = randomColor();
}
pieces[id] = piece;
pieces[id].timestamp = new Date().getTime();
};
var draw = function() {
canvas.getContext('2d').clearRect(0, 0, Board.WIDTH, Board.HEIGHT);
var buffer = document.createElement('canvas');
buffer.width = Board.WIDTH;
buffer.height = Board.HEIGHT;
drawBuffer(buffer.getContext('2d'));
canvas.getContext('2d').drawImage(buffer, 0, 0);
};
var drawBuffer = function(context) {
drawBoard(context);
var now = new Date().getTime();
for (id in pieces) {
if (pieces.hasOwnProperty(id)) {
var piece = pieces[id];
if (now - piece.timestamp > Board.TIMEOUT) {
delete pieces[id];
} else {
drawPiece(context, piece);
}
}
}
if (Board.myPiece.updated) {
drawPiece(context, Board.myPiece);
}
};
var drawBoard = function(context) {
context.save();
context.beginPath();
for (var x = 0.5; x < Board.WIDTH; x += 10) {
context.moveTo(x, 0);
context.lineTo(x, Board.HEIGHT);
}
for (var y = 0.5; y < Board.HEIGHT; y += 10) {
context.moveTo(0, y);
context.lineTo(Board.WIDTH, y);
}
context.closePath();
context.strokeStyle = '#eee';
context.stroke();
context.restore();
};
var drawPiece = function(context, piece) {
context.save();
context.fillStyle = piece.color;
context.beginPath();
context.arc(piece.center.x, piece.center.y,
Board.CIRCLE_RADIUS, 0, Math.PI * 2, false);
context.closePath();
context.fill();
context.restore();
};
Board.updateCenter = function(acceleration) {
c = Board.myPiece.center;
c.xShift = c.xShift * 0.8 + acceleration.x * 2.0;
c.yShift = c.yShift * 0.8 + acceleration.y * 2.0;
c.x = Math.floor(c.x + c.xShift);
// use *minus* to compute the center's new y
c.y = Math.floor(c.y - c.yShift);
// do not go outside the boundaries of the canvas
if (c.x < Board.CIRCLE_RADIUS) {
c.x = Board.CIRCLE_RADIUS;
}
if (c.x > Board.WIDTH - Board.CIRCLE_RADIUS) {
c.x = Board.WIDTH - Board.CIRCLE_RADIUS;
}
if (c.y < Board.CIRCLE_RADIUS) {
c.y = Board.CIRCLE_RADIUS;
}
if (c.y > Board.HEIGHT - Board.CIRCLE_RADIUS) {
c.y = Board.HEIGHT - Board.CIRCLE_RADIUS;
}
Board.myPiece.center = c;
Board.myPiece.updated = true;
};
// the canvas is drawn at regular interval
setInterval(function() {
draw();
}, Board.REFRESH);
// first time, only the board will be drawn
draw();
window.Board = Board;
})(window);