A web-based drawing app
git clone git://soophie.de/outline
blob: fbfe1e78097e2ee49f5fb558c0c408f4362b080b (21893 bytes)[doc][blame][raw]
1
import { uuid } from './util.js';2
import { load } from './locale.js';3
import { read, key_pressed } from './gamepad.js';5
import Ripple from './addon/ripple.js';7
window.toggle_modal = toggle_modal;8
window.open_tab = open_tab;9
window.close_tab = close_tab;10
window.change_device = change_device;11
window.change_language = change_language;12
window.clear_screen = clear_screen;14
const COLORS = {15
0: '#FFCDD2',16
1: '#E57373',17
2: '#F44336',18
3: '#D32F2F',19
4: '#B71C1C',20
5: '#F8BBD0',21
6: '#F06292',22
7: '#E91E63',23
8: '#C2185B',24
9: '#880E4F',25
10: '#E1BEE7',26
11: '#BA68C8',27
12: '#9C27B0',28
13: '#7B1FA2',29
14: '#4A148C',30
15: '#BBDEFB',31
16: '#64B5F6',32
17: '#2196F3',33
18: '#1976D2',34
19: '#0D47A1',35
20: '#B2EBF2',36
21: '#4DD0E1',37
22: '#00BCD4',38
23: '#0097A7',39
24: '#006064',40
25: '#B2DFDB',41
26: '#4DB6AC',42
27: '#009688',43
28: '#00796B',44
29: '#004D40',45
30: '#C8E6C9',46
31: '#81C784',47
32: '#4CAF50',48
33: '#388E3C',49
34: '#1B5E20',50
35: '#FFF9C4',51
36: '#FFF176',52
37: '#FFEB3B',53
38: '#FBC02D',54
39: '#F57F17',55
40: '#FFE0B2',56
41: '#FFB74D',57
42: '#FF9800',58
43: '#F57C00',59
44: '#E65100',60
45: '#D7CCC8',61
46: '#A1887F',62
47: '#795548',63
48: '#5D4037',64
49: '#3E2723',65
50: '#F5F5F5',66
51: '#E0E0E0',67
52: '#9E9E9E',68
53: '#616161',69
54: '#141414'70
};72
const COLOR = {73
0: '#141414',74
1: '#F44336',75
2: '#2196F3',76
3: '#009688',77
4: '#FFEB3B',78
5: '#9C27B0'79
};81
const COLORS_LENGTH = Object.values(COLORS).length;82
const COLOR_LENGTH = Object.values(COLOR).length;84
let LANG;86
let node;87
let canvas;88
let overlay;89
let g;90
let o;91
let ad;92
let hotbar;93
let toolbar;94
let actionbar;95
let colors;96
let actions;97
let tools;98
let modals;99
let device_options;100
let device_empty;101
let current_modal;102
let current_tab;103
let modal_colors;104
let colors_modal;105
let capture_modal;106
let clear_modal;107
let scale_range;109
let device;110
let devices = {111
mouse: false,112
touch: false,113
pen: false,114
gamepad: false115
};116
let fallback_device;118
let lang = 'en';119
let notification_code;121
let cache;122
let point;123
let last;124
let start;125
let index = 0;126
let radius = 14;127
let lock_x = false;128
let lock_y = false;129
let draw;130
let erase;131
let arrow_y = 0;132
let arrow_x = 0;133
let left_modal = false;135
window.addEventListener('load', init);137
async function init() {138
Ripple.init();139
LANG = await load('../asset/lang', ['en', 'de']);140
canvas = document.querySelector('#canvas');141
canvas.width = window.innerWidth;142
canvas.height = window.innerHeight;143
g = canvas.getContext('2d');144
overlay = document.querySelector('#overlay');145
overlay.width = window.innerWidth;146
overlay.height = window.innerHeight;147
o = overlay.getContext('2d');148
window.onresize = function() {149
cache = g.getImageData(0, 0, canvas.width, canvas.height);150
canvas.width = window.innerWidth;151
canvas.height = window.innerHeight;152
g.putImageData(cache, 0, 0);153
cache = o.getImageData(0, 0, overlay.width, overlay.height);154
overlay.width = window.innerWidth;155
overlay.height = window.innerHeight;156
o.putImageData(cache, 0, 0);157
}158
document.addEventListener('touchmove', function(event) {159
let { target } = event;160
if(target !== scale_range) {161
event.preventDefault();162
}163
}, { passive: false });164
device_options = document.querySelectorAll('.settings-modal .device-tab .item[data-device]');165
device_empty = document.querySelector('.settings-modal .device-tab .item.empty');167
ad = document.querySelector('.controls .ad');168
hotbar = document.querySelector('.controls .hotbar');169
toolbar = document.querySelector('.controls .toolbar');170
actionbar = document.querySelector('.controls .actionbar');171
colors = Array.from(hotbar.querySelectorAll('.color'));172
actions = document.querySelectorAll('.actionbar .action');173
tools = document.querySelectorAll('.toolbar .tool');174
modals = document.querySelectorAll('.modal');175
modal_colors = document.querySelectorAll('.colors-modal .color');176
colors_modal = document.querySelector('.toolbar .tool[data-modal="colors"]');177
capture_modal = document.querySelector('.toolbar .tool[data-modal="capture"]');178
clear_modal = document.querySelector('.toolbar .tool[data-modal="clear"]');179
scale_range = document.querySelector('.scale-modal input[type="range"]');180
for(let i in colors) {181
if(COLOR_LENGTH > i) {182
if(i == index) {183
colors[i].classList.add('active');184
}185
colors[i].style.background = COLOR[i];186
colors[i].addEventListener('pointerdown', function(event) {187
for(let j in colors) {188
if(COLOR_LENGTH > j) {189
if(j == i) {190
index = j;191
}192
colors[j].classList[j == i ? 'add' : 'remove']('active');193
}194
}195
});196
}197
}198
for(let i in modal_colors) {199
if(COLORS_LENGTH > i) {200
modal_colors[i].style.background = COLORS[i];201
}202
}203
document.addEventListener('dragstart', function(event) {204
event.preventDefault();205
});206
document.addEventListener('contextmenu', function(event) {207
event.preventDefault();208
});209
scale_range.addEventListener('input', function(event) {210
radius = parseInt(scale_range.value);211
left_modal = false;212
point.x = parseInt(window.innerWidth / 2);213
point.y = parseInt(window.innerHeight / 2);214
});215
document.addEventListener('wheel', function(event) {216
if(device !== 'gamepad') {217
let { deltaY } = event;218
let y = deltaY < 0 ? 1 : -1;219
if(radius + y > 2 - 1 && radius + y < 50 + 1) {220
radius += y;221
scale_range.value = radius;222
}223
}224
});225
document.addEventListener('wheel', function(event) {226
if(device !== 'gamepad') {227
let { ctrlKey } = event;228
if(ctrlKey) {229
event.preventDefault();230
return;231
}232
}233
}, { passive: false });234
canvas.addEventListener('pointerdown', function(event) {235
let { layerX, layerY, buttons, pointerType, target } = event;236
if(!devices.touch && pointerType === 'touch') {237
devices.touch = true;238
if(device) {239
show_notification('notification.input-device.touch.add');240
}241
}242
if(!devices.pen && pointerType === 'pen') {243
devices.pen = true;244
show_notification('notification.input-device.pencil.add');245
}246
if(device !== 'gamepad') {247
if(!lock_x) {248
start.x = layerX;249
}250
if(!lock_y) {251
start.y = layerY;252
}253
if(current_modal && target === canvas) {254
toggle_modal(current_modal);255
left_modal = true;256
}257
else {258
left_modal = false;259
}260
if(device === pointerType) {261
if(!current_modal && !left_modal) {262
if(start.x !== null && start.y !== null) {263
if(buttons == 1) {264
draw_line(start, null, radius, COLOR[index]);265
}266
if(buttons == 2) {267
draw_line(start, null, radius, 'white');268
erase = true;269
last.x = null;270
last.y = null;271
}272
}273
}274
ad.style.pointerEvents = 'none';275
hotbar.style.pointerEvents = 'none';276
toolbar.style.pointerEvents = 'none';277
actionbar.style.pointerEvents = 'none';278
for(let modal of modals) {279
modal.style.pointerEvents = 'none';280
}281
}282
}283
});284
document.addEventListener('pointermove', function(event) {285
let { layerX, layerY, target, pointerType, buttons } = event;286
if(!devices.mouse && pointerType === 'mouse') {287
devices.mouse = true;288
}289
if(device !== 'gamepad') {290
draw = buttons == 1;291
erase = buttons == 2;292
if(buttons == 3) {293
draw = true;294
erase = true;295
}296
if(start.x !== null || start.y !== null) {297
last = start;298
start = {299
x: null,300
y: null301
};302
}303
if(device === pointerType) {304
left_modal = false;305
if(target == canvas) {306
if(!lock_x) {307
point.x = layerX;308
}309
if(!lock_y) {310
point.y = layerY;311
}312
}313
if(draw && erase) {314
last.x = null;315
last.y = null;316
}317
}318
if(!document.hasFocus()) {319
draw = false;320
erase = false;321
}322
}323
});324
document.addEventListener('pointerout', function(event) {325
if(device !== 'gamepad') {326
if(!lock_x && !lock_y) {327
point.x = null;328
point.y = null;329
}330
}331
});332
document.addEventListener('pointerup', function(event) {333
if(device !== 'gamepad') {334
let { buttons } = event;335
draw = buttons == 1;336
erase = buttons == 2;337
last.x = null;338
last.y = null;339
ad.style.pointerEvents = '';340
hotbar.style.pointerEvents = '';341
toolbar.style.pointerEvents = '';342
actionbar.style.pointerEvents = '';343
for(let modal of modals) {344
modal.style.pointerEvents = '';345
}346
}347
});348
document.addEventListener('keydown', function(event) {349
let { code, ctrlKey, shiftKey, altKey } = event;350
if(device !== 'gamepad') {351
let lock = event.getModifierState("CapsLock");352
if(!ctrlKey && !shiftKey && !altKey) {353
if(code === 'KeyX') {354
lock_y = !lock_y;355
lock_x = false;356
}357
if(code === 'KeyZ') {358
lock_x = !lock_x;359
lock_y = false;360
}361
if(!lock) {362
if(code.startsWith('Digit')) {363
let number = parseInt(code.substr(5)) - 1;364
if(number >= 0 && number < COLOR_LENGTH) {365
index = number;366
for(let i in colors) {367
if(COLOR_LENGTH > i) {368
colors[i].classList[i == index ? 'add' : 'remove']('active');369
}370
}371
}372
}373
}374
}375
}376
if(code === 'Escape') {377
if(current_tab) {378
close_tab(current_tab);379
}380
else if(current_modal) {381
toggle_modal(current_modal);382
}383
}384
});385
point = { x: null, y: null };386
last = { x: null, y: null };387
start = { x: null, y: null };388
update();389
}391
function update() {392
if(radius < 2) {393
radius = 2;394
}395
if(radius > 50) {396
radius = 50;397
}398
let gamepad = navigator.getGamepads()[0];399
if(gamepad) {400
if(!devices.gamepad) {401
devices.gamepad = true;402
show_notification('notification.input-device.gamepad.add');403
if(gamepad.vibrationActuator) {404
gamepad.vibrationActuator.playEffect("dual-rumble", {405
startDelay: 0,406
duration: 100,407
weakMagnitude: 1.0,408
strongMagnitude: 1.0409
});410
}411
}412
if(device === 'gamepad') {413
if(point.x == null && point.y == null) {414
point.x = parseInt(window.innerWidth / 2);415
point.y = parseInt(window.innerHeight / 2);416
}417
let input = read(gamepad);418
let speed = 4;419
draw = input.button['A'];420
erase = input.button['B'];421
if(pressed['L2'] && !pressed['R2']) {422
speed = 10;423
}424
if(pressed['R2'] && !pressed['L2']) {425
speed = 1;426
}427
// move428
if(!lock_x) {429
point.x += input.axes['Left'].x * speed;430
if(point.x < 0 ) {431
point.x = 0;432
}433
if(point.x > window.innerWidth) {434
point.x = window.innerWidth;435
}436
}437
if(!lock_y) {438
point.y += input.axes['Left'].y * speed;439
if(point.y < 0) {440
point.y = 0;441
}442
if(point.y > window.innerHeight) {443
point.y = window.innerHeight;444
}445
}446
// colors447
key_pressed('Option', input, function() {448
toggle_modal(colors_modal);449
});450
// capture451
key_pressed('Capture', input, function() {452
toggle_modal(capture_modal);453
});454
// draw455
key_pressed('A', input, function() {456
last.x = null;457
last.y = null;458
if(!current_modal) {459
left_modal = false;460
}461
});462
// erase463
key_pressed('B', input, function() {464
last.x = null;465
last.y = null;466
if(current_tab) {467
close_tab(current_tab);468
left_modal = true;469
}470
else if(current_modal) {471
toggle_modal(current_modal);472
left_modal = true;473
}474
else {475
left_modal = false;476
}477
});478
// sprint479
key_pressed('L2', input);480
// sneak481
key_pressed('R2', input);482
// clear483
key_pressed('Y', input, function() {484
toggle_modal(clear_modal);485
});486
// radius487
let y = -(input.axes['Right'].y / 2);488
if(y !== 0 && radius + y > 2 && radius + y < 50 + 1) {489
radius += y;490
scale_range.value = radius;491
}492
// lock y493
key_pressed('L3', input, function() {494
lock_y = !lock_y;495
lock_x = false;496
});497
// lock x498
key_pressed('R3', input, function() {499
lock_x = !lock_x;500
lock_y = false;501
});502
// color-wheel left503
key_pressed('L1', input, function() {504
if(index > 0) {505
index--;506
for(let i in colors) {507
if(COLOR_LENGTH > i) {508
colors[i].classList[i == index ? 'add' : 'remove']('active');509
}510
}511
}512
});513
// color-wheel right514
key_pressed('R1', input, function() {515
if(index < COLOR_LENGTH - 1) {516
index++;517
for(let i in colors) {518
if(COLOR_LENGTH > i) {519
colors[i].classList[i == index ? 'add' : 'remove']('active');520
}521
}522
}523
});524
key_pressed('Up', input, function() {525
if(arrow_y > 0) {526
arrow_y--;527
}528
});529
key_pressed('Down', input, function() {530
if(arrow_y < 10) {531
arrow_y++;532
}533
});534
key_pressed('Left', input, function() {535
if(arrow_x > 0) {536
arrow_x--;537
}538
});539
key_pressed('Right', input, function() {540
if(arrow_x < 4) {541
arrow_x++;542
}543
});544
}545
}546
else {547
if(devices.gamepad) {548
devices.gamepad = false;549
show_notification('notification.input-device.gamepad.remove');550
}551
}552
if(device !== 'gamepad') {553
let pos = arrow_y * 5 + arrow_x;554
if(modal_colors[pos].classList.contains('focus')) {555
modal_colors[pos].classList.remove('focus');556
}557
}558
// device mouse option559
let mouse_option = document.querySelector('.settings-modal .device-tab .item[data-device="mouse"]');560
if(devices.mouse && mouse_option.classList.contains('inactive')) {561
mouse_option.classList.remove('inactive');562
if(!device) {563
device = 'mouse';564
fallback_device = 'mouse';565
mouse_option.classList.add('active');566
}567
}568
// device touch option569
let touch_option = document.querySelector('.settings-modal .device-tab .item[data-device="touch"]');570
if(devices.touch && touch_option.classList.contains('inactive')) {571
touch_option.classList.remove('inactive');572
if(!device) {573
device = 'touch';574
fallback_device = 'touch';575
touch_option.classList.add('active');576
}577
}578
// device pen option579
let pen_option = document.querySelector('.settings-modal .device-tab .item[data-device="pen"]');580
if(devices.pen && pen_option.classList.contains('inactive')) {581
pen_option.classList.remove('inactive');582
if(!device) {583
device = 'pen';584
fallback_device = 'pen';585
pen_option.classList.add('active');586
}587
}588
// device gamepad option589
let gamepad_option = document.querySelector('.settings-modal .device-tab .item[data-device="gamepad"]');590
if(devices.gamepad && gamepad_option.classList.contains('inactive')) {591
gamepad_option.classList.remove('inactive');592
if(!device) {593
device = 'gamepad';594
fallback_device = 'gamepad';595
gamepad_option.classList.add('active');596
}597
}598
if(!devices.gamepad && !gamepad_option.classList.contains('inactive')) {599
gamepad_option.classList.add('inactive');600
gamepad_option.classList.remove('active');601
if(device === 'gamepad') {602
if(device !== fallback_device) {603
device = fallback_device;604
let option = document.querySelector(`.settings-modal .device-tab .item[data-device="${device}"]`);605
option.classList.add('active');606
}607
else {608
device = null;609
}610
point.x = null;611
point.y = null;612
last.x = null;613
last.y = null;614
}615
}616
// cursor visible617
canvas.style.cursor = device === 'mouse' ? 'none' : 'default';618
if(!current_modal && !left_modal) {619
// pencil620
if(draw && !erase) {621
if(point.x !== null && point.y !== null) {622
if(last.x !== null && last.y !== null) {623
draw_line(point, last, radius, COLOR[index]);624
}625
}626
}627
// eraser628
if(erase && !draw) {629
if(point.x !== null && point.y !== null) {630
if(last.x !== null && last.y !== null) {631
draw_line(point, last, radius, 'white');632
}633
}634
}635
}636
// at least one device637
let one_device = Object.values(devices).some(function(device) {638
return device;639
});640
if(one_device && !device_empty.classList.contains('hidden')) {641
device_empty.classList.add('hidden');642
}643
if(!one_device && device_empty.classList.contains('hidden')) {644
device_empty.classList.remove('hidden');645
}646
// navigate colors647
let pos = arrow_y * 5 + arrow_x;648
if(device === 'gamepad') {649
for(let [ i, item ] of Object.entries(modal_colors)) {650
item.classList[parseInt(i) == pos ? 'add' : 'remove']('focus');651
}652
}653
render();654
if(draw || erase) {655
last.x = point.x;656
last.y = point.y;657
}658
requestAnimationFrame(update);659
}661
function render() {662
if(radius < 2) {663
radius = 2;664
}665
o.clearRect(0, 0, window.innerWidth, window.innerHeight);666
if(point.x !== null && point.y !== null) {667
let touch_device = (device === 'touch' || device === 'pen');668
if(touch_device) {669
if(!left_modal) {670
o.fillStyle = 'white';671
o.beginPath();672
o.arc(point.x, point.y, parseInt(radius) + 2, 2 * Math.PI, 0);673
o.fill();674
o.fillStyle = COLOR[index];675
o.beginPath();676
o.arc(point.x, point.y, parseInt(radius), 2 * Math.PI, 0);677
o.fill();678
}679
}680
else {681
o.fillStyle = 'white';682
o.beginPath();683
o.arc(point.x, point.y, parseInt(radius) + 2, 2 * Math.PI, 0);684
o.fill();685
o.fillStyle = COLOR[index];686
o.beginPath();687
o.arc(point.x, point.y, parseInt(radius), 2 * Math.PI, 0);688
o.fill();689
}690
}691
if(!left_modal) {692
if(erase && !draw) {693
o.beginPath();694
o.lineWidth = 2;695
o.strokeStyle = 'white';696
o.moveTo(point.x, point.y - parseInt(radius) * 0.5);697
o.lineTo(point.x, point.y + parseInt(radius) * 0.5);698
o.moveTo(point.x - parseInt(radius) * 0.5, point.y);699
o.lineTo(point.x + parseInt(radius) * 0.5, point.y);700
o.stroke();701
}702
}703
if(lock_x || lock_y) {704
o.beginPath();705
o.lineWidth = 2;706
o.strokeStyle = 'white';707
if(lock_x) {708
o.moveTo(point.x, point.y - parseInt(radius));709
o.lineTo(point.x, point.y + parseInt(radius));710
}711
if(lock_y) {712
o.moveTo(point.x - parseInt(radius), point.y);713
o.lineTo(point.x + parseInt(radius), point.y);714
}715
o.stroke();716
}717
}719
function draw_line(point, last, radius, color) {720
if(last) {721
g.lineCap = 'round';722
g.strokeStyle = color;723
g.lineWidth = 2 * parseInt(radius);724
g.beginPath();725
g.moveTo(last.x + 0.5, last.y + 0.5);726
g.lineTo(point.x + 0.5, point.y + 0.5);727
g.stroke();728
}729
else {730
g.fillStyle = color;731
g.beginPath();732
g.arc(point.x, point.y, parseInt(radius), 2 * Math.PI, 0);733
g.fill();734
}735
}737
function change_device(element) {738
let value = element.dataset.device;739
if(devices[value]) {740
device = value;741
for(let item of device_options) {742
item.classList[element === item ? 'add' : 'remove']('active');743
}744
if(device !== 'gamepad') {745
point.x = null;746
point.y = null;747
last.x = null;748
last.y = null;749
}750
}751
}753
function change_language(element) {754
let code = element.dataset.code;755
lang = code;756
let items = document.querySelectorAll('[data-lang]');757
for(let item of items) {758
let prime = item.dataset.prime === 'true';759
let beta = item.dataset.beta === 'true';760
let type = item.dataset.type;761
let text = LANG[code][item.dataset.lang];762
let prime_content;763
let beta_content;764
if(prime) {765
prime_content = item.querySelector('b.prime-badge').textContent;766
}767
if(beta) {768
beta_content = item.querySelector('b.beta-badge').textContent;769
}770
if(text) {771
if(type === 'tooltip') {772
item.dataset.title = text;773
}774
else {775
item.textContent = text;776
}777
if(prime && type !== 'tooltip') {778
item.innerHTML += `<b class="prime-badge">${prime_content}</b>`;779
}780
if(beta && type !== 'tooltip') {781
item.innerHTML += `<b class="beta-badge">${beta_content}</b>`;782
}783
}784
}785
let options = document.querySelectorAll('.language-tab .item');786
for(let option of options) {787
option.classList[option === element ? 'add' : 'remove']('active');788
}789
}791
function toggle_modal(element) {792
let code = element.dataset.modal;793
let modal = document.querySelector(`.${code}-modal`);794
if(modal) {795
let open = modal.classList.contains('hidden');796
if(open) {797
current_modal = element;798
for(let item of actions) {799
if(item !== element) {800
item.classList.remove('active');801
}802
}803
for(let item of tools) {804
if(item !== element) {805
item.classList.remove('active');806
}807
}808
for(let item of modals) {809
if(item !== modal) {810
item.classList.add('hidden');811
}812
let main = item.querySelector('.main');813
if(main) {814
main.classList.remove('hidden');815
main.style.marginLeft = '0';816
}817
let tab_list = item.querySelector('.tab-list');818
if(tab_list) {819
let tabs = tab_list.querySelectorAll('.tab');820
for(let tab of tabs) {821
tab.classList.add('hidden');822
}823
}824
}825
if(code === 'colors') {826
if(device === 'gamepad') {827
modal_colors[0].classList.add('focus');828
arrow_x = 0;829
arrow_y = 0;830
}831
}832
}833
else {834
current_modal = null;835
}836
element.classList[open ? 'add' : 'remove']('active');837
modal.classList[open ? 'remove' : 'add']('hidden');838
modal.animate([839
{ opacity: 0, marginTop: '-6px' },840
{ opacity: 1, marginTop: '0' }841
], {842
duration: 100,843
fill: 'both'844
});845
}846
}848
function open_tab(element) {849
let code = element.dataset.tab;850
let main = element.parentNode.parentNode851
let tab = document.querySelector(`.${code}-tab`);852
if(tab) {853
main.style.marginLeft = '-100%';854
current_tab = tab.querySelector('.back');855
tab.classList.remove('hidden');856
main.classList.add('hidden');857
}858
}860
function close_tab(element) {861
let modal = element.parentNode.parentNode.parentNode.parentNode;862
let main = modal.querySelector('.main');863
let tab_list = modal.querySelector('.tab-list');864
let tabs = tab_list.querySelectorAll('.tab');865
for(let tab of tabs) {866
tab.classList.add('hidden');867
}868
main.classList.remove('hidden');869
main.style.marginLeft = '0';870
current_tab = null;871
}873
function show_notification(message) {874
let code = uuid();875
notification_code = code;876
let element = document.querySelector('.notification');877
let content = element.querySelector('.content');878
content.textContent = LANG[lang][message];879
element.animate([880
{ opacity: 0 },881
{ opacity: 1}882
], {883
duration: 200,884
fill: 'both'885
});886
setTimeout(function() {887
if(notification_code === code) {888
element.animate([889
{ opacity: 1 },890
{ opacity: 0}891
], {892
duration: 200,893
fill: 'both'894
});895
}896
}, 2000);897
}899
function clear_screen() {900
g.clearRect(0, 0, canvas.width, canvas.height);901
show_notification('notification.clear');902
toggle_modal(clear_modal);903
}