A web-based drawing app
git clone git://soophie.de/outline
blob: 85c920e6b5f7d6db5b82d872b31e0307cb370a2b (1829 bytes)[doc][blame][raw]
1
const express = require('express');2
const app = express();3
const http = require('http').createServer(app);4
const io = require('socket.io')(http);5
const { createCanvas } = require('canvas');6
const paint = require('./public/js/mod/paint');8
let port = 80;9
let client = {};11
let width = 1920;12
let height = 1080;13
let canvas = createCanvas(width, height);14
let g = canvas.getContext('2d');15
g.fillStyle = 'white';16
g.fillRect(0, 0, width, height);18
app.use(express.static(__dirname + '/public'));20
const proxy = http.listen(port, function() {21
console.log('Server started on port: ' + port);22
});24
paint.init(g);26
io.path('/pipe');27
io.listen(proxy);29
io.on('connection', function(socket) {30
client[socket] = {};31
socket.on('request', function(data) {32
let { x, y, width, height } = data;33
let image = g.getImageData(x, y, width, height);34
socket.emit('load', new Uint8Array(image.data).buffer);35
});36
socket.on('data' , function(data) {37
paint.draw(data);38
socket.broadcast.emit('data', data);39
});40
socket.on('clear' , function(data) {41
g.clearRect(0, 0, width, height);42
socket.broadcast.emit('clear', data);43
});44
socket.on('background' , function(data) {45
g.fillStyle = data;46
g.fillRect(0, 0, width, height);47
g.fillStyle = 'black';48
socket.broadcast.emit('background', data);49
});50
socket.on('cursor' , function(data) {51
if(!client[socket]) {52
client[socket] = {};53
}54
client[socket].uuid = data.uuid;55
socket.broadcast.emit('cursor', data);56
});57
socket.on('disconnect', function() {58
if(client[socket]) {59
socket.broadcast.emit('cursor', {60
quit: true,61
uuid: client[socket].uuid62
});63
}64
delete client[socket];65
});66
});67
io.on('disconnect', function(socket) {68
if(client[socket]) {69
socket.broadcast.emit('cursor', {70
quit: true,71
uuid: client[socket].uuid72
});73
delete client[socket];74
}75
});