A web-based drawing app
git clone git://soophie.de/outline
blob: 4722987ea80ac926f1265c0a6653fa805251d675 (21763 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('en');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 = create_capture(0, 0, window.innerWidth, window.innerHeight);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
canvas.get_canvas().style.cursor = state.device === 'mouse' ? 'none' : '';298
for(let item of node.device_list) {299
let code = item.dataset.code;300
item.classList[code === state.device ? 'add' : 'remove']('active');301
item.classList[state.device_list[code] ? 'remove' : 'add']('inactive');302
}303
}305
function update_pencil_list() {306
for(let item of node.pencil_list) {307
let code = item.dataset.code;308
item.classList[code === state.pencil ? 'add' : 'remove']('active');309
if(code === state.pencil) {310
node.pencil_tool.querySelector('i.ico').innerHTML = item.querySelector('i.ico').innerHTML;311
}312
}313
}315
function update_hotbar() {316
for(let item of node.hotbar_list) {317
item.classList[node.hotbar_list[state.index] === item ? 'add' : 'remove']('active');318
}319
}321
function update_focus(bool) {322
for(let item of node.hotbar_list) {323
item.style.pointerEvents = bool ? '' : 'none';324
}325
for(let item of node.tool_list) {326
item.style.pointerEvents = bool ? '' : 'none';327
}328
for(let item of node.action_list) {329
item.style.pointerEvents = bool ? '' : 'none';330
}331
}333
function update_modal_list() {334
for(let item of node.modal_list) {335
item.classList.add('hidden');336
}337
for(let item of node.modal_event_list) {338
item.classList.remove('active');339
}340
state.tab = null;341
update_tab_list();342
state.caption = 'controls';343
update_caption_list();344
if(state.modal) {345
canvas.on_release(event, state, true);346
let modal = document.querySelector(`.modal.${state.modal}-modal`);347
let button = document.querySelector(`[data-event*=".modal"][data-code="${state.modal}"]`);348
if(modal) {349
modal.classList.remove('hidden');350
modal.animate([351
{ opacity: 0, marginTop: '-6px' },352
{ opacity: 1, marginTop: '0' }353
], {354
duration: 100,355
fill: 'both'356
});357
}358
if(button) {359
button.classList.add('active');360
}361
}362
}364
function update_tab_list() {365
for(let item of node.modal_list) {366
let main = item.querySelector('.main');367
if(main) {368
main.classList.remove('hidden');369
main.style.marginLeft = '0';370
}371
}372
for(let item of node.tab_list) {373
item.classList.add('hidden');374
}375
if(state.modal && state.tab) {376
let modal = document.querySelector(`.modal.${state.modal}-modal`);377
let tab = document.querySelector(`.${state.tab}-tab`);378
let main = modal.querySelector('.main');379
if(modal && tab && main) {380
tab.classList.remove('hidden');381
main.style.marginLeft = '-100%';382
main.classList.add('hidden');383
}384
}385
}387
function update_fullscreen_option() {388
let fullscreen = document.fullscreenElement;389
node.option_event_fullscreen.classList[fullscreen ? 'add' : 'remove']('active');390
node.option_event_fullscreen.querySelector('.ico').textContent = fullscreen ? 'toggle_on' : 'toggle_off';391
}393
function update_view_mode_option() {394
let view_mode = state.option.view_mode;395
show_notification(`notification.view-mode.${view_mode ? 'on' : 'off'}`);396
node.option_event_view_mode.classList[view_mode ? 'add' : 'remove']('active');397
node.option_event_view_mode.querySelector('.ico').textContent = view_mode ? 'toggle_on' : 'toggle_off';398
state.modal = null;399
state.tab = null;400
update_modal_list();401
if(view_mode) {402
canvas.on_release(null, state);403
canvas.clear_cursor();404
canvas.get_canvas().style.cursor = 'default';405
node.hotbar.classList.add('inactive');406
node.header.classList.add('inactive');407
node.hotbar.animate([408
{ opacity: 1 },409
{ opacity: 0, bottom: '-10px', pointerEvents: 'none' }410
], {411
easing: 'ease-out',412
duration: 250,413
fill: 'both'414
});415
node.mainbar.animate([416
{ opacity: 1, marginTop: 0 },417
{ opacity: 0, marginTop: '-60px' }418
], {419
easing: 'ease-out',420
duration: 250,421
fill: 'both'422
});423
node.sidebar.classList.remove('hidden');424
node.sidebar.animate([425
{ opacity: 0 },426
{ opacity: 1 }427
], {428
delay: 250,429
easing: 'ease-out',430
duration: 250,431
fill: 'both'432
});433
}434
else {435
state.option.crop_mode = false;436
canvas.draw_crop(state);437
canvas.on_release(null, state, true);438
canvas.get_canvas().style.cursor = 'none';439
node.hotbar.classList.remove('inactive');440
node.header.classList.remove('inactive');441
node.hotbar.animate([442
{ opacity: 0, bottom: '-10px' },443
{ opacity: 1, bottom: '30px' }444
], {445
easing: 'ease-out',446
duration: 250,447
fill: 'both'448
});449
node.mainbar.animate([450
{ opacity: 0, marginTop: '-60px' },451
{ opacity: 1, marginTop: 0 }452
], {453
easing: 'ease-out',454
duration: 250,455
fill: 'both'456
});457
node.sidebar.classList.add('hidden');458
}459
}461
function update_undo_and_redo() {462
node.undo_tool.classList[state.history.length > 0 ? 'remove' : 'add']('inactive');463
node.redo_tool.classList[state.redo_history.length > 0 ? 'remove' : 'add']('inactive');464
}466
function update_caption_list() {467
for(let item of node.caption_list) {468
let code = item.dataset.code;469
item.classList[code === state.caption ? 'add' : 'remove']('active');470
}471
for(let item of node.content_list) {472
let code = item.dataset.code;473
item.classList[code === state.caption ? 'remove' : 'add']('hidden');474
}475
}477
function on_pointerdown(event) {478
// detect touch479
if(!state.device_list.touch && event.pointerType === 'touch') {480
state.device_list.touch = true;481
if(state.device) {482
show_notification('notification.input-device.touch.add');483
}484
else {485
state.device = 'touch';486
state.device_fallback = 'touch';487
}488
update_device_list();489
}490
// detect pen491
if(!state.device_list.pen && event.pointerType === 'pen') {492
state.device_list.pen = true;493
show_notification('notification.input-device.pencil.add');494
if(!state.device) {495
state.device = 'pen';496
state.device_fallback = 'pen';497
}498
update_device_list();499
}500
// drawing501
if(!state.option.view_mode && state.device === event.pointerType) {502
if(event.target === canvas.get_canvas()) {503
if(!PRIORITY_MODAL.includes(state.modal)) {504
update_focus(false);505
canvas.on_press(event, state);506
update_undo_and_redo();507
state.point = {508
x: event.layerX,509
y: event.layerY510
};511
canvas.draw_cursor(state);512
}513
}514
}515
if(state.option.view_mode && state.option.crop_mode) {516
if(event.target === canvas.get_canvas()) {517
state.crop = true;518
state.region = [];519
state.region[0] = { x: event.layerX, y: event.layerY };520
canvas.draw_crop(state);521
}522
}523
}525
function on_pointermove(event) {526
// detect mouse527
if(!state.device_list.mouse && event.pointerType === 'mouse') {528
state.device_list.mouse = true;529
if(state.device) {530
show_notification('notification.input-device.mouse.add');531
}532
else {533
state.device = 'mouse';534
state.device_fallback = 'mouse';535
}536
update_device_list();537
}538
// drawing539
if(!state.option.view_mode && state.device === event.pointerType) {540
if(event.target === canvas.get_canvas()) {541
if(!PRIORITY_MODAL.includes(state.modal)) {542
canvas.on_move(event, state);543
}544
state.point = {545
x: event.layerX,546
y: event.layerY547
};548
}549
canvas.draw_cursor(state);550
}551
if(state.option.view_mode && state.option.crop_mode) {552
if(state.crop && event.target === canvas.get_canvas()) {553
state.region[1] = { x: event.layerX, y: event.layerY };554
canvas.draw_crop(state);555
}556
}557
}559
function on_pointerup(event) {560
if(!state.option.view_mode && state.device === event.pointerType) {561
canvas.on_release(event, state);562
update_undo_and_redo();563
update_focus(true);564
}565
if(state.option.view_mode && state.option.crop_mode) {566
if(state.crop && event.target === canvas.get_canvas()) {567
state.crop = false;568
state.region[1] = { x: event.layerX, y: event.layerY };569
canvas.draw_crop(state);570
let [ begin, end ] = state.region;571
let delta = {572
x: end.x - begin.x,573
y: end.y - begin.y574
};575
let x = begin.x < end.x ? begin.x : end.x;576
let y = begin.y < end.y ? begin.y : end.y;577
if(delta.x < 0) {578
delta.x *= -1;579
}580
if(delta.y < 0) {581
delta.y *= -1;582
}583
if(delta.x != 0 && delta.y != 0) {584
let data = create_capture(x, y, delta.x, delta.y);585
if(data) {586
node.download.download = `outline-${uuid()}.png`;587
node.download.href = data;588
node.download.click();589
state.option.crop_mode = false;590
canvas.draw_crop(state);591
canvas.get_canvas().style.cursor = 'default';592
}593
else {594
console.warn('Unable to create capture from current state!');595
}596
}597
}598
}599
}601
function on_pointerout(event) {602
if(!state.option.view_mode && state.device === event.pointerType) {603
canvas.on_release(event, state);604
update_undo_and_redo();605
state.point = null;606
canvas.draw_cursor(state);607
}608
if(state.option.view_mode && state.option.crop_mode) {609
if(state.crop && event.target === canvas.get_canvas()) {610
state.crop = false;611
state.region = [];612
canvas.draw_crop(state);613
}614
}615
}617
function on_keydown(event) {618
if(event.code === 'Escape') {619
if(state.tab) {620
state.tab = null;621
update_tab_list();622
return;623
}624
if(state.modal) {625
state.modal = null;626
update_modal_list();627
return;628
}629
if(state.option.view_mode && state.option.crop_mode) {630
state.option.crop_mode = false;631
canvas.draw_crop(state);632
canvas.get_canvas().style.cursor = 'default';633
}634
}635
if(!event.ctrlKey && !event.shiftKey && !event.altKey) {636
if(!state.option.view_mode) {637
if(event.code.startsWith('Digit')) {638
let i = parseInt(event.code.substr(5)) - 1;639
if(state.index !== i) {640
if(i >= 0 && i < state.color_list.length) {641
canvas.on_release(event, state);642
state.color = state.color_list[i];643
state.index = i;644
canvas.draw_cursor(state);645
update_hotbar();646
}647
}648
}649
if(event.code === 'KeyZ') {650
state.modal = state.modal !== 'colors' ? 'colors' : null;651
update_modal_list();652
}653
if(event.code === 'KeyX') {654
state.modal = state.modal !== 'scale' ? 'scale' : null;655
update_modal_list();656
}657
if(event.code === 'KeyC') {658
state.modal = state.modal !== 'pencil' ? 'pencil' : null;659
update_modal_list();660
}661
if(event.code === 'KeyV') {662
state.modal = state.modal !== 'clear' ? 'clear' : null;663
update_modal_list();664
}665
/*666
if(event.code === 'KeyB') {667
state.modal = state.modal !== 'multiplayer' ? 'multiplayer' : null;668
update_modal_list();669
}670
if(event.code === 'KeyN') {671
state.modal = state.modal !== 'account' ? 'account' : null;672
update_modal_list();673
}674
*/675
if(event.code === 'KeyM') {676
state.modal = state.modal !== 'more' ? 'more' : null;677
update_modal_list();678
}679
if(event.code === 'KeyQ') {680
canvas.on_release(event, state);681
state.pencil = state.pencil_list[0];682
canvas.draw_cursor(state);683
update_pencil_list();684
}685
if(event.code === 'KeyW') {686
canvas.on_release(event, state);687
state.pencil = state.pencil_list[1];688
canvas.draw_cursor(state);689
update_pencil_list();690
}691
if(event.code === 'KeyE') {692
canvas.on_release(event, state);693
state.pencil = state.pencil_list[2];694
canvas.draw_cursor(state);695
update_pencil_list();696
}697
if(event.code === 'KeyF') {698
if(!document.fullscreenElement) {699
document.documentElement.requestFullscreen();700
}701
else if(document.exitFullscreen) {702
document.exitFullscreen();703
}704
}705
}706
if(event.code === 'Space') {707
state.option.view_mode = !state.option.view_mode;708
update_view_mode_option();709
}710
}711
if(event.ctrlKey && !event.shiftKey && !event.altKey) {712
if(event.code === 'KeyY') {713
canvas.undo(state);714
update_undo_and_redo();715
}716
if(event.code === 'KeyZ') {717
canvas.redo(state);718
update_undo_and_redo();719
}720
}721
}723
function on_wheel(event) {724
if(!state.option.view_mode && state.device === 'mouse') {725
if(event.target === canvas.get_canvas()) {726
let y = event.deltaY < 0 ? 1 : -1;727
if(state.radius + y > 2 - 1 && state.radius + y < 50 + 1) {728
canvas.on_release(event, state);729
state.radius += y;730
node.scale_input.value = state.radius;731
canvas.draw_cursor(state);732
}733
}734
}735
}737
function on_fullscreenchange(event) {738
update_fullscreen_option();739
}741
function fill_hotbar_list() {742
for(let [ i, item ] of Object.entries(node.hotbar_list)) {743
if(state.color_list.length > i) {744
item.style.background = COLORS[state.color_list[i]];745
item.addEventListener('click', function(event) {746
canvas.on_release(event, state);747
state.color = state.color_list[i];748
state.index = i;749
canvas.draw_cursor(state);750
update_hotbar();751
});752
if(state.index == i) {753
item.classList.add('active');754
}755
}756
}757
}759
function fill_modal_color_list() {760
for(let [ i, item ] of Object.entries(node.modal_color_list)) {761
if(COLORS.length > i) {762
item.style.background = COLORS[i];763
item.addEventListener('click', function(event) {764
canvas.on_release(event, state);765
state.color = i;766
state.modal = null;767
update_modal_list();768
canvas.draw_cursor(state);769
state.index = null;770
update_hotbar();771
});772
}773
}774
}776
function show_notification(message) {777
let code = uuid();778
state.timestamp = code;779
let content = node.notification.querySelector('.content');780
content.textContent = locale.get()[message];781
node.notification.animate([782
{ opacity: 0 },783
{ opacity: 1 }784
], {785
duration: 200,786
fill: 'both'787
});788
setTimeout(function() {789
if(state.timestamp === code) {790
node.notification.animate([791
{ opacity: 1 },792
{ opacity: 0 }793
], {794
duration: 200,795
fill: 'both'796
});797
}798
}, 1000);799
}801
function create_capture(x, y, width, height) {802
let cache = document.createElement('canvas');803
let c = cache.getContext('2d');804
canvas.scale_canvas(cache, c, width, height);805
c.translate(-x, -y);806
c.fillStyle = 'white';807
c.fillRect(0, 0, window.innerWidth, window.innerHeight);808
c.drawImage(canvas.get_canvas(), 0, 0);809
node.download.download = 'test.png';810
let data = cache.toDataURL() || null;811
cache.remove();812
return data;813
}