so://phie

outline

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

blob: d64f86c351d79a01be49e750b5c3b26ad4c32e74 (19393 bytes)[doc][blame][raw]

1import { COLORS, PRIORITY_MODAL } from './constant.js';

2import { uuid, query, prevent } from './util.js';

3

4// addons

5import canvas from './addon/canvas.js';

6import locale from './addon/locale.js';

7import ripple from './addon/ripple.js';

8// import gamepad from './addon/gamepad.js';

9

10let node;

11let state = {

12 color: 54,

13 color_list: [54, 2, 17, 27, 37, 12],

14 radius: 14,

15 index: 0,

16 device: null,

17 device_fallback: null,

18 device_list: {

19 mouse: false,

20 touch: false,

21 pen: false,

22 gamepad: false

23 },

24 pencil_list: [

25 'pen',

26 'marker',

27 'eraser'

28 ],

29 pencil: 'pen',

30 timestamp: null,

31 modal: null,

32 tab: null,

33 caption: null,

34 option: {

35 view_mode: false

36 },

37 history: [],

38 redo_history: []

39};

40

41window.addEventListener('load', init);

42

43function init() {

44

45 console.log('%cOutline', 'font-size: 24px; font-weight: 600;');

46 console.log('%cDraw with friends together.\n', 'font-size: 14px;');

47 console.log('Found a bug? Tell us:', 'https://github.com/typable/outline/issues/new/choose');

48

49 canvas.init();

50 locale.init('en');

51 locale.load('./asset/lang', ['en', 'de'], function() {

52 if('localStorage' in window) {

53 let user_lang = localStorage.getItem('outline.user.lang');

54 if(user_lang) {

55 locale.change(user_lang);

56 update_language_list();

57 }

58 }

59 });

60 ripple.init();

61

62 node = query({

63 wrapper: '.wrapper',

64 controls: '.controls',

65 modal_list: { query: '.modal', all: true },

66 tab_list: { query: '.tab', all: true },

67 header: '.header-list',

68 hotbar: '.hotbar',

69 hotbar_list: { query: '.hotbar .color', all: true },

70 modal_color_list: { query: '.colors-modal .color', all: true },

71 language_list: { query: '.more-modal .language-tab .item[data-event="change.language"]', all: true },

72 device_list: { query: '.more-modal .device-tab .item[data-event="change.device"]', all: true },

73 pencil_list: { query: '.pencil-modal .item[data-event="change.pencil"]', all: true },

74 modal_event_list: { query: '[data-event*=".modal"]', all: true },

75 tab_event_list: { query: '[data-event*=".tab"]', all: true },

76 tool_list: { query: '.tool', all: true },

77 action_list: { query: '.action', all: true },

78 notification: '.notification',

79 scale_input: 'input.scale[type="range"]',

80 clear: 'button.btn-apply[data-event="clear"]',

81 pencil_tool: '.tool-item[data-code="pencil"]',

82 option_event_fullscreen: '[data-event="toggle.fullscreen"]',

83 option_event_view_mode: '[data-event="toggle.view-mode"]',

84 undo_tool: '[data-lang="action.undo"]',

85 redo_tool: '[data-lang="action.redo"]',

86 colors_modal: '.colors-modal',

87 mainbar: '.mainbar',

88 sidebar: '.sidebar',

89 edit_mode: '[data-event="close.view-mode"]',

90 content: '.modal .content',

91 caption_list: { query: '[data-event="open.content"]' , all: true },

92 content_list: { query: '.content-item' , all: true }

93 });

94

95 fill_hotbar_list();

96 fill_modal_color_list();

97

98 bind_events();

99

100 node.wrapper.animate([

101 { opacity: 1 },

102 { opacity: 0, pointerEvents: 'none' }

103 ], {

104 delay: 100,

105 duration: 500,

106 fill: 'both'

107 });

108 node.hotbar.animate([

109 { opacity: 0, bottom: '-10px' },

110 { opacity: 1 }

111 ], {

112 delay: 600,

113 easing: 'ease-out',

114 duration: 250,

115 fill: 'both'

116 });

117}

118

