so://phie

outline

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

blob: 85168f4d0e4fc3580df3c3fe307d96cd7a67faf5 (14418 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: 'Grey', code: '#808080' },

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

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

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

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

14 ],

15 colorlist: [

16 { name: 'Salmon', code: '#FA8072' },

17 { name: 'Crimson', code: '#DC143C' },

18 { name: 'Firebrick', code: '#B22222' },

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

20 { name: 'Hotpink', code: '#FF69B4' },

21 { name: 'Coral', code: '#FF7F50' },

22 { name: 'Tomato', code: '#FF6347' },

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

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

25 { name: 'Khaki', code: '#F0E68C' },

26 { name: 'Lavender', code: '#E6E6FA' },

27 { name: 'Plum', code: '#DDA0DD' },

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

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

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

31 { name: 'Indigo', code: '#4B0082' },

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

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

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

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

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

37 { name: 'Turquoise', code: '#40E0D0' },

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

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

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

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

42 { name: 'Chocolate', code: '#D2691E' },

43 { name: 'Sienna', code: '#A0522D' },

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

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

46 ],

47 socket: null,

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

49 client: {},

50 uuid: null,

51 name: null

52};

53

54let canvas;

55let overlay

56let g;

57let o;

58

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

60

61 open('join');

62

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

64 canvas.draggable = true;

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

66

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

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

69

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

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

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

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

74 });

75

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

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

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

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

80 });

81

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

83 event.preventDefault();

84 });

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

86 event.preventDefault();

87 });

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

89 if(env.drag) {

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

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

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

93 env.last = pos;

94 }

95 });

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

97 if(env.drag) {

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

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

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

101 env.last = pos;

102 }

103 });

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

105 env.drag = true;

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

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

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

109 env.last = pos;

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

111 sendCursor();

112 update();

113 });

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

115 env.drag = true;

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

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

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

119 env.last = pos;

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

121 sendCursor();

122 update();

123 });

124

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

126 env.name = document.querySelector('.action-uuid').value.replace(/\s+/g, '');

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

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

129 close();

130 connect();

131 sendCursor();

132 }

133 });

134

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

136 env.drag = false;

137 env.last = null;

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

139 });

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

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

142 env.drag = false;

143 env.last = null;

144 });

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

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

147 if(event.deltaY < 0) {

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

149 }

150 else {

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

152 }

153 sendCursor();

154 update();

155 }

156 });

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

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

159 if(event.keyCode >= 48 && event.keyCode <= 55) {

160 let index = event.keyCode - 49;

161 if(index == -1) {

162 index = 9;

163 }

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

165 .forEach(function(item, i) {

166 if(index == i) {

167 env.color = item.dataset.color;

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

169 }

170 else {

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

172 }

173 });

174 }

175 sendCursor();

176 update();

177 }

178 else {

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

180 if(event.keyCode == 27) {

181 close();

182 }

183 }

184 }

185 });

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

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

188 sendCursor();

189 update();

190 });

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

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

193 sendCursor();

194 update();

195 });

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

197 env.cursor = null;

198 sendCursor();

199 update();

200 });

201

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

203

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

205 .forEach(function(item) {

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

207 env.color = item.dataset.color;

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

209 .forEach(function(_item) {

210 if(_item != item) {

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

212 }

213 });

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

215 });

216 });

217

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

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

220 open('clear');

221 });

222

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

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

225 open('background');

226 });

227

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

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

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

231 open('save');

232 });

233

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

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

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

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

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

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

240 open('scaling');

241 });

242

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

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

245 Array.from(document.querySelectorAll('.modal ul.bucket-list li.bucket'))

246 .forEach(function(item) {

247 if(item.dataset.color === env.color) {

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

249 }

250 });

251 open('color');

252 });

253

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

255 .forEach(function(item) {

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

257 close();

258 });

259 });

260

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

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

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

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

265 close();

266 });

