A web-based drawing app
git clone git://soophie.de/outline
blob: ef3f0900b8cd0651c78086524fee5f199b74bc8a (19279 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
}282
}284
function update_language_list() {285
let lang = locale.get_current();286
for(let item of node.language_list) {287
let code = item.dataset.code;288
item.classList[code === lang ? 'add' : 'remove']('active');289
}290
}292
function update_device_list() {293
canvas.get_canvas().style.cursor = state.device === 'mouse' ? 'none' : '';294
for(let item of node.device_list) {295
let code = item.dataset.code;296
item.classList[code === state.device ? 'add' : 'remove']('active');297
item.classList[state.device_list[code] ? 'remove' : 'add']('inactive');298
}299
}301
function update_pencil_list() {302
for(let item of node.pencil_list) {303
let code = item.dataset.code;304
item.classList[code === state.pencil ? 'add' : 'remove']('active');305
if(code === state.pencil) {306
node.pencil_tool.querySelector('i.ico').innerHTML = item.querySelector('i.ico').innerHTML;307
}308
}309
}311
function update_hotbar() {312
for(let item of node.hotbar_list) {313
item.classList[node.hotbar_list[state.index] === item ? 'add' : 'remove']('active');314
}315
}317
function update_focus(bool) {318
for(let item of node.hotbar_list) {319
item.style.pointerEvents = bool ? '' : 'none';320
}321
for(let item of node.tool_list) {322
item.style.pointerEvents = bool ? '' : 'none';323
}324
for(let item of node.action_list) {325
item.style.pointerEvents = bool ? '' : 'none';326
}327
}329
function update_modal_list() {330
for(let item of node.modal_list) {331
item.classList.add('hidden');332
}333
for(let item of node.modal_event_list) {334
item.classList.remove('active');335
}336
state.tab = null;337
update_tab_list();338
state.caption = 'controls';339
update_caption_list();340
if(state.modal) {341
canvas.on_release(event, state, true);342
let modal = document.querySelector(`.modal.${state.modal}-modal`);343
let button = document.querySelector(`[data-event*=".modal"][data-code="${state.modal}"]`);344
if(modal) {345
modal.classList.remove('hidden');346
modal.animate([347
{ opacity: 0, marginTop: '-6px' },348
{ opacity: 1, marginTop: '0' }349
], {350
duration: 100,351
fill: 'both'352
});353
}354
if(button) {355
button.classList.add('active');356
}357
}358
}360
function update_tab_list() {361
for(let item of node.modal_list) {362
let main = item.querySelector('.main');363
if(main) {364
main.classList.remove('hidden');365
main.style.marginLeft = '0';366
}367
}368
for(let item of node.tab_list) {369
item.classList.add('hidden');370
}371
if(state.modal && state.tab) {372
let modal = document.querySelector(`.modal.${state.modal}-modal`);373
let tab = document.querySelector(`.${state.tab}-tab`);374
let main = modal.querySelector('.main');375
if(modal && tab && main) {376
tab.classList.remove('hidden');377
main.style.marginLeft = '-100%';378
main.classList.add('hidden');379
}380
}381
}383
function update_fullscreen_option() {384
let fullscreen = document.fullscreenElement;385
node.option_event_fullscreen.classList[fullscreen ? 'add' : 'remove']('active');386
node.option_event_fullscreen.querySelector('.ico').textContent = fullscreen ? 'toggle_on' : 'toggle_off';387
}389
function update_view_mode_option() {390
let view_mode = state.option.view_mode;391
show_notification(`notification.view-mode.${view_mode ? 'on' : 'off'}`);392
node.option_event_view_mode.classList[view_mode ? 'add' : 'remove']('active');393
node.option_event_view_mode.querySelector('.ico').textContent = view_mode ? 'toggle_on' : 'toggle_off';394
if(view_mode) {395
state.modal = null;396
state.tab = null;397
update_modal_list();398
canvas.on_release(null, state);399
canvas.clear_cursor();400
canvas.get_canvas().style.cursor = 'default';401
node.hotbar.classList.add('inactive');402
node.header.classList.add('inactive');403
node.hotbar.animate([404
{ opacity: 1 },405
{ opacity: 0, bottom: '-10px', pointerEvents: 'none' }406
], {407
easing: 'ease-out',408
duration: 250,409
fill: 'both'410
});411
node.mainbar.animate([412
{ opacity: 1, marginTop: 0 },413
{ opacity: 0, marginTop: '-60px' }414
], {415
easing: 'ease-out',416
duration: 250,417
fill: 'both'418
});419
node.sidebar.classList.remove('hidden');420
node.sidebar.animate([421
{ opacity: 0 },422
{ opacity: 1 }423
], {424
delay: 250,425
easing: 'ease-out',426
duration: 250,427
fill: 'both'428
});429
}430
else {431
canvas.get_canvas().style.cursor = 'none';432
node.hotbar.classList.remove('inactive');433
node.header.classList.remove('inactive');434
node.hotbar.animate([435
{ opacity: 0, bottom: '-10px' },436
{ opacity: 1, bottom: '30px' }437
], {438
easing: 'ease-out',439
duration: 250,440
fill: 'both'441
});442
node.mainbar.animate([443
{ opacity: 0, marginTop: '-60px' },444
{ opacity: 1, marginTop: 0 }445
], {446
easing: 'ease-out',447
duration: 250,448
fill: 'both'449
});450
node.sidebar.classList.add('hidden');451
}452
}454
function update_undo_and_redo() {455
node.undo_tool.classList[state.history.length > 0 ? 'remove' : 'add']('inactive');456
node.redo_tool.classList[state.redo_history.length > 0 ? 'remove' : 'add']('inactive');457
}459
function update_caption_list() {460
for(let item of node.caption_list) {461
let code = item.dataset.code;462
item.classList[code === state.caption ? 'add' : 'remove']('active');463
}464
for(let item of node.content_list) {465
let code = item.dataset.code;466
item.classList[code === state.caption ? 'remove' : 'add']('hidden');467
}468
}470
function on_pointerdown(event) {471
// detect touch472
if(!state.device_list.touch && event.pointerType === 'touch') {473
state.device_list.touch = true;474
if(state.device) {475
show_notification('notification.input-device.touch.add');476
}477
else {478
state.device = 'touch';479
state.device_fallback = 'touch';480
}481
update_device_list();482
}483
// detect pen484
if(!state.device_list.pen && event.pointerType === 'pen') {485
state.device_list.pen = true;486
show_notification('notification.input-device.pencil.add');487
if(!state.device) {488
state.device = 'pen';489
state.device_fallback = 'pen';490
}491
update_device_list();492
}493
// drawing494
if(!state.option.view_mode && state.device === event.pointerType) {495
if(event.target === canvas.get_canvas()) {496
if(!PRIORITY_MODAL.includes(state.modal)) {497
update_focus(false);498
canvas.on_press(event, state);499
update_undo_and_redo();500
state.point = {501
x: event.layerX,502
y: event.layerY503
};504
canvas.draw_cursor(state);505
}506
}507
}508
}510
function on_pointermove(event) {511
// detect mouse512
if(!state.device_list.mouse && event.pointerType === 'mouse') {513
state.device_list.mouse = true;514
if(state.device) {515
show_notification('notification.input-device.mouse.add');516
}517
else {518
state.device = 'mouse';519
state.device_fallback = 'mouse';520
}521
update_device_list();522
}523
// drawing524
if(!state.option.view_mode && state.device === event.pointerType) {525
if(event.target === canvas.get_canvas()) {526
if(!PRIORITY_MODAL.includes(state.modal)) {527
canvas.on_move(event, state);528
}529
state.point = {530
x: event.layerX,531
y: event.layerY532
};533
}534
canvas.draw_cursor(state);535
}536
}538
function on_pointerup(event) {539
if(!state.option.view_mode && state.device === event.pointerType) {540
canvas.on_release(event, state);541
update_undo_and_redo();542
update_focus(true);543
}544
}546
function on_pointerout(event) {547
if(!state.option.view_mode && state.device === event.pointerType) {548
canvas.on_release(event, state);549
update_undo_and_redo();550
state.point = null;551
canvas.draw_cursor(state);552
}553
}555
function on_keydown(event) {556
if(event.code === 'Escape') {557
if(state.tab) {558
state.tab = null;559
update_tab_list();560
return;561
}562
if(state.modal) {563
state.modal = null;564
update_modal_list();565
return;566
}567
}568
if(!event.ctrlKey && !event.shiftKey && !event.altKey) {569
if(!state.option.view_mode) {570
if(event.code.startsWith('Digit')) {571
let i = parseInt(event.code.substr(5)) - 1;572
if(state.index !== i) {573
if(i >= 0 && i < state.color_list.length) {574
canvas.on_release(event, state);575
state.color = state.color_list[i];576
state.index = i;577
canvas.draw_cursor(state);578
update_hotbar();579
}580
}581
}582
if(event.code === 'KeyZ') {583
state.modal = state.modal !== 'colors' ? 'colors' : null;584
update_modal_list();585
}586
if(event.code === 'KeyX') {587
state.modal = state.modal !== 'scale' ? 'scale' : null;588
update_modal_list();589
}590
if(event.code === 'KeyC') {591
state.modal = state.modal !== 'pencil' ? 'pencil' : null;592
update_modal_list();593
}594
if(event.code === 'KeyV') {595
state.modal = state.modal !== 'clear' ? 'clear' : null;596
update_modal_list();597
}598
/*599
if(event.code === 'KeyB') {600
state.modal = state.modal !== 'multiplayer' ? 'multiplayer' : null;601
update_modal_list();602
}603
if(event.code === 'KeyN') {604
state.modal = state.modal !== 'account' ? 'account' : null;605
update_modal_list();606
}607
*/608
if(event.code === 'KeyM') {609
state.modal = state.modal !== 'more' ? 'more' : null;610
update_modal_list();611
}612
if(event.code === 'KeyQ') {613
canvas.on_release(event, state);614
state.pencil = state.pencil_list[0];615
canvas.draw_cursor(state);616
update_pencil_list();617
}618
if(event.code === 'KeyW') {619
canvas.on_release(event, state);620
state.pencil = state.pencil_list[1];621
canvas.draw_cursor(state);622
update_pencil_list();623
}624
if(event.code === 'KeyE') {625
canvas.on_release(event, state);626
state.pencil = state.pencil_list[2];627
canvas.draw_cursor(state);628
update_pencil_list();629
}630
if(event.code === 'KeyF') {631
if(!document.fullscreenElement) {632
document.documentElement.requestFullscreen();633
}634
else if(document.exitFullscreen) {635
document.exitFullscreen();636
}637
}638
}639
if(event.code === 'Space') {640
state.option.view_mode = !state.option.view_mode;641
update_view_mode_option();642
}643
}644
if(event.ctrlKey && !event.shiftKey && !event.altKey) {645
if(event.code === 'KeyY') {646
canvas.undo(state);647
update_undo_and_redo();648
}649
if(event.code === 'KeyZ') {650
canvas.redo(state);651
update_undo_and_redo();652
}653
}654
}656
function on_wheel(event) {657
if(!state.option.view_mode && state.device === 'mouse') {658
if(event.target === canvas.get_canvas()) {659
let y = event.deltaY < 0 ? 1 : -1;660
if(state.radius + y > 2 - 1 && state.radius + y < 50 + 1) {661
canvas.on_release(event, state);662
state.radius += y;663
node.scale_input.value = state.radius;664
canvas.draw_cursor(state);665
}666
}667
}668
}670
function on_fullscreenchange(event) {671
update_fullscreen_option();672
}674
function fill_hotbar_list() {675
for(let [ i, item ] of Object.entries(node.hotbar_list)) {676
if(state.color_list.length > i) {677
item.style.background = COLORS[state.color_list[i]];678
item.addEventListener('click', function(event) {679
canvas.on_release(event, state);680
state.color = state.color_list[i];681
state.index = i;682
canvas.draw_cursor(state);683
update_hotbar();684
});685
if(state.index == i) {686
item.classList.add('active');687
}688
}689
}690
}692
function fill_modal_color_list() {693
for(let [ i, item ] of Object.entries(node.modal_color_list)) {694
if(COLORS.length > i) {695
item.style.background = COLORS[i];696
item.addEventListener('click', function(event) {697
canvas.on_release(event, state);698
state.color = i;699
state.modal = null;700
update_modal_list();701
canvas.draw_cursor(state);702
state.index = null;703
update_hotbar();704
});705
}706
}707
}709
function show_notification(message) {710
let code = uuid();711
state.timestamp = code;712
let content = node.notification.querySelector('.content');713
content.textContent = locale.get()[message];714
node.notification.animate([715
{ opacity: 0 },716
{ opacity: 1 }717
], {718
duration: 200,719
fill: 'both'720
});721
setTimeout(function() {722
if(state.timestamp === code) {723
node.notification.animate([724
{ opacity: 1 },725
{ opacity: 0 }726
], {727
duration: 200,728
fill: 'both'729
});730
}731
}, 1000);732
}