119function bind_events() {

120 window.onerror = function(error, url, line) {

121 fetch('https://server.typable.dev/log', {

122 method: 'POST',

123 headers: {

124 'Accept': 'application/json',

125 'Content-Type': 'application/json',

126 },

127 body: JSON.stringify({

128 date: new Date(),

129 error: {

130 message: error,

131 file: url,

132 line: line

133 }

134 })

135 });

136 }

137 document.addEventListener('dragstart', prevent);

138 document.addEventListener('contextmenu', prevent);

139 document.addEventListener('pointerdown', on_pointerdown);

140 document.addEventListener('pointermove', on_pointermove);

141 document.addEventListener('pointerup', on_pointerup);

142 document.addEventListener('pointerout', on_pointerout);

143 document.addEventListener('keydown', on_keydown);

144 document.addEventListener('wheel', on_wheel);

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

146 if(event.ctrlKey) {

147 prevent(event);

148 }

149 }, { passive: false });

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

151 if(event.target !== node.scale_input && event.target !== node.colors_modal && !node.modal_color_list.includes(event.target) && event.target !== node.content) {

152 event.preventDefault();

153 }

154 }, { passive: false });

155

156 for(let item of node.language_list) {

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

158 let code = item.dataset.code;

159 locale.change(code);

160 update_language_list();

161 });

162 }

163 for(let item of node.device_list) {

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

165 let code = item.dataset.code;

166 if(state.device_list[code]) {

167 state.device = code;

168 update_device_list();

169 }

170 });

171 }

172 for(let item of node.pencil_list) {

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

174 state.pencil = item.dataset.code;

175 update_pencil_list();

176 state.modal = null;

177 update_modal_list();

178 });

179 }

180 for(let item of node.modal_event_list) {

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

182 let code = item.dataset.code;

183 let match = /^(toggle|open|close).modal$/.exec(item.dataset.event);

184 if(match) {

185 if(!item.classList.contains('inactive')) {

186 switch(match[1]) {

187 case 'toggle':

188 if(code) {

189 state.modal = state.modal !== code ? code : null;

190 update_modal_list();

191 }

192 break;

193 case 'open':

194 if(code && state.modal !== code) {

195 state.modal = code;

196 update_modal_list();

197 }

198 break;

199 case 'close':

200 if(state.modal !== null) {

201 state.modal = null;

202 update_modal_list();

203 }

204 break;

205 default:

206 break;

207 }

208 }

209 }

210 });

211 }

212 for(let item of node.tab_event_list) {

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

214 let code = item.dataset.code;

215 let match = /^(open|close).tab/.exec(item.dataset.event);

216 if(match) {

217 switch(match[1]) {

218 case 'open':

219 if(code && state.tab !== code) {

220 state.tab = code;

221 update_tab_list();

222 }

223 break;

224 case 'close':

225 if(state.tab !== null) {

226 state.tab = null;

227 update_tab_list();

228 }

229 break;

230 default:

231 break;

232 }

233 }

234 });

235 }

236 node.scale_input.addEventListener('input', function(event) {

237 state.radius = parseInt(node.scale_input.value);

238 state.point = {

239 x: parseInt(window.innerWidth / 2),

240 y: parseInt(window.innerHeight / 2)

241 };

242 canvas.draw_cursor(state);

243 });

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

245 canvas.clear(state);

246 update_undo_and_redo();

247 show_notification('notification.clear');

248 state.modal = null;

249 update_modal_list();

250 });

251 node.option_event_fullscreen.addEventListener('click', function(event) {

252 if(!document.fullscreenElement) {

253 document.documentElement.requestFullscreen();

254 }

255 else if(document.exitFullscreen) {

256 document.exitFullscreen();

257 }

258 });

259 window.addEventListener('fullscreenchange', on_fullscreenchange);

260 node.option_event_view_mode.addEventListener('click', function(event) {

261 state.option.view_mode = !state.option.view_mode;

262 update_view_mode_option();

263 });

264 node.undo_tool.addEventListener('click', function(event) {

265 canvas.undo(state);

266 update_undo_and_redo();

267 });

268 node.redo_tool.addEventListener('click', function(event) {

269 canvas.redo(state);

270 update_undo_and_redo();

271 });

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

273 state.option.view_mode = false;

274 update_view_mode_option();

275 });

276 for(let item of node.caption_list) {

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

278 state.caption = item.dataset.code;

279 update_caption_list();

280 });

281 }

282

283 window.addEventListener('beforeunload', function(event) {

284 // ...

285 });

286}

287

288function update_language_list() {

289 let lang = locale.get_current();

290 for(let item of node.language_list) {

291 let code = item.dataset.code;

292 item.classList[code === lang ? 'add' : 'remove']('active');

293 }

294}

295

296function update_device_list() {

297 canvas.get_canvas().style.cursor = state.device === 'mouse' ? 'none' : '';

298 for(let item of node.device_list) {

299 let code = item.dataset.code;

300 item.classList[code === state.device ? 'add' : 'remove']('active');

301 item.classList[state.device_list[code] ? 'remove' : 'add']('inactive');

302 }

303}

