A web-based drawing app
git clone git://soophie.de/outline
Binary file
1
const DEVICE_PIXEL_RATIO = window.devicePixelRatio || 1;2
const MAX_HISTORY_STEP = 10;4
import { COLORS } from '../constant.js';
14
let canvas;15
let overlay;16
let memory;17
let temporary;18
let g;19
let o;20
let m;21
let t;23
function init() {24
width = window.innerWidth;
26
canvas = document.querySelector('#canvas');27
overlay = document.querySelector('#overlay');28
memory = document.createElement('canvas');29
temporary = document.createElement('canvas');30
g = canvas.getContext('2d');31
o = overlay.getContext('2d');32
m = memory.getContext('2d');33
t = temporary.getContext('2d');34
resize();35
window.addEventListener('resize', resize);36
}
67
scale_canvas(memory, m);68
m.imageSmoothingEnabled = false;69
m.drawImage(canvas, 0, 0, size, size);70
scale_canvas(temporary, t);71
t.imageSmoothingEnabled = false;72
}74
function scale_canvas(canvas, g) {
87
g.scale(ratio, ratio);88
}83
function clear() {90
function clear(state) {91
g.clearRect(0, 0, size, size);92
m.clearRect(0, 0, size, size);93
t.clearRect(0, 0, size, size);94
state.history = [];95
state.redo_history = [];96
}98
function on_press(event, state) {
101
y: event.layerY102
});103
active = true;104
state.redo_history = [];105
}107
function on_move(event, state) {
112
x: event.layerX,113
y: event.layerY114
});104
draw_curve(state);115
draw_curve(g, state, point_list);116
}117
}108
function on_release(event) {119
function on_release(event, state) {120
if(active) {121
if(event && event.type === 'pointerup') {122
active = false;123
}124
if(point_list.length > 0) {125
state.history.push({126
list: point_list,127
state: Object.assign({}, state)128
});129
if(state.history.length > MAX_HISTORY_STEP) {130
let line = state.history.shift();131
draw_curve(t, line.state, line.list);132
}133
}134
m.clearRect(0, 0, size, size);135
m.drawImage(canvas, 0, 0, size, size);136
point_list = [];137
}138
}119
function draw_curve(state) {140
function undo(state) {141
if(state.history.length > 0) {142
state.redo_history.push(state.history.pop());143
g.clearRect(0, 0, size, size);144
m.clearRect(0, 0, size, size);145
g.drawImage(temporary, 0, 0, size, size);146
m.drawImage(temporary, 0, 0, size, size);147
for(let line of state.history) {148
draw_curve(g, line.state, line.list);149
draw_curve(m, line.state, line.list);150
}151
}152
}154
function redo(state) {155
if(state.redo_history.length > 0) {156
let line = state.redo_history.pop();157
state.history.push(line);158
draw_curve(g, line.state, line.list);159
draw_curve(m, line.state, line.list);160
}161
}163
function draw_curve(g, state, point_list) {164
if(state.pencil === 'pen') {165
g.strokeStyle = COLORS[state.color];166
}
242
on_press,243
on_move,244
on_release,245
undo,246
redo,247
draw_curve,248
draw_cursor,249
clear_cursor
32
tab: null,33
option: {34
view_mode: false35
}35
},36
history: [],37
redo_history: []38
};40
window.addEventListener('load', init);
67
clear: 'button.btn-apply[data-event="clear"]',68
pencil_tool: '.tool-item[data-code="pencil"]',69
option_event_fullscreen: '[data-event="toggle.fullscreen"]',68
option_event_view_mode: '[data-event="toggle.view-mode"]'70
option_event_view_mode: '[data-event="toggle.view-mode"]',71
undo_tool: '[data-lang="tool.undo"]',72
redo_tool: '[data-lang="tool.redo"]'73
});75
fill_hotbar_list();
205
canvas.draw_cursor(state);206
});207
node.clear.addEventListener('click', function(event) {204
canvas.clear();208
canvas.clear(state);209
update_undo_and_redo();210
show_notification('notification.clear');211
state.modal = null;212
update_modal_list();
224
state.option.view_mode = !state.option.view_mode;225
update_view_mode_option();226
});227
node.undo_tool.addEventListener('click', function(event) {228
canvas.undo(state);229
update_undo_and_redo();230
});231
node.redo_tool.addEventListener('click', function(event) {232
canvas.redo(state);233
update_undo_and_redo();234
});235
}237
function update_language_list() {
262
}264
function update_hotbar() {252
canvas.on_release(event);265
for(let item of node.hotbar_list) {266
item.classList[node.hotbar_list[state.index] === item ? 'add' : 'remove']('active');267
}
345
state.modal = null;346
state.tab = null;347
update_modal_list();336
canvas.on_release();348
canvas.on_release(null, state);349
canvas.clear_cursor();350
canvas.get_canvas().style.cursor = 'default';351
node.hotbar.classList.add('inactive');
390
}391
}393
function update_undo_and_redo() {394
node.undo_tool.classList[state.history.length > 0 ? 'remove' : 'add']('inactive');395
node.redo_tool.classList[state.redo_history.length > 0 ? 'remove' : 'add']('inactive');396
}398
function on_pointerdown(event) {399
// detect touch400
if(!state.device_list.touch && event.pointerType === 'touch') {
423
if(event.target === canvas.get_canvas()) {424
update_focus(false);425
canvas.on_press(event, state);426
update_undo_and_redo();427
state.point = {428
x: event.layerX,429
y: event.layerY
462
function on_pointerup(event) {463
if(!state.option.view_mode && state.device === event.pointerType) {446
canvas.on_release(event);464
canvas.on_release(event, state);465
update_undo_and_redo();466
canvas.draw_cursor(state);467
update_focus(true);468
}
471
function on_pointerout(event) {472
if(!state.option.view_mode && state.device === event.pointerType) {454
canvas.on_release(event);473
canvas.on_release(event, state);474
update_undo_and_redo();475
state.point = null;476
canvas.draw_cursor(state);477
}
498
let i = parseInt(event.code.substr(5)) - 1;499
if(state.index !== i) {500
if(i >= 0 && i < state.color_list.length) {501
canvas.on_release(event, state);502
state.color = state.color_list[i];503
state.index = i;504
canvas.draw_cursor(state);
532
state.option.view_mode = !state.option.view_mode;533
update_view_mode_option();534
}535
if(event.code === 'ArrowLeft') {536
canvas.undo(state);537
update_undo_and_redo();538
}539
if(event.code === 'ArrowRight') {540
canvas.redo(state);541
update_undo_and_redo();542
}543
}544
// alt545
if(event.altKey && !event.ctrlKey && !event.shiftKey) {
548
if(event.code.startsWith('Digit')) {549
let i = parseInt(event.code.substr(5)) - 1;550
if(i >= 0 && i < state.pencil_list.length) {551
canvas.on_release(event, state);552
state.pencil = state.pencil_list[i];523
canvas.on_release(event);553
canvas.draw_cursor(state);554
update_pencil_list();555
}
623
if(!state.option.view_mode && state.device === 'mouse') {624
let y = event.deltaY < 0 ? 1 : -1;625
if(state.radius + y > 2 - 1 && state.radius + y < 50 + 1) {597
canvas.on_release(event);626
canvas.on_release(event, state);627
state.radius += y;628
node.scale_input.value = state.radius;629
canvas.draw_cursor(state);
640
if(state.color_list.length > i) {641
item.style.background = COLORS[state.color_list[i]];642
item.addEventListener('click', function(event) {643
canvas.on_release(event, state);644
state.color = state.color_list[i];645
state.index = i;646
canvas.draw_cursor(state);
658
if(COLORS.length > i) {659
item.style.background = COLORS[i];660
item.addEventListener('pointerdown', function(event) {661
canvas.on_release(event, state);662
state.color = i;663
state.modal = null;664
update_modal_list();