so://phie

outline

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

blob: 7cbeaa014f790e2cf9f0903e1f61d88f14f0cde1 (15268 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 // temp

337 env.name = 'typable';

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

339 close();

340 connect();

341 sendCursor();

342});

343

344function resize() {

345 canvas.width = window.innerWidth;

346 canvas.height = window.innerHeight;

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

348 overlay.width = window.innerWidth;

349 overlay.height = window.innerHeight;

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

351 if(env.socket) {

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

353 x: 0,

354 y: 0,

355 width: canvas.width,

356 height: canvas.height

357 });

358 }

359}

360

361function connect() {

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

363 path: '/pipe'

364 });

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

366 x: 0,

367 y: 0,

368 width: canvas.width,

369 height: canvas.height

370 });

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

372 let buffer = new Uint8Array(data);

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

374 let array = image.data;

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

376 array[i] = buffer[i];

377 }

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

379 });

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

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

382 });

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

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

385 });

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

387 g.fillStyle = data;

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

389 g.fillStyle = 'black';

390 });

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

392 if(data.quit) {

393 delete env.client[data.uuid];

394 }

395 else {

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

397 setTimeout(function() {

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

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

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

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

402 delete env.client[data.uuid];

403 update();

404 }

405 }

406 }

407 }, 10 * 1000);

408 }

409 update();

410 });

411}

412

413function send({ pos, last }) {

414 if(env.socket) {

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

416 pos: pos,

417 last: last,

418 radius: env.radius,

419 color: env.color

420 });

421 }

422}

423

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

425 if(radius < 1) {

426 radius = 1;

427 env.radius = radius;

428 }

429 if(radius > 15) {

430 radius = 15;

431 env.radius = radius;

432 }

433 if(last) {

434 let diff = {

435 x: last.x - pos.x,

436 y: last.y - pos.y

437 };

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

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

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

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

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

443 g.fillStyle = color;

444 g.beginPath();

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

446 g.fill();

447 g.fillStyle = 'black';

448 }

449 }

450 else {

451 g.fillStyle = color;

452 g.beginPath();

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

454 g.fill();

455 g.fillStyle = 'black';

456 }

457}

458

459function sendCursor() {

460 if(env.socket) {

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

462 cursor: env.cursor,

463 color: env.color,

464 radius: env.radius,

465 name: env.name

466 });

467 }

468}

469

470function update() {

471 if(env.radius < 1) {

472 env.radius = 1;

473 }

474 if(env.radius > 15) {

475 env.radius = 15;

476 }

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

478 if(env.cursor) {

479 o.fillStyle = env.color;

480 o.lineWidth = 4;

481 o.beginPath();

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

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

484 o.stroke();

485 o.beginPath();

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

487 o.fill();

488 }

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

490 let client = env.client[key];

491 if(client.cursor) {

492 o.fillStyle = client.color;

493 o.lineWidth = 4;

494 o.beginPath();

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

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

497 o.stroke();

498 o.beginPath();

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

500 o.fill();

501 o.fillStyle = '#212121';

502 o.font = '14px Roboto';

503 o.textBaseline = 'top';

504 o.textAlign = 'left';

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

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

507 o.fillStyle = 'white';

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

509 }

510 }

511}

512

513function dark(color) {

514 color = color.substr(1);

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

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

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

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

519}

520

521function open(modal) {

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

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

524}

525

526function close() {

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

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

529 .forEach(function(item) {

530 item.style.display = 'none';

531 });

532}

533

534function uuidv4() {

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

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

537 return v.toString(16);

538 });

539}

540

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

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

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

544 g.beginPath();

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

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

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

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

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

550 g.fill();

551}