A web-based drawing app
git clone git://soophie.de/outline
blob: 719f23c568ec49a6a1b05b8656cf9cc6ccb2d63e (6086 bytes)[doc][blame][raw]
1
import { uuid } from './util.js';3
export default {4
init: function(app) {5
this.app = app;6
},7
bind: function(element, args) {8
if(typeof args === 'object') {9
for(let entry of Object.entries(args)) {10
let [ key, value ] = entry;11
if(typeof this[key] === 'function' || typeof this.app.state.action[key] === 'object') {12
this.bindType(element, key, value);13
}14
}15
}16
},17
action: function(action, type, func, args) {18
action.element.addEventListener(type, function(event) {19
if(action.before) {20
action.before();21
}22
func(event);23
if(action.after) {24
action.after();25
}26
}, args);27
},28
bindType: function(element, key, value) {29
let that = this;30
if(typeof value === 'string') {31
if(Array.isArray(element)) {32
for(let item of element) {33
item.addEventListener(value, function(event) {34
if(that[key]) {35
that[key](event);36
}37
if(that.app.state.action[key]) {38
that.app.state.action[key].trigger();39
}40
});41
}42
}43
else {44
element.addEventListener(value, function(event) {45
if(that[key]) {46
that[key](event);47
}48
if(that.app.state.action[key]) {49
that.app.state.action[key].trigger();50
}51
});52
}53
}54
if(typeof value === 'object') {55
if(Array.isArray(value)) {56
for(let item of value) {57
this.bindType(element, key, item);58
}59
}60
else {61
let { type } = value;62
if(typeof type === 'string') {63
delete value.type;64
if(Array.isArray(element)) {65
for(let item of element) {66
item.addEventListener(type, function(event) {67
if(that[key]) {68
that[key](event);69
}70
if(that.app.state.action[key]) {71
that.app.state.action[key].trigger();72
}73
}, value);74
}75
}76
else {77
element.addEventListener(type, function(event) {78
if(that[key]) {79
that[key](event);80
}81
if(that.app.state.action[key]) {82
that.app.state.action[key].trigger();83
}84
}, value);85
}86
}87
}88
}89
},90
prevent: function(event) {91
event.preventDefault();92
},93
begin: function(event) {94
this.app.state.dragging = true;95
let state = this.state(event);96
this.app.socket.data(state);97
this.app.paint.draw(state);98
this.app.state.last = state.pos;99
this.app.node.toolbar.style.pointerEvents = 'none';100
},101
drag: function(event) {102
let state = this.state(event);103
if(this.app.state.dragging && document.hasFocus()) {104
this.app.socket.data(state);105
this.app.paint.draw(state);106
this.app.state.last = state.pos;107
if(event.type === 'touchmove') {108
this.prevent(event);109
}110
}111
},112
end: function(event) {113
this.state(event);114
this.app.state.dragging = false;115
this.app.state.last = null;116
this.app.node.toolbar.style.pointerEvents = '';117
},118
move: function(event) {119
this.app.state.cursor = event.target === this.app.node.canvas ? this.app.state.pos : null;120
},121
out: function(event) {122
this.app.state.cursor = null;123
},124
scroll: function(event) {125
if(event.ctrlKey) {126
event.preventDefault();127
return;128
}129
if(!this.app.modal.opened) {130
if(event.deltaY < 0) {131
this.app.state.radius += this.app.state.radius < 15 ? 1 : 0;132
}133
else {134
this.app.state.radius -= this.app.state.radius > 1 ? 1 : 0;135
}136
}137
},138
key: function(event) {139
if(!this.app.state.opened) {140
if(!event.getModifierState("CapsLock")) {141
let index;142
if(event.code.startsWith('Digit')) {143
index = parseInt(event.code.substr(5));144
}145
if(event.code.startsWith('Numpad')) {146
index = parseInt(event.code.substr(6));147
}148
if(Number.isInteger(index) && index < 7 && index > 0) {149
index--;150
for(let i in this.app.node.bucket) {151
let item = this.app.node.bucket[i];152
if(index === parseInt(i)) {153
item.classList.add('active');154
this.app.state.color = item.dataset.color;155
}156
else {157
item.classList.remove('active');158
}159
}160
}161
if(event.code === 'KeyC') {162
for(let item of this.app.node.color) {163
item.classList[this.app.state.color === item.dataset.color ? 'add' : 'remove']('active');164
}165
this.app.modal.open('color');166
}167
if(event.code === 'KeyV') {168
this.app.modal.open('scaling');169
}170
if(event.code === 'KeyT') {171
this.app.modal.open('clear');172
}173
if(event.code === 'KeyS') {174
this.app.modal.get('save').element.querySelector('input.action-file').value = 'outline-image-' + uuid();175
let input = this.app.modal.get('save').element.querySelector('.modal input.action-file');176
input.classList.remove('invalid');177
this.app.modal.open('save');178
}179
}180
}181
else if(this.app.state.current && this.app.state.current.action.cancel) {182
if(event.keyCode === 27) {183
this.app.modal.close();184
}185
}186
},187
button: function(event) {188
for(let item of this.app.node.bucket) {189
if(item === event.target) {190
item.classList.add('active');191
this.app.state.color = item.dataset.color;192
}193
else {194
item.classList.remove('active');195
}196
}197
},198
resize: function(event) {199
if(typeof event !== 'undefined') {200
let that = this;201
clearTimeout(this.app.state.delay);202
this.app.state.delay = setTimeout(function() {203
that.delayResize();204
}, 100);205
}206
else {207
this.delayResize();208
}209
},210
delayResize: function(event) {211
this.app.node.canvas.width = window.innerWidth;212
this.app.node.canvas.height = window.innerHeight;213
let cache = this.app.paint.o.getImageData(0, 0, this.app.node.overlay.width, this.app.node.overlay.height);214
this.app.node.overlay.width = window.innerWidth;215
this.app.node.overlay.height = window.innerHeight;216
this.app.paint.o.putImageData(cache, 0, 0);217
this.app.socket.request();218
},219
state: function(event) {220
let pos = null;221
if(event.type === 'mousemove' || event.type === 'mousedown') {222
pos = {223
x: event.layerX,224
y: event.layerY225
};226
}227
if(event.type === 'touchmove' || event.type === 'touchstart') {228
pos = {229
x: event.touches[0].pageX,230
y: event.touches[0].pageY231
};232
}233
return Object.assign(this.app.state, { pos: pos });234
}235
}