A web-based drawing app
git clone git://soophie.de/outline
blob: 06311cbd58bb07a5b6ae9d8b6f44800029f601d0 (19352 bytes)[doc][blame][raw]
1
import { COLORS, PRIORITY_MODAL, LOCALES } 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';9
let node;10
let state = {11
color: 54,12
color_list: [54, 2, 17, 27, 37, 12],13
radius: 14,14
index: 0,15
device: null,16
device_fallback: null,17
device_list: {18
mouse: false,19
touch: false,20
pen: false,21
gamepad: false22
},23
pencil_list: [24
'pen',25
'marker',26
'eraser'27
],28
pencil: 'pen',29
timestamp: null,30
modal: null,31
tab: null,32
caption: 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', LOCALES, 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
content: '.modal .content',90
caption_list: { query: '[data-event="open.content"]' , all: true },91
content_list: { query: '.content-item' , all: true }92
});94
fill_hotbar_list();95
fill_modal_color_list();97
bind_events();99
node.wrapper.animate([100
{ opacity: 1 },101
{ opacity: 0, pointerEvents: 'none' }102
], {103
delay: 100,104
duration: 500,105
fill: 'both'106
});107
node.hotbar.animate([108
{ opacity: 0, bottom: '-10px' },109
{ opacity: 1 }110
], {111
delay: 600,112
easing: 'ease-out',113
duration: 250,114
fill: 'both'115
});116
}118
function bind_events() {119
window.onerror = function(error, url, line) {120
fetch('https://server.typable.dev/log', {121
method: 'POST',122
headers: {123
'Accept': 'application/json',124
'Content-Type': 'application/json',125
},126
body: JSON.stringify({127
date: new Date(),128
error: {129
message: error,130
file: url,131
line: line132
}133
})134
});135
}136
document.addEventListener('dragstart', prevent);137
document.addEventListener('contextmenu', prevent);138
document.addEventListener('pointerdown', on_pointerdown);139
document.addEventListener('pointermove', on_pointermove);140
document.addEventListener('pointerup', on_pointerup);141
document.addEventListener('pointerout', on_pointerout);142
document.addEventListener('keydown', on_keydown);143
document.addEventListener('wheel', on_wheel);144
document.addEventListener('wheel', function(event) {145
if(event.ctrlKey) {146
prevent(event);147
}148
}, { passive: false });149
document.addEventListener('touchmove', function(event) {150
if(event.target !== node.scale_input && event.target !== node.colors_modal && !node.modal_color_list.includes(event.target) && event.target !== node.content) {151
event.preventDefault();152
}153
}, { passive: false });155
for(let item of node.language_list) {156
item.addEventListener('click', function(event) {157
let code = item.dataset.code;158
locale.change(code);159
update_language_list();160
});161
}162
for(let item of node.device_list) {163
item.addEventListener('click', function(event) {164
let code = item.dataset.code;165
if(state.device_list[code]) {166
state.device = code;167
update_device_list();168
}169
});170
}171
for(let item of node.pencil_list) {172
item.addEventListener('click', function(event) {173
state.pencil = item.dataset.code;174
update_pencil_list();175
state.modal = null;176
update_modal_list();177
});178
}179
for(let item of node.modal_event_list) {180
item.addEventListener('click', function(event) {181
let code = item.dataset.code;182
let match = /^(toggle|open|close).modal$/.exec(item.dataset.event);183
if(match) {184
if(!item.classList.contains('inactive')) {185
switch(match[1]) {186
case 'toggle':187
if(code) {188
state.modal = state.modal !== code ? code : null;189
update_modal_list();190
}191
break;192
case 'open':193
if(code && state.modal !== code) {194
state.modal = code;195
update_modal_list();196
}197
break;198
case 'close':199
if(state.modal !== null) {200
state.modal = null;201
update_modal_list();202
}203
break;204
default:205
break;206
}207
}208
}209
});210
}211
for(let item of node.tab_event_list) {212
item.addEventListener('click', function(event) {213
let code = item.dataset.code;214
let match = /^(open|close).tab/.exec(item.dataset.event);215
if(match) {216
switch(match[1]) {217
case 'open':218
if(code && state.tab !== code) {219
state.tab = code;220
update_tab_list();221
}222
break;223
case 'close':224
if(state.tab !== null) {225
state.tab = null;226
update_tab_list();227
}228
break;229
default:230
break;231
}232
}233
});234
}235
node.scale_input.addEventListener('input', function(event) {236
state.radius = parseInt(node.scale_input.value);237
state.point = {238
x: parseInt(window.innerWidth / 2),239
y: parseInt(window.innerHeight / 2)240
};241
canvas.draw_cursor(state);242
});243
node.clear.addEventListener('click', function(event) {244
canvas.clear(state);245
update_undo_and_redo();246
show_notification('notification.clear');247
state.modal = null;248
update_modal_list();249
});250
node.option_event_fullscreen.addEventListener('click', function(event) {251
if(!document.fullscreenElement) {252
document.documentElement.requestFullscreen();253
}254
else if(document.exitFullscreen) {255
document.exitFullscreen();256
}257
});258
window.addEventListener('fullscreenchange', on_fullscreenchange);259
node.option_event_view_mode.addEventListener('click', function(event) {260
state.option.view_mode = !state.option.view_mode;261
update_view_mode_option();262
});263
node.undo_tool.addEventListener('click', function(event) {264
canvas.undo(state);265
update_undo_and_redo();266
});267
node.redo_tool.addEventListener('click', function(event) {268
canvas.redo(state);269
update_undo_and_redo();270
});271
node.edit_mode.addEventListener('click', function(event) {272
state.option.view_mode = false;273
update_view_mode_option();274
});275
for(let item of node.caption_list) {276
item.addEventListener('click', function(event) {277
state.caption = item.dataset.code;278
update_caption_list();279
});280
}282
window.addEventListener('beforeunload', function(event) {283
// ...284
});285
}287
function update_language_list() {288
let lang = locale.get_current();289
for(let item of node.language_list) {290
let code = item.dataset.code;291
item.classList[code === lang ? 'add' : 'remove']('active');292
}293
}295
function update_device_list() {296
canvas.get_canvas().style.cursor = state.device === 'mouse' ? 'none' : '';297
for(let item of node.device_list) {298
let code = item.dataset.code;299
item.classList[code === state.device ? 'add' : 'remove']('active');300
item.classList[state.device_list[code] ? 'remove' : 'add']('inactive');301
}302
}304
function update_pencil_list() {305
for(let item of node.pencil_list) {306
let code = item.dataset.code;307
item.classList[code === state.pencil ? 'add' : 'remove']('active');308
if(code === state.pencil) {309
node.pencil_tool.querySelector('i.ico').innerHTML = item.querySelector('i.ico').innerHTML;310
}311
}312
}314
function update_hotbar() {315
for(let item of node.hotbar_list) {316
item.classList[node.hotbar_list[state.index] === item ? 'add' : 'remove']('active');317
}318
}320
function update_focus(bool) {321
for(let item of node.hotbar_list) {322
item.style.pointerEvents = bool ? '' : 'none';323
}324
for(let item of node.tool_list) {325
item.style.pointerEvents = bool ? '' : 'none';326
}327
for(let item of node.action_list) {328
item.style.pointerEvents = bool ? '' : 'none';329
}330
}332
function update_modal_list() {333
for(let item of node.modal_list) {334
item.classList.add('hidden');335
}336
for(let item of node.modal_event_list) {337
item.classList.remove('active');338
}339
state.tab = null;340
update_tab_list();341
state.caption = 'controls';342
update_caption_list();343
if(state.modal) {344
canvas.on_release(event, state, true);345
let modal = document.querySelector(`.modal.${state.modal}-modal`);346
let button = document.querySelector(`[data-event*=".modal"][data-code="${state.modal}"]`);347
if(modal) {348
modal.classList.remove('hidden');349
modal.animate([350
{ opacity: 0, marginTop: '-6px' },351
{ opacity: 1, marginTop: '0' }352
], {353
duration: 100,354
fill: 'both'355
});356
}357
if(button) {358
button.classList.add('active');359
}360
}361
}363
function update_tab_list() {364
for(let item of node.modal_list) {365
let main = item.querySelector('.main');366
if(main) {367
main.classList.remove('hidden');368
main.style.marginLeft = '0';369
}370
}371
for(let item of node.tab_list) {372
item.classList.add('hidden');373
}374
if(state.modal && state.tab) {375
let modal = document.querySelector(`.modal.${state.modal}-modal`);376
let tab = document.querySelector(`.${state.tab}-tab`);377
let main = modal.querySelector('.main');378
if(modal && tab && main) {379
tab.classList.remove('hidden');380
main.style.marginLeft = '-100%';381
main.classList.add('hidden');382
}383
}384
}386
function update_fullscreen_option() {387
let fullscreen = document.fullscreenElement;388
node.option_event_fullscreen.classList[fullscreen ? 'add' : 'remove']('active');389
node.option_event_fullscreen.querySelector('.ico').textContent = fullscreen ? 'toggle_on' : 'toggle_off';390
}392
function update_view_mode_option() {393
let view_mode = state.option.view_mode;394
show_notification(`notification.view-mode.${view_mode ? 'on' : 'off'}`);395
node.option_event_view_mode.classList[view_mode ? 'add' : 'remove']('active');396
node.option_event_view_mode.querySelector('.ico').textContent = view_mode ? 'toggle_on' : 'toggle_off';397
if(view_mode) {398
state.modal = null;399
state.tab = null;400
update_modal_list();401
canvas.on_release(null, state);402
canvas.clear_cursor();403
canvas.get_canvas().style.cursor = 'default';404
node.hotbar.classList.add('inactive');405
node.header.classList.add('inactive');406
node.hotbar.animate([407
{ opacity: 1 },408
{ opacity: 0, bottom: '-10px', pointerEvents: 'none' }409
], {410
easing: 'ease-out',411
duration: 250,412
fill: 'both'413
});414
node.mainbar.animate([415
{ opacity: 1, marginTop: 0 },416
{ opacity: 0, marginTop: '-60px' }417
], {418
easing: 'ease-out',419
duration: 250,420
fill: 'both'421
});422
node.sidebar.classList.remove('hidden');423
node.sidebar.animate([424
{ opacity: 0 },425
{ opacity: 1 }426
], {427
delay: 250,428
easing: 'ease-out',429
duration: 250,430
fill: 'both'431
});432
}433
else {434
canvas.on_release(null, state, true);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
}