so://phie

outline

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

blob: 2d44d0690416e8f0cba2f2a6789c4019b3ea9b59 (13710 bytes)[doc][blame][raw]

1let env = {

2 drag: false,

3 last: null,

4 color: '#000000',

5 radius: 3,

6 bucket: [

7 { name: 'Black', code: '#000000', active: true },

8 { name: 'White', code: '#FFFFFF' },

9 { name: 'Red', code: '#FF0000' },

10 { name: 'Blue', code: '#0000FF' },

11 { name: 'Green', code: '#00FF00' },

12 { name: 'Yellow', code: '#FFFF00' },

13 { name: 'Brown', code: '#A52A2A' },

14 { name: 'Grey', code: '#808080' },

15 { name: 'Purple', code: '#800080' },

16 { name: 'Bisque', code: '#FFE4C4' }

17 ],

18 colorlist: [

19 { name: 'Pink', code: '#FFC0CB' },

20 { name: 'Violet', code: '#EE82EE' },

21 { name: 'Magenta', code: '#FF00FF' },

22 { name: 'Red', code: '#FF0000' },

23 { name: 'Orange', code: '#D2691E' },

24 { name: 'Gold', code: '#FFD700' },

25 { name: 'Yellow', code: '#FFFF00' },

26 { name: 'Lime', code: '#00FF00' },

27 { name: 'Green', code: '#00FF00' },

28 { name: 'Teal', code: '#008080' },

29 { name: 'Cyan', code: '#00FFFF' },

30 { name: 'Aquamarine', code: '#7FFFD4' },

31 { name: 'Blue', code: '#0000FF' },

32 { name: 'Navy', code: '#000080' },

33 { name: 'Cornsilk', code: '#FFF8DC' },

34 { name: 'Bisque', code: '#FFE4C4' },

35 { name: 'Wheat', code: '#F5DEB3' },

36 { name: 'Tan', code: '#D2B48C' },

37 { name: 'Peru', code: '#CD853F' },

38 { name: 'Olive', code: '#808000' },

39 { name: 'Sinna', code: '#A0522D' },

40 { name: 'Brown', code: '#A52A2A' },

41 { name: 'Maroon', code: '#800000' },

42 { name: 'Silver', code: '#C0C0C0' },

43 { name: 'Black', code: '#000000' },

44 { name: 'White', code: '#FFFFFF' }

45 ],

46 socket: null,

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

48 client: {},

49 uuid: null,

50 name: null

51};

52

53let canvas;

54let overlay

55let g;

56let o;

57

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

59

60 open('join');

61

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

63 canvas.draggable = true;

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

65

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

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

68

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

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

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

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

73 });

74

75 env.colorlist.forEach(function(item) {

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

77 template.innerHTML = `<li class="bucket tooltip" data-title="${item.name}" data-color="${item.code}" style="background: ${item.code}"></li>`;

78 document.querySelector('.modal.modal-color ul.bucket-list').append(template.content.cloneNode(true));

79 });

80

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

82 event.preventDefault();

83 });

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

85 event.preventDefault();

86 });

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

88 if(env.drag) {

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

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

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

92 env.last = pos;

93 }

94 });

95 canvas.addEventListener('touchmove', function(event) {

96 if(env.drag) {

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

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

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

100 env.last = pos;

101 }

102 });

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

104 env.drag = true;

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

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

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

108 env.last = pos;

109 document.querySelector('.toolbar').style.pointerEvents = 'none';

110 sendCursor();

111 update();

112 });

113 canvas.addEventListener('touchstart', function(event) {

114 env.drag = true;

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

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

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

118 env.last = pos;

119 document.querySelector('.toolbar').style.pointerEvents = 'none';

120 sendCursor();

121 update();

122 });

123

124 document.querySelector('.action-join').addEventListener('click', function(event) {

125 env.name = document.querySelector('.action-uuid').value;

126 if(env.name.length >= 3 && env.name.length <= 20) {

127 env.uuid = env.name + '-' + uuidv4();

128 close();

129 connect();

130 sendCursor();

131 }

132 });

133

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

135 env.drag = false;

136 env.last = null;

137 document.querySelector('.toolbar').style.pointerEvents = '';

138 });

