A web-based drawing app
git clone git://soophie.de/outline
blob: 9a79c3f5b1ffc37832a7edfea73d861c24e2a94d (2276 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; i++) {29
array[i] = buffer[i];30
}31
that.app.paint.g.putImageData(image, 0, 0);32
that.app.modal.close();33
});34
this.socket.on('data', function(data) {35
that.app.paint.draw(data);36
});37
this.socket.on('clear', function(data) {38
that.app.paint.g.clearRect(0, 0, window.innerWidth, window.innerHeight);39
});40
this.socket.on('background', function(data) {41
that.app.paint.g.fillStyle = data;42
that.app.paint.g.fillRect(0, 0, window.innerWidth, window.innerHeight);43
that.app.paint.g.fillStyle = 'black';44
});45
this.socket.on('cursor', function(data) {46
if(data.quit) {47
delete that.app.state.client[data.uuid];48
}49
else {50
that.app.state.client[data.uuid] = data;51
}52
that.app.update();53
});54
},55
reconnect: function() {56
this.socket.open();57
},58
_send: function(key, data) {59
if(this.socket) {60
this.socket.emit(key, data);61
}62
},63
data: function({ pos, last }) {64
this._send('data', {65
pos: pos,66
last: last,67
radius: this.app.state.radius,68
color: this.app.state.color69
});70
},71
cursor: function() {72
this._send('cursor', {73
cursor: this.app.state.cursor,74
color: this.app.state.color,75
radius: this.app.state.radius,76
name: this.app.state.name,77
uuid: this.app.state.uuid78
});79
},80
request: function() {81
if(this.app.socket.connected) {82
this.app.modal.load();83
}84
this._send('request', {85
x: 0,86
y: 0,87
width: window.innerWidth,88
height: window.innerHeight89
});90
},91
clear: function() {92
this._send('clear');93
}94
}