so://phie

outline

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

blob: aa14124318ea3723ff5d84a1cd1e794b9dbcf705 (5314 bytes)[doc][blame][raw]

1let env = {

2 drag: false,

3 last: null,

4 color: 'black',

5 radius: 3,

6 bucket: [

7 { name: 'Black', code: 'black', active: true },

8 { name: 'White', code: 'white' },

9 { name: 'Red', code: 'red' },

10 { name: 'Blue', code: 'blue' },

11 { name: 'Green', code: 'green' },

12 { name: 'Yellow', code: 'yellow' },

13 { name: 'Brown', code: 'brown' },

14 { name: 'Grey', code: 'grey' },

15 { name: 'Purple', code: 'purple' },

16 { name: 'Bisque', code: 'bisque' }

17 ],

18 socket: null,

19 cursor: { x: 0, y: 0 }

20};

21

22let canvas;

23let overlay

24let g;

25let o;

26

27window.addEventListener('load', function(event) {

28

29 canvas = document.querySelector('.canvas');

30 canvas.draggable = true;

31 overlay = document.querySelector('.overlay');

32

33 g = canvas.getContext('2d');

34 o = overlay.getContext('2d');

35

36 env.bucket.forEach(function(item) {

37 let template = document.createElement('template');

38 template.innerHTML = `<div class="bucket tooltip up${item.active ? ' focused' : ''}" data-title="${item.name}" style="background: ${item.code}"></div>`;

39 document.querySelector('.toolbar').append(template.content.cloneNode(true));

40 });

41

42 canvas.addEventListener('dragstart', function(event) {

43 event.preventDefault();

44 });

45 canvas.addEventListener('contextmenu', function(event) {

46 event.preventDefault();

47 });

48 canvas.addEventListener('mousemove', function(event) {

49 if(env.drag) {

50 let pos = { x: event.layerX, y: event.layerY };

51 send({ pos: pos, last: env.last });

52 draw(pos, env.last, env.radius, env.color);

53 env.last = pos;

54 }

55 });

56 canvas.addEventListener('mousedown', function(event) {

57 env.drag = true;

58 let pos = { x: event.layerX, y: event.layerY };

59 send({ pos: pos, last: env.last });

60 draw(pos, env.last, env.radius, env.color);

61 env.last = pos;

62 });

63

64 document.addEventListener('mouseup', function(event) {

65 env.drag = false;

66 env.last = null;

67 });

68 document.addEventListener('wheel', function(event) {

69 if(event.deltaY < 0) {

70 env.radius += env.radius < 30 ? 0.5 : 0;

71 }

72 else {

73 env.radius -= env.radius > 0.5 ? 0.5 : 0;

74 }

75 update();

76 });

77 document.addEventListener('keydown', function(event) {

78

79 if(event.keyCode >= 48 && event.keyCode <= 57) {

80 let index = event.keyCode - 49;

81 if(index == -1) {

82 index = 9;

83 }

84 Array.from(document.querySelectorAll('.bucket'))

85 .forEach(function(item, i) {

86 if(index == i) {

87 env.color = item.style.background;

88 item.classList.add('focused');

89 }

90 else {

91 item.classList.remove('focused');

92 }

93 });

94 }

95 });

96 document.addEventListener('mousemove', function(event) {

97 env.cursor = event.target === canvas ? { x: event.layerX, y: event.layerY } : null;

98 update();

99 });

100

101 window.addEventListener('resize', resize);

102

103 Array.from(document.querySelectorAll('div.bucket'))

104 .forEach(function(item) {

105 item.addEventListener('click', function(event) {

106 env.color = item.style.background;

107 Array.from(document.querySelectorAll('.bucket'))

108 .forEach(function(_item) {

109 if(_item != item) {

110 _item.classList.remove('focused');

111 }

112 });

113 item.classList.add('focused');

114 });

115 });

116

117 document.querySelector('button.action-clear')

118 .addEventListener('click', function(event) {

119 env.socket.emit('clear');

120 g.clearRect(0, 0, window.innerWidth, window.innerHeight);

121 });

122

123 document.querySelector('button.action-background')

124 .addEventListener('click', function(event) {

125 env.socket.emit('background', env.color);

126 g.fillStyle = env.color;

127 g.fillRect(0, 0, window.innerWidth, window.innerHeight);

128 g.fillStyle = 'black';

129 });

130

131 resize();

132 connect();

133});

134

135function resize() {

136 canvas.width = window.innerWidth;

137 canvas.height = window.innerHeight;

138 overlay.width = window.innerWidth;

139 overlay.height = window.innerHeight;

140}

141

142function connect() {

143 env.socket = io('http://' + window.location.host, {

144 path: '/pipe'

145 });

146 env.socket.on('data', function(data) {

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

148 });

149 env.socket.on('clear', function(data) {

150 g.clearRect(0, 0, window.innerWidth, window.innerHeight);

151 });

152 env.socket.on('background', function(data) {

153 g.fillStyle = data;

154 g.fillRect(0, 0, window.innerWidth, window.innerHeight);

155 g.fillStyle = 'black';

156 });

157}

158

159function send({ pos, last }) {

160 if(env.socket) {

161 env.socket.emit('data', {

162 pos: pos,

163 last: last,

164 radius: env.radius,

165 color: env.color

166 });

167 }

168}

169

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

171 if(last) {

172 let diff = {

173 x: last.x - pos.x,

174 y: last.y - pos.y

175 };

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

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

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

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

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

181 g.fillStyle = color;

182 g.beginPath();

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

184 g.fill();

185 g.fillStyle = 'black';

186 }

187 }

188 else {

189 g.fillStyle = color;

190 g.beginPath();

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

192 g.fill();

193 g.fillStyle = 'black';

194 }

195}

196

197function update() {

198 o.clearRect(0, 0, window.innerWidth, window.innerHeight);

199 if(env.cursor) {

200 o.fillStyle = env.color;

201 o.beginPath();

202 o.arc(env.cursor.x, env.cursor.y, 2 * env.radius, 0, 2 * Math.PI);

203 o.stroke();

204 o.beginPath();

205 o.arc(env.cursor.x, env.cursor.y, 2 * env.radius, 0, 2 * Math.PI);

206 o.fill();

207 }

208}