A web-based drawing app
git clone git://soophie.de/outline
blob: 94229630a1e390f17421640df8885afab9fc4160 (2390 bytes)[doc][blame][raw]
1
const fs = require('fs');2
const express = require('express');3
const app = express();4
const http = require('http');5
const https = require('https');6
const io = require('socket.io');8
let env = {9
port: 443,10
path: '/etc/letsencrypt/live/',11
domain: 'prototype-studio.de',12
ssl: true13
};15
if(fs.existsSync(env.path)) {16
env.credentials = {17
key: fs.readFileSync(env.path + env.domain + '/privkey.pem', 'utf8'),18
cert: fs.readFileSync(env.path + env.domain + '/cert.pem', 'utf8'),19
ca: fs.readFileSync(env.path + env.domain + '/chain.pem', 'utf8')20
};21
}22
else {23
env.ssl = false;24
env.port = 80;25
}27
const proxy = env.ssl ? https.createServer(env.credentials, app) : http.createServer(app);28
const server = io(https);30
app.use(express.static(__dirname + '/public'));32
proxy.listen(env.port, function() {33
console.log('Server started on port:', env.port);34
});36
server.path('/pipe');37
server.listen(proxy);39
const { createCanvas } = require('canvas');40
const paint = require('./public/js/mod/paint');42
let client = {};43
let width = 1920;44
let height = 1080;45
let canvas = createCanvas(width, height);46
let g = canvas.getContext('2d');47
g.fillStyle = 'white';48
g.fillRect(0, 0, width, height);50
paint.init(g);52
server.on('connection', function(socket) {53
client[socket] = {};54
socket.on('request', function(data) {55
let { x, y, width, height } = data;56
let image = g.getImageData(x, y, width, height);57
socket.emit('load', new Uint8Array(image.data).buffer);58
});59
socket.on('data' , function(data) {60
paint.draw(data);61
socket.broadcast.emit('data', data);62
});63
socket.on('clear' , function(data) {64
g.clearRect(0, 0, width, height);65
socket.broadcast.emit('clear', data);66
});67
socket.on('background' , function(data) {68
g.fillStyle = data;69
g.fillRect(0, 0, width, height);70
g.fillStyle = 'black';71
socket.broadcast.emit('background', data);72
});73
socket.on('cursor' , function(data) {74
if(!client[socket]) {75
client[socket] = {};76
}77
client[socket].uuid = data.uuid;78
socket.broadcast.emit('cursor', data);79
});80
socket.on('disconnect', function() {81
if(client[socket]) {82
socket.broadcast.emit('cursor', {83
quit: true,84
uuid: client[socket].uuid85
});86
}87
delete client[socket];88
});89
});90
server.on('disconnect', function(socket) {91
if(client[socket]) {92
socket.broadcast.emit('cursor', {93
quit: true,94
uuid: client[socket].uuid95
});96
delete client[socket];97
}98
});