so://phie

outline

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

blob: 42a2a8842316a7a3e704a576a151481bc8fa9a25 (13653 bytes)[doc][blame][raw]

1import util from './mod/util.js';

2import modal from './mod/modal.js';

3

4let env = {

5 drag: false,

6 last: null,

7 color: '#000000',

8 radius: 3,

9 bucket: [

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

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

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

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

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

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

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

17 ],

18 colorlist: [

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

49 ],

50 socket: null,

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

52 client: {},

53 uuid: null,

54 name: null

55};

56

57let canvas;

58let overlay

59let g;

60let o;

61let delay;

62

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

64

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

66 canvas.draggable = true;

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

68

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

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

71

72 paint.init(g);

73 modal.init();

74

75 modal.open('join');

76 modal.get('join').action.uuid.element.focus();

77

78 document.querySelector('.action-uuid').addEventListener('keydown', function(event) {

79 if(event.keyCode === 13) {

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

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

82 env.uuid = env.name + '-' + util.uuid();

83 modal.close();

84 connect();

85 sendCursor();

86 }

87 }

88 });

89

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

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

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

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

94 });

95

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

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

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

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

100 });

101

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

103 event.preventDefault();

104 });

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

106 event.preventDefault();

107 });

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

109 if(env.drag) {

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

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

112 paint.draw(Object.assign({ pos: pos }, env));

113 env.last = pos;

114 }

115 });

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

117 if(env.drag) {

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

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

120 paint.draw(Object.assign({ pos: pos }, env));

121 env.last = pos;

122 event.preventDefault();

123 }

124 });

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

126 env.drag = true;

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

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

129 paint.draw(Object.assign({ pos: pos }, env));

130 env.last = pos;

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

132 sendCursor();

133 update();

134 });

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

136 env.drag = true;

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

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

139 paint.draw(Object.assign({ pos: pos }, env));

140 env.last = pos;

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

142 sendCursor();

143 update();

144 });

145

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

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

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

149 env.uuid = env.name + '-' + util.uuid();

150 modal.close();

151 connect();

152 sendCursor();

153 }

154 });

155

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

157 env.drag = false;

158 env.last = null;

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

160 });

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

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

163 env.drag = false;

164 env.last = null;

165 });

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

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

168 if(event.deltaY < 0) {

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

170 }

171 else {

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

173 }

174 sendCursor();

175 update();

176 }

177 });

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

179 if(!modal.opened) {

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

181 let index = event.keyCode - 49;

182 if(index == -1) {

183 index = 9;

184 }

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

186 .forEach(function(item, i) {

187 if(index == i) {

188 env.color = item.dataset.color;

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

190 }

191 else {

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

193 }

194 });

195 }

196 sendCursor();

197 update();

198 }

199 });

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

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

202 sendCursor();

203 update();

204 });

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

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

207 sendCursor();

208 update();

209 });

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

211 env.cursor = null;

212 sendCursor();

213 update();

214 });

215

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

217 clearTimeout(delay);

218 delay = setTimeout(resize, 100);

219 });

220

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

222 .forEach(function(item) {

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

224 env.color = item.dataset.color;

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

226 .forEach(function(_item) {

227 if(_item != item) {

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

229 }

230 });

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

232 });

233 });

234

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

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

237 modal.open('clear');

238 });

239

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

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

242 modal.open('background');

243 });

244

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

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

247 document.querySelector('input.action-file').value = `outline-image-${util.uuid()}`;

248 modal.open('save');

249 });

250

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

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

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

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

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

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

257 modal.open('scaling');

258 });

259

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

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

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

263 .forEach(function(item) {

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

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

266 }

267 });

268 modal.open('color');

269 });

270

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

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

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

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

275 modal.close();

276 });

277

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

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

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

281 g.fillStyle = env.color;

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

283 g.fillStyle = 'black';

284 modal.close();

285 });

286

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

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

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

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

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

292 link.style.display = 'none';

293 link.download = file;

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

295 document.body.appendChild(link);

296 link.click();

297 modal.close();

298 }

299 });

300

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

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

303 setTimeout(function() {

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

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

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

307 }, 10);

308 });

309

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

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

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

313 sendCursor();

314 update();

315 modal.close();

316 });

317

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

319 .forEach(function(item) {

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

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

322 .forEach(function(_item) {

323 if(_item != item) {

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

325 }

326 });

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

328 env.color = item.dataset.color;

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

330 .forEach(function(item) {

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

332 });

333 modal.close();

334 });

335 });

336

337 resize();

338 g.fillStyle = 'white';

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

340});

341

342function resize() {

343 canvas.width = window.innerWidth;

344 canvas.height = window.innerHeight;

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

346 overlay.width = window.innerWidth;

347 overlay.height = window.innerHeight;

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

349 if(env.socket) {

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

351 x: 0,

352 y: 0,

353 width: canvas.width,

354 height: canvas.height

355 });

356 }

357}

358

359function connect() {

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

361 path: '/pipe'

362 });

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

364 x: 0,

365 y: 0,

366 width: canvas.width,

367 height: canvas.height

368 });

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

370 let buffer = new Uint8Array(data);

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

372 let array = image.data;

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

374 array[i] = buffer[i];

375 }

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

377 });

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

379 paint.draw(data.pos, data.last, data.radius, data.color);

380 });

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

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

383 });

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

385 g.fillStyle = data;

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

387 g.fillStyle = 'black';

388 });

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

390 if(data.quit) {

391 delete env.client[data.uuid];

392 }

393 else {

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

395 setTimeout(function() {

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

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

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

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

400 delete env.client[data.uuid];

401 update();

402 }

403 }

404 }

405 }, 10 * 1000);

406 }

407 update();

408 });

409}

410

411function send({ pos, last }) {

412 if(env.socket) {

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

414 pos: pos,

415 last: last,

416 radius: env.radius,

417 color: env.color

418 });

419 }

420}

421

422function sendCursor() {

423 if(env.socket) {

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

425 cursor: env.cursor,

426 color: env.color,

427 radius: env.radius,

428 name: env.name

429 });

430 }

431}

432

433function update() {

434 if(env.radius < 1) {

435 env.radius = 1;

436 }

437 if(env.radius > 15) {

438 env.radius = 15;

439 }

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

441 if(env.cursor) {

442 o.fillStyle = env.color;

443 o.lineWidth = 4;

444 o.beginPath();

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

446 o.strokeStyle = util.luma(env.color) ? 'black' : 'white';

447 o.stroke();

448 o.beginPath();

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

450 o.fill();

451 }

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

453 let client = env.client[key];

454 if(client.cursor) {

455 o.fillStyle = client.color;

456 o.lineWidth = 4;

457 o.beginPath();

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

459 o.strokeStyle = util.luma(client.color) ? 'black' : 'white';

460 o.stroke();

461 o.beginPath();

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

463 o.fill();

464 o.fillStyle = '#212121';

465 o.font = '14px Roboto';

466 o.textBaseline = 'top';

467 o.textAlign = 'left';

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

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

470 o.fill();

471 o.fillStyle = 'white';

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

473 }

474 }

475}