304

305function update_pencil_list() {

306 for(let item of node.pencil_list) {

307 let code = item.dataset.code;

308 item.classList[code === state.pencil ? 'add' : 'remove']('active');

309 if(code === state.pencil) {

310 node.pencil_tool.querySelector('i.ico').innerHTML = item.querySelector('i.ico').innerHTML;

311 }

312 }

313}

314

315function update_hotbar() {

316 for(let item of node.hotbar_list) {

317 item.classList[node.hotbar_list[state.index] === item ? 'add' : 'remove']('active');

318 }

319}

320

321function update_focus(bool) {

322 for(let item of node.hotbar_list) {

323 item.style.pointerEvents = bool ? '' : 'none';

324 }

325 for(let item of node.tool_list) {

326 item.style.pointerEvents = bool ? '' : 'none';

327 }

328 for(let item of node.action_list) {

329 item.style.pointerEvents = bool ? '' : 'none';

330 }

331}

332

333function update_modal_list() {

334 for(let item of node.modal_list) {

335 item.classList.add('hidden');

336 }

337 for(let item of node.modal_event_list) {

338 item.classList.remove('active');

339 }

340 state.tab = null;

341 update_tab_list();

342 state.caption = 'controls';

343 update_caption_list();

344 if(state.modal) {

345 canvas.on_release(event, state, true);

346 let modal = document.querySelector(`.modal.${state.modal}-modal`);

347 let button = document.querySelector(`[data-event*=".modal"][data-code="${state.modal}"]`);

348 if(modal) {

349 modal.classList.remove('hidden');

350 modal.animate([

351 { opacity: 0, marginTop: '-6px' },

352 { opacity: 1, marginTop: '0' }

353 ], {

354 duration: 100,

355 fill: 'both'

356 });

357 }

358 if(button) {

359 button.classList.add('active');

360 }

361 }

362}

363

364function update_tab_list() {

365 for(let item of node.modal_list) {

366 let main = item.querySelector('.main');

367 if(main) {

368 main.classList.remove('hidden');

369 main.style.marginLeft = '0';

370 }

371 }

372 for(let item of node.tab_list) {

373 item.classList.add('hidden');

374 }

375 if(state.modal && state.tab) {

376 let modal = document.querySelector(`.modal.${state.modal}-modal`);

377 let tab = document.querySelector(`.${state.tab}-tab`);

378 let main = modal.querySelector('.main');

379 if(modal && tab && main) {

380 tab.classList.remove('hidden');

381 main.style.marginLeft = '-100%';

382 main.classList.add('hidden');

383 }

384 }

385}

386

387function update_fullscreen_option() {

388 let fullscreen = document.fullscreenElement;

389 node.option_event_fullscreen.classList[fullscreen ? 'add' : 'remove']('active');

390 node.option_event_fullscreen.querySelector('.ico').textContent = fullscreen ? 'toggle_on' : 'toggle_off';

391}

392

393function update_view_mode_option() {

394 let view_mode = state.option.view_mode;

395 show_notification(`notification.view-mode.${view_mode ? 'on' : 'off'}`);

396 node.option_event_view_mode.classList[view_mode ? 'add' : 'remove']('active');

397 node.option_event_view_mode.querySelector('.ico').textContent = view_mode ? 'toggle_on' : 'toggle_off';

398 if(view_mode) {

399 state.modal = null;

400 state.tab = null;

401 update_modal_list();

402 canvas.on_release(null, state);

403 canvas.clear_cursor();

404 canvas.get_canvas().style.cursor = 'default';

405 node.hotbar.classList.add('inactive');

406 node.header.classList.add('inactive');

407 node.hotbar.animate([

408 { opacity: 1 },

409 { opacity: 0, bottom: '-10px', pointerEvents: 'none' }

410 ], {

411 easing: 'ease-out',

412 duration: 250,

413 fill: 'both'

414 });

415 node.mainbar.animate([

416 { opacity: 1, marginTop: 0 },

417 { opacity: 0, marginTop: '-60px' }

418 ], {

419 easing: 'ease-out',

420 duration: 250,

421 fill: 'both'

422 });

423 node.sidebar.classList.remove('hidden');

424 node.sidebar.animate([

425 { opacity: 0 },

426 { opacity: 1 }

427 ], {

428 delay: 250,

429 easing: 'ease-out',

430 duration: 250,

431 fill: 'both'

432 });

433 }

434 else {

435 canvas.on_release(null, state, true);

436 canvas.get_canvas().style.cursor = 'none';

437 node.hotbar.classList.remove('inactive');

438 node.header.classList.remove('inactive');

439 node.hotbar.animate([

440 { opacity: 0, bottom: '-10px' },

441 { opacity: 1, bottom: '30px' }

442 ], {

443 easing: 'ease-out',

444 duration: 250,

445 fill: 'both'

446 });

447 node.mainbar.animate([

448 { opacity: 0, marginTop: '-60px' },

449 { opacity: 1, marginTop: 0 }

450 ], {

451 easing: 'ease-out',

452 duration: 250,

453 fill: 'both'

454 });

455 node.sidebar.classList.add('hidden');

456 }

457}

