A web-based drawing app
git clone git://soophie.de/outline
37
dark_mode: false38
},39
history: [],40
redo_history: []40
redo_history: [],41
key: {}42
};44
export async function init() {
147
document.addEventListener('pointerup', on_pointerup);148
document.addEventListener('pointerout', on_pointerout);149
document.addEventListener('keydown', on_keydown);150
document.addEventListener('keyup', on_keyup);151
document.addEventListener('wheel', on_wheel);152
document.addEventListener('wheel', function(event) {153
if(event.ctrlKey) {
557
update_device_list();558
}559
// drawing558
if(!state.option.view_mode && state.device === event.pointerType) {559
if(event.target === canvas.get_canvas()) {560
if(!PRIORITY_MODAL.includes(state.modal)) {561
update_focus(false);562
canvas.on_press(event, state);563
update_undo_and_redo();564
state.point = {565
x: event.layerX,566
y: event.layerY567
};568
canvas.draw_cursor(state);560
if(event.button == 0) {561
if(!state.option.view_mode && state.device === event.pointerType) {562
if(event.target === canvas.get_canvas()) {563
if(!PRIORITY_MODAL.includes(state.modal)) {564
update_focus(false);565
canvas.on_press(event, state);566
update_undo_and_redo();567
state.point = {568
x: event.layerX,569
y: event.layerY570
};571
canvas.draw_cursor(state);572
}573
}574
}571
}572
if(state.option.view_mode && state.option.crop_mode) {573
if(event.target === canvas.get_canvas()) {574
state.crop = true;575
state.region = [];576
state.region[0] = { x: event.layerX, y: event.layerY };577
canvas.draw_crop(state);575
if(state.option.view_mode && state.option.crop_mode) {576
if(event.target === canvas.get_canvas()) {577
state.crop = true;578
state.region = [];579
state.region[0] = { x: event.layerX, y: event.layerY };580
canvas.draw_crop(state);581
}582
}583
}584
}
618
}620
function on_pointerup(event) {617
if(!state.option.view_mode && state.device === event.pointerType) {618
canvas.on_release(event, state);619
update_undo_and_redo();620
update_focus(true);621
}622
if(state.option.view_mode && state.option.crop_mode) {623
if(state.crop && event.target === canvas.get_canvas()) {624
state.crop = false;625
state.region[1] = { x: event.layerX, y: event.layerY };626
canvas.draw_crop(state);627
let [ begin, end ] = state.region;628
let delta = {629
x: end.x - begin.x,630
y: end.y - begin.y631
};632
let x = begin.x < end.x ? begin.x : end.x;633
let y = begin.y < end.y ? begin.y : end.y;634
if(delta.x < 0) {635
delta.x *= -1;636
}637
if(delta.y < 0) {638
delta.y *= -1;639
}640
if(delta.x != 0 && delta.y != 0) {641
let data = canvas.create_capture(x, y, delta.x, delta.y);642
if(data) {643
node.download.download = `outline-${uuid()}.png`;644
node.download.href = data;645
node.download.click();646
state.option.crop_mode = false;647
canvas.draw_crop(state);648
canvas.get_canvas().style.cursor = 'default';621
if(event.button == 0) {622
if(!state.option.view_mode && state.device === event.pointerType) {623
canvas.on_release(event, state);624
update_undo_and_redo();625
update_focus(true);626
}627
if(state.option.view_mode && state.option.crop_mode) {628
if(state.crop && event.target === canvas.get_canvas()) {629
state.crop = false;630
state.region[1] = { x: event.layerX, y: event.layerY };631
canvas.draw_crop(state);632
let [ begin, end ] = state.region;633
let delta = {634
x: end.x - begin.x,635
y: end.y - begin.y636
};637
let x = begin.x < end.x ? begin.x : end.x;638
let y = begin.y < end.y ? begin.y : end.y;639
if(delta.x < 0) {640
delta.x *= -1;641
}650
else {651
console.warn('Unable to create capture from current state!');642
if(delta.y < 0) {643
delta.y *= -1;644
}645
if(delta.x != 0 && delta.y != 0) {646
let data = canvas.create_capture(x, y, delta.x, delta.y);647
if(data) {648
node.download.download = `outline-${uuid()}.png`;649
node.download.href = data;650
node.download.click();651
state.option.crop_mode = false;652
canvas.draw_crop(state);653
canvas.get_canvas().style.cursor = 'default';654
}655
else {656
console.warn('Unable to create capture from current state!');657
}658
}659
}660
}
678
}680
function on_keydown(event) {675
if(event.code === 'Escape') {676
if(state.tab) {677
state.tab = null;678
update_tab_list();679
return;680
}681
if(state.modal) {682
state.modal = null;683
update_modal_list();684
return;685
}686
if(state.option.view_mode && state.option.crop_mode) {687
state.option.crop_mode = false;688
canvas.draw_crop(state);689
canvas.get_canvas().style.cursor = 'default';690
}691
}692
if(!event.ctrlKey && !event.shiftKey && !event.altKey) {693
if(!state.option.view_mode) {694
if(event.code.startsWith('Digit')) {695
let i = parseInt(event.code.substr(5)) - 1;696
if(state.index !== i) {697
if(i >= 0 && i < state.color_list.length) {698
canvas.on_release(event, state);699
state.color = state.color_list[i];700
state.index = i;701
canvas.draw_cursor(state);702
update_hotbar();703
}704
}705
}706
if(event.code === 'KeyZ') {707
state.modal = state.modal !== 'colors' ? 'colors' : null;708
update_modal_list();709
}710
if(event.code === 'KeyX') {711
state.modal = state.modal !== 'scale' ? 'scale' : null;712
update_modal_list();713
}714
if(event.code === 'KeyC') {715
state.modal = state.modal !== 'pencil' ? 'pencil' : null;716
update_modal_list();717
}718
if(event.code === 'KeyV') {719
state.modal = state.modal !== 'clear' ? 'clear' : null;720
update_modal_list();721
}722
/*723
if(event.code === 'KeyB') {724
state.modal = state.modal !== 'multiplayer' ? 'multiplayer' : null;725
update_modal_list();726
}727
if(event.code === 'KeyN') {728
state.modal = state.modal !== 'account' ? 'account' : null;729
update_modal_list();681
if(!state.key[event.code]) {682
if(event.code === 'Escape') {683
if(state.tab) {684
state.tab = null;685
update_tab_list();686
return;687
}731
*/732
if(event.code === 'KeyM') {733
state.modal = state.modal !== 'more' ? 'more' : null;688
if(state.modal) {689
state.modal = null;690
update_modal_list();691
return;692
}736
if(event.code === 'KeyQ') {737
canvas.on_release(event, state);738
state.pencil = state.pencil_list[0];739
canvas.draw_cursor(state);740
update_pencil_list();741
}742
if(event.code === 'KeyW') {743
canvas.on_release(event, state);744
state.pencil = state.pencil_list[1];745
canvas.draw_cursor(state);746
update_pencil_list();747
}748
if(event.code === 'KeyE') {749
canvas.on_release(event, state);750
state.pencil = state.pencil_list[2];751
canvas.draw_cursor(state);752
update_pencil_list();693
if(state.option.view_mode && state.option.crop_mode) {694
state.option.crop_mode = false;695
canvas.draw_crop(state);696
canvas.get_canvas().style.cursor = 'default';697
}754
if(event.code === 'KeyF') {755
if(!document.fullscreenElement) {756
document.documentElement.requestFullscreen();698
}699
if(!event.ctrlKey && !event.shiftKey && !event.altKey) {700
if(!state.option.view_mode) {701
if(event.code.startsWith('Digit')) {702
let i = parseInt(event.code.substr(5)) - 1;703
if(state.index !== i) {704
if(i >= 0 && i < state.color_list.length) {705
canvas.on_release(event, state);706
state.color = state.color_list[i];707
state.index = i;708
canvas.draw_cursor(state);709
update_hotbar();710
}711
}712
}758
else if(document.exitFullscreen) {759
document.exitFullscreen();713
if(event.code === 'KeyZ') {714
state.modal = state.modal !== 'colors' ? 'colors' : null;715
update_modal_list();716
}717
if(event.code === 'KeyX') {718
state.modal = state.modal !== 'scale' ? 'scale' : null;719
update_modal_list();720
}721
if(event.code === 'KeyC') {722
state.modal = state.modal !== 'pencil' ? 'pencil' : null;723
update_modal_list();724
}725
if(event.code === 'KeyV') {726
state.modal = state.modal !== 'clear' ? 'clear' : null;727
update_modal_list();728
}729
/*730
if(event.code === 'KeyB') {731
state.modal = state.modal !== 'multiplayer' ? 'multiplayer' : null;732
update_modal_list();733
}734
if(event.code === 'KeyN') {735
state.modal = state.modal !== 'account' ? 'account' : null;736
update_modal_list();737
}738
*/739
if(event.code === 'KeyM') {740
state.modal = state.modal !== 'more' ? 'more' : null;741
update_modal_list();742
}743
if(event.code === 'KeyQ') {744
canvas.on_release(event, state);745
state.pencil = state.pencil_list[0];746
canvas.draw_cursor(state);747
update_pencil_list();748
}749
if(event.code === 'KeyW') {750
canvas.on_release(event, state);751
state.pencil = state.pencil_list[1];752
canvas.draw_cursor(state);753
update_pencil_list();754
}755
if(event.code === 'KeyE') {756
canvas.on_release(event, state);757
state.pencil = state.pencil_list[2];758
canvas.draw_cursor(state);759
update_pencil_list();760
}761
if(event.code === 'KeyF') {762
if(!document.fullscreenElement) {763
document.documentElement.requestFullscreen();764
}765
else if(document.exitFullscreen) {766
document.exitFullscreen();767
}768
}769
}770
if(event.code === 'Space') {771
state.option.view_mode = !state.option.view_mode;772
update_view_mode_option();773
}774
}763
if(event.code === 'Space') {764
state.option.view_mode = !state.option.view_mode;765
update_view_mode_option();766
}767
}768
if(event.ctrlKey && !event.shiftKey && !event.altKey) {769
if(event.code === 'KeyY') {770
canvas.undo(state);771
update_undo_and_redo();772
}773
if(event.code === 'KeyZ') {774
canvas.redo(state);775
update_undo_and_redo();775
if(event.ctrlKey && !event.shiftKey && !event.altKey) {776
if(event.code === 'KeyY') {777
canvas.undo(state);778
update_undo_and_redo();779
}780
if(event.code === 'KeyZ') {781
canvas.redo(state);782
update_undo_and_redo();783
}784
}777
}778
if(event.code === 'Enter') {779
if(state.modal === 'clear') {780
node.clear.click();785
if(event.code === 'Enter') {786
if(state.modal === 'clear') {787
node.clear.click();788
}789
}790
state.key[event.code] = true;791
}792
}794
function on_keyup(event) {795
state.key[event.code] = false;796
}798
function on_wheel(event) {799
if(!state.option.view_mode && state.device === 'mouse') {800
if(event.target === canvas.get_canvas()) {
8
let elements = document.querySelectorAll('.ripple');9
for(let item of elements) {10
item.addEventListener('pointerdown', function(event) {11
item.classList.add('ripple-active');11
if(event.button == 0) {12
item.classList.add('ripple-active');13
}14
});15
}16
document.addEventListener('pointerup', function(event) {15
for(let item of elements) {16
item.classList.remove('ripple-active');17
if(event.button == 0) {18
for(let item of elements) {19
item.classList.remove('ripple-active');20
}21
}22
});23
}