Skip to content

Commit 9d52ce5

Browse files
author
Евгений А. Семенов
committed
init
0 parents  commit 9d52ce5

File tree

832 files changed

+240904
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

832 files changed

+240904
-0
lines changed

.DS_Store

12 KB
Binary file not shown.

app.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Including libraries
2+
var express = require('express');
3+
var app = express(),
4+
server = app.listen('8080'),
5+
io = require('socket.io').listen(server),
6+
static = require('node-static'); // for serving files
7+
8+
//var app = express();
9+
// This will make all the files in the current folder
10+
// accessible from the web
11+
// var fileServer = new static.Server('./');
12+
13+
// This is the port for our web server.
14+
// you will need to go to http://localhost:8080 to see it
15+
// app.listen(8080);
16+
17+
18+
app.engine('html', require('ejs').renderFile);
19+
20+
21+
app.get('/', function(req, res){
22+
res.render('../index.html')
23+
});
24+
app.use("/assets", express.static(__dirname + '/assets'));
25+
26+
27+
// If the URL of the socket server is opened in a browser
28+
function handler (request, response) {
29+
30+
request.addListener('end', function () {
31+
fileServer.serve(request, response);
32+
});
33+
}
34+
35+
// Delete this row if you want to see debug messages
36+
// io.set('log level', 1);
37+
38+
// Listen for incoming connections from clients
39+
io.sockets.on('connection', function (socket) {
40+
41+
// Start listening for mouse move events
42+
socket.on('mousemove', function (data) {
43+
44+
// This line sends the event (broadcasts it)
45+
// to everyone except the originating client.
46+
socket.broadcast.emit('moving', data);
47+
});
48+
});

assets/css/styles.css

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*-------------------------
2+
Simple reset
3+
--------------------------*/
4+
5+
6+
*{
7+
margin:0;
8+
padding:0;
9+
}
10+
11+
12+
/*-------------------------
13+
General Styles
14+
--------------------------*/
15+
16+
17+
html{
18+
background:url('../img/bg.png') repeat #e4e4e4;
19+
20+
/* This will hide the extra canvas if the screen is small */
21+
overflow:hidden;
22+
}
23+
24+
body{
25+
font:14px/1.3 'Segoe UI',Arial, sans-serif;
26+
color:#444;
27+
}
28+
29+
a, a:visited {
30+
outline:none;
31+
color:#43819b;
32+
}
33+
34+
a:hover{
35+
text-decoration:none;
36+
}
37+
38+
section, footer, header{
39+
display: block;
40+
}
41+
42+
43+
/*-------------------------
44+
Main Area
45+
--------------------------*/
46+
47+
hgroup{
48+
position: fixed;
49+
background-color: rgba(255, 255, 255, 0.7);
50+
width: 400px;
51+
height: 78px;
52+
top: 50%;
53+
left: 50%;
54+
margin: -120px 0 0 -225px;
55+
text-align: center;
56+
padding: 25px;
57+
border-radius: 10px;
58+
box-shadow: 0 0 3px rgba(100, 100, 100, 0.1), 0 3px 1px #D4D4D4;
59+
color: #555;
60+
}
61+
62+
h1{
63+
font-size: 22px;
64+
padding-bottom: 8px;
65+
}
66+
67+
h2{
68+
font-size: 13px;
69+
font-weight: normal;
70+
padding-bottom: 8px;
71+
}
72+
73+
h3{
74+
font-size: 12px;
75+
font-weight: normal;
76+
font-style: italic;
77+
color: #999;
78+
}
79+
80+
#cursors{
81+
position:relative;
82+
}
83+
84+
#cursors .cursor{
85+
position:absolute;
86+
width:15px;
87+
height:22px;
88+
background:url('../img/pointer.png') no-repeat -4px 0;
89+
}
90+
91+
92+
93+
/*----------------------------
94+
The Footer
95+
-----------------------------*/
96+
97+
98+
footer{
99+
background-color: #111111;
100+
bottom: 0;
101+
box-shadow: 0 -1px 2px rgba(0,0,0,0.4);
102+
height: 45px;
103+
left: 0;
104+
position: fixed;
105+
width: 100%;
106+
z-index: 100000;
107+
}
108+
109+
footer h2{
110+
color: #EEEEEE;
111+
font-size: 14px;
112+
font-weight: normal;
113+
left: 50%;
114+
margin-left: -400px;
115+
padding: 13px 0 0;
116+
position: absolute;
117+
width: 540px;
118+
}
119+
120+
footer h2 i{
121+
font-style:normal;
122+
color:#888;
123+
}
124+
125+
footer a.tzine,a.tzine:visited{
126+
color: #999999;
127+
font-size: 12px;
128+
left: 50%;
129+
margin: 16px 0 0 110px;
130+
position: absolute;
131+
text-decoration: none;
132+
top: 0;
133+
}
134+
135+
footer a i{
136+
color:#ccc;
137+
font-style: normal;
138+
}
139+
140+
footer a i b{
141+
color:#c92020;
142+
font-weight: normal;
143+
}

assets/img/bg.png

224 Bytes
Loading

