A web-based drawing app
git clone git://soophie.de/outline
blob: 6d94191e3115dfacde6707ceef697aefed32da7a (18666 bytes)[doc][blame][raw]
1
import { COLORS, PRIORITY_MODAL } from './constant.js';2
import { uuid, query, prevent } from './util.js';4
// addons5
import canvas from './addon/canvas.js';6
import locale from './addon/locale.js';7
import ripple from './addon/ripple.js';8
// import gamepad from './addon/gamepad.js';10
let node;11
let state = {12
color: 54,13
color_list: [54, 2, 17, 27, 37, 12],14
radius: 14,15
index: 0,16
device: null,17
device_fallback: null,18
device_list: {19
mouse: false,20
touch: false,21
pen: false,22
gamepad: false23
},24
pencil_list: [25
'pen',26
'marker',27
'eraser'28
],29
pencil: 'pen',30
timestamp: null,31
modal: null,32
tab: null,33
option: {34
view_mode: false35
},36
history: [],37
redo_history: []38
};40
window.addEventListener('load', init);42
function init() {44
console.log('%cOutline', 'font-size: 24px; font-weight: 600;');45
console.log('%cDraw with friends together.\n', 'font-size: 14px;');46
console.log('Found a bug? Tell us:', 'https://github.com/typable/outline/issues/new/choose');48
canvas.init();49
locale.init('en');50
locale.load('./asset/lang', ['en', 'de'], function() {51
if('localStorage' in window) {52
let user_lang = localStorage.getItem('outline.user.lang');53
if(user_lang) {54
locale.change(user_lang);55
update_language_list();56
}57
}58
});59
ripple.init();61
node = query({62
wrapper: '.wrapper',63
controls: '.controls',64
modal_list: { query: '.modal', all: true },65
tab_list: { query: '.tab', all: true },66
header: '.header-list',67
hotbar: '.hotbar',68
hotbar_list: { query: '.hotbar .color', all: true },69
modal_color_list: { query: '.colors-modal .color', all: true },70
language_list: { query: '.more-modal .language-tab .item[data-event="change.language"]', all: true },71
device_list: { query: '.more-modal .device-tab .item[data-event="change.device"]', all: true },72
pencil_list: { query: '.pencil-modal .item[data-event="change.pencil"]', all: true },73
modal_event_list: { query: '[data-event*=".modal"]', all: true },74
tab_event_list: { query: '[data-event*=".tab"]', all: true },75
tool_list: { query: '.tool', all: true },76
action_list: { query: '.action', all: true },77
notification: '.notification',78
scale_input: 'input.scale[type="range"]',79
clear: 'button.btn-apply[data-event="clear"]',80
pencil_tool: '.tool-item[data-code="pencil"]',81
option_event_fullscreen: '[data-event="toggle.fullscreen"]',82
option_event_view_mode: '[data-event="toggle.view-mode"]',83
undo_tool: '[data-lang="action.undo"]',84
redo_tool: '[data-lang="action.redo"]',85
colors_modal: '.colors-modal',86
mainbar: '.mainbar',87
sidebar: '.sidebar',88
edit_mode: '[data-event="close.view-mode"]'89
});91
fill_hotbar_list();92
fill_modal_color_list();94
bind_events();96
node.wrapper.animate([97
{ opacity: 1 },98
{ opacity: 0, pointerEvents: 'none' }99
], {100
delay: 100,101
duration: 500,102
fill: 'both'103
});104
node.hotbar.animate([105
{ opacity: 0, bottom: '-10px' },106
{ opacity: 1 }107
], {108
delay: 600,109
easing: 'ease-out',110
duration: 250,111
fill: 'both'112
});113
}115
function bind_events() {116
window.onerror = function(error, url, line) {117
fetch('https://server.typable.dev/log', {118
method: 'POST',119
headers: {120
'Accept': 'application/json',121
'Content-Type': 'application/json',122
},123
body: JSON.stringify({124
date: new Date(),125
error: {126
message: error,127
file: url,128
line: line129
}130
})131
});132
}133
document.addEventListener('dragstart', prevent);134
document.addEventListener('contextmenu', prevent);135
document.addEventListener('pointerdown', on_pointerdown);136
document.addEventListener('pointermove', on_pointermove);137
document.addEventListener('pointerup', on_pointerup);138
document.addEventListener('pointerout', on_pointerout);139
document.addEventListener('keydown', on_keydown);140
document.addEventListener('wheel', on_wheel);141
document.addEventListener('wheel', function(event) {142
if(event.ctrlKey) {143
prevent(event);144
}145
}, { passive: false });146
document.addEventListener('touchmove', function(event) {147
if(event.target !== node.scale_input && event.target !== node.colors_modal && !node.modal_color_list.includes(event.target)) {148
event.preventDefault();149
}150
}, { passive: false });152
for(let item of node.language_list) {153
item.addEventListener('click', function(event) {154
let code = item.dataset.code;155
locale.change(code);156
update_language_list();157
});158
}159
for(let item of node.device_list) {160
item.addEventListener('click', function(event) {161
let code = item.dataset.code;162
if(state.device_list[code]) {163
state.device = code;164
update_device_list();165
}166
});167
}168
for(let item of node.pencil_list) {169
item.addEventListener('click', function(event) {170
state.pencil = item.dataset.code;171
update_pencil_list();172
state.modal = null;173
update_modal_list();174
});175
}176
for(let item of node.modal_event_list) {177
item.addEventListener('click', function(event) {178
let code = item.dataset.code;179
let match = /^(toggle|open|close).modal$/.exec(item.dataset.event);180
if(match) {181
if(!item.classList.contains('inactive')) {182
switch(match[1]) {183
case 'toggle':184
if(code) {185
state.modal = state.modal !== code ? code : null;186
update_modal_list();187
}188
break;189
case 'open':190
if(code && state.modal !== code) {191
state.modal = code;192
update_modal_list();193
}194
break;195
case 'close':196
if(state.modal !== null) {197
state.modal = null;198
update_modal_list();199
}200
break;201
default:202
break;203
}204
}205
}206
});207
}208
for(let item of node.tab_event_list) {209
item.addEventListener('click', function(event) {210
let code = item.dataset.code;211
let match = /^(open|close).tab/.exec(item.dataset.event);212
if(match) {213
switch(match[1]) {214
case 'open':215
if(code && state.tab !== code) {216
state.tab = code;217
update_tab_list();218
}219
break;220
case 'close':221
if(state.tab !== null) {222
state.tab = null;223
update_tab_list();224
}225
break;226
default:227
break;228
}229
}230
});231
}232
node.scale_input.addEventListener('input', function(event) {233
state.radius = parseInt(node.scale_input.value);234
state.point = {235
x: parseInt(window.innerWidth / 2),236
y: parseInt(window.innerHeight / 2)237
};238
canvas.draw_cursor(state);239
});240
node.clear.addEventListener('click', function(event) {241
canvas.clear(state);242
update_undo_and_redo();243
show_notification('notification.clear');244
state.modal = null;245
update_modal_list();246
});247
node.option_event_fullscreen.addEventListener('click', function(event) {248
if(!document.fullscreenElement) {249
document.documentElement.requestFullscreen();250
}251
else if(document.exitFullscreen) {252
document.exitFullscreen();253
}254
});255
window.addEventListener('fullscreenchange', on_fullscreenchange);256
node.option_event_view_mode.addEventListener('click', function(event) {257
state.option.view_mode = !state.option.view_mode;258
update_view_mode_option();259
});260
node.undo_tool.addEventListener('click', function(event) {261
canvas.undo(state);262
update_undo_and_redo();263
});264
node.redo_tool.addEventListener('click', function(event) {265
canvas.redo(state);266
update_undo_and_redo();267
});268
node.edit_mode.addEventListener('click', function(event) {269
state.option.view_mode = false;270
update_view_mode_option();271
});272
}274
function update_language_list() {275
let lang = locale.get_current();276
for(let item of node.language_list) {277
let code = item.dataset.code;278
item.classList[code === lang ? 'add' : 'remove']('active');279
}280
}282
function update_device_list() {283
canvas.get_canvas().style.cursor = state.device === 'mouse' ? 'none' : '';284
for(let item of node.device_list) {285
let code = item.dataset.code;286
item.classList[code === state.device ? 'add' : 'remove']('active');287
item.classList[state.device_list[code] ? 'remove' : 'add']('inactive');288
}289
}291
function update_pencil_list() {292
for(let item of node.pencil_list) {293
let code = item.dataset.code;294
item.classList[code === state.pencil ? 'add' : 'remove']('active');295
if(code === state.pencil) {296
node.pencil_tool.querySelector('i.ico').innerHTML = item.querySelector('i.ico').innerHTML;297
}298
}299
}301
function update_hotbar() {302
for(let item of node.hotbar_list) {303
item.classList[node.hotbar_list[state.index] === item ? 'add' : 'remove']('active');304
}305
}307
function update_focus(bool) {308
for(let item of node.hotbar_list) {309
item.style.pointerEvents = bool ? '' : 'none';310
}311
for(let item of node.tool_list) {312
item.style.pointerEvents = bool ? '' : 'none';313
}314
for(let item of node.action_list) {315
item.style.pointerEvents = bool ? '' : 'none';316
}317
}319
function update_modal_list() {320
for(let item of node.modal_list) {321
item.classList.add('hidden');322
}323
for(let item of node.modal_event_list) {324
item.classList.remove('active');325
}326
state.tab = null;327
update_tab_list();328
if(state.modal) {329
canvas.on_release(event, state, true);330
let modal = document.querySelector(`.modal.${state.modal}-modal`);331
let button = document.querySelector(`[data-event*=".modal"][data-code="${state.modal}"]`);332
if(modal) {333
modal.classList.remove('hidden');334
modal.animate([335
{ opacity: 0, marginTop: '-6px' },336
{ opacity: 1, marginTop: '0' }337
], {338
duration: 100,339
fill: 'both'340
});341
}342
if(button) {343
button.classList.add('active');344
}345
}346
}348
function update_tab_list() {349
for(let item of node.modal_list) {350
let main = item.querySelector('.main');351
if(main) {352
main.classList.remove('hidden');353
main.style.marginLeft = '0';354
}355
}356
for(let item of node.tab_list) {357
item.classList.add('hidden');358
}359
if(state.modal && state.tab) {360
let modal = document.querySelector(`.modal.${state.modal}-modal`);361
let tab = document.querySelector(`.${state.tab}-tab`);362
let main = modal.querySelector('.main');363
if(modal && tab && main) {364
tab.classList.remove('hidden');365
main.style.marginLeft = '-100%';366
main.classList.add('hidden');367
}368
}369
}371
function update_fullscreen_option() {372
let fullscreen = document.fullscreenElement;373
node.option_event_fullscreen.classList[fullscreen ? 'add' : 'remove']('active');374
node.option_event_fullscreen.querySelector('.ico').textContent = fullscreen ? 'toggle_on' : 'toggle_off';375
}377
function update_view_mode_option() {378
let view_mode = state.option.view_mode;379
show_notification(`notification.view-mode.${view_mode ? 'on' : 'off'}`);380
node.option_event_view_mode.classList[view_mode ? 'add' : 'remove']('active');381
node.option_event_view_mode.querySelector('.ico').textContent = view_mode ? 'toggle_on' : 'toggle_off';382
if(view_mode) {383
state.modal = null;384
state.tab = null;385
update_modal_list();386
canvas.on_release(null, state);387
canvas.clear_cursor();388
canvas.get_canvas().style.cursor = 'default';389
node.hotbar.classList.add('inactive');390
node.header.classList.add('inactive');391
node.hotbar.animate([392
{ opacity: 1 },393
{ opacity: 0, bottom: '-10px', pointerEvents: 'none' }394
], {395
easing: 'ease-out',396
duration: 250,397
fill: 'both'398
});399
node.mainbar.animate([400
{ opacity: 1, marginTop: 0 },401
{ opacity: 0, marginTop: '-60px' }402
], {403
easing: 'ease-out',404
duration: 250,405
fill: 'both'406
});407
node.sidebar.classList.remove('hidden');408
node.sidebar.animate([409
{ opacity: 0 },410
{ opacity: 1 }411
], {412
delay: 250,413
easing: 'ease-out',414
duration: 250,415
fill: 'both'416
});417
}418
else {419
canvas.get_canvas().style.cursor = 'none';420
node.hotbar.classList.remove('inactive');421
node.header.classList.remove('inactive');422
node.hotbar.animate([423
{ opacity: 0, bottom: '-10px' },424
{ opacity: 1, bottom: '30px' }425
], {426
easing: 'ease-out',427
duration: 250,428
fill: 'both'429
});430
node.mainbar.animate([431
{ opacity: 0, marginTop: '-60px' },432
{ opacity: 1, marginTop: 0 }433
], {434
easing: 'ease-out',435
duration: 250,436
fill: 'both'437
});438
node.sidebar.classList.add('hidden');439
}440
}442
function update_undo_and_redo() {443
node.undo_tool.classList[state.history.length > 0 ? 'remove' : 'add']('inactive');444
node.redo_tool.classList[state.redo_history.length > 0 ? 'remove' : 'add']('inactive');445
}447
function on_pointerdown(event) {448
// detect touch449
if(!state.device_list.touch && event.pointerType === 'touch') {450
state.device_list.touch = true;451
if(state.device) {452
show_notification('notification.input-device.touch.add');453
}454
else {455
state.device = 'touch';456
state.device_fallback = 'touch';457
}458
update_device_list();459
}460
// detect pen461
if(!state.device_list.pen && event.pointerType === 'pen') {462
state.device_list.pen = true;463
show_notification('notification.input-device.pencil.add');464
if(!state.device) {465
state.device = 'pen';466
state.device_fallback = 'pen';467
}468
update_device_list();469
}470
// drawing471
if(!state.option.view_mode && state.device === event.pointerType) {472
if(event.target === canvas.get_canvas()) {473
if(!PRIORITY_MODAL.includes(state.modal)) {474
update_focus(false);475
canvas.on_press(event, state);476
update_undo_and_redo();477
state.point = {478
x: event.layerX,479
y: event.layerY480
};481
canvas.draw_cursor(state);482
}483
}484
}485
}487
function on_pointermove(event) {488
// detect mouse489
if(!state.device_list.mouse && event.pointerType === 'mouse') {490
state.device_list.mouse = true;491
if(state.device) {492
show_notification('notification.input-device.mouse.add');493
}494
else {495
state.device = 'mouse';496
state.device_fallback = 'mouse';497
}498
update_device_list();499
}500
// drawing501
if(!state.option.view_mode && state.device === event.pointerType) {502
if(event.target === canvas.get_canvas()) {503
if(!PRIORITY_MODAL.includes(state.modal)) {504
canvas.on_move(event, state);505
}506
state.point = {507
x: event.layerX,508
y: event.layerY509
};510
}511
canvas.draw_cursor(state);512
}513
}515
function on_pointerup(event) {516
if(!state.option.view_mode && state.device === event.pointerType) {517
canvas.on_release(event, state);518
update_undo_and_redo();519
update_focus(true);520
}521
}523
function on_pointerout(event) {524
if(!state.option.view_mode && state.device === event.pointerType) {525
canvas.on_release(event, state);526
update_undo_and_redo();527
state.point = null;528
canvas.draw_cursor(state);529
}530
}532
function on_keydown(event) {533
if(event.code === 'Escape') {534
if(state.tab) {535
state.tab = null;536
update_tab_list();537
return;538
}539
if(state.modal) {540
state.modal = null;541
update_modal_list();542
return;543
}544
}545
if(!event.ctrlKey && !event.shiftKey && !event.altKey) {546
if(!state.option.view_mode) {547
if(event.code.startsWith('Digit')) {548
let i = parseInt(event.code.substr(5)) - 1;549
if(state.index !== i) {550
if(i >= 0 && i < state.color_list.length) {551
canvas.on_release(event, state);552
state.color = state.color_list[i];553
state.index = i;554
canvas.draw_cursor(state);555
update_hotbar();556
}557
}558
}559
if(event.code === 'KeyZ') {560
state.modal = state.modal !== 'colors' ? 'colors' : null;561
update_modal_list();562
}563
if(event.code === 'KeyX') {564
state.modal = state.modal !== 'scale' ? 'scale' : null;565
update_modal_list();566
}567
if(event.code === 'KeyC') {568
state.modal = state.modal !== 'pencil' ? 'pencil' : null;569
update_modal_list();570
}571
if(event.code === 'KeyV') {572
state.modal = state.modal !== 'clear' ? 'clear' : null;573
update_modal_list();574
}575
/*576
if(event.code === 'KeyB') {577
state.modal = state.modal !== 'multiplayer' ? 'multiplayer' : null;578
update_modal_list();579
}580
if(event.code === 'KeyN') {581
state.modal = state.modal !== 'account' ? 'account' : null;582
update_modal_list();583
}584
*/585
if(event.code === 'KeyM') {586
state.modal = state.modal !== 'more' ? 'more' : null;587
update_modal_list();588
}589
if(event.code === 'ArrowLeft') {590
canvas.undo(state);591
update_undo_and_redo();592
}593
if(event.code === 'ArrowRight') {594
canvas.redo(state);595
update_undo_and_redo();596
}597
if(event.code === 'KeyQ') {598
canvas.on_release(event, state);599
state.pencil = state.pencil_list[0];600
canvas.draw_cursor(state);601
update_pencil_list();602
}603
if(event.code === 'KeyW') {604
canvas.on_release(event, state);605
state.pencil = state.pencil_list[1];606
canvas.draw_cursor(state);607
update_pencil_list();608
}609
if(event.code === 'KeyE') {610
canvas.on_release(event, state);611
state.pencil = state.pencil_list[2];612
canvas.draw_cursor(state);613
update_pencil_list();614
}615
if(event.code === 'KeyF') {616
if(!document.fullscreenElement) {617
document.documentElement.requestFullscreen();618
}619
else if(document.exitFullscreen) {620
document.exitFullscreen();621
}622
}623
}624
if(event.code === 'Space') {625
state.option.view_mode = !state.option.view_mode;626
update_view_mode_option();627
}628
}629
if(event.ctrlKey && !event.shiftKey && !event.altKey) {630
if(event.code === 'KeyY') {631
canvas.undo(state);632
update_undo_and_redo();633
}634
if(event.code === 'KeyZ') {635
canvas.redo(state);636
update_undo_and_redo();637
}638
}639
}641
function on_wheel(event) {642
if(!state.option.view_mode && state.device === 'mouse') {643
let y = event.deltaY < 0 ? 1 : -1;644
if(state.radius + y > 2 - 1 && state.radius + y < 50 + 1) {645
canvas.on_release(event, state);646
state.radius += y;647
node.scale_input.value = state.radius;648
canvas.draw_cursor(state);649
}650
}651
}653
function on_fullscreenchange(event) {654
update_fullscreen_option();655
}657
function fill_hotbar_list() {658
for(let [ i, item ] of Object.entries(node.hotbar_list)) {659
if(state.color_list.length > i) {660
item.style.background = COLORS[state.color_list[i]];661
item.addEventListener('click', function(event) {662
canvas.on_release(event, state);663
state.color = state.color_list[i];664
state.index = i;665
canvas.draw_cursor(state);666
update_hotbar();667
});668
if(state.index == i) {669
item.classList.add('active');670
}671
}672
}673
}675
function fill_modal_color_list() {676
for(let [ i, item ] of Object.entries(node.modal_color_list)) {677
if(COLORS.length > i) {678
item.style.background = COLORS[i];679
item.addEventListener('click', function(event) {680
canvas.on_release(event, state);681
state.color = i;682
state.modal = null;683
update_modal_list();684
canvas.draw_cursor(state);685
state.index = null;686
update_hotbar();687
});688
}689
}690
}692
function show_notification(message) {693
let code = uuid();694
state.timestamp = code;695
let content = node.notification.querySelector('.content');696
content.textContent = locale.get()[message];697
node.notification.animate([698
{ opacity: 0 },699
{ opacity: 1 }700
], {701
duration: 200,702
fill: 'both'703
});704
setTimeout(function() {705
if(state.timestamp === code) {706
node.notification.animate([707
{ opacity: 1 },708
{ opacity: 0 }709
], {710
duration: 200,711
fill: 'both'712
});713
}714
}, 1000);715
}