A web-based drawing app
git clone git://soophie.de/outline
blob: 70c9621c58d90f47bf3a18581c78a697e014d559 (14624 bytes)[doc][blame][raw]
1
let env = {2
drag: false,3
last: null,4
color: '#000000',5
radius: 3,6
bucket: [7
{ name: 'Black', code: '#000000', active: true },8
{ name: 'White', code: '#FFFFFF' },9
{ name: 'Grey', code: '#808080' },10
{ name: 'Red', code: '#FF0000' },11
{ name: 'Blue', code: '#0000FF' },12
{ name: 'Green', code: '#00FF00' },13
{ name: 'Yellow', code: '#FFFF00' }14
],15
colorlist: [16
{ name: 'Salmon', code: '#FA8072' },17
{ name: 'Crimson', code: '#DC143C' },18
{ name: 'Firebrick', code: '#B22222' },19
{ name: 'Pink', code: '#FFC0CB' },20
{ name: 'Hotpink', code: '#FF69B4' },21
{ name: 'Coral', code: '#FF7F50' },22
{ name: 'Tomato', code: '#FF6347' },23
{ name: 'Orange', code: '#FFA500' },24
{ name: 'Gold', code: '#FFD700' },25
{ name: 'Khaki', code: '#F0E68C' },26
{ name: 'Lavender', code: '#E6E6FA' },27
{ name: 'Plum', code: '#DDA0DD' },28
{ name: 'Violet', code: '#EE82EE' },29
{ name: 'Magenta', code: '#FF00FF' },30
{ name: 'Purple', code: '#800080' },31
{ name: 'Indigo', code: '#4B0082' },32
{ name: 'Lime', code: '#00FF00' },33
{ name: 'Olive', code: '#808000' },34
{ name: 'Teal', code: '#008080' },35
{ name: 'Cyan', code: '#00FFFF' },36
{ name: 'Aquamarine', code: '#7FFFD4' },37
{ name: 'Turquoise', code: '#40E0D0' },38
{ name: 'Navy', code: '#000080' },39
{ name: 'Bisque', code: '#FFE4C4' },40
{ name: 'Tan', code: '#D2B48C' },41
{ name: 'Peru', code: '#CD853F' },42
{ name: 'Chocolate', code: '#D2691E' },43
{ name: 'Sienna', code: '#A0522D' },44
{ name: 'Brown', code: '#A52A2A' },45
{ name: 'Silver', code: '#C0C0C0' }46
],47
socket: null,48
cursor: { x: 0, y: 0 },49
client: {},50
uuid: null,51
name: null52
};54
let canvas;55
let overlay56
let g;57
let o;59
window.addEventListener('load', function(event) {61
open('join');63
canvas = document.querySelector('.canvas');64
canvas.draggable = true;65
overlay = document.querySelector('.overlay');67
g = canvas.getContext('2d');68
o = overlay.getContext('2d');70
env.bucket.forEach(function(item) {71
let template = document.createElement('template');72
template.innerHTML = `<div class="bucket tooltip up${item.active ? ' focused' : ''}" data-title="${item.name}" data-color="${item.code}" style="background: ${item.code}"></div>`;73
document.querySelector('.toolbar').append(template.content.cloneNode(true));74
});76
env.colorlist.forEach(function(item) {77
let template = document.createElement('template');78
template.innerHTML = `<li class="bucket tooltip" data-title="${item.name}" data-color="${item.code}" style="background: ${item.code}"></li>`;79
document.querySelector('.modal.modal-color ul.bucket-list').append(template.content.cloneNode(true));80
});82
canvas.addEventListener('dragstart', function(event) {83
event.preventDefault();84
});85
canvas.addEventListener('contextmenu', function(event) {86
event.preventDefault();87
});88
canvas.addEventListener('mousemove', function(event) {89
if(env.drag) {90
let pos = { x: event.layerX, y: event.layerY };91
send({ pos: pos, last: env.last });92
draw(pos, env.last, env.radius, env.color);93
env.last = pos;94
}95
});96
canvas.addEventListener('touchmove', function(event) {97
if(env.drag) {98
let pos = { x: event.layerX, y: event.layerY };99
send({ pos: pos, last: env.last });100
draw(pos, env.last, env.radius, env.color);101
env.last = pos;102
}103
});104
canvas.addEventListener('mousedown', function(event) {105
env.drag = true;106
let pos = { x: event.layerX, y: event.layerY };107
send({ pos: pos, last: env.last });108
draw(pos, env.last, env.radius, env.color);109
env.last = pos;110
document.querySelector('.toolbar').style.pointerEvents = 'none';111
sendCursor();112
update();113
});114
canvas.addEventListener('touchstart', function(event) {115
env.drag = true;116
let pos = { x: event.layerX, y: event.layerY };117
send({ pos: pos, last: env.last });118
draw(pos, env.last, env.radius, env.color);119
env.last = pos;120
document.querySelector('.toolbar').style.pointerEvents = 'none';121
sendCursor();122
update();123
});125
document.querySelector('.action-join').addEventListener('click', function(event) {126
env.name = document.querySelector('.action-uuid').value.replace(/\s+/g, '');127
if(env.name.length >= 3 && env.name.length <= 20) {128
env.uuid = env.name + '-' + uuidv4();129
close();130
connect();131
sendCursor();132
}133
});135
document.addEventListener('mouseup', function(event) {136
env.drag = false;137
env.last = null;138
document.querySelector('.toolbar').style.pointerEvents = '';139
});140
document.addEventListener('touchend', function(event) {141
document.querySelector('.toolbar').style.pointerEvents = '';142
env.drag = false;143
env.last = null;144
});145
document.addEventListener('wheel', function(event) {146
if(document.querySelector('.wrapper').style.display === 'none') {147
if(event.deltaY < 0) {148
env.radius += env.radius < 15 ? 1 : 0;149
}150
else {151
env.radius -= env.radius > 1 ? 1 : 0;152
}153
sendCursor();154
update();155
}156
});157
document.addEventListener('keydown', function(event) {158
if(document.querySelector('.wrapper').style.display === 'none') {159
if(event.keyCode >= 48 && event.keyCode <= 55) {160
let index = event.keyCode - 49;161
if(index == -1) {162
index = 9;163
}164
Array.from(document.querySelectorAll('.bucket'))165
.forEach(function(item, i) {166
if(index == i) {167
env.color = item.dataset.color;168
item.classList.add('focused');169
}170
else {171
item.classList.remove('focused');172
}173
});174
}175
sendCursor();176
update();177
}178
else {179
if(document.querySelector('.modal.modal-join').style.display === 'none') {180
if(event.keyCode == 27) {181
close();182
}183
}184
}185
});186
document.addEventListener('mousemove', function(event) {187
env.cursor = event.target === canvas ? { x: event.layerX, y: event.layerY } : null;188
sendCursor();189
update();190
});191
document.addEventListener('touchmove', function(event) {192
env.cursor = event.target === canvas ? { x: event.layerX, y: event.layerY } : null;193
sendCursor();194
update();195
});196
document.addEventListener('mouseout', function(event) {197
env.cursor = null;198
sendCursor();199
update();200
});202
window.addEventListener('resize', resize);204
Array.from(document.querySelectorAll('div.bucket'))205
.forEach(function(item) {206
item.addEventListener('mousedown', function(event) {207
env.color = item.dataset.color;208
Array.from(document.querySelectorAll('.bucket'))209
.forEach(function(_item) {210
if(_item != item) {211
_item.classList.remove('focused');212
}213
});214
item.classList.add('focused');215
});216
});218
document.querySelector('.actions button.action-clear')219
.addEventListener('click', function(event) {220
open('clear');221
});223
document.querySelector('.actions button.action-background')224
.addEventListener('click', function(event) {225
open('background');226
});228
document.querySelector('.actions button.action-save')229
.addEventListener('click', function(event) {230
document.querySelector('input.action-file').value = `outline-image-${uuidv4()}`;231
open('save');232
});234
document.querySelector('.actions button.action-scaling')235
.addEventListener('click', function(event) {236
document.querySelector('.modal input.action-range').value = env.radius;237
let circle = document.querySelector('.modal .circle');238
circle.style.width = env.radius * 4 + 'px';239
circle.style.height = env.radius * 4 + 'px';240
open('scaling');241
});243
document.querySelector('.actions button.action-color')244
.addEventListener('click', function(event) {245
Array.from(document.querySelectorAll('.modal ul.bucket-list li.bucket'))246
.forEach(function(item) {247
if(item.dataset.color === env.color) {248
item.classList.add('focused');249
}250
});251
open('color');252
});254
Array.from(document.querySelectorAll('button.action-cancel'))255
.forEach(function(item) {256
item.addEventListener('click', function(event) {257
close();258
});259
});261
document.querySelector('.modal button.action-clear')262
.addEventListener('click', function(event) {263
env.socket.emit('clear');264
g.clearRect(0, 0, window.innerWidth, window.innerHeight);265
close();266
});268
document.querySelector('.modal button.action-background')269
.addEventListener('click', function(event) {270
env.socket.emit('background', env.color);271
g.fillStyle = env.color;272
g.fillRect(0, 0, window.innerWidth, window.innerHeight);273
g.fillStyle = 'black';274
close();275
});277
document.querySelector('.modal button.action-save')278
.addEventListener('click', function(event) {279
let file = document.querySelector('.modal input.action-file').value.replace(/\s+/g, '');280
if(file.length >= 3 && file.length <= 40) {281
let link = document.createElement('a');282
link.style.display = 'none';283
link.download = file;284
link.href = canvas.toDataURL('image/jpg');285
document.body.appendChild(link);286
link.click();287
close();288
}289
});291
document.querySelector('.modal input.action-range')292
.addEventListener('input', function(event) {293
setTimeout(function() {294
let circle = document.querySelector('.modal .circle');295
circle.style.width = event.target.value * 4 + 'px';296
circle.style.height = event.target.value * 4 + 'px';297
}, 10);298
});300
document.querySelector('.modal button.action-scaling')301
.addEventListener('click', function(event) {302
env.radius = parseInt(document.querySelector('.modal input.action-range').value);303
sendCursor();304
update();305
close();306
});308
Array.from(document.querySelectorAll('.modal ul.bucket-list li.bucket'))309
.forEach(function(item) {310
item.addEventListener('click', function(event) {311
Array.from(document.querySelectorAll('.modal ul.bucket-list li.bucket'))312
.forEach(function(_item) {313
if(_item != item) {314
_item.classList.remove('focused');315
}316
});317
item.classList.add('focused');318
env.color = item.dataset.color;319
Array.from(document.querySelectorAll('.bucket'))320
.forEach(function(item) {321
item.classList.remove('focused');322
});323
close();324
});325
});327
resize();328
g.fillStyle = 'white';329
g.fillRect(0, 0, canvas.width, canvas.height);330
});332
function resize() {333
let ctemp = g.getImageData(0, 0, canvas.width, canvas.height);334
canvas.width = window.innerWidth;335
canvas.height = window.innerHeight;336
g.putImageData(ctemp, 0, 0);337
let otemp = o.getImageData(0, 0, overlay.width, overlay.height);338
overlay.width = window.innerWidth;339
overlay.height = window.innerHeight;340
o.putImageData(otemp, 0, 0);341
}343
function connect() {344
env.socket = io('http://' + window.location.host, {345
path: '/pipe'346
});347
env.socket.on('data', function(data) {348
draw(data.pos, data.last, data.radius, data.color);349
});350
env.socket.on('clear', function(data) {351
g.clearRect(0, 0, window.innerWidth, window.innerHeight);352
});353
env.socket.on('background', function(data) {354
g.fillStyle = data;355
g.fillRect(0, 0, window.innerWidth, window.innerHeight);356
g.fillStyle = 'black';357
});358
env.socket.on('cursor', function(data) {359
if(data.quit) {360
delete env.client[data.uuid];361
}362
else {363
env.client[data.uuid] = data;364
setTimeout(function() {365
if(env.client[data.uuid]) {366
let last = env.client[data.uuid];367
if(last.cursor && data.cursor) {368
if(last.cursor.x === data.cursor.x && last.cursor.y === data.cursor.y) {369
delete env.client[data.uuid];370
update();371
}372
}373
}374
}, 10 * 1000);375
}376
update();377
});378
}380
function send({ pos, last }) {381
if(env.socket) {382
env.socket.emit('data', {383
pos: pos,384
last: last,385
radius: env.radius,386
color: env.color387
});388
}389
}391
function draw(pos, last, radius, color) {392
if(radius < 1) {393
radius = 1;394
env.radius = radius;395
}396
if(radius > 15) {397
radius = 15;398
env.radius = radius;399
}400
if(last) {401
let diff = {402
x: last.x - pos.x,403
y: last.y - pos.y404
};405
let dist = Math.sqrt(Math.pow(diff.x, 2) + Math.pow(diff.y, 2), 2);406
let length = dist / (radius / 2);407
for(let i = 0; i < length; i++) {408
let x = pos.x + (diff.x / length * i);409
let y = pos.y + (diff.y / length * i);410
g.fillStyle = color;411
g.beginPath();412
g.arc(x, y, 2 * radius, 0, 2 * Math.PI);413
g.fill();414
g.fillStyle = 'black';415
}416
}417
else {418
g.fillStyle = color;419
g.beginPath();420
g.arc(pos.x, pos.y, 2 * radius, 0, 2 * Math.PI);421
g.fill();422
g.fillStyle = 'black';423
}424
}426
function sendCursor() {427
if(env.socket) {428
env.socket.emit('cursor', {429
cursor: env.cursor,430
color: env.color,431
radius: env.radius,432
name: env.name433
});434
}435
}437
function update() {438
if(env.radius < 1) {439
env.radius = 1;440
}441
if(env.radius > 15) {442
env.radius = 15;443
}444
o.clearRect(0, 0, window.innerWidth, window.innerHeight);445
if(env.cursor) {446
o.fillStyle = env.color;447
o.lineWidth = 4;448
o.beginPath();449
o.arc(env.cursor.x, env.cursor.y, 2 * env.radius, 0, 2 * Math.PI);450
o.strokeStyle = dark(env.color) ? 'black' : 'white';451
o.stroke();452
o.beginPath();453
o.arc(env.cursor.x, env.cursor.y, 2 * env.radius, 0, 2 * Math.PI);454
o.fill();455
}456
for(let key of Object.keys(env.client)) {457
let client = env.client[key];458
if(client.cursor) {459
o.fillStyle = client.color;460
o.lineWidth = 4;461
o.beginPath();462
o.arc(client.cursor.x, client.cursor.y, 2 * client.radius, 0, 2 * Math.PI);463
o.strokeStyle = dark(client.color) ? 'black' : 'white';464
o.stroke();465
o.beginPath();466
o.arc(client.cursor.x, client.cursor.y, 2 * client.radius, 0, 2 * Math.PI);467
o.fill();468
o.fillStyle = '#212121';469
o.font = '14px Roboto';470
o.textBaseline = 'top';471
o.textAlign = 'left';472
let text = o.measureText(client.name);473
roundRect(o, client.cursor.x, client.cursor.y, text.width + 12, 22, 4);474
o.fillStyle = 'white';475
o.fillText(client.name, client.cursor.x + 6, client.cursor.y + 5);476
}477
}478
}480
function dark(color) {481
color = color.substr(1);482
let r = parseInt(color.substr(0, 2), 16);483
let g = parseInt(color.substr(2, 4), 16);484
let b = parseInt(color.substr(4, 6), 16);485
return ((r * 0.299) + (g * 0.587) + (b * 0.114)) > 50;486
}488
function open(modal) {489
document.querySelector('.wrapper').style.display = '';490
document.querySelector(`.modal.modal-${modal}`).style.display = '';491
}493
function close() {494
document.querySelector('.wrapper').style.display = 'none';495
Array.from(document.querySelectorAll('.modal'))496
.forEach(function(item) {497
item.style.display = 'none';498
});499
}501
function uuidv4() {502
return 'xxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {503
let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);504
return v.toString(16);505
});506
}508
function roundRect(g, x, y, w, h, r) {509
if(w < 2 * r) r = w / 2;510
if(h < 2 * r) r = h / 2;511
g.beginPath();512
g.moveTo(x + r, y);513
g.arcTo(x + w, y, x + w, y + h, r);514
g.arcTo(x + w, y + h, x, y + h, r);515
g.arcTo(x, y + h, x, y, r);516
g.arcTo(x, y, x + w, y, r);517
g.fill();518
}