139 document.addEventListener('touchend', function(event) {

140 document.querySelector('.toolbar').style.pointerEvents = '';

141 env.drag = false;

142 env.last = null;

143 });

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

145 if(document.querySelector('.wrapper').style.display === 'none') {

146 if(event.deltaY < 0) {

147 env.radius += env.radius < 15 ? 1 : 0;

148 }

149 else {

150 env.radius -= env.radius > 1 ? 1 : 0;

151 }

152 sendCursor();

153 update();

154 }

155 });

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

157 if(document.querySelector('.wrapper').style.display === 'none') {

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

159 let index = event.keyCode - 49;

160 if(index == -1) {

161 index = 9;

162 }

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

164 .forEach(function(item, i) {

165 if(index == i) {

166 env.color = item.dataset.color;

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

168 }

169 else {

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

171 }

172 });

173 }

174 sendCursor();

175 update();

176 }

177 else {

178 if(document.querySelector('.modal.modal-join').style.display === 'none') {

179 if(event.keyCode == 27) {

180 close();

181 }

182 }

183 }

184 });

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

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

187 sendCursor();

188 update();

189 });

190 document.addEventListener('touchmove', function(event) {

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

192 sendCursor();

193 update();

194 });

195 document.addEventListener('mouseout', function(event) {

196 env.cursor = null;

197 sendCursor();

198 update();

199 });

200

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

202

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

204 .forEach(function(item) {

205 item.addEventListener('mousedown', function(event) {

206 env.color = item.dataset.color;

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

208 .forEach(function(_item) {

209 if(_item != item) {

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

211 }

212 });

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

214 });

215 });

216

217 document.querySelector('.actions button.action-clear')

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

219 open('clear');

220 });

221

222 document.querySelector('.actions button.action-background')

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

224 open('background');

225 });

226

227 document.querySelector('.actions button.action-save')

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

229 document.querySelector('input.action-file').value = `outline-image-${uuidv4()}`;

230 open('save');

231 });

232

233 document.querySelector('.actions button.action-scaling')

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

235 document.querySelector('.modal input.action-range').value = env.radius;

236 let circle = document.querySelector('.modal .circle');

237 circle.style.width = env.radius * 4 + 'px';

238 circle.style.height = env.radius * 4 + 'px';

239 open('scaling');

240 });

241

242 document.querySelector('.actions button.action-color')

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

244 open('color');

245 });

246

247 Array.from(document.querySelectorAll('button.action-cancel'))

248 .forEach(function(item) {

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

250 close();

251 });

252 });

253

254 document.querySelector('.modal button.action-clear')

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

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

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

258 close();

259 });

260

261 document.querySelector('.modal button.action-background')

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

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

264 g.fillStyle = env.color;

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

266 g.fillStyle = 'black';

267 close();

268 });

269

270 document.querySelector('.modal button.action-save')

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

272 let file = document.querySelector('.modal input.action-file').value;

273 if(file.length >= 3 && file.length <= 40) {

274 let link = document.createElement('a');

275 link.style.display = 'none';

276 link.download = file;

277 link.href = canvas.toDataURL('image/png');

278 document.body.appendChild(link);

279 link.click();

280 close();

281 }

282 });

283

284 document.querySelector('.modal input.action-range')

285 .addEventListener('input', function(event) {

286 setTimeout(function() {

287 let circle = document.querySelector('.modal .circle');

288 circle.style.width = event.target.value * 4 + 'px';

289 circle.style.height = event.target.value * 4 + 'px';

290 }, 10);

291 });

292

293 document.querySelector('.modal button.action-scaling')

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

295 env.radius = parseInt(document.querySelector('.modal input.action-range').value);

296 sendCursor();

297 update();

298 close();

299 });

300

301 document.querySelector('.modal button.action-default')

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

303 close();

304 });

305

306 document.querySelector('.modal button.action-color')

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

308 close();

309 });

310

311 resize();

312});

313

314function resize() {

315 let ctemp = g.getImageData(0, 0, canvas.width, canvas.height);

316 canvas.width = window.innerWidth;

317 canvas.height = window.innerHeight;

318 g.putImageData(ctemp, 0, 0);

319 let otemp = o.getImageData(0, 0, overlay.width, overlay.height);

320 overlay.width = window.innerWidth;

321 overlay.height = window.innerHeight;

322 o.putImageData(otemp, 0, 0);

323}

