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