458

459function update_undo_and_redo() {

460 node.undo_tool.classList[state.history.length > 0 ? 'remove' : 'add']('inactive');

461 node.redo_tool.classList[state.redo_history.length > 0 ? 'remove' : 'add']('inactive');

462}

463

464function update_caption_list() {

465 for(let item of node.caption_list) {

466 let code = item.dataset.code;

467 item.classList[code === state.caption ? 'add' : 'remove']('active');

468 }

469 for(let item of node.content_list) {

470 let code = item.dataset.code;

471 item.classList[code === state.caption ? 'remove' : 'add']('hidden');

472 }

473}

474

475function on_pointerdown(event) {

476 // detect touch

477 if(!state.device_list.touch && event.pointerType === 'touch') {

478 state.device_list.touch = true;

479 if(state.device) {

480 show_notification('notification.input-device.touch.add');

481 }

482 else {

483 state.device = 'touch';

484 state.device_fallback = 'touch';

485 }

486 update_device_list();

487 }

488 // detect pen

489 if(!state.device_list.pen && event.pointerType === 'pen') {

490 state.device_list.pen = true;

491 show_notification('notification.input-device.pencil.add');

492 if(!state.device) {

493 state.device = 'pen';

494 state.device_fallback = 'pen';

495 }

496 update_device_list();

497 }

498 // drawing

499 if(!state.option.view_mode && state.device === event.pointerType) {

500 if(event.target === canvas.get_canvas()) {

501 if(!PRIORITY_MODAL.includes(state.modal)) {

502 update_focus(false);

503 canvas.on_press(event, state);

504 update_undo_and_redo();

505 state.point = {

506 x: event.layerX,

507 y: event.layerY

508 };

509 canvas.draw_cursor(state);

510 }

511 }

512 }

513}

514

515function on_pointermove(event) {

516 // detect mouse

517 if(!state.device_list.mouse && event.pointerType === 'mouse') {

518 state.device_list.mouse = true;

519 if(state.device) {

520 show_notification('notification.input-device.mouse.add');

521 }

522 else {

523 state.device = 'mouse';

524 state.device_fallback = 'mouse';

525 }

526 update_device_list();

527 }

528 // drawing

529 if(!state.option.view_mode && state.device === event.pointerType) {

530 if(event.target === canvas.get_canvas()) {

531 if(!PRIORITY_MODAL.includes(state.modal)) {

532 canvas.on_move(event, state);

533 }

534 state.point = {

535 x: event.layerX,

536 y: event.layerY

537 };

538 }

539 canvas.draw_cursor(state);

540 }

541}

542

543function on_pointerup(event) {

544 if(!state.option.view_mode && state.device === event.pointerType) {

545 canvas.on_release(event, state);

546 update_undo_and_redo();

547 update_focus(true);

548 }

549}

550

551function on_pointerout(event) {

552 if(!state.option.view_mode && state.device === event.pointerType) {

553 canvas.on_release(event, state);

554 update_undo_and_redo();

555 state.point = null;

556 canvas.draw_cursor(state);

557 }

558}

559

