A web-based drawing app
git clone git://soophie.de/outline
blob: 14fd627247061469b2478ba83ed9f0ef09c985a6 (6858 bytes)[doc][blame][raw]
1
let env = {2
drag: false,3
last: null,4
color: 'black',5
radius: 3,6
bucket: [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 },20
client: {},21
uuid: window.location.search.substr(6)22
};24
let canvas;25
let overlay26
let g;27
let o;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');37
g = canvas.getContext('2d');38
o = overlay.getContext('2d');40
env.bucket.forEach(function(item) {41
let template = document.createElement('template');42
template.innerHTML = `<div class="bucket tooltip up${item.active ? ' focused' : ''}" data-title="${item.name}" style="background: ${item.code}"></div>`;43
document.querySelector('.toolbar').append(template.content.cloneNode(true));44
});46
canvas.addEventListener('dragstart', function(event) {47
event.preventDefault();48
});49
canvas.addEventListener('contextmenu', function(event) {50
event.preventDefault();51
});52
canvas.addEventListener('mousemove', function(event) {53
if(env.drag) {54
let pos = { x: event.layerX, y: event.layerY };55
send({ pos: pos, last: env.last });56
draw(pos, env.last, env.radius, env.color);57
env.last = pos;58
}59
});60
canvas.addEventListener('mousedown', function(event) {61
env.drag = true;62
let pos = { x: event.layerX, y: event.layerY };63
send({ pos: pos, last: env.last });64
draw(pos, env.last, env.radius, env.color);65
env.last = pos;66
});68
document.addEventListener('mouseup', function(event) {69
env.drag = false;70
env.last = null;71
});72
document.addEventListener('wheel', function(event) {73
if(event.deltaY < 0) {74
env.radius += env.radius < 30 ? 0.5 : 0;75
}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) {86
index = 9;87
}88
Array.from(document.querySelectorAll('.bucket'))89
.forEach(function(item, i) {90
if(index == i) {91
env.color = item.style.background;92
item.classList.add('focused');93
}94
else {95
item.classList.remove('focused');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
});113
window.addEventListener('resize', resize);115
Array.from(document.querySelectorAll('div.bucket'))116
.forEach(function(item) {117
item.addEventListener('click', function(event) {118
env.color = item.style.background;119
Array.from(document.querySelectorAll('.bucket'))120
.forEach(function(_item) {121
if(_item != item) {122
_item.classList.remove('focused');123
}124
});125
item.classList.add('focused');126
});127
});129
document.querySelector('button.action-clear')130
.addEventListener('click', function(event) {131
env.socket.emit('clear');132
g.clearRect(0, 0, window.innerWidth, window.innerHeight);133
});135
document.querySelector('button.action-background')136
.addEventListener('click', function(event) {137
env.socket.emit('background', env.color);138
g.fillStyle = env.color;139
g.fillRect(0, 0, window.innerWidth, window.innerHeight);140
g.fillStyle = 'black';141
});143
resize();144
connect();145
sendCursor();146
});148
function resize() {149
canvas.width = window.innerWidth;150
canvas.height = window.innerHeight;151
overlay.width = window.innerWidth;152
overlay.height = window.innerHeight;153
}155
function connect() {156
env.socket = io('http://' + window.location.host, {157
path: '/pipe'158
});159
env.socket.on('data', function(data) {160
draw(data.pos, data.last, data.radius, data.color);161
});162
env.socket.on('clear', function(data) {163
g.clearRect(0, 0, window.innerWidth, window.innerHeight);164
});165
env.socket.on('background', function(data) {166
g.fillStyle = data;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 }) {182
if(env.socket) {183
env.socket.emit('data', {184
pos: pos,185
last: last,186
radius: env.radius,187
color: env.color188
});189
}190
}192
function draw(pos, last, radius, color) {193
if(last) {194
let diff = {195
x: last.x - pos.x,196
y: last.y - pos.y197
};198
let dist = Math.sqrt(Math.pow(diff.x, 2) + Math.pow(diff.y, 2), 2);199
let length = dist / (radius / 2);200
for(let i = 0; i < length; i++) {201
let x = pos.x + (diff.x / length * i);202
let y = pos.y + (diff.y / length * i);203
g.fillStyle = color;204
g.beginPath();205
g.arc(x, y, 2 * radius, 0, 2 * Math.PI);206
g.fill();207
g.fillStyle = 'black';208
}209
}210
else {211
g.fillStyle = color;212
g.beginPath();213
g.arc(pos.x, pos.y, 2 * radius, 0, 2 * Math.PI);214
g.fill();215
g.fillStyle = 'black';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();238
o.beginPath();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
}