A web-based drawing app
git clone git://soophie.de/outline
blob: d5133f8c36ac2073fac693243fa261dfaac09d89 (2814 bytes)[doc][blame][raw]
1
export default {2
socket: null,3
connected: false,4
timeout: false,5
interval: null,6
init: function(app) {7
this.app = app;8
},9
connect: function() {10
let that = this;11
this.socket = io('https://server.typable.dev', {12
path: '/pipe',13
reconnection: false14
});15
this.socket.on('connect', function() {16
that.connected = true;17
that.timeout = false;18
that.request();19
that.cursor();20
});21
this.socket.on('disconnect', function() {22
that.connected = false;23
that.timeout = true;24
that.app.modal.open('error');25
that.app.state.client = [];26
that.app.update();27
});28
this.socket.on('connect_error', function() {29
that.connected = false;30
that.timeout = true;31
that.app.modal.open('error');32
that.app.state.client = [];33
that.app.update();34
});35
this.socket.on('load', function(data) {36
let buffer = new Uint8Array(data);37
let image = that.app.paint.g.getImageData(0, 0, that.app.node.canvas.width, that.app.node.canvas.height);38
let array = image.data;39
for(let i = 0; i < buffer.length / 4; i++) {40
let p = i * 4;41
let data = buffer.slice(i * 4, i * 4 + 4);42
if(data[3] === 0) {43
data = [255, 255, 255, 255];44
}45
array[p] = data[0];46
array[p + 1] = data[1];47
array[p + 2] = data[2];48
array[p + 3] = data[3];49
}50
that.app.paint.g.putImageData(image, 0, 0);51
that.app.modal.close();52
});53
this.socket.on('data', function(data) {54
that.app.paint.draw(data);55
});56
this.socket.on('clear', function(data) {57
that.app.paint.g.fillStyle = 'white';58
that.app.paint.g.fillRect(0, 0, window.innerWidth, window.innerHeight);59
});60
this.socket.on('background', function(data) {61
that.app.paint.g.fillStyle = data;62
that.app.paint.g.fillRect(0, 0, window.innerWidth, window.innerHeight);63
that.app.paint.g.fillStyle = 'black';64
});65
this.socket.on('cursor', function(data) {66
if(data.quit) {67
delete that.app.state.client[data.uuid];68
}69
else {70
that.app.state.client[data.uuid] = data;71
}72
that.app.update();73
});74
},75
reconnect: function() {76
this.socket.open();77
this.timeout = false;78
},79
_send: function(key, data) {80
if(this.socket) {81
this.socket.emit(key, data);82
}83
},84
data: function({ pos, last }) {85
this._send('data', {86
pos: pos,87
last: last,88
radius: this.app.state.radius,89
color: this.app.state.color90
});91
},92
cursor: function() {93
this._send('cursor', {94
cursor: this.app.state.cursor,95
color: this.app.state.color,96
radius: this.app.state.radius,97
name: this.app.state.name,98
uuid: this.app.state.uuid99
});100
},101
request: function() {102
if(this.app.socket.connected) {103
this.app.modal.load();104
}105
this._send('request', {106
x: 0,107
y: 0,108
width: window.innerWidth,109
height: window.innerHeight110
});111
},112
clear: function() {113
this._send('clear');114
}115
}