A web-based drawing app
git clone git://soophie.de/outline
4
const io = require('socket.io')(http);6
let port = 80;7
let client = {};9
app.use(express.static(__dirname + '/public'));
16
io.listen(proxy);18
io.on('connection', function(socket) {19
client[socket] = {};20
socket.on('data' , function(data) {21
socket.broadcast.emit('data', data);22
});
26
socket.on('background' , function(data) {27
socket.broadcast.emit('background', data);28
});29
socket.on('cursor' , function(data) {30
if(!client[socket]) {31
client[socket] = {};32
}33
client[socket].uuid = data.uuid;34
socket.broadcast.emit('cursor', data);35
});36
socket.on('disconnect', function() {37
socket.broadcast.emit('cursor', {38
quit: true,39
uuid: client[socket].uuid40
});41
delete client[socket];42
});43
});44
io.on('disconnect', function(socket) {45
socket.broadcast.emit('cursor', {46
quit: true,47
uuid: client[socket].uuid48
});49
delete client[socket];50
});
20
<button class="action action-color tooltip" data-title="Color">21
<i class="ico">color_lens</i>22
</button>23
<button class="action action-brush tooltip" data-title="Brush">24
<i class="ico">brush</i>25
</button>23
<button class="action action-background tooltip" data-title="Background">24
<i class="ico">format_paint</i>25
</button>
4
color: 'black',5
radius: 3,6
bucket: [7
{ name: 'Black', code: 'black', active: true },8
{ name: 'White', code: 'white' },9
{ name: 'Red', code: 'red' },10
{ name: 'Blue', code: 'blue' },11
{ name: 'Green', code: 'green' },12
{ name: 'Yellow', code: 'yellow' },13
{ name: 'Brown', code: 'brown' },14
{ name: 'Grey', code: 'grey' },15
{ name: 'Purple', code: 'purple' },16
{ name: 'Bisque', code: 'bisque' }7
{ name: 'Black', code: '#000000', active: true },8
{ name: 'White', code: '#FFFFFF' },9
{ name: 'Red', code: '#FF0000' },10
{ name: 'Blue', code: '#0000FF' },11
{ name: 'Green', code: '#00FF00' },12
{ name: 'Yellow', code: '#FFFF00' },13
{ name: 'Brown', code: '#A52A2A' },14
{ name: 'Grey', code: '#808080' },15
{ name: 'Purple', code: '#800080' },16
{ name: 'Bisque', code: '#FFE4C4' }17
],18
socket: null,19
cursor: { x: 0, y: 0 }19
cursor: { x: 0, y: 0 },20
client: {},21
uuid: window.location.search.substr(6)22
};24
let canvas;
29
window.addEventListener('load', function(event) {31
console.log(env.uuid);33
canvas = document.querySelector('.canvas');34
canvas.draggable = true;35
overlay = document.querySelector('.overlay');
76
else {77
env.radius -= env.radius > 0.5 ? 0.5 : 0;78
}79
sendCursor();80
update();81
});82
document.addEventListener('keydown', function(event) {83
if(event.keyCode >= 48 && event.keyCode <= 57) {84
let index = event.keyCode - 49;85
if(index == -1) {
96
}97
});98
}99
sendCursor();100
update();101
});102
document.addEventListener('mousemove', function(event) {103
env.cursor = event.target === canvas ? { x: event.layerX, y: event.layerY } : null;104
sendCursor();105
update();106
});107
document.addEventListener('mouseout', function(event) {108
env.cursor = null;109
sendCursor();110
update();111
});
143
resize();144
connect();145
sendCursor();146
});148
function resize() {
167
g.fillRect(0, 0, window.innerWidth, window.innerHeight);168
g.fillStyle = 'black';169
});170
env.socket.on('cursor', function(data) {171
if(data.quit) {172
delete env.client[data.uuid];173
}174
else {175
env.client[data.uuid] = data;176
}177
update();178
});179
}181
function send({ pos, last }) {
216
}217
}219
function sendCursor() {220
if(env.socket) {221
env.socket.emit('cursor', {222
cursor: env.cursor,223
color: env.color,224
radius: env.radius,225
uuid: env.uuid226
});227
}228
}230
function update() {231
o.clearRect(0, 0, window.innerWidth, window.innerHeight);232
if(env.cursor) {233
o.fillStyle = env.color;234
o.lineWidth = 4;235
o.beginPath();236
o.arc(env.cursor.x, env.cursor.y, 2 * env.radius, 0, 2 * Math.PI);237
o.stroke();
239
o.arc(env.cursor.x, env.cursor.y, 2 * env.radius, 0, 2 * Math.PI);240
o.fill();241
}242
for(let key of Object.keys(env.client)) {243
let client = env.client[key];244
if(client.cursor) {245
o.fillStyle = client.color;246
o.lineWidth = 4;247
o.beginPath();248
o.arc(client.cursor.x, client.cursor.y, 2 * client.radius, 0, 2 * Math.PI);249
o.stroke();250
o.beginPath();251
o.arc(client.cursor.x, client.cursor.y, 2 * client.radius, 0, 2 * Math.PI);252
o.fill();253
o.fillStyle = 'black';254
o.font = '14px Roboto';255
o.textBaseline = 'top';256
o.textAlign = 'left';257
let text = o.measureText(client.uuid);258
o.fillRect(client.cursor.x, client.cursor.y, text.width + 12, 24);259
o.fillStyle = 'white';260
o.fillText(client.uuid, client.cursor.x + 6, client.cursor.y + 5);261
}262
}263
}265
function dark(color) {266
let rgb = parseInt(color.substring(1));267
let r = (rgb >> 16) & 0xff;268
let g = (rgb >> 8) & 0xff;269
let b = (rgb >> 9) & 0xff;270
let luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;271
return luma < 40;272
}