assets/img/pointer.png

519 Bytes
Loading

assets/js/script.js

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
$(function(){
2+
3+
// This demo depends on the canvas element
4+
if(!('getContext' in document.createElement('canvas'))){
5+
alert('Sorry, it looks like your browser does not support canvas!');
6+
return false;
7+
}
8+
9+
// The URL of your web server (the port is set in app.js)
10+
var url = 'http://localhost:8080';
11+
12+
var doc = $(document),
13+
win = $(window),
14+
canvas = $('#paper'),
15+
ctx = canvas[0].getContext('2d'),
16+
instructions = $('#instructions');
17+
18+
// Generate an unique ID
19+
var id = Math.round($.now()*Math.random());
20+
21+
// A flag for drawing activity
22+
var drawing = false;
23+
24+
var clients = {};
25+
var cursors = {};
26+
27+
var socket = io.connect(url);
28+
29+
socket.on('moving', function (data) {
30+
31+
if(! (data.id in clients)){
32+
// a new user has come online. create a cursor for them
33+
cursors[data.id] = $('<div class="cursor">').appendTo('#cursors');
34+
}
35+
36+
// Move the mouse pointer
37+
cursors[data.id].css({
38+
'left' : data.x,
39+
'top' : data.y
40+
});
41+
42+
// Is the user drawing?
43+
if(data.drawing && clients[data.id]){
44+
45+
// Draw a line on the canvas. clients[data.id] holds
46+
// the previous position of this user's mouse pointer
47+
48+
drawLine(clients[data.id].x, clients[data.id].y, data.x, data.y);
49+
}
50+
51+
// Saving the current client state
52+
clients[data.id] = data;
53+
clients[data.id].updated = $.now();
54+
});
55+
56+
var prev = {};
57+
58+
canvas.on('mousedown',function(e){
59+
e.preventDefault();
60+
drawing = true;
61+
prev.x = e.pageX;
62+
prev.y = e.pageY;
63+
64+
// Hide the instructions
65+
instructions.fadeOut();
66+
});
67+
68+
doc.bind('mouseup mouseleave',function(){
69+
drawing = false;
70+
});
71+
72+
var lastEmit = $.now();
73+
74+
doc.on('mousemove',function(e){
75+
if($.now() - lastEmit > 30){
76+
socket.emit('mousemove',{
77+
'x': e.pageX,
78+
'y': e.pageY,
79+
'drawing': drawing,
80+
'id': id
81+
});
82+
lastEmit = $.now();
83+
}
84+
85+
// Draw a line for the current user's movement, as it is
86+
// not received in the socket.on('moving') event above
87+
88+
if(drawing){
89+
90+
drawLine(prev.x, prev.y, e.pageX, e.pageY);
91+
92+
prev.x = e.pageX;
93+
prev.y = e.pageY;
94+
}
95+
});
96+
97+
// Remove inactive clients after 10 seconds of inactivity
98+
setInterval(function(){
99+
100+
for(ident in clients){
101+
if($.now() - clients[ident].updated > 10000){
102+
103+
// Last update was more than 10 seconds ago.
104+
// This user has probably closed the page
105+
106+
cursors[ident].remove();
107+
delete clients[ident];
108+
delete cursors[ident];
109+
}
110+
}
111+
112+
},10000);
113+
114+
function drawLine(fromx, fromy, tox, toy){
115+
ctx.moveTo(fromx, fromy);
116+
ctx.lineTo(tox, toy);
117+
ctx.stroke();
118+
}
119+
120+
});

index.html

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Node.js Multiplayer Drawing Game | Tutorialzine Demo</title>
6+
7+
<!-- The stylesheets -->
8+
<link rel="stylesheet" href="assets/css/styles.css" />
9+
10+
<!--[if lt IE 9]>
11+
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
12+
<![endif]-->
13+
</head>
14+
15+
<body>
16+
<div id="cursors">
17+
<!-- The mouse pointers will be created here -->
18+
</div>
19+
20+
<canvas id="paper" width="1900" height="1000">
21+
Your browser needs to support canvas for this to work!
22+
</canvas>
23+
24+
<hgroup id="instructions">
25+
<h1>Draw anywhere!</h1>
26+
<h2>You will see everyone else who's doing the same.</h2>
27+
<h3>Tip: if the stage gets dirty, simply reload the page</h3>
28+
</hgroup>
29+
30+
<footer>
31+
<h2><i>Tutorial:</i> Node.js Multiplayer Drawing Game</h2>
32+
<a class="tzine" href="http://tutorialzine.com/2012/08/nodejs-drawing-game/">Head on to <i>Tutorial<b>zine</b></i> to read and download</a>
33+
</footer>
34+
35+
<!-- JavaScript includes. Notice that socket.io.js is served by node.js -->
36+
<script src="/socket.io/socket.io.js"></script>
37+
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
38+
<script src="assets/js/script.js"></script>
39+
40+
</body>
41+
</html>

node_modules/.bin/static

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/ejs/.gitmodules

Whitespace-only changes.

node_modules/ejs/.npmignore

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/ejs/.travis.yml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)