267

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

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

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

271 g.fillStyle = env.color;

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

273 g.fillStyle = 'black';

274 close();

275 });

276

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

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

279 let file = document.querySelector('.modal input.action-file').value.replace(/\s+/g, '');

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

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

282 link.style.display = 'none';

283 link.download = file;

284 link.href = canvas.toDataURL('image/jpg');

285 document.body.appendChild(link);

286 link.click();

287 close();

288 }

289 });

290

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

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

293 setTimeout(function() {

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

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

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

297 }, 10);

298 });

299

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

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

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

303 sendCursor();

304 update();

305 close();

306 });

307

308 Array.from(document.querySelectorAll('.modal ul.bucket-list li.bucket'))

309 .forEach(function(item) {

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

311 Array.from(document.querySelectorAll('.modal ul.bucket-list li.bucket'))

312 .forEach(function(_item) {

313 if(_item != item) {

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

315 }

316 });

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

318 env.color = item.dataset.color;

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

320 .forEach(function(item) {

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

322 });

323 close();

324 });

325 });

326

327 resize();

328 g.fillStyle = 'white';

329 g.fillRect(0, 0, canvas.width, canvas.height);

330});

331

332function resize() {

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

334 canvas.width = window.innerWidth;

335 canvas.height = window.innerHeight;

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

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

338 overlay.width = window.innerWidth;

339 overlay.height = window.innerHeight;

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

341}

342

343function connect() {

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

345 path: '/pipe'

346 });

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

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

349 });

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

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

352 });

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

354 g.fillStyle = data;

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

356 g.fillStyle = 'black';

357 });

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

359 if(data.quit) {

360 delete env.client[data.uuid];

361 }

362 else {

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

364 setTimeout(function() {

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

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

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

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

369 delete env.client[data.uuid];

370 update();

371 }

372 }

373 }

374 }, 10 * 1000);

375 }

376 update();

377 });

378}

379

380function send({ pos, last }) {

381 if(env.socket) {

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

383 pos: pos,

384 last: last,

385 radius: env.radius,

386 color: env.color

387 });

388 }

389}

390

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

392 if(last) {

393 let diff = {

394 x: last.x - pos.x,

395 y: last.y - pos.y

396 };

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

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

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

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

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

402 g.fillStyle = color;

403 g.beginPath();

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

405 g.fill();

406 g.fillStyle = 'black';

407 }

408 }

409 else {

410 g.fillStyle = color;

411 g.beginPath();

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

413 g.fill();

414 g.fillStyle = 'black';

415 }

416}

417

418function sendCursor() {

419 if(env.socket) {

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

421 cursor: env.cursor,

422 color: env.color,

423 radius: env.radius,

424 name: env.name

425 });

426 }

427}

428

429function update() {

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

431 if(env.cursor) {

432 o.fillStyle = env.color;

433 o.lineWidth = 4;

434 o.beginPath();

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

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

437 o.stroke();

438 o.beginPath();

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

440 o.fill();

441 }

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

443 let client = env.client[key];

444 if(client.cursor) {

445 o.fillStyle = client.color;

446 o.lineWidth = 4;

447 o.beginPath();

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

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

450 o.stroke();

451 o.beginPath();

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

453 o.fill();

454 o.fillStyle = '#212121';

455 o.font = '14px Roboto';

456 o.textBaseline = 'top';

457 o.textAlign = 'left';

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

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

460 o.fillStyle = 'white';

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

462 }

463 }

464}

465

466function dark(color) {

467 color = color.substr(1);

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

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

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

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

472}

473

474function open(modal) {

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

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

477}

478

479function close() {

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

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

482 .forEach(function(item) {

483 item.style.display = 'none';

484 });

485}

486

487function uuidv4() {

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

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

490 return v.toString(16);

491 });

492}

493

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

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

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

497 g.beginPath();

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

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

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

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

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

503 g.fill();

504}