560function on_keydown(event) {

561 if(event.code === 'Escape') {

562 if(state.tab) {

563 state.tab = null;

564 update_tab_list();

565 return;

566 }

567 if(state.modal) {

568 state.modal = null;

569 update_modal_list();

570 return;

571 }

572 }

573 if(!event.ctrlKey && !event.shiftKey && !event.altKey) {

574 if(!state.option.view_mode) {

575 if(event.code.startsWith('Digit')) {

576 let i = parseInt(event.code.substr(5)) - 1;

577 if(state.index !== i) {

578 if(i >= 0 && i < state.color_list.length) {

579 canvas.on_release(event, state);

580 state.color = state.color_list[i];

581 state.index = i;

582 canvas.draw_cursor(state);

583 update_hotbar();

584 }

585 }

586 }

587 if(event.code === 'KeyZ') {

588 state.modal = state.modal !== 'colors' ? 'colors' : null;

589 update_modal_list();

590 }

591 if(event.code === 'KeyX') {

592 state.modal = state.modal !== 'scale' ? 'scale' : null;

593 update_modal_list();

594 }

595 if(event.code === 'KeyC') {

596 state.modal = state.modal !== 'pencil' ? 'pencil' : null;

597 update_modal_list();

598 }

599 if(event.code === 'KeyV') {

600 state.modal = state.modal !== 'clear' ? 'clear' : null;

601 update_modal_list();

602 }

603 /*

604 if(event.code === 'KeyB') {

605 state.modal = state.modal !== 'multiplayer' ? 'multiplayer' : null;

606 update_modal_list();

607 }

608 if(event.code === 'KeyN') {

609 state.modal = state.modal !== 'account' ? 'account' : null;

610 update_modal_list();

611 }

612 */

613 if(event.code === 'KeyM') {

614 state.modal = state.modal !== 'more' ? 'more' : null;

615 update_modal_list();

616 }

617 if(event.code === 'KeyQ') {

618 canvas.on_release(event, state);

619 state.pencil = state.pencil_list[0];

620 canvas.draw_cursor(state);

621 update_pencil_list();

622 }

623 if(event.code === 'KeyW') {

624 canvas.on_release(event, state);

625 state.pencil = state.pencil_list[1];

626 canvas.draw_cursor(state);

627 update_pencil_list();

628 }

629 if(event.code === 'KeyE') {

630 canvas.on_release(event, state);

631 state.pencil = state.pencil_list[2];

632 canvas.draw_cursor(state);

633 update_pencil_list();

634 }

635 if(event.code === 'KeyF') {

636 if(!document.fullscreenElement) {

637 document.documentElement.requestFullscreen();

638 }

639 else if(document.exitFullscreen) {

640 document.exitFullscreen();

641 }

642 }

643 }

644 if(event.code === 'Space') {

645 state.option.view_mode = !state.option.view_mode;

646 update_view_mode_option();

647 }

648 }

649 if(event.ctrlKey && !event.shiftKey && !event.altKey) {

650 if(event.code === 'KeyY') {

651 canvas.undo(state);

652 update_undo_and_redo();

653 }

654 if(event.code === 'KeyZ') {

655 canvas.redo(state);

656 update_undo_and_redo();

657 }

658 }

659}

660

661function on_wheel(event) {

662 if(!state.option.view_mode && state.device === 'mouse') {

663 if(event.target === canvas.get_canvas()) {

664 let y = event.deltaY < 0 ? 1 : -1;

665 if(state.radius + y > 2 - 1 && state.radius + y < 50 + 1) {

666 canvas.on_release(event, state);

667 state.radius += y;

668 node.scale_input.value = state.radius;

669 canvas.draw_cursor(state);

670 }

671 }

672 }

673}

674

675function on_fullscreenchange(event) {

676 update_fullscreen_option();

677}

678

679function fill_hotbar_list() {

680 for(let [ i, item ] of Object.entries(node.hotbar_list)) {

681 if(state.color_list.length > i) {

682 item.style.background = COLORS[state.color_list[i]];

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

684 canvas.on_release(event, state);

685 state.color = state.color_list[i];

686 state.index = i;

687 canvas.draw_cursor(state);

688 update_hotbar();

689 });

690 if(state.index == i) {

691 item.classList.add('active');

692 }

693 }

694 }

695}

696

697function fill_modal_color_list() {

698 for(let [ i, item ] of Object.entries(node.modal_color_list)) {

699 if(COLORS.length > i) {

700 item.style.background = COLORS[i];

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

702 canvas.on_release(event, state);

703 state.color = i;

704 state.modal = null;

705 update_modal_list();

706 canvas.draw_cursor(state);

707 state.index = null;

708 update_hotbar();

709 });

710 }

711 }

712}

713

714function show_notification(message) {

715 let code = uuid();

716 state.timestamp = code;

717 let content = node.notification.querySelector('.content');

718 content.textContent = locale.get()[message];

719 node.notification.animate([

720 { opacity: 0 },

721 { opacity: 1 }

722 ], {

723 duration: 200,

724 fill: 'both'

725 });

726 setTimeout(function() {

727 if(state.timestamp === code) {

728 node.notification.animate([

729 { opacity: 1 },

730 { opacity: 0 }

731 ], {

732 duration: 200,

733 fill: 'both'

734 });

735 }

736 }, 1000);

737}