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