so://phie

outline

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

blob: d08405dacc37dae0cf720c40cfe87aeeea9b3a3f (2534 bytes)[doc][blame][raw]

1const express = require('express');

2const app = express();

3const http = require('http').createServer(app);

4const io = require('socket.io')(http);

5const { createCanvas } = require('canvas');

6

7let port = 80;

8let client = {};

9

10let width = 1920;

11let height = 1080;

12let canvas = createCanvas(width, height);

13let g = canvas.getContext('2d');

14g.fillStyle = 'white';

15g.fillRect(0, 0, width, height);

16

17app.use(express.static(__dirname + '/public'));

18

19const proxy = http.listen(port, function() {

20 console.log('Server started on port: ' + port);

21});

22

23io.path('/pipe');

24io.listen(proxy);

25

26io.on('connection', function(socket) {

27 client[socket] = {};

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

29 let { x, y, width, height } = data;

30 let image = g.getImageData(x, y, width, height);

31 socket.emit('load', new Uint8Array(image.data).buffer);

32 });

33 socket.on('data' , function(data) {

34 draw(data.pos, data.last, data.radius, data.color);

35 socket.broadcast.emit('data', data);

36 });

37 socket.on('clear' , function(data) {

38 g.clearRect(0, 0, width, height);

39 socket.broadcast.emit('clear', data);

40 });

41 socket.on('background' , function(data) {

42 g.fillStyle = data;

43 g.fillRect(0, 0, width, height);

44 g.fillStyle = 'black';

45 socket.broadcast.emit('background', data);

46 });

47 socket.on('cursor' , function(data) {

48 if(!client[socket]) {

49 client[socket] = {};

50 }

51 client[socket].uuid = data.uuid;

52 socket.broadcast.emit('cursor', data);

53 });

54 socket.on('disconnect', function() {

55 if(client[socket]) {

56 socket.broadcast.emit('cursor', {

57 quit: true,

58 uuid: client[socket].uuid

59 });

60 }

61 delete client[socket];

62 });

63});

64io.on('disconnect', function(socket) {

65 if(client[socket]) {

66 socket.broadcast.emit('cursor', {

67 quit: true,

68 uuid: client[socket].uuid

69 });

70 delete client[socket];

71 }

72});

73

74function draw(pos, last, radius, color) {

75 if(radius < 1) {

76 radius = 1;

77 env.radius = radius;

78 }

79 if(radius > 15) {

80 radius = 15;

81 env.radius = radius;

82 }

83 if(last) {

84 let diff = {

85 x: last.x - pos.x,

86 y: last.y - pos.y

87 };

88 let dist = Math.sqrt(Math.pow(diff.x, 2) + Math.pow(diff.y, 2), 2);

89 let length = dist / (radius / 2);

90 for(let i = 0; i < length; i++) {

91 let x = pos.x + (diff.x / length * i);

92 let y = pos.y + (diff.y / length * i);

93 g.fillStyle = color;

94 g.beginPath();

95 g.arc(x, y, 2 * radius, 0, 2 * Math.PI);

96 g.fill();

97 g.fillStyle = 'black';

98 }

99 }

100 else {

101 g.fillStyle = color;

102 g.beginPath();

103 g.arc(pos.x, pos.y, 2 * radius, 0, 2 * Math.PI);

104 g.fill();

105 g.fillStyle = 'black';

106 }

107}