so://phie

outline

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

blob: 0a8a41ae85de9053e858381568df66911f501b8b (14439 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 env.temp = null;

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

210 .forEach(function(_item) {

211 if(_item != item) {

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

213 }

214 });

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

216 });

217 });

218

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

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

221 open('clear');

222 });

223

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

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

226 open('background');

227 });

228

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

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

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

232 open('save');

233 });

234

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

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

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

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

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

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

241 open('scaling');

242 });

243

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

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

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

247 .forEach(function(item) {

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

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

250 }

251 });

252 open('color');

253 });

254

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

256 .forEach(function(item) {

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

258 close();

259 });

260 });

261

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

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

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

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

266 close();

267 });

268

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

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

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

272 g.fillStyle = env.color;

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

274 g.fillStyle = 'black';

275 close();

276 });

277

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

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

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

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

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

283 link.style.display = 'none';

284 link.download = file;

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

286 document.body.appendChild(link);

287 link.click();

288 close();

289 }

290 });

291

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

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

294 setTimeout(function() {

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

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

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

298 }, 10);

299 });

300

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

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

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

304 sendCursor();

305 update();

306 close();

307 });

308

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

310 .forEach(function(item) {

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

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

313 .forEach(function(_item) {

314 if(_item != item) {

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

316 }

317 });

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

319 env.color = item.dataset.color;

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

321 .forEach(function(item) {

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

323 });

324 close();

325 });

326 });

327

328 resize();

329 g.fillStyle = 'white';

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

331});

332

333function resize() {

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

335 canvas.width = window.innerWidth;

336 canvas.height = window.innerHeight;

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

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

339 overlay.width = window.innerWidth;

340 overlay.height = window.innerHeight;

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

342}

343

344function connect() {

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

346 path: '/pipe'

347 });

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

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

350 });

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

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

353 });

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

355 g.fillStyle = data;

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

357 g.fillStyle = 'black';

358 });

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

360 if(data.quit) {

361 delete env.client[data.uuid];

362 }

363 else {

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

365 setTimeout(function() {

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

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

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

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

370 delete env.client[data.uuid];

371 update();

372 }

373 }

374 }

375 }, 10 * 1000);

376 }

377 update();

378 });

379}

380

381function send({ pos, last }) {

382 if(env.socket) {

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

384 pos: pos,

385 last: last,

386 radius: env.radius,

387 color: env.color

388 });

389 }

390}

391

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

393 if(last) {

394 let diff = {

395 x: last.x - pos.x,

396 y: last.y - pos.y

397 };

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

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

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

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

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

403 g.fillStyle = color;

404 g.beginPath();

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

406 g.fill();

407 g.fillStyle = 'black';

408 }

409 }

410 else {

411 g.fillStyle = color;

412 g.beginPath();

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

414 g.fill();

415 g.fillStyle = 'black';

416 }

417}

418

419function sendCursor() {

420 if(env.socket) {

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

422 cursor: env.cursor,

423 color: env.color,

424 radius: env.radius,

425 name: env.name

426 });

427 }

428}

429

430function update() {

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

432 if(env.cursor) {

433 o.fillStyle = env.color;

434 o.lineWidth = 4;

435 o.beginPath();

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

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

438 o.stroke();

439 o.beginPath();

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

441 o.fill();

442 }

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

444 let client = env.client[key];

445 if(client.cursor) {

446 o.fillStyle = client.color;

447 o.lineWidth = 4;

448 o.beginPath();

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

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

451 o.stroke();

452 o.beginPath();

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

454 o.fill();

455 o.fillStyle = '#212121';

456 o.font = '14px Roboto';

457 o.textBaseline = 'top';

458 o.textAlign = 'left';

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

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

461 o.fillStyle = 'white';

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

463 }

464 }

465}

466

467function dark(color) {

468 color = color.substr(1);

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

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

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

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

473}

474

475function open(modal) {

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

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

478}

479

480function close() {

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

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

483 .forEach(function(item) {

484 item.style.display = 'none';

485 });

486}

487

488function uuidv4() {

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

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

491 return v.toString(16);

492 });

493}

494

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

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

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

498 g.beginPath();

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

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

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

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

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

504 g.fill();

505}