so://phie

outline

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

blob: 9e80e8ac9fef85dd227f6e389ac2ca383c137bf1 (1985 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(window.location.origin, {

10 path: '/pipe'

11 });

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

13 that.connected = true;

14 that.request();

15 });

16 this.socket.on('disconnect', function() {

17 that.connected = false;

18 });

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

20 let buffer = new Uint8Array(data);

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

22 let array = image.data;

23 for(let i = 0; i < buffer.length; i++) {

24 array[i] = buffer[i];

25 }

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

27 });

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

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

30 });

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

32 that.app.paint.g.clearRect(0, 0, window.innerWidth, window.innerHeight);

33 });

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

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

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

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

38 });

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

40 if(data.quit) {

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

42 }

43 else {

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

45 }

46 that.app.update();

47 });

48 },

49 _send: function(key, data) {

50 if(this.socket) {

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

52 }

53 },

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

55 this._send('data', {

56 pos: pos,

57 last: last,

58 radius: this.app.state.radius,

59 color: this.app.state.color

60 });

61 },

62 cursor: function() {

63 this._send('cursor', {

64 cursor: this.app.state.cursor,

65 color: this.app.state.color,

66 radius: this.app.state.radius,

67 name: this.app.state.name,

68 uuid: this.app.state.uuid

69 });

70 },

71 request: function() {

72 this._send('request', {

73 x: 0,

74 y: 0,

75 width: window.innerWidth,

76 height: window.innerHeight

77 });

78 },

79 clear: function() {

80 this._send('clear');

81 }

82}