A web-based drawing app
git clone git://soophie.de/outline
blob: 8d457426b262848b6f7b65ba0631e071629875b0 (15265 bytes)[doc][blame][raw]
1
import { COLORS } 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
timestamp: null,25
modal: null,26
tab: null,27
option: {28
view_mode: false29
}30
};32
window.addEventListener('load', init);34
function init() {36
canvas.init();37
locale.init('en');38
locale.load('./asset/lang', ['en', 'de']);39
ripple.init();41
node = query({42
wrapper: '.wrapper',43
controls: '.controls',44
modal_list: { query: '.modal', all: true },45
tab_list: { query: '.tab', all: true },46
hotbar: '.hotbar',47
toolbar: '.toolbar',48
hotbar_list: { query: '.hotbar .color', all: true },49
modal_color_list: { query: '.colors-modal .color', all: true },50
language_list: { query: '.settings-modal .language-tab .item[data-event="change.language"]', all: true },51
device_list: { query: '.settings-modal .device-tab .item[data-event="change.device"]', all: true },52
modal_event_list: { query: '[data-event*=".modal"]', all: true },53
tab_event_list: { query: '[data-event*=".tab"]', all: true },54
tool_list: { query: '.tool', all: true },55
action_list: { query: '.action', all: true },56
notification: '.notification',57
scale_input: 'input.scale[type="range"]',58
clear: 'button.btn-apply[data-event="clear"]',59
option_event_fullscreen: '[data-event="toggle.fullscreen"]',60
option_event_view_mode: '[data-event="toggle.view-mode"]'61
});63
fill_hotbar_list();64
fill_modal_color_list();66
bind_events();68
node.wrapper.animate([69
{ opacity: 1 },70
{ opacity: 0, pointerEvents: 'none' }71
], {72
delay: 100,73
duration: 500,74
fill: 'both'75
});76
node.hotbar.animate([77
{ opacity: 0, bottom: '-10px' },78
{ opacity: 1 }79
], {80
delay: 600,81
easing: 'ease-out',82
duration: 250,83
fill: 'both'84
});85
}87
function bind_events() {88
document.addEventListener('dragstart', prevent);89
document.addEventListener('contextmenu', prevent);90
document.addEventListener('pointerdown', on_pointerdown);91
document.addEventListener('pointermove', on_pointermove);92
document.addEventListener('pointerup', on_pointerup);93
document.addEventListener('pointerout', on_pointerout);94
document.addEventListener('keydown', on_keydown);95
document.addEventListener('wheel', on_wheel);96
document.addEventListener('wheel', function(event) {97
if(event.ctrlKey) {98
prevent(event);99
}100
}, { passive: false });101
document.addEventListener('touchmove', function(event) {102
if(event.target !== node.scale_input) {103
event.preventDefault();104
}105
}, { passive: false });107
for(let item of node.language_list) {108
item.addEventListener('click', function(event) {109
let code = item.dataset.code;110
locale.change(code);111
update_language_list();112
});113
}114
for(let item of node.device_list) {115
item.addEventListener('click', function(event) {116
let code = item.dataset.code;117
if(state.device_list[code]) {118
state.device = code;119
update_device_list();120
}121
});122
}123
for(let item of node.modal_event_list) {124
item.addEventListener('click', function(event) {125
let code = item.dataset.code;126
let match = /^(toggle|open|close).modal$/.exec(item.dataset.event);127
if(match) {128
switch(match[1]) {129
case 'toggle':130
if(code) {131
state.modal = state.modal !== code ? code : null;132
update_modal_list();133
}134
break;135
case 'open':136
if(code && state.modal !== code) {137
state.modal = code;138
update_modal_list();139
}140
break;141
case 'close':142
if(state.modal !== null) {143
state.modal = null;144
update_modal_list();145
}146
break;147
default:148
break;149
}150
}151
});152
}153
for(let item of node.tab_event_list) {154
item.addEventListener('click', function(event) {155
let code = item.dataset.code;156
let match = /^(open|close).tab/.exec(item.dataset.event);157
if(match) {158
switch(match[1]) {159
case 'open':160
if(code && state.tab !== code) {161
state.tab = code;162
update_tab_list();163
}164
break;165
case 'close':166
if(state.tab !== null) {167
state.tab = null;168
update_tab_list();169
}170
break;171
default:172
break;173
}174
}175
});176
}177
node.scale_input.addEventListener('input', function(event) {178
state.radius = parseInt(node.scale_input.value);179
state.point = {180
x: parseInt(window.innerWidth / 2),181
y: parseInt(window.innerHeight / 2)182
};183
canvas.draw_cursor(state);184
});185
node.clear.addEventListener('click', function(event) {186
canvas.clear();187
show_notification('notification.clear');188
state.modal = null;189
update_modal_list();190
});191
node.option_event_fullscreen.addEventListener('click', function(event) {192
if(!document.fullscreenElement) {193
document.documentElement.requestFullscreen();194
}195
else if(document.exitFullscreen) {196
document.exitFullscreen();197
}198
});199
document.addEventListener('fullscreenchange', on_fullscreenchange);200
node.option_event_view_mode.addEventListener('click', function(event) {201
state.option.view_mode = !state.option.view_mode;202
update_view_mode_option();203
});204
}206
function update_language_list() {207
let lang = locale.get_current();208
for(let item of node.language_list) {209
let code = item.dataset.code;210
item.classList[code === lang ? 'add' : 'remove']('active');211
}212
}214
function update_device_list() {215
canvas.get_canvas().style.cursor = state.device === 'mouse' ? 'none' : '';216
for(let item of node.device_list) {217
let code = item.dataset.code;218
item.classList[code === state.device ? 'add' : 'remove']('active');219
item.classList[state.device_list[code] ? 'remove' : 'add']('inactive');220
}221
}223
function update_hotbar() {224
canvas.on_release(event);225
for(let item of node.hotbar_list) {226
item.classList[node.hotbar_list[state.index] === item ? 'add' : 'remove']('active');227
}228
}230
function update_focus(bool) {231
for(let item of node.hotbar_list) {232
item.style.pointerEvents = bool ? '' : 'none';233
}234
for(let item of node.tool_list) {235
item.style.pointerEvents = bool ? '' : 'none';236
}237
for(let item of node.action_list) {238
item.style.pointerEvents = bool ? '' : 'none';239
}240
}242
function update_modal_list() {243
for(let item of node.modal_list) {244
item.classList.add('hidden');245
}246
for(let item of node.tool_list) {247
item.classList.remove('active');248
}249
for(let item of node.action_list) {250
item.classList.remove('active');251
}252
state.tab = null;253
update_tab_list();254
if(state.modal) {255
let modal = document.querySelector(`.modal.${state.modal}-modal`);256
let button = document.querySelector(`[data-event*=".modal"][data-code="${state.modal}"]`);257
if(modal) {258
modal.classList.remove('hidden');259
modal.animate([260
{ opacity: 0, marginTop: '-6px' },261
{ opacity: 1, marginTop: '0' }262
], {263
duration: 100,264
fill: 'both'265
});266
}267
if(button) {268
button.classList.add('active');269
}270
}271
}273
function update_tab_list() {274
for(let item of node.modal_list) {275
let main = item.querySelector('.main');276
if(main) {277
main.classList.remove('hidden');278
main.style.marginLeft = '0';279
}280
}281
for(let item of node.tab_list) {282
item.classList.add('hidden');283
}284
if(state.modal && state.tab) {285
let modal = document.querySelector(`.modal.${state.modal}-modal`);286
let tab = document.querySelector(`.${state.tab}-tab`);287
let main = modal.querySelector('.main');288
if(modal && tab && main) {289
tab.classList.remove('hidden');290
main.style.marginLeft = '-100%';291
main.classList.add('hidden');292
}293
}294
}296
function update_fullscreen_option() {297
let fullscreen = document.fullscreenElement;298
node.option_event_fullscreen.classList[fullscreen ? 'add' : 'remove']('active');299
node.option_event_fullscreen.querySelector('.ico').textContent = fullscreen ? 'toggle_on' : 'toggle_off';300
}302
function update_view_mode_option() {303
let view_mode = state.option.view_mode;304
node.option_event_view_mode.classList[view_mode ? 'add' : 'remove']('active');305
node.option_event_view_mode.querySelector('.ico').textContent = view_mode ? 'toggle_on' : 'toggle_off';306
if(view_mode) {307
canvas.clear_cursor();308
canvas.get_canvas().style.cursor = 'default';309
node.hotbar.classList.add('inactive');310
node.toolbar.classList.add('inactive');311
node.hotbar.animate([312
{ opacity: 1 },313
{ opacity: 0, bottom: '-10px', pointerEvents: 'none' }314
], {315
easing: 'ease-out',316
duration: 250,317
fill: 'both'318
});319
node.toolbar.animate([320
{ opacity: 1 },321
{ opacity: 0 }322
], {323
easing: 'ease-out',324
duration: 250,325
fill: 'both'326
});327
}328
else {329
canvas.get_canvas().style.cursor = 'none';330
node.hotbar.classList.remove('inactive');331
node.toolbar.classList.remove('inactive');332
node.hotbar.animate([333
{ opacity: 0, bottom: '-10px' },334
{ opacity: 1, bottom: '30px' }335
], {336
easing: 'ease-out',337
duration: 250,338
fill: 'both'339
});340
node.toolbar.animate([341
{ opacity: 0 },342
{ opacity: 1 }343
], {344
easing: 'ease-out',345
duration: 250,346
fill: 'both'347
});348
}349
}351
function on_pointerdown(event) {352
// detect touch353
if(!state.device_list.touch && event.pointerType === 'touch') {354
state.device_list.touch = true;355
if(state.device) {356
show_notification('notification.input-device.touch.add');357
}358
else {359
state.device = 'touch';360
state.device_fallback = 'touch';361
}362
update_device_list();363
}364
// detect pen365
if(!state.device_list.pen && event.pointerType === 'pen') {366
state.device_list.pen = true;367
show_notification('notification.input-device.pencil.add');368
if(!state.device) {369
state.device = 'pen';370
state.device_fallback = 'pen';371
}372
update_device_list();373
}374
// drawing375
if(!state.option.view_mode && state.device === event.pointerType) {376
if(event.target === canvas.get_canvas()) {377
update_focus(false);378
canvas.on_press(event, state);379
state.point = {380
x: event.layerX,381
y: event.layerY382
};383
canvas.draw_cursor(state);384
}385
}386
}388
function on_pointermove(event) {389
// detect mouse390
if(!state.device_list.mouse && event.pointerType === 'mouse') {391
state.device_list.mouse = true;392
if(state.device) {393
show_notification('notification.input-device.mouse.add');394
}395
else {396
state.device = 'mouse';397
state.device_fallback = 'mouse';398
}399
update_device_list();400
}401
// drawing402
if(!state.option.view_mode && state.device === event.pointerType) {403
if(event.target === canvas.get_canvas()) {404
canvas.on_move(event, state);405
state.point = {406
x: event.layerX,407
y: event.layerY408
};409
canvas.draw_cursor(state);410
}411
}412
}414
function on_pointerup(event) {415
if(!state.option.view_mode && state.device === event.pointerType) {416
canvas.on_release(event);417
canvas.draw_cursor(state);418
update_focus(true);419
}420
}422
function on_pointerout(event) {423
if(!state.option.view_mode && state.device === event.pointerType) {424
canvas.on_release(event);425
state.point = null;426
canvas.draw_cursor(state);427
}428
}430
function on_keydown(event) {431
let lock = event.getModifierState("CapsLock");432
if(event.code === 'Escape') {433
if(state.tab) {434
state.tab = null;435
update_tab_list();436
return;437
}438
if(state.modal) {439
state.modal = null;440
update_modal_list();441
return;442
}443
}444
if(!event.ctrlKey && !event.shiftKey && !event.altKey) {445
if(!state.option.view_mode && !lock) {446
if(event.code.startsWith('Digit')) {447
let i = parseInt(event.code.substr(5)) - 1;448
if(state.index !== i) {449
if(i >= 0 && i < state.color_list.length) {450
state.color = state.color_list[i];451
state.index = i;452
canvas.draw_cursor(state);453
update_hotbar();454
}455
}456
}457
}458
if(event.code === 'KeyC') {459
if(state.modal !== 'colors') {460
state.modal = 'colors';461
update_modal_list();462
}463
}464
if(event.code === 'KeyS') {465
if(state.modal !== 'scale') {466
state.modal = 'scale';467
update_modal_list();468
}469
}470
if(event.code === 'KeyE') {471
if(state.modal !== 'pencil') {472
state.modal = 'pencil';473
update_modal_list();474
}475
}476
if(event.code === 'KeyD') {477
if(state.modal !== 'clear') {478
state.modal = 'clear';479
update_modal_list();480
}481
}482
if(event.code === 'KeyQ') {483
if(state.modal !== 'capture') {484
state.modal = 'capture';485
update_modal_list();486
}487
}488
if(event.code === 'KeyV') {489
state.option.view_mode = !state.option.view_mode;490
update_view_mode_option();491
}492
}493
// alt494
if(event.altKey && !event.ctrlKey && !event.shiftKey) {495
if(event.code === 'KeyM') {496
if(state.modal !== 'multiplayer') {497
state.modal = 'multiplayer';498
update_modal_list();499
}500
}501
if(event.code === 'KeyP') {502
if(state.modal !== 'account') {503
state.modal = 'account';504
update_modal_list();505
}506
}507
if(event.code === 'KeyS') {508
if(state.modal !== 'settings') {509
state.modal = 'settings';510
update_modal_list();511
}512
}513
if(event.code === 'KeyH') {514
if(state.tab !== 'help') {515
state.modal = 'settings';516
update_modal_list();517
state.tab = 'help';518
update_tab_list();519
}520
}521
if(event.code === 'KeyL') {522
if(state.tab !== 'language') {523
state.modal = 'settings';524
update_modal_list();525
state.tab = 'language';526
update_tab_list();527
}528
}529
if(event.code === 'KeyI') {530
if(state.tab !== 'device') {531
state.modal = 'settings';532
update_modal_list();533
state.tab = 'device';534
update_tab_list();535
}536
}537
if(event.code === 'KeyA') {538
if(state.tab !== 'appearance') {539
state.modal = 'settings';540
update_modal_list();541
state.tab = 'appearance';542
update_tab_list();543
}544
}545
}546
}548
function on_wheel(event) {549
if(!state.option.view_mode && state.device === 'mouse') {550
let y = event.deltaY < 0 ? 1 : -1;551
if(state.radius + y > 2 - 1 && state.radius + y < 50 + 1) {552
canvas.on_release(event);553
state.radius += y;554
node.scale_input.value = state.radius;555
canvas.draw_cursor(state);556
}557
}558
}560
function on_fullscreenchange(event) {561
update_fullscreen_option();562
}564
function fill_hotbar_list() {565
for(let [ i, item ] of Object.entries(node.hotbar_list)) {566
if(state.color_list.length > i) {567
item.style.background = COLORS[state.color_list[i]];568
item.addEventListener('click', function(event) {569
state.color = state.color_list[i];570
state.index = i;571
canvas.draw_cursor(state);572
update_hotbar();573
});574
if(state.index == i) {575
item.classList.add('active');576
}577
}578
}579
}581
function fill_modal_color_list() {582
for(let [ i, item ] of Object.entries(node.modal_color_list)) {583
if(COLORS.length > i) {584
item.style.background = COLORS[i];585
item.addEventListener('pointerdown', function(event) {586
state.color = i;587
state.modal = null;588
update_modal_list();589
canvas.draw_cursor(state);590
});591
}592
}593
}595
function show_notification(message) {596
let code = uuid();597
state.timestamp = code;598
let content = node.notification.querySelector('.content');599
content.textContent = locale.get()[message];600
node.notification.animate([601
{ opacity: 0 },602
{ opacity: 1 }603
], {604
duration: 200,605
fill: 'both'606
});607
setTimeout(function() {608
if(state.timestamp === code) {609
node.notification.animate([610
{ opacity: 1 },611
{ opacity: 0 }612
], {613
duration: 200,614
fill: 'both'615
});616
}617
}, 2000);618
}