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