so://phie

outline

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

blob: bf86c6a18809ecf5af7cd195aab411753aaa1f76 (15159 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;

58let delay;

59

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

61

62 open('join');

63

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

65 canvas.draggable = true;

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

67

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

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

70

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

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

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

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

75 });

76

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

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

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

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

81 });

82

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

84 event.preventDefault();

85 });

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

87 event.preventDefault();

88 });

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

90 if(env.drag) {

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

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

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

94 env.last = pos;

95 }

96 });

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

98 if(env.drag) {

99 let pos = { x: event.touches[0].pageX, y: event.touches[0].pageY };

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

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

102 env.last = pos;

103 event.preventDefault();

104 }

105 });

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

107 env.drag = true;

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

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

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

111 env.last = pos;

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

113 sendCursor();

114 update();

115 });

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

117 env.drag = true;

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

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

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

121 env.last = pos;

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

123 sendCursor();

124 update();

125 });

126

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

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

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

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

131 close();

132 connect();

133 sendCursor();

134 }

135 });

136

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

138 env.drag = false;

139 env.last = null;

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

141 });

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

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

144 env.drag = false;

145 env.last = null;

146 });

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

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

149 if(event.deltaY < 0) {

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

151 }

152 else {

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

154 }

155 sendCursor();

156 update();

157 }

158 });

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

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

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

162 let index = event.keyCode - 49;

163 if(index == -1) {

164 index = 9;

165 }

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

167 .forEach(function(item, i) {

168 if(index == i) {

169 env.color = item.dataset.color;

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

171 }

172 else {

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

174 }

175 });

176 }

177 sendCursor();

178 update();

179 }

180 else {

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

182 if(event.keyCode == 27) {

183 close();

184 }

185 }

186 }

187 });

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

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

190 sendCursor();

191 update();

192 });

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

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

195 sendCursor();

196 update();

197 });

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

199 env.cursor = null;

200 sendCursor();

201 update();

202 });

203

204 window.addEventListener('resize', function(event) {

205 clearTimeout(delay);

206 delay = setTimeout(resize, 100);

207 });

208

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

210 .forEach(function(item) {

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

212 env.color = item.dataset.color;

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

214 .forEach(function(_item) {

215 if(_item != item) {

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

217 }

218 });

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

220 });

221 });

222

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

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

225 open('clear');

226 });

227

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

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

230 open('background');

231 });

232

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

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

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

236 open('save');

237 });

238

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

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

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

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

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

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

245 open('scaling');

246 });

247

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

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

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

251 .forEach(function(item) {

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

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

254 }

255 });

256 open('color');

257 });

258

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

260 .forEach(function(item) {

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

262 close();

263 });

264 });

265

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

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

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

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

270 close();

271 });

272

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

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

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

276 g.fillStyle = env.color;

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

278 g.fillStyle = 'black';

279 close();

280 });

281

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

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

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

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

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

287 link.style.display = 'none';

288 link.download = file;

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

290 document.body.appendChild(link);

291 link.click();

292 close();

293 }

294 });

295

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

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

298 setTimeout(function() {

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

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

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

302 }, 10);

303 });

304

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

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

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

308 sendCursor();

309 update();

310 close();

311 });

312

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

314 .forEach(function(item) {

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

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

317 .forEach(function(_item) {

318 if(_item != item) {

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

320 }

321 });

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

323 env.color = item.dataset.color;

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

325 .forEach(function(item) {

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

327 });

328 close();

329 });

330 });

331

332 resize();

333 g.fillStyle = 'white';

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

335});

336

337function resize() {

338 canvas.width = window.innerWidth;

339 canvas.height = window.innerHeight;

340 let temp = o.getImageData(0, 0, overlay.width, overlay.height);

341 overlay.width = window.innerWidth;

342 overlay.height = window.innerHeight;

343 o.putImageData(temp, 0, 0);

344 if(env.socket) {

345 env.socket.emit('request', {

346 x: 0,

347 y: 0,

348 width: canvas.width,

349 height: canvas.height

350 });

351 }

352}

353

354function connect() {

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

356 path: '/pipe'

357 });

358 env.socket.emit('request', {

359 x: 0,

360 y: 0,

361 width: canvas.width,

362 height: canvas.height

363 });

364 env.socket.on('load', function(data) {

365 let buffer = new Uint8Array(data);

366 let image = g.getImageData(0, 0, canvas.width, canvas.height);

367 let array = image.data;

368 for(let i = 0; i < buffer.length; i++) {

369 array[i] = buffer[i];

370 }

371 g.putImageData(image, 0, 0);

372 });

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

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

375 });

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

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

378 });

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

380 g.fillStyle = data;

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

382 g.fillStyle = 'black';

383 });

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

385 if(data.quit) {

386 delete env.client[data.uuid];

387 }

388 else {

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

390 setTimeout(function() {

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

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

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

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

395 delete env.client[data.uuid];

396 update();

397 }

398 }

399 }

400 }, 10 * 1000);

401 }

402 update();

403 });

404}

405

406function send({ pos, last }) {

407 if(env.socket) {

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

409 pos: pos,

410 last: last,

411 radius: env.radius,

412 color: env.color

413 });

414 }

415}

416

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

418 if(radius < 1) {

419 radius = 1;

420 env.radius = radius;

421 }

422 if(radius > 15) {

423 radius = 15;

424 env.radius = radius;

425 }

426 if(last) {

427 let diff = {

428 x: last.x - pos.x,

429 y: last.y - pos.y

430 };

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

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

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

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

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

436 g.fillStyle = color;

437 g.beginPath();

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

439 g.fill();

440 g.fillStyle = 'black';

441 }

442 }

443 else {

444 g.fillStyle = color;

445 g.beginPath();

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

447 g.fill();

448 g.fillStyle = 'black';

449 }

450}

451

452function sendCursor() {

453 if(env.socket) {

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

455 cursor: env.cursor,

456 color: env.color,

457 radius: env.radius,

458 name: env.name

459 });

460 }

461}

462

463function update() {

464 if(env.radius < 1) {

465 env.radius = 1;

466 }

467 if(env.radius > 15) {

468 env.radius = 15;

469 }

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

471 if(env.cursor) {

472 o.fillStyle = env.color;

473 o.lineWidth = 4;

474 o.beginPath();

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

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

477 o.stroke();

478 o.beginPath();

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

480 o.fill();

481 }

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

483 let client = env.client[key];

484 if(client.cursor) {

485 o.fillStyle = client.color;

486 o.lineWidth = 4;

487 o.beginPath();

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

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

490 o.stroke();

491 o.beginPath();

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

493 o.fill();

494 o.fillStyle = '#212121';

495 o.font = '14px Roboto';

496 o.textBaseline = 'top';

497 o.textAlign = 'left';

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

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

500 o.fillStyle = 'white';

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

502 }

503 }

504}

505

506function dark(color) {

507 color = color.substr(1);

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

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

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

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

512}

513

514function open(modal) {

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

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

517}

518

519function close() {

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

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

522 .forEach(function(item) {

523 item.style.display = 'none';

524 });

525}

526

527function uuidv4() {

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

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

530 return v.toString(16);

531 });

532}

533

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

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

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

537 g.beginPath();

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

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

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

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

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

543 g.fill();

544}