A web-based drawing app
git clone git://soophie.de/outline
blob: 4ab1768f6258e78183602003cd2bee1e77b2f58f (21968 bytes)[doc][blame][raw]
1
import { FIREBASE, COLORS, PRIORITY_MODAL, LOCALES } from './constant.js';2
import { uuid, query, prevent } from './util.js';4
import locale from './mod/locale.js';5
import canvas from './mod/canvas.js';6
import cookie from './mod/cookie.js';8
let node;9
let state = {10
color: 54,11
color_list: [54, 2, 17, 27, 37, 12],12
radius: 14,13
region: [],14
crop: false,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: false,36
crop_mode: false37
},38
history: [],39
redo_history: []40
};42
export function init() {44
locale.init();45
locale.load('./asset/lang', LOCALES);46
canvas.init();48
node = query({49
wrapper: '.wrapper',50
controls: '.controls',51
modal_list: { query: '.modal', all: true },52
tab_list: { query: '.tab', all: true },53
header: '.header-list',54
hotbar: '.hotbar',55
hotbar_list: { query: '.hotbar .color', all: true },56
modal_color_list: { query: '.colors-modal .color', all: true },57
language_list: { query: '.more-modal .language-tab .item[data-event="change.language"]', all: true },58
device_list: { query: '.more-modal .device-tab .item[data-event="change.device"]', all: true },59
pencil_list: { query: '.pencil-modal .item[data-event="change.pencil"]', all: true },60
modal_event_list: { query: '[data-event*=".modal"]', all: true },61
tab_event_list: { query: '[data-event*=".tab"]', all: true },62
capture_event_list: { query: '[data-event*=".capture"]', all: true },63
tool_list: { query: '.tool', all: true },64
action_list: { query: '.action', all: true },65
notification: '.notification',66
scale_input: 'input.scale[type="range"]',67
clear: 'button.btn-apply[data-event="clear"]',68
pencil_tool: '.tool-item[data-code="pencil"]',69
option_event_fullscreen: '[data-event="toggle.fullscreen"]',70
option_event_view_mode: '[data-event="toggle.view-mode"]',71
undo_tool: '[data-lang="action.undo"]',72
redo_tool: '[data-lang="action.redo"]',73
colors_modal: '.colors-modal',74
mainbar: '.mainbar',75
sidebar: '.sidebar',76
edit_mode: '[data-event="close.view-mode"]',77
content: '.modal .content',78
caption_list: { query: '[data-event="open.content"]' , all: true },79
content_list: { query: '.content-item' , all: true },80
download: '#download',81
cookie: '.cookie',82
accept: '.button.accept'83
});85
update_language_list();87
fill_hotbar_list();88
fill_modal_color_list();90
bind_events(locale);92
node.wrapper.animate([93
{ opacity: 1 },94
{ opacity: 0, pointerEvents: 'none' }95
], {96
delay: 100,97
duration: 500,98
fill: 'both'99
});100
node.hotbar.animate([101
{ opacity: 0, bottom: '-10px' },102
{ opacity: 1 }103
], {104
delay: 600,105
easing: 'ease-out',106
duration: 250,107
fill: 'both'108
});110
if(!cookie.hasAccepted()) {111
node.cookie.classList.remove('hidden');112
cookie.requestPermission(node.accept)113
.then(function() {114
node.cookie.classList.add('hidden');115
try {116
firebase.initializeApp(FIREBASE);117
firebase.analytics();118
}119
catch(error) {120
console.warn('Unable to load Google Analytics!');121
}122
});123
}124
}126
function bind_events(locale) {127
document.addEventListener('dragstart', prevent);128
document.addEventListener('contextmenu', prevent);129
document.addEventListener('pointerdown', on_pointerdown);130
document.addEventListener('pointermove', on_pointermove);131
document.addEventListener('pointerup', on_pointerup);132
document.addEventListener('pointerout', on_pointerout);133
document.addEventListener('keydown', on_keydown);134
document.addEventListener('wheel', on_wheel);135
document.addEventListener('wheel', function(event) {136
if(event.ctrlKey) {137
prevent(event);138
}139
}, { passive: false });140
document.addEventListener('touchmove', function(event) {141
if(event.target !== node.scale_input && event.target !== node.colors_modal && !node.modal_color_list.includes(event.target) && event.target !== node.content) {142
event.preventDefault();143
}144
}, { passive: false });146
for(let item of node.language_list) {147
item.addEventListener('click', function(event) {148
let code = item.dataset.code;149
locale.change(code);150
update_language_list();151
});152
}153
for(let item of node.device_list) {154
item.addEventListener('click', function(event) {155
let code = item.dataset.code;156
if(state.device_list[code]) {157
state.device = code;158
update_device_list();159
}160
});161
}162
for(let item of node.pencil_list) {163
item.addEventListener('click', function(event) {164
state.pencil = item.dataset.code;165
update_pencil_list();166
state.modal = null;167
update_modal_list();168
});169
}170
for(let item of node.modal_event_list) {171
item.addEventListener('click', function(event) {172
let code = item.dataset.code;173
let match = /^(toggle|open|close).modal$/.exec(item.dataset.event);174
if(match) {175
if(!item.classList.contains('inactive')) {176
switch(match[1]) {177
case 'toggle':178
if(code) {179
state.modal = state.modal !== code ? code : null;180
update_modal_list();181
}182
break;183
case 'open':184
if(code && state.modal !== code) {185
state.modal = code;186
update_modal_list();187
}188
break;189
case 'close':190
if(state.modal !== null) {191
state.modal = null;192
update_modal_list();193
}194
break;195
default:196
break;197
}198
}199
}200
});201
}202
for(let item of node.tab_event_list) {203
item.addEventListener('click', function(event) {204
let code = item.dataset.code;205
let match = /^(open|close).tab/.exec(item.dataset.event);206
if(match) {207
switch(match[1]) {208
case 'open':209
if(code && state.tab !== code) {210
state.tab = code;211
update_tab_list();212
}213
break;214
case 'close':215
if(state.tab !== null) {216
state.tab = null;217
update_tab_list();218
}219
break;220
default:221
break;222
}223
}224
});225
}226
for(let item of node.capture_event_list) {227
item.addEventListener('click', function(event) {228
let code = item.dataset.code;229
if(code === 'download') {230
if(state.option.crop_mode) {231
state.option.crop_mode = false;232
canvas.draw_crop(state);233
canvas.get_canvas().style.cursor = 'default';234
}235
let data = canvas.create_capture(0, 0, window.innerWidth, window.innerHeight, 60);236
if(data) {237
node.download.download = `outline-${uuid()}.png`;238
node.download.href = data;239
node.download.click();240
}241
else {242
console.warn('Unable to create capture from current state!');243
}244
}245
if(code === 'crop') {246
state.option.crop_mode = true;247
state.region = [];248
canvas.draw_crop(state);249
canvas.get_canvas().style.cursor = 'crosshair';250
}251
state.modal = null;252
state.tab = null;253
update_modal_list();254
})255
}256
node.scale_input.addEventListener('input', function(event) {257
state.radius = parseInt(node.scale_input.value);258
state.point = {259
x: parseInt(window.innerWidth / 2),260
y: parseInt(window.innerHeight / 2)261
};262
canvas.draw_cursor(state);263
});264
node.clear.addEventListener('click', function(event) {265
canvas.clear(state);266
update_undo_and_redo();267
show_notification('notification.clear');268
state.modal = null;269
update_modal_list();270
});271
node.option_event_fullscreen.addEventListener('click', function(event) {272
if(!document.fullscreenElement) {273
document.documentElement.requestFullscreen();274
}275
else if(document.exitFullscreen) {276
document.exitFullscreen();277
}278
});279
window.addEventListener('fullscreenchange', on_fullscreenchange);280
node.option_event_view_mode.addEventListener('click', function(event) {281
state.option.view_mode = !state.option.view_mode;282
update_view_mode_option();283
});284
node.undo_tool.addEventListener('click', function(event) {285
canvas.undo(state);286
update_undo_and_redo();287
});288
node.redo_tool.addEventListener('click', function(event) {289
canvas.redo(state);290
update_undo_and_redo();291
});292
node.edit_mode.addEventListener('click', function(event) {293
state.option.view_mode = false;294
update_view_mode_option();295
});296
for(let item of node.caption_list) {297
item.addEventListener('click', function(event) {298
state.caption = item.dataset.code;299
update_caption_list();300
});301
}303
window.addEventListener('beforeunload', function(event) {304
// ...305
});306
}308
function update_language_list() {309
let lang = locale.get_current();310
for(let item of node.language_list) {311
let code = item.dataset.code;312
item.classList[code === lang ? 'add' : 'remove']('active');313
}314
}316
function update_device_list() {317
if(!state.option.view_mode && !state.option.crop_mode) {318
canvas.get_canvas().style.cursor = state.device === 'mouse' ? 'none' : '';319
}320
for(let item of node.device_list) {321
let code = item.dataset.code;322
item.classList[code === state.device ? 'add' : 'remove']('active');323
item.classList[state.device_list[code] ? 'remove' : 'add']('inactive');324
}325
}327
function update_pencil_list() {328
for(let item of node.pencil_list) {329
let code = item.dataset.code;330
item.classList[code === state.pencil ? 'add' : 'remove']('active');331
if(code === state.pencil) {332
node.pencil_tool.querySelector('i.ico').innerHTML = item.querySelector('i.ico').innerHTML;333
}334
}335
}337
function update_hotbar() {338
for(let item of node.hotbar_list) {339
item.classList[node.hotbar_list[state.index] === item ? 'add' : 'remove']('active');340
}341
}343
function update_focus(bool) {344
for(let item of node.hotbar_list) {345
item.style.pointerEvents = bool ? '' : 'none';346
}347
for(let item of node.tool_list) {348
item.style.pointerEvents = bool ? '' : 'none';349
}350
for(let item of node.action_list) {351
item.style.pointerEvents = bool ? '' : 'none';352
}353
}355
function update_modal_list() {356
for(let item of node.modal_list) {357
item.classList.add('hidden');358
}359
for(let item of node.modal_event_list) {360
item.classList.remove('active');361
}362
state.tab = null;363
update_tab_list();364
state.caption = 'controls';365
update_caption_list();366
if(state.modal) {367
canvas.on_release(event, state, true);368
let modal = document.querySelector(`.modal.${state.modal}-modal`);369
let button = document.querySelector(`[data-event*=".modal"][data-code="${state.modal}"]`);370
if(modal) {371
modal.classList.remove('hidden');372
modal.animate([373
{ opacity: 0, marginTop: '-6px' },374
{ opacity: 1, marginTop: '0' }375
], {376
duration: 100,377
fill: 'both'378
});379
}380
if(button) {381
button.classList.add('active');382
}383
}384
}386
function update_tab_list() {387
for(let item of node.modal_list) {388
let main = item.querySelector('.main');389
if(main) {390
main.classList.remove('hidden');391
main.style.marginLeft = '0';392
}393
}394
for(let item of node.tab_list) {395
item.classList.add('hidden');396
}397
if(state.modal && state.tab) {398
let modal = document.querySelector(`.modal.${state.modal}-modal`);399
let tab = document.querySelector(`.${state.tab}-tab`);400
let main = modal.querySelector('.main');401
if(modal && tab && main) {402
tab.classList.remove('hidden');403
main.style.marginLeft = '-100%';404
main.classList.add('hidden');405
}406
}407
}409
function update_fullscreen_option() {410
let fullscreen = document.fullscreenElement;411
node.option_event_fullscreen.classList[fullscreen ? 'add' : 'remove']('active');412
node.option_event_fullscreen.querySelector('.ico').textContent = fullscreen ? 'toggle_on' : 'toggle_off';413
}415
function update_view_mode_option() {416
let view_mode = state.option.view_mode;417
show_notification(`notification.view-mode.${view_mode ? 'on' : 'off'}`);418
node.option_event_view_mode.classList[view_mode ? 'add' : 'remove']('active');419
node.option_event_view_mode.querySelector('.ico').textContent = view_mode ? 'toggle_on' : 'toggle_off';420
state.modal = null;421
state.tab = null;422
update_modal_list();423
if(view_mode) {424
canvas.on_release(null, state);425
canvas.clear_cursor();426
canvas.get_canvas().style.cursor = 'default';427
node.hotbar.classList.add('inactive');428
node.header.classList.add('inactive');429
node.hotbar.animate([430
{ opacity: 1 },431
{ opacity: 0, bottom: '-10px', pointerEvents: 'none' }432
], {433
easing: 'ease-out',434
duration: 250,435
fill: 'both'436
});437
node.mainbar.animate([438
{ opacity: 1, marginTop: 0 },439
{ opacity: 0, marginTop: '-60px' }440
], {441
easing: 'ease-out',442
duration: 250,443
fill: 'both'444
});445
node.sidebar.classList.remove('hidden');446
node.sidebar.animate([447
{ opacity: 0 },448
{ opacity: 1 }449
], {450
delay: 250,451
easing: 'ease-out',452
duration: 250,453
fill: 'both'454
});455
}456
else {457
state.option.crop_mode = false;458
canvas.draw_crop(state);459
canvas.on_release(null, state, true);460
canvas.get_canvas().style.cursor = 'none';461
node.hotbar.classList.remove('inactive');462
node.header.classList.remove('inactive');463
node.hotbar.animate([464
{ opacity: 0, bottom: '-10px' },465
{ opacity: 1, bottom: '30px' }466
], {467
easing: 'ease-out',468
duration: 250,469
fill: 'both'470
});471
node.mainbar.animate([472
{ opacity: 0, marginTop: '-60px' },473
{ opacity: 1, marginTop: 0 }474
], {475
easing: 'ease-out',476
duration: 250,477
fill: 'both'478
});479
node.sidebar.classList.add('hidden');480
}481
}483
function update_undo_and_redo() {484
node.undo_tool.classList[state.history.length > 0 ? 'remove' : 'add']('inactive');485
node.redo_tool.classList[state.redo_history.length > 0 ? 'remove' : 'add']('inactive');486
}488
function update_caption_list() {489
for(let item of node.caption_list) {490
let code = item.dataset.code;491
item.classList[code === state.caption ? 'add' : 'remove']('active');492
}493
for(let item of node.content_list) {494
let code = item.dataset.code;495
item.classList[code === state.caption ? 'remove' : 'add']('hidden');496
}497
}499
function on_pointerdown(event) {500
// detect touch501
if(!state.device_list.touch && event.pointerType === 'touch') {502
state.device_list.touch = true;503
if(state.device) {504
show_notification('notification.input-device.touch.add');505
}506
else {507
state.device = 'touch';508
state.device_fallback = 'touch';509
}510
update_device_list();511
}512
// detect pen513
if(!state.device_list.pen && event.pointerType === 'pen') {514
state.device_list.pen = true;515
show_notification('notification.input-device.pencil.add');516
if(!state.device) {517
state.device = 'pen';518
state.device_fallback = 'pen';519
}520
update_device_list();521
}522
// drawing523
if(!state.option.view_mode && state.device === event.pointerType) {524
if(event.target === canvas.get_canvas()) {525
if(!PRIORITY_MODAL.includes(state.modal)) {526
update_focus(false);527
canvas.on_press(event, state);528
update_undo_and_redo();529
state.point = {530
x: event.layerX,531
y: event.layerY532
};533
canvas.draw_cursor(state);534
}535
}536
}537
if(state.option.view_mode && state.option.crop_mode) {538
if(event.target === canvas.get_canvas()) {539
state.crop = true;540
state.region = [];541
state.region[0] = { x: event.layerX, y: event.layerY };542
canvas.draw_crop(state);543
}544
}545
}547
function on_pointermove(event) {548
// detect mouse549
if(!state.device_list.mouse && event.pointerType === 'mouse') {550
state.device_list.mouse = true;551
if(state.device) {552
show_notification('notification.input-device.mouse.add');553
}554
else {555
state.device = 'mouse';556
state.device_fallback = 'mouse';557
}558
update_device_list();559
}560
// drawing561
if(!state.option.view_mode && state.device === event.pointerType) {562
if(event.target === canvas.get_canvas()) {563
if(!PRIORITY_MODAL.includes(state.modal)) {564
canvas.on_move(event, state);565
}566
state.point = {567
x: event.layerX,568
y: event.layerY569
};570
}571
canvas.draw_cursor(state);572
}573
if(state.option.view_mode && state.option.crop_mode) {574
if(state.crop && event.target === canvas.get_canvas()) {575
state.region[1] = { x: event.layerX, y: event.layerY };576
canvas.draw_crop(state);577
}578
}579
}581
function on_pointerup(event) {582
if(!state.option.view_mode && state.device === event.pointerType) {583
canvas.on_release(event, state);584
update_undo_and_redo();585
update_focus(true);586
}587
if(state.option.view_mode && state.option.crop_mode) {588
if(state.crop && event.target === canvas.get_canvas()) {589
state.crop = false;590
state.region[1] = { x: event.layerX, y: event.layerY };591
canvas.draw_crop(state);592
let [ begin, end ] = state.region;593
let delta = {594
x: end.x - begin.x,595
y: end.y - begin.y596
};597
let x = begin.x < end.x ? begin.x : end.x;598
let y = begin.y < end.y ? begin.y : end.y;599
if(delta.x < 0) {600
delta.x *= -1;601
}602
if(delta.y < 0) {603
delta.y *= -1;604
}605
if(delta.x != 0 && delta.y != 0) {606
let data = canvas.create_capture(x, y, delta.x, delta.y);607
if(data) {608
node.download.download = `outline-${uuid()}.png`;609
node.download.href = data;610
node.download.click();611
state.option.crop_mode = false;612
canvas.draw_crop(state);613
canvas.get_canvas().style.cursor = 'default';614
}615
else {616
console.warn('Unable to create capture from current state!');617
}618
}619
}620
}621
}623
function on_pointerout(event) {624
if(!state.option.view_mode && state.device === event.pointerType) {625
canvas.on_release(event, state);626
update_undo_and_redo();627
state.point = null;628
canvas.draw_cursor(state);629
}630
if(state.option.view_mode && state.option.crop_mode) {631
if(state.crop && event.target === canvas.get_canvas()) {632
state.crop = false;633
state.region = [];634
canvas.draw_crop(state);635
}636
}637
}639
function on_keydown(event) {640
if(event.code === 'Escape') {641
if(state.tab) {642
state.tab = null;643
update_tab_list();644
return;645
}646
if(state.modal) {647
state.modal = null;648
update_modal_list();649
return;650
}651
if(state.option.view_mode && state.option.crop_mode) {652
state.option.crop_mode = false;653
canvas.draw_crop(state);654
canvas.get_canvas().style.cursor = 'default';655
}656
}657
if(!event.ctrlKey && !event.shiftKey && !event.altKey) {658
if(!state.option.view_mode) {659
if(event.code.startsWith('Digit')) {660
let i = parseInt(event.code.substr(5)) - 1;661
if(state.index !== i) {662
if(i >= 0 && i < state.color_list.length) {663
canvas.on_release(event, state);664
state.color = state.color_list[i];665
state.index = i;666
canvas.draw_cursor(state);667
update_hotbar();668
}669
}670
}671
if(event.code === 'KeyZ') {672
state.modal = state.modal !== 'colors' ? 'colors' : null;673
update_modal_list();674
}675
if(event.code === 'KeyX') {676
state.modal = state.modal !== 'scale' ? 'scale' : null;677
update_modal_list();678
}679
if(event.code === 'KeyC') {680
state.modal = state.modal !== 'pencil' ? 'pencil' : null;681
update_modal_list();682
}683
if(event.code === 'KeyV') {684
state.modal = state.modal !== 'clear' ? 'clear' : null;685
update_modal_list();686
}687
/*688
if(event.code === 'KeyB') {689
state.modal = state.modal !== 'multiplayer' ? 'multiplayer' : null;690
update_modal_list();691
}692
if(event.code === 'KeyN') {693
state.modal = state.modal !== 'account' ? 'account' : null;694
update_modal_list();695
}696
*/697
if(event.code === 'KeyM') {698
state.modal = state.modal !== 'more' ? 'more' : null;699
update_modal_list();700
}701
if(event.code === 'KeyQ') {702
canvas.on_release(event, state);703
state.pencil = state.pencil_list[0];704
canvas.draw_cursor(state);705
update_pencil_list();706
}707
if(event.code === 'KeyW') {708
canvas.on_release(event, state);709
state.pencil = state.pencil_list[1];710
canvas.draw_cursor(state);711
update_pencil_list();712
}713
if(event.code === 'KeyE') {714
canvas.on_release(event, state);715
state.pencil = state.pencil_list[2];716
canvas.draw_cursor(state);717
update_pencil_list();718
}719
if(event.code === 'KeyF') {720
if(!document.fullscreenElement) {721
document.documentElement.requestFullscreen();722
}723
else if(document.exitFullscreen) {724
document.exitFullscreen();725
}726
}727
}728
if(event.code === 'Space') {729
state.option.view_mode = !state.option.view_mode;730
update_view_mode_option();731
}732
}733
if(event.ctrlKey && !event.shiftKey && !event.altKey) {734
if(event.code === 'KeyY') {735
canvas.undo(state);736
update_undo_and_redo();737
}738
if(event.code === 'KeyZ') {739
canvas.redo(state);740
update_undo_and_redo();741
}742
}743
if(event.code === 'Enter') {744
if(state.modal === 'clear') {745
node.clear.click();746
}747
}748
}750
function on_wheel(event) {751
if(!state.option.view_mode && state.device === 'mouse') {752
if(event.target === canvas.get_canvas()) {753
let y = event.deltaY < 0 ? 1 : -1;754
if(state.radius + y > 2 - 1 && state.radius + y < 50 + 1) {755
canvas.on_release(event, state);756
state.radius += y;757
node.scale_input.value = state.radius;758
canvas.draw_cursor(state);759
}760
}761
}762
}764
function on_fullscreenchange(event) {765
update_fullscreen_option();766
}768
function fill_hotbar_list() {769
for(let [ i, item ] of Object.entries(node.hotbar_list)) {770
if(state.color_list.length > i) {771
item.style.background = COLORS[state.color_list[i]];772
item.addEventListener('click', function(event) {773
canvas.on_release(event, state);774
state.color = state.color_list[i];775
state.index = i;776
canvas.draw_cursor(state);777
update_hotbar();778
});779
if(state.index == i) {780
item.classList.add('active');781
}782
}783
}784
}786
function fill_modal_color_list() {787
for(let [ i, item ] of Object.entries(node.modal_color_list)) {788
if(COLORS.length > i) {789
item.style.background = COLORS[i];790
item.addEventListener('click', function(event) {791
canvas.on_release(event, state);792
state.color = i;793
state.modal = null;794
update_modal_list();795
canvas.draw_cursor(state);796
state.index = null;797
update_hotbar();798
});799
}800
}801
}803
function show_notification(message) {804
let code = uuid();805
state.timestamp = code;806
let content = node.notification.querySelector('.content');807
content.textContent = locale.get()[message];808
node.notification.animate([809
{ opacity: 0 },810
{ opacity: 1 }811
], {812
duration: 200,813
fill: 'both'814
});815
setTimeout(function() {816
if(state.timestamp === code) {817
node.notification.animate([818
{ opacity: 1 },819
{ opacity: 0 }820
], {821
duration: 200,822
fill: 'both'823
});824
}825
}, 1000);826
}