so://phie

outline

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

blob: 1659d31fb439d2fca54cf2615a9faed8fcdaec7e (2804 bytes)[doc][blame][raw]

1export default {

2 socket: null,

3 connected: false,

4 timeout: false,

5 init: function(app) {

6 this.app = app;

7 },

8 connect: function() {

9 let that = this;

10 this.socket = io('https://server.typable.dev', {

11 path: '/pipe',

12 reconnection: false

13 });

14 this.socket.on('connect', function() {

15 that.connected = true;

16 that.timeout = false;

17 that.request();

18 that.cursor();

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 });

26 this.socket.on('connect_error', function() {

27 that.connected = false;

28 that.timeout = true;

29 that.app.modal.open('error');

30 that.app.state.client = [];

31 });

32 this.socket.on('load', function(data) {

33 let buffer = new Uint8Array(data);

34 let image = that.app.paint.g.getImageData(0, 0, that.app.node.canvas.width, that.app.node.canvas.height);

35 let array = image.data;

36 for(let i = 0; i < buffer.length / 4; i++) {

37 let p = i * 4;

38 let data = buffer.slice(i * 4, i * 4 + 4);

39 if(data[3] === 0) {

40 data = [255, 255, 255, 255];

41 }

42 array[p] = data[0];

43 array[p + 1] = data[1];

44 array[p + 2] = data[2];

45 array[p + 3] = data[3];

46 }

47 that.app.paint.g.putImageData(image, 0, 0);

48 if(!that.app.state.current) {

49 that.app.modal.close();

50 }

51 });

52 this.socket.on('data', function(data) {

53 that.app.paint.draw(data);

54 });

55 this.socket.on('clear', function(data) {

56 that.app.paint.g.fillStyle = 'white';

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

58 });

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

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

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

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

63 });

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

65 if(data.quit) {

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

67 }

68 else {

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

70 }

71 });

72 },

73 reconnect: function() {

74 this.socket.open();

75 this.timeout = false;

76 },

77 send: function(key, data) {

78 if(this.socket) {

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

80 }

81 },

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

83 this.send('data', {

84 pos: pos,

85 last: last,

86 radius: this.app.state.radius,

87 color: this.app.state.color

88 });

89 },

90 cursor: function() {

91 this.send('cursor', {

92 cursor: this.app.state.cursor,

93 color: this.app.state.color,

94 radius: this.app.state.radius,

95 name: this.app.state.name,

96 uuid: this.app.state.uuid

97 });

98 },

99 request: function() {

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

101 if(!this.app.state.current) {

102 this.app.modal.load();

103 }

104 }

105 this.send('request', {

106 x: 0,

107 y: 0,

108 width: window.innerWidth,

109 height: window.innerHeight

110 });

111 },

112 clear: function() {

113 this.send('clear');

114 }

115}