A web-based drawing app
git clone git://soophie.de/outline
blob: 46686adc9a18c634afc0167c85642ab39d001de1 (11340 bytes)[doc][blame][raw]
1
import { COLORS } from './constant.js';2
import { uuid, query, prevent } from './util.js';4
// addons5
import canvas from './addon/canvas.js';6
import locale from './addon/locale.js';7
import ripple from './addon/ripple.js';8
// import gamepad from './addon/gamepad.js';10
let node;11
let state = {12
color: 54,13
color_list: [54, 2, 17, 27, 37, 12],14
radius: 14,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
timestamp: null,25
modal: null,26
tab: null27
};29
window.addEventListener('load', init);31
function init() {33
canvas.init();34
locale.init('en');35
locale.load('../asset/lang', ['en', 'de']);36
ripple.init();38
node = query({39
modal_list: { query: '.modal', all: true },40
tab_list: { query: '.tab', all: true },41
hotbar_list: { query: '.hotbar .color', all: true },42
modal_color_list: { query: '.colors-modal .color', all: true },43
language_list: { query: '.settings-modal .language-tab .item[data-event="change.language"]', all: true },44
device_list: { query: '.settings-modal .device-tab .item[data-event="change.device"]', all: true },45
modal_event_list: { query: '[data-event*=".modal"]', all: true },46
tab_event_list: { query: '[data-event*=".tab"]', all: true },47
tool_list: { query: '.tool', all: true },48
action_list: { query: '.action', all: true },49
notification: '.notification',50
scale_input: 'input.scale[type="range"]',51
clear: 'button.btn-apply[data-event="clear"]',52
option_event_fullscreen: '[data-event="toggle.fullscreen"]'53
});55
fill_hotbar_list();56
fill_modal_color_list();58
bind_events();59
}61
function bind_events() {62
document.addEventListener('dragstart', prevent);63
document.addEventListener('contextmenu', prevent);64
document.addEventListener('pointerdown', on_pointerdown);65
document.addEventListener('pointermove', on_pointermove);66
document.addEventListener('pointerup', on_pointerup);67
document.addEventListener('pointerout', on_pointerout);68
document.addEventListener('keydown', on_keydown);69
document.addEventListener('wheel', on_wheel);70
document.addEventListener('wheel', function(event) {71
if(event.ctrlKey) {72
prevent(event);73
}74
}, { passive: false });75
document.addEventListener('touchmove', function(event) {76
if(event.target !== node.scale_input) {77
event.preventDefault();78
}79
}, { passive: false });81
for(let item of node.language_list) {82
item.addEventListener('click', function(event) {83
let code = item.dataset.code;84
locale.change(code);85
update_language_list();86
});87
}88
for(let item of node.device_list) {89
item.addEventListener('click', function(event) {90
let code = item.dataset.code;91
if(state.device_list[code]) {92
state.device = code;93
update_device_list();94
}95
});96
}97
for(let item of node.modal_event_list) {98
item.addEventListener('click', function(event) {99
let code = item.dataset.code;100
let match = /^(toggle|open|close).modal$/.exec(item.dataset.event);101
if(match) {102
switch(match[1]) {103
case 'toggle':104
if(code) {105
state.modal = state.modal !== code ? code : null;106
update_modal_list();107
}108
break;109
case 'open':110
if(code && state.modal !== code) {111
state.modal = code;112
update_modal_list();113
}114
break;115
case 'close':116
if(state.modal !== null) {117
state.modal = null;118
update_modal_list();119
}120
break;121
default:122
break;123
}124
}125
});126
}127
for(let item of node.tab_event_list) {128
item.addEventListener('click', function(event) {129
let code = item.dataset.code;130
let match = /^(open|close).tab/.exec(item.dataset.event);131
if(match) {132
switch(match[1]) {133
case 'open':134
if(code && state.tab !== code) {135
state.tab = code;136
update_tab_list();137
}138
break;139
case 'close':140
if(state.tab !== null) {141
state.tab = null;142
update_tab_list();143
}144
break;145
default:146
break;147
}148
}149
});150
}151
node.scale_input.addEventListener('input', function(event) {152
state.radius = parseInt(node.scale_input.value);153
state.point = {154
x: parseInt(window.innerWidth / 2),155
y: parseInt(window.innerHeight / 2)156
};157
canvas.draw_cursor(state);158
});159
node.clear.addEventListener('click', function(event) {160
canvas.clear();161
show_notification('notification.clear');162
state.modal = null;163
update_modal_list();164
});165
node.option_event_fullscreen.addEventListener('click', function(event) {166
if(!document.fullscreenElement) {167
document.documentElement.requestFullscreen();168
}169
else if(document.exitFullscreen) {170
document.exitFullscreen();171
}172
});173
document.addEventListener('fullscreenchange', on_fullscreenchange);174
}176
function update_language_list() {177
let lang = locale.get_current();178
for(let item of node.language_list) {179
let code = item.dataset.code;180
item.classList[code === lang ? 'add' : 'remove']('active');181
}182
}184
function update_device_list() {185
canvas.get_canvas().style.cursor = state.device === 'mouse' ? 'none' : '';186
for(let item of node.device_list) {187
let code = item.dataset.code;188
item.classList[code === state.device ? 'add' : 'remove']('active');189
item.classList[state.device_list[code] ? 'remove' : 'add']('inactive');190
}191
}193
function update_hotbar() {194
canvas.on_release(event);195
for(let item of node.hotbar_list) {196
item.classList[node.hotbar_list[state.index] === item ? 'add' : 'remove']('active');197
}198
}200
function update_focus(bool) {201
for(let item of node.hotbar_list) {202
item.style.pointerEvents = bool ? '' : 'none';203
}204
for(let item of node.tool_list) {205
item.style.pointerEvents = bool ? '' : 'none';206
}207
for(let item of node.action_list) {208
item.style.pointerEvents = bool ? '' : 'none';209
}210
}212
function update_modal_list() {213
for(let item of node.modal_list) {214
item.classList.add('hidden');215
}216
for(let item of node.tool_list) {217
item.classList.remove('active');218
}219
for(let item of node.action_list) {220
item.classList.remove('active');221
}222
state.tab = null;223
update_tab_list();224
if(state.modal) {225
let modal = document.querySelector(`.modal.${state.modal}-modal`);226
let button = document.querySelector(`[data-event*=".modal"][data-code="${state.modal}"]`);227
if(modal) {228
modal.classList.remove('hidden');229
modal.animate([230
{ opacity: 0, marginTop: '-6px' },231
{ opacity: 1, marginTop: '0' }232
], {233
duration: 100,234
fill: 'both'235
});236
}237
if(button) {238
button.classList.add('active');239
}240
}241
}243
function update_tab_list() {244
for(let item of node.modal_list) {245
let main = item.querySelector('.main');246
if(main) {247
main.classList.remove('hidden');248
main.style.marginLeft = '0';249
}250
}251
for(let item of node.tab_list) {252
item.classList.add('hidden');253
}254
if(state.modal && state.tab) {255
let modal = document.querySelector(`.modal.${state.modal}-modal`);256
let tab = document.querySelector(`.${state.tab}-tab`);257
let main = modal.querySelector('.main');258
if(modal && tab && main) {259
tab.classList.remove('hidden');260
main.style.marginLeft = '-100%';261
main.classList.add('hidden');262
}263
}264
}266
function update_fullscreen_option() {267
let fullscreen = document.fullscreenElement;268
node.option_event_fullscreen.classList[fullscreen ? 'add' : 'remove']('active');269
node.option_event_fullscreen.querySelector('.ico').textContent = fullscreen ? 'toggle_on' : 'toggle_off';270
}272
function on_pointerdown(event) {273
// detect touch274
if(!state.device_list.touch && event.pointerType === 'touch') {275
state.device_list.touch = true;276
if(state.device) {277
show_notification('notification.input-device.touch.add');278
}279
else {280
state.device = 'touch';281
state.device_fallback = 'touch';282
}283
update_device_list();284
}285
// detect pen286
if(!state.device_list.pen && event.pointerType === 'pen') {287
state.device_list.pen = true;288
show_notification('notification.input-device.pencil.add');289
if(!state.device) {290
state.device = 'pen';291
state.device_fallback = 'pen';292
}293
update_device_list();294
}295
// drawing296
if(state.device === event.pointerType) {297
if(event.target === canvas.get_canvas()) {298
update_focus(false);299
canvas.on_press(event, state);300
state.point = {301
x: event.layerX,302
y: event.layerY303
};304
canvas.draw_cursor(state);305
}306
}307
}309
function on_pointermove(event) {310
// detect mouse311
if(!state.device_list.mouse && event.pointerType === 'mouse') {312
state.device_list.mouse = true;313
if(state.device) {314
show_notification('notification.input-device.mouse.add');315
}316
else {317
state.device = 'mouse';318
state.device_fallback = 'mouse';319
}320
update_device_list();321
}322
// drawing323
if(state.device === event.pointerType) {324
if(event.target === canvas.get_canvas()) {325
canvas.on_move(event, state);326
state.point = {327
x: event.layerX,328
y: event.layerY329
};330
canvas.draw_cursor(state);331
}332
}333
}335
function on_pointerup(event) {336
if(state.device === event.pointerType) {337
canvas.on_release(event);338
canvas.draw_cursor(state);339
update_focus(true);340
}341
}343
function on_pointerout(event) {344
if(state.device === event.pointerType) {345
canvas.on_release(event);346
state.point = null;347
canvas.draw_cursor(state);348
}349
}351
function on_keydown(event) {352
let lock = event.getModifierState("CapsLock");353
if(!event.ctrlKey && !event.shiftKey && !event.altKey) {354
if(!lock) {355
if(event.code.startsWith('Digit')) {356
let i = parseInt(event.code.substr(5)) - 1;357
if(state.index !== i) {358
if(i >= 0 && i < state.color_list.length) {359
state.color = state.color_list[i];360
state.index = i;361
canvas.draw_cursor(state);362
update_hotbar();363
}364
}365
}366
}367
}368
if(event.code === 'Escape') {369
if(state.tab) {370
state.tab = null;371
update_tab_list();372
return;373
}374
if(state.modal) {375
state.modal = null;376
update_modal_list();377
return;378
}379
}380
}382
function on_wheel(event) {383
if(state.device === 'mouse') {384
let y = event.deltaY < 0 ? 1 : -1;385
if(state.radius + y > 2 - 1 && state.radius + y < 50 + 1) {386
state.radius += y;387
node.scale_input.value = state.radius;388
canvas.draw_cursor(state);389
}390
}391
}393
function on_fullscreenchange(event) {394
update_fullscreen_option();395
}397
function fill_hotbar_list() {398
for(let [ i, item ] of Object.entries(node.hotbar_list)) {399
if(state.color_list.length > i) {400
item.style.background = COLORS[state.color_list[i]];401
item.addEventListener('pointerdown', function(event) {402
state.color = state.color_list[i];403
state.index = i;404
canvas.draw_cursor(state);405
update_hotbar();406
});407
if(state.index == i) {408
item.classList.add('active');409
}410
}411
}412
}414
function fill_modal_color_list() {415
for(let [ i, item ] of Object.entries(node.modal_color_list)) {416
if(COLORS.length > i) {417
item.style.background = COLORS[i];418
item.addEventListener('pointerdown', function(event) {419
state.color = i;420
state.modal = null;421
update_modal_list();422
canvas.draw_cursor(state);423
});424
}425
}426
}428
function show_notification(message) {429
let code = uuid();430
state.timestamp = code;431
let content = node.notification.querySelector('.content');432
content.textContent = locale.get()[message];433
node.notification.animate([434
{ opacity: 0 },435
{ opacity: 1 }436
], {437
duration: 200,438
fill: 'both'439
});440
setTimeout(function() {441
if(state.timestamp === code) {442
node.notification.animate([443
{ opacity: 1 },444
{ opacity: 0 }445
], {446
duration: 200,447
fill: 'both'448
});449
}450
}, 2000);451
}