so://phie

outline

A web-based drawing app
git clone git://soophie.de/outline

blob: 75607780fecffecb5400f2dc1614629825e13dbb (2492 bytes)[doc][blame][raw]

1export 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: false

12 });

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.clearRect(0, 0, window.innerWidth, window.innerHeight);

47 });

48 this.socket.on('background', function(data) {

49 that.app.paint.g.fillStyle = data;

50 that.app.paint.g.fillRect(0, 0, window.innerWidth, window.innerHeight);

51 that.app.paint.g.fillStyle = 'black';

52 });

53 this.socket.on('cursor', function(data) {

54 if(data.quit) {

55 delete that.app.state.client[data.uuid];

56 }

57 else {

58 that.app.state.client[data.uuid] = data;

59 }

60 that.app.update();

61 });

62 },

63 reconnect: function() {

64 this.socket.open();

65 },

66 _send: function(key, data) {

67 if(this.socket) {

68 this.socket.emit(key, data);

69 }

70 },

71 data: function({ pos, last }) {

72 this._send('data', {

73 pos: pos,

74 last: last,

75 radius: this.app.state.radius,

76 color: this.app.state.color

77 });

78 },

79 cursor: function() {

80 this._send('cursor', {

81 cursor: this.app.state.cursor,

82 color: this.app.state.color,

83 radius: this.app.state.radius,

84 name: this.app.state.name,

85 uuid: this.app.state.uuid

86 });

87 },

88 request: function() {

89 if(this.app.socket.connected) {

90 this.app.modal.load();

91 }

92 this._send('request', {

93 x: 0,

94 y: 0,

95 width: window.innerWidth,

96 height: window.innerHeight

97 });

98 },

99 clear: function() {

100 this._send('clear');

101 }

102}