-
Notifications
You must be signed in to change notification settings - Fork 0
/
livewall.js
98 lines (75 loc) · 2.57 KB
/
livewall.js
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
//--------------------------------------------------------------
// handles the socket.io connections and live streaming
var config = require('./config');
//socket.io instance
var io;
//the canvas constructor
var Canvas = require('canvas');
//a global canvas object that will be the 'database' almost...
var canvas;
module.exports.init = function(socketio, cb)
{
//keep reference
io = socketio;
//what to do with a new connection
io.sockets.on('connection', function (socket) {
//need to send them the live image
sendLiveImage(socket);
//assign listeners
socket.on('draw', function(data) {
drawChannel(socket, data);
});
//standard chat channel
socket.on('chat', function(data) {
io.sockets.emit('chat', data); //just pass everything on
});
});
//create a global canvas to use
clearCanvas();
//start up a cronjob to make a new image every hour (at half past)
var cronJob = require('cron').CronJob;
new cronJob('1 30 * * * *', function(){
var fs = require('fs')
, out = fs.createWriteStream(config.web.temp_dir + new Date().getTime() + '.png')
, stream = canvas.createPNGStream();
stream.on('data', function(chunk){
out.write(chunk);
});
stream.on('end', function(){
//create a new canvas so everyone can start again
clearCanvas();
//tell everyone about it
io.sockets.emit('clear', 'true');
});
}, null, true);
cb();
}
//called when a socket sends a chunk of data
function drawChannel(socket, data)
{
//need to update main drawing image
var ctx = canvas.getContext("2d"); //the context to draw to
ctx.strokeStyle = '#' + data.fill;
ctx.beginPath();
ctx.moveTo(data.x1,data.y1);
ctx.lineTo(data.x2,data.y2);
ctx.closePath();
ctx.stroke();
//send update to all other clients
io.sockets.emit('draw', data);
}
//sends the current live image to a socket
function sendLiveImage(socket)
{
canvas.toDataURL('image/png', function(err, str){
socket.emit('image', str);
});
}
//creates a new canvas with a white background
function clearCanvas()
{
canvas = new Canvas(config.canvas_width, config.canvas_height);
var ctx = canvas.getContext("2d");
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(0, 0, config.canvas_width, config.canvas_height);
}