324

325function connect() {

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

327 path: '/pipe'

328 });

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

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

331 });

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

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

334 });

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

336 g.fillStyle = data;

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

338 g.fillStyle = 'black';

339 });

340 env.socket.on('cursor', function(data) {

341 if(data.quit) {

342 delete env.client[data.uuid];

343 }

344 else {

345 env.client[data.uuid] = data;

346 setTimeout(function() {

347 if(env.client[data.uuid]) {

348 let last = env.client[data.uuid];

349 if(last.cursor && data.cursor) {

350 if(last.cursor.x === data.cursor.x && last.cursor.y === data.cursor.y) {

351 delete env.client[data.uuid];

352 update();

353 }

354 }

355 }

356 }, 10 * 1000);

357 }

358 update();

359 });

360}

361

362function send({ pos, last }) {

363 if(env.socket) {

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

365 pos: pos,

366 last: last,

367 radius: env.radius,

368 color: env.color

369 });

370 }

371}

372

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

374 if(last) {

375 let diff = {

376 x: last.x - pos.x,

377 y: last.y - pos.y

378 };

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

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

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

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

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

384 g.fillStyle = color;

385 g.beginPath();

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

387 g.fill();

388 g.fillStyle = 'black';

389 }

390 }

391 else {

392 g.fillStyle = color;

393 g.beginPath();

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

395 g.fill();

396 g.fillStyle = 'black';

397 }

398}

399

400function sendCursor() {

401 if(env.socket) {

402 env.socket.emit('cursor', {

403 cursor: env.cursor,

404 color: env.color,

405 radius: env.radius,

406 name: env.name

407 });

408 }

409}

410

411function update() {

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

413 if(env.cursor) {

414 o.fillStyle = env.color;

415 o.lineWidth = 4;

416 o.beginPath();

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

418 o.strokeStyle = dark(env.color) ? 'black' : 'white';

419 o.stroke();

420 o.beginPath();

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

422 o.fill();

423 }

424 for(let key of Object.keys(env.client)) {

425 let client = env.client[key];

426 if(client.cursor) {

427 o.fillStyle = client.color;

428 o.lineWidth = 4;

429 o.beginPath();

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

431 o.strokeStyle = dark(client.color) ? 'black' : 'white';

432 o.stroke();

433 o.beginPath();

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

435 o.fill();

436 o.fillStyle = '#212121';

437 o.font = '14px Roboto';

438 o.textBaseline = 'top';

439 o.textAlign = 'left';

440 let text = o.measureText(client.name);

441 roundRect(o, client.cursor.x, client.cursor.y, text.width + 12, 22, 4);

442 o.fillStyle = 'white';

443 o.fillText(client.name, client.cursor.x + 6, client.cursor.y + 5);

444 }

445 }

446}

447

448function dark(color) {

449 color = color.substr(1);

450 let r = parseInt(color.substr(0, 2), 16);

451 let g = parseInt(color.substr(2, 4), 16);

452 let b = parseInt(color.substr(4, 6), 16);

453 return ((r * 0.299) + (g * 0.587) + (b * 0.114)) > 50;

454}

455

456function open(modal) {

457 document.querySelector('.wrapper').style.display = '';

458 document.querySelector(`.modal.modal-${modal}`).style.display = '';

459}

460

461function close() {

462 document.querySelector('.wrapper').style.display = 'none';

463 Array.from(document.querySelectorAll('.modal'))

464 .forEach(function(item) {

465 item.style.display = 'none';

466 });

467}

468

469function uuidv4() {

470 return 'xxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {

471 let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);

472 return v.toString(16);

473 });

474}

475

476function roundRect(g, x, y, w, h, r) {

477 if(w < 2 * r) r = w / 2;

478 if(h < 2 * r) r = h / 2;

479 g.beginPath();

480 g.moveTo(x + r, y);

481 g.arcTo(x + w, y, x + w, y + h, r);

482 g.arcTo(x + w, y + h, x, y + h, r);

483 g.arcTo(x, y + h, x, y, r);

484 g.arcTo(x, y, x + w, y, r);

485 g.fill();

486}