-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (85 loc) · 2.56 KB
/
app.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
99
100
101
const express = require('express')
const app = express()
var http = require('http').Server(app);
var io = require('socket.io')(http);
var probe = require('probe-image-size');
const { createCanvas, loadImage } = require('canvas')
const fs = require('fs')
var port = process.env.PORT || 8080;
var line_history = [];
var lastSave = 0;
var saveInterval = 3000;
var saveScheduled = false;
var _static = require('node-static'); // for serving files
var fileServer = new _static.Server('./');
app.use(express.static('public'));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
saveCanvas();
// Listen for incoming connections from clients
io.origins('*:*');
io.sockets.on('connection', function (socket) {
console.log("client connected: " + socket.id);
for (var i in line_history) {
socket.emit('drawline', { line: line_history[i] } );
}
socket.on("pointer", function(data) {
console.log("client " + socket.id + " is a pointer :)");
});
socket.on('drawline', function (data) {
line_history.push(data.line);
trySaveCanvas();
io.emit('drawline', { line: data.line });
});
socket.on('clearall', function (data) {
line_history = [];
trySaveCanvas();
io.emit('clearall');
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
// taking care that canvas gets saved minimally each x milliseconds (x = saveInterval)
function trySaveCanvas() {
if(saveScheduled) return;
if(new Date().getTime() - lastSave > saveInterval) {
saveCanvas();
lastSave = new Date().getTime();
} else {
if(!saveScheduled) {
saveScheduled = true;
setTimeout(scheduledSaveCanvas, 500);
}
}
}
function scheduledSaveCanvas() {
saveScheduled = false;
trySaveCanvas();
}
function saveCanvas() {
var data = probe.sync(fs.readFileSync(__dirname + '/public/img/segmentationinput.png'));
var w = data.width;
var h = data.height;
const canvas = createCanvas(w, h);
const ctx = canvas.getContext('2d');
//background
ctx.fillStyle = 'black';
ctx.fillRect(0,0,w,h);
for (var i in line_history) {
drawLine(ctx, line_history[i]);
}
const out = fs.createWriteStream(__dirname + '/public/img/labeling.png');
const stream = canvas.createPNGStream();
stream.pipe(out);
out.on('finish', () => console.log('saved /public/img/labeling.png'));
}
function drawLine(context, line) {
context.beginPath();
context.strokeStyle = line.strokeStyle;
context.lineWidth = line.lineWidth;
context.moveTo(line.p1[0], line.p1[1]);
context.lineTo(line.p2[0], line.p2[1]);
context.stroke();
}