Skip to content

Commit 2d41cb9

Browse files
committed
multiple canvas worlds now
1 parent 23be03d commit 2d41cb9

File tree

4 files changed

+60
-26
lines changed

4 files changed

+60
-26
lines changed

public/draw.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ $(function(){
1414
setColor = '#000';
1515
defaultName = "Guest",
1616
draggingTool = false,
17+
canvasName = $('#canvasName').text(),
1718
dragging = false;
1819
alertify.set({ delay: 1000*30 });
1920

@@ -30,10 +31,18 @@ $(function(){
3031
var clients = {};
3132
var cursors = {};
3233

34+
if(canvasName === ""){
35+
canvasName = "lobby";
36+
$('#canvasName').text('Main Lobby');
37+
}
38+
3339
var socket = io.connect();
3440

3541
socket.on('connect', function(){
3642
$('#bigTitle').text('Connected to server');
43+
socket.emit('drawActionHistory', {
44+
canvasName: canvasName
45+
});
3746
});
3847

3948
socket.on('moving', function (data) {
@@ -75,7 +84,7 @@ $(function(){
7584
$('#bigTitle').text("Rendering history...");
7685
//offscreen canvas
7786
var osc = document.createElement('canvas');
78-
osc.width = 1600;
87+
osc.width = 1900;
7988
osc.height = 1000;
8089
var osctx = osc.getContext('2d');
8190

@@ -155,7 +164,8 @@ $(function(){
155164
'y': e.pageY,
156165
'drawing': drawing,
157166
'id': id,
158-
'color': setColor
167+
'color': setColor,
168+
'canvasName': canvasName
159169
});
160170
lastEmit = $.now();
161171
}
@@ -230,7 +240,7 @@ $(function(){
230240
$('#settingsPanel').animate({bottom: 0}, 200);
231241
settingsTabOpen = true;
232242
} else {
233-
$('#settingsPanel').animate({bottom: -300}, 200);
243+
$('#settingsPanel').animate({bottom: -320}, 200);
234244
settingsTabOpen = false;
235245
}
236246

public/style.css

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ h3{
134134
#settingsPanel {
135135
position: fixed;
136136
right: 20px;
137-
bottom: -300px;
137+
bottom: -320px;
138138
width: 200px;
139-
height: 300px;
139+
height: 320px;
140140
background-color: #3C5969;
141141
z-index: 12;
142142
border-radius: 10px 0px 0px 0px;
@@ -145,7 +145,7 @@ h3{
145145

146146
#settingsPanel {
147147
color: white;
148-
font-size: 0.8em;
148+
font-size: 0.85em;
149149
}
150150
#settingsPanel input, p, label, table {
151151
margin: 10px;
@@ -158,6 +158,17 @@ h3{
158158
display: none;
159159
}
160160

161+
#settingsPanel h2 {
162+
font-size: 1.6em;
163+
text-align: center;
164+
}
165+
166+
#settingsPanel h3 {
167+
font-size: 1.1em;
168+
text-align: center;
169+
text-transform: uppercase;
170+
}
171+
161172

162173
#bottomAttribution {
163174
position: absolute;

server.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ new compressor.minify({
5050

5151
app.use(compression())
5252
app.use(express.static(__dirname + '/public', {maxAge: 60*60*24*1000}));
53-
app.use(morgan('dev'));
53+
app.use(morgan('combined'));
5454

5555
app.set('views', __dirname + '/views');
5656
app.set('view engine', 'jade');
@@ -64,7 +64,7 @@ app.get('/', function(req, res){
6464

6565
/* User canvas */
6666
app.get("/c/:canvasname", function (req, res, next) {
67-
res.render('main.jade');
67+
res.render('main.jade', {canvasName: req.params.canvasname});
6868
});
6969

7070

@@ -76,22 +76,29 @@ redisClient.on("error", function(err){
7676
redisClient.setnx("clientcount", 0);
7777

7878
io.sockets.on('connection', function(socket){
79-
var drawActions = [];
8079
localConnectedClients += 1;
8180
redisClient.incr("clientcount");
8281

83-
//TODO Need to optimise this
84-
redisClient.lrange("drawactions", 0, -1, function(err, replies){
85-
replies.forEach(function(reply, i){
86-
drawActions.push(JSON.parse(reply));
87-
});
82+
socket.on('drawActionHistory', function(data){
83+
//client has asked for all the draw action history.
84+
var drawActions = [];
85+
var key = "drawactions:"+data.canvasName;
86+
redisClient.lrange(key, 0, -1, function(err, replies){
87+
if(err){
88+
console.log("Error fetching draw history key: " + key);
89+
return;
90+
}
91+
replies.forEach(function(reply, i){
92+
drawActions.push(JSON.parse(reply));
93+
});
8894

89-
//compress drawaction history data
90-
var compressedActions = lzwCompress.pack(drawActions)
91-
//send it
92-
socket.emit('drawActionHistory', compressedActions);
93-
//increment netusage tracker.
94-
netUsage += sizeof(compressedActions);
95+
//compress drawaction history data
96+
var compressedActions = lzwCompress.pack(drawActions)
97+
//send it
98+
socket.emit('drawActionHistory', compressedActions);
99+
//increment netusage tracker.
100+
netUsage += sizeof(compressedActions);
101+
});
95102
});
96103

97104
socket.on('mousemove', function(data){
@@ -106,9 +113,8 @@ io.sockets.on('connection', function(socket){
106113
toY: data.y,
107114
color: data.color
108115
};
109-
110-
redisClient.rpush("drawactions",
111-
JSON.stringify(drawAction));
116+
var key = "drawactions:"+data.canvasName;
117+
redisClient.rpush(key, JSON.stringify(drawAction));
112118
}
113119

114120
} else {
@@ -146,10 +152,12 @@ io.sockets.on('connection', function(socket){
146152
netUsageKB: (netUsage / 1000).toFixed(1),
147153
nodeConnections: localConnectedClients
148154
};
149-
155+
156+
/*
150157
multi.llen("drawactions", function(err, reply){
151158
data['drawStackSize'] = reply;
152159
});
160+
*/
153161

154162
multi.get('clientcount', function(err, reply){
155163
data['globalConnectedClients'] = reply;

views/main.jade

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
doctype html
22
html
33
head
4-
title udraw: Multiplayer Drawing
4+
if canvasName
5+
title #{canvasName} | udraw Multiplay Drawing
6+
else
7+
title udraw: Multiplayer Drawing
58
meta(name="viewport" content="initial-scale=1")
69
meta(name="description" content="Draw with everyone else in realtime on a HTML5 canvas")
710
meta(property="og:url" content="https://udraw.me")
@@ -33,14 +36,16 @@ html
3336
h3 Note: Drawings are saved on the server. Come back any time!
3437
div#settingsPanel
3538
div#settingsPanelTab Settings
39+
h2 You're drawing in
40+
h3#canvasName #{canvasName}
3641
label(for="usernameInput") Chat Name
3742
input(type="text" id="usernameInput" placeholder="Username: Guest")
3843

3944
table#serverStats
4045
div#bottomAttribution
4146
p
4247
a(id="statsShow" href="javascript:void(0)") Server Stats
43-
p node-multidraw v0.5 by <a href="https://timatooth.com">Tim Sullivan</a>
48+
p v0.5 by <a href="https://timatooth.com">Tim Sullivan</a>
4449
p Source code on <a href="https://github.com/timatooth/node-multidraw">GitHub</a>
4550
input(type="text" id="chatBox" placeholder="Chat" maxlength="135")
4651
script(src="//code.jquery.com/jquery-2.1.1.min.js")

0 commit comments

Comments
 (0)