A web-based drawing app
git clone git://soophie.de/outline
blob: 3d593c30d58a798b98d4b273cc277cfc818ff0ed (3048 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
});20
this.socket.on('disconnect', function() {21
that.connected = false;22
that.timeout = true;23
that.app.modal.open('error');24
that.app.state.client = [];25
that.app.update();26
that._reconnectInterval();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
that._reconnectInterval();35
});36
this.socket.on('load', function(data) {37
let buffer = new Uint8Array(data);38
let image = that.app.paint.g.getImageData(0, 0, that.app.node.canvas.width, that.app.node.canvas.height);39
let array = image.data;40
for(let i = 0; i < buffer.length / 4; i++) {41
let p = i * 4;42
let data = buffer.slice(i * 4, i * 4 + 4);43
if(data[3] === 0) {44
data = [255, 255, 255, 255];45
}46
array[p] = data[0];47
array[p + 1] = data[1];48
array[p + 2] = data[2];49
array[p + 3] = data[3];50
}51
that.app.paint.g.putImageData(image, 0, 0);52
that.app.modal.close();53
});54
this.socket.on('data', function(data) {55
that.app.paint.draw(data);56
});57
this.socket.on('clear', function(data) {58
that.app.paint.g.fillStyle = 'white';59
that.app.paint.g.fillRect(0, 0, window.innerWidth, window.innerHeight);60
});61
this.socket.on('background', function(data) {62
that.app.paint.g.fillStyle = data;63
that.app.paint.g.fillRect(0, 0, window.innerWidth, window.innerHeight);64
that.app.paint.g.fillStyle = 'black';65
});66
this.socket.on('cursor', function(data) {67
if(data.quit) {68
delete that.app.state.client[data.uuid];69
}70
else {71
that.app.state.client[data.uuid] = data;72
}73
that.app.update();74
});75
},76
_reconnectInterval: function() {77
let that = this;78
this.interval = setInterval(function() {79
if(!that.timeout) {80
clearInterval(this.interval);81
}82
else {83
that.reconnect();84
}85
}, 30 * 1000);86
},87
reconnect: function() {88
this.socket.open();89
},90
_send: function(key, data) {91
if(this.socket) {92
this.socket.emit(key, data);93
}94
},95
data: function({ pos, last }) {96
this._send('data', {97
pos: pos,98
last: last,99
radius: this.app.state.radius,100
color: this.app.state.color101
});102
},103
cursor: function() {104
this._send('cursor', {105
cursor: this.app.state.cursor,106
color: this.app.state.color,107
radius: this.app.state.radius,108
name: this.app.state.name,109
uuid: this.app.state.uuid110
});111
},112
request: function() {113
if(this.app.socket.connected) {114
this.app.modal.load();115
}116
this._send('request', {117
x: 0,118
y: 0,119
width: window.innerWidth,120
height: window.innerHeight121
});122
},123
clear: function() {124
this._send('clear');125
}126
}