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