A web-based drawing app
git clone git://soophie.de/outline
blob: 20ec6fa2c138bea9c6489d62ab3e4bca2bc49b62 (15672 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
show_notification(`notification.view-mode.${view_mode ? 'on' : 'off'}`);305
node.option_event_view_mode.classList[view_mode ? 'add' : 'remove']('active');306
node.option_event_view_mode.querySelector('.ico').textContent = view_mode ? 'toggle_on' : 'toggle_off';307
if(view_mode) {308
state.modal = null;309
state.tab = null;310
update_modal_list();311
canvas.on_release();312
canvas.clear_cursor();313
canvas.get_canvas().style.cursor = 'default';314
node.hotbar.classList.add('inactive');315
node.toolbar.classList.add('inactive');316
node.hotbar.animate([317
{ opacity: 1 },318
{ opacity: 0, bottom: '-10px', pointerEvents: 'none' }319
], {320
easing: 'ease-out',321
duration: 250,322
fill: 'both'323
});324
node.toolbar.animate([325
{ opacity: 1 },326
{ opacity: 0 }327
], {328
easing: 'ease-out',329
duration: 250,330
fill: 'both'331
});332
}333
else {334
canvas.get_canvas().style.cursor = 'none';335
node.hotbar.classList.remove('inactive');336
node.toolbar.classList.remove('inactive');337
node.hotbar.animate([338
{ opacity: 0, bottom: '-10px' },339
{ opacity: 1, bottom: '30px' }340
], {341
easing: 'ease-out',342
duration: 250,343
fill: 'both'344
});345
node.toolbar.animate([346
{ opacity: 0 },347
{ opacity: 1 }348
], {349
easing: 'ease-out',350
duration: 250,351
fill: 'both'352
});353
}354
}356
function on_pointerdown(event) {357
// detect touch358
if(!state.device_list.touch && event.pointerType === 'touch') {359
state.device_list.touch = true;360
if(state.device) {361
show_notification('notification.input-device.touch.add');362
}363
else {364
state.device = 'touch';365
state.device_fallback = 'touch';366
}367
update_device_list();368
}369
// detect pen370
if(!state.device_list.pen && event.pointerType === 'pen') {371
state.device_list.pen = true;372
show_notification('notification.input-device.pencil.add');373
if(!state.device) {374
state.device = 'pen';375
state.device_fallback = 'pen';376
}377
update_device_list();378
}379
// drawing380
if(!state.option.view_mode && state.device === event.pointerType) {381
if(event.target === canvas.get_canvas()) {382
update_focus(false);383
canvas.on_press(event, state);384
state.point = {385
x: event.layerX,386
y: event.layerY387
};388
canvas.draw_cursor(state);389
}390
}391
}393
function on_pointermove(event) {394
// detect mouse395
if(!state.device_list.mouse && event.pointerType === 'mouse') {396
state.device_list.mouse = true;397
if(state.device) {398
show_notification('notification.input-device.mouse.add');399
}400
else {401
state.device = 'mouse';402
state.device_fallback = 'mouse';403
}404
update_device_list();405
}406
// drawing407
if(!state.option.view_mode && state.device === event.pointerType) {408
if(event.target === canvas.get_canvas()) {409
canvas.on_move(event, state);410
state.point = {411
x: event.layerX,412
y: event.layerY413
};414
canvas.draw_cursor(state);415
}416
}417
}419
function on_pointerup(event) {420
if(!state.option.view_mode && state.device === event.pointerType) {421
canvas.on_release(event);422
canvas.draw_cursor(state);423
update_focus(true);424
}425
}427
function on_pointerout(event) {428
if(!state.option.view_mode && state.device === event.pointerType) {429
canvas.on_release(event);430
state.point = null;431
canvas.draw_cursor(state);432
}433
}435
function on_keydown(event) {436
let lock = event.getModifierState("CapsLock");437
if(event.code === 'Escape') {438
if(state.tab) {439
state.tab = null;440
update_tab_list();441
return;442
}443
if(state.modal) {444
state.modal = null;445
update_modal_list();446
return;447
}448
}449
if(!event.ctrlKey && !event.shiftKey && !event.altKey) {450
if(!state.option.view_mode && !lock) {451
if(event.code.startsWith('Digit')) {452
let i = parseInt(event.code.substr(5)) - 1;453
if(state.index !== i) {454
if(i >= 0 && i < state.color_list.length) {455
state.color = state.color_list[i];456
state.index = i;457
canvas.draw_cursor(state);458
update_hotbar();459
}460
}461
}462
}463
if(event.code === 'KeyC') {464
state.modal = state.modal !== 'colors' ? 'colors' : null;465
update_modal_list();466
}467
if(event.code === 'KeyS') {468
state.modal = state.modal !== 'scale' ? 'scale' : null;469
update_modal_list();470
}471
if(event.code === 'KeyE') {472
state.modal = state.modal !== 'pencil' ? 'pencil' : null;473
update_modal_list();474
}475
if(event.code === 'KeyD') {476
state.modal = state.modal !== 'clear' ? 'clear' : null;477
update_modal_list();478
}479
if(event.code === 'KeyQ') {480
state.modal = state.modal !== 'capture' ? 'capture' : null;481
update_modal_list();482
}483
if(event.code === 'KeyV') {484
state.option.view_mode = !state.option.view_mode;485
update_view_mode_option();486
}487
}488
// alt489
if(event.altKey && !event.ctrlKey && !event.shiftKey) {490
if(event.code === 'KeyM') {491
state.modal = state.modal !== 'multiplayer' ? 'multiplayer' : null;492
update_modal_list();493
}494
if(event.code === 'KeyP') {495
state.modal = state.modal !== 'account' ? 'account' : null;496
update_modal_list();497
}498
if(event.code === 'KeyS') {499
state.modal = state.modal !== 'settings' ? 'settings' : null;500
update_modal_list();501
}502
if(event.code === 'KeyH') {503
if(state.tab !== 'help') {504
state.modal = 'settings';505
update_modal_list();506
state.tab = 'help';507
update_tab_list();508
}509
else {510
state.modal = null;511
update_modal_list();512
}513
}514
if(event.code === 'KeyL') {515
if(state.tab !== 'language') {516
state.modal = 'settings';517
update_modal_list();518
state.tab = 'language';519
update_tab_list();520
}521
else {522
state.modal = null;523
update_modal_list();524
}525
}526
if(event.code === 'KeyI') {527
if(state.tab !== 'device') {528
state.modal = 'settings';529
update_modal_list();530
state.tab = 'device';531
update_tab_list();532
}533
else {534
state.modal = null;535
update_modal_list();536
}537
}538
if(event.code === 'KeyA') {539
if(state.tab !== 'appearance') {540
state.modal = 'settings';541
update_modal_list();542
state.tab = 'appearance';543
update_tab_list();544
}545
else {546
state.modal = null;547
update_modal_list();548
}549
}550
}551
}553
function on_wheel(event) {554
if(!state.option.view_mode && state.device === 'mouse') {555
let y = event.deltaY < 0 ? 1 : -1;556
if(state.radius + y > 2 - 1 && state.radius + y < 50 + 1) {557
canvas.on_release(event);558
state.radius += y;559
node.scale_input.value = state.radius;560
canvas.draw_cursor(state);561
}562
}563
}565
function on_fullscreenchange(event) {566
update_fullscreen_option();567
}569
function fill_hotbar_list() {570
for(let [ i, item ] of Object.entries(node.hotbar_list)) {571
if(state.color_list.length > i) {572
item.style.background = COLORS[state.color_list[i]];573
item.addEventListener('click', function(event) {574
state.color = state.color_list[i];575
state.index = i;576
canvas.draw_cursor(state);577
update_hotbar();578
});579
if(state.index == i) {580
item.classList.add('active');581
}582
}583
}584
}586
function fill_modal_color_list() {587
for(let [ i, item ] of Object.entries(node.modal_color_list)) {588
if(COLORS.length > i) {589
item.style.background = COLORS[i];590
item.addEventListener('pointerdown', function(event) {591
state.color = i;592
state.modal = null;593
update_modal_list();594
canvas.draw_cursor(state);595
state.index = null;596
update_hotbar();597
});598
}599
}600
}602
function show_notification(message) {603
let code = uuid();604
state.timestamp = code;605
let content = node.notification.querySelector('.content');606
content.textContent = locale.get()[message];607
node.notification.animate([608
{ opacity: 0 },609
{ opacity: 1 }610
], {611
duration: 200,612
fill: 'both'613
});614
setTimeout(function() {615
if(state.timestamp === code) {616
node.notification.animate([617
{ opacity: 1 },618
{ opacity: 0 }619
], {620
duration: 200,621
fill: 'both'622
});623
}624
}, 2000);625
}