A web-based drawing app
git clone git://soophie.de/outline
blob: 710f2e9fb3c88256495bf5948fcae92ae576292c (2185 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
});33
this.socket.on('data', function(data) {34
that.app.paint.draw(data);35
});36
this.socket.on('clear', function(data) {37
that.app.paint.g.clearRect(0, 0, window.innerWidth, window.innerHeight);38
});39
this.socket.on('background', function(data) {40
that.app.paint.g.fillStyle = data;41
that.app.paint.g.fillRect(0, 0, window.innerWidth, window.innerHeight);42
that.app.paint.g.fillStyle = 'black';43
});44
this.socket.on('cursor', function(data) {45
if(data.quit) {46
delete that.app.state.client[data.uuid];47
}48
else {49
that.app.state.client[data.uuid] = data;50
}51
that.app.update();52
});53
},54
reconnect: function() {55
this.socket.open();56
},57
_send: function(key, data) {58
if(this.socket) {59
this.socket.emit(key, data);60
}61
},62
data: function({ pos, last }) {63
this._send('data', {64
pos: pos,65
last: last,66
radius: this.app.state.radius,67
color: this.app.state.color68
});69
},70
cursor: function() {71
this._send('cursor', {72
cursor: this.app.state.cursor,73
color: this.app.state.color,74
radius: this.app.state.radius,75
name: this.app.state.name,76
uuid: this.app.state.uuid77
});78
},79
request: function() {80
this._send('request', {81
x: 0,82
y: 0,83
width: window.innerWidth,84
height: window.innerHeight85
});86
},87
clear: function() {88
this._send('clear');89
}90
}