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