so://phie

outline

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

blob: 3e80162a6e63a441b790679669a093befa405bf7 (2726 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 that.app.modal.close();

49 });

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

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

52 });

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

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

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

56 });

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

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

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

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

61 });

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

63 if(data.quit) {

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

65 }

66 else {

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

68 }

69 });

70 },

71 reconnect: function() {

72 this.socket.open();

73 this.timeout = false;

74 },

75 send: function(key, data) {

76 if(this.socket) {

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

78 }

79 },

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

81 this.send('data', {

82 pos: pos,

83 last: last,

84 radius: this.app.state.radius,

85 color: this.app.state.color

86 });

87 },

88 cursor: function() {

89 this.send('cursor', {

90 cursor: this.app.state.cursor,

91 color: this.app.state.color,

92 radius: this.app.state.radius,

93 name: this.app.state.name,

94 uuid: this.app.state.uuid

95 });

96 },

97 request: function() {

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

99 this.app.modal.load();

100 }

101 this.send('request', {

102 x: 0,

103 y: 0,

104 width: window.innerWidth,

105 height: window.innerHeight

106 });

107 },

108 clear: function() {

109 this.send('clear');

110 }

111}