so://phie

outline

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

blob: 433ff2af0103aba3eb8933547d3c43fca51f5f44 (19563 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 if('localStorage' in window) {

63 let user_cache = localStorage.getItem('outline.user.cache');

64 if(user_cache) {

65 canvas.set_data(user_cache);

66 }

67 }

68

69 node = query({

70 wrapper: '.wrapper',

71 controls: '.controls',

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

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

74 header: '.header-list',

75 hotbar: '.hotbar',

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

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

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

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

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

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

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

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

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

85 notification: '.notification',

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

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

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

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

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

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

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

93 colors_modal: '.colors-modal',

94 mainbar: '.mainbar',

95 sidebar: '.sidebar',

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

97 content: '.modal .content',

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

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

100 });

101

102 fill_hotbar_list();

103 fill_modal_color_list();

104

105 bind_events();

106

107 node.wrapper.animate([

108 { opacity: 1 },

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

110 ], {

111 delay: 100,

112 duration: 500,

113 fill: 'both'

114 });

115 node.hotbar.animate([

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

117 { opacity: 1 }

118 ], {

119 delay: 600,

120 easing: 'ease-out',

121 duration: 250,

122 fill: 'both'

123 });

124}

125

126function bind_events() {

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

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

129 method: 'POST',

130 headers: {

131 'Accept': 'application/json',

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

133 },

134 body: JSON.stringify({

135 date: new Date(),

136 error: {

137 message: error,

138 file: url,

139 line: line

140 }

141 })

142 });

143 }

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

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

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

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

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

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

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

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

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

153 if(event.ctrlKey) {

154 prevent(event);

155 }

156 }, { passive: false });

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

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

159 event.preventDefault();

160 }

161 }, { passive: false });

162

163 for(let item of node.language_list) {

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

165 let code = item.dataset.code;

166 locale.change(code);

167 update_language_list();

168 });

169 }

170 for(let item of node.device_list) {

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

172 let code = item.dataset.code;

173 if(state.device_list[code]) {

174 state.device = code;

175 update_device_list();

176 }

177 });

178 }

179 for(let item of node.pencil_list) {

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

181 state.pencil = item.dataset.code;

182 update_pencil_list();

183 state.modal = null;

184 update_modal_list();

185 });

186 }

187 for(let item of node.modal_event_list) {

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

189 let code = item.dataset.code;

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

191 if(match) {

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

193 switch(match[1]) {

194 case 'toggle':

195 if(code) {

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

197 update_modal_list();

198 }

199 break;

200 case 'open':

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

202 state.modal = code;

203 update_modal_list();

204 }

205 break;

206 case 'close':

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

208 state.modal = null;

209 update_modal_list();

210 }

211 break;

212 default:

213 break;

214 }

215 }

216 }

217 });

218 }

219 for(let item of node.tab_event_list) {

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

221 let code = item.dataset.code;

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

223 if(match) {

224 switch(match[1]) {

225 case 'open':

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

227 state.tab = code;

228 update_tab_list();

229 }

230 break;

231 case 'close':

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

233 state.tab = null;

234 update_tab_list();

235 }

236 break;

237 default:

238 break;

239 }

240 }

241 });

242 }

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

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

245 state.point = {

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

247 y: parseInt(window.innerHeight / 2)

248 };

249 canvas.draw_cursor(state);

250 });

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

252 canvas.clear(state);

253 update_undo_and_redo();

254 show_notification('notification.clear');

255 state.modal = null;

256 update_modal_list();

257 });

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

259 if(!document.fullscreenElement) {

260 document.documentElement.requestFullscreen();

261 }

262 else if(document.exitFullscreen) {

263 document.exitFullscreen();

264 }

265 });

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

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

268 state.option.view_mode = !state.option.view_mode;

269 update_view_mode_option();

270 });

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

272 canvas.undo(state);

273 update_undo_and_redo();

274 });

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

276 canvas.redo(state);

277 update_undo_and_redo();

278 });

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

280 state.option.view_mode = false;

281 update_view_mode_option();

282 });

283 for(let item of node.caption_list) {

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

285 state.caption = item.dataset.code;

286 update_caption_list();

287 });

288 }

289

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

291 localStorage.setItem('outline.user.cache', canvas.get_data());

292 });

293}

294

295function update_language_list() {

296 let lang = locale.get_current();

297 for(let item of node.language_list) {

298 let code = item.dataset.code;

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

300 }

301}

302

303function update_device_list() {

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

305 for(let item of node.device_list) {

306 let code = item.dataset.code;

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

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

309 }

310}

311

312function update_pencil_list() {

313 for(let item of node.pencil_list) {

314 let code = item.dataset.code;

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

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

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

318 }

319 }

320}

321

322function update_hotbar() {

323 for(let item of node.hotbar_list) {

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

325 }

326}

327

328function update_focus(bool) {

329 for(let item of node.hotbar_list) {

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

331 }

332 for(let item of node.tool_list) {

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

334 }

335 for(let item of node.action_list) {

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

337 }

338}

339

340function update_modal_list() {

341 for(let item of node.modal_list) {

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

343 }

344 for(let item of node.modal_event_list) {

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

346 }

347 state.tab = null;

348 update_tab_list();

349 state.caption = 'controls';

350 update_caption_list();

351 if(state.modal) {

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

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

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

355 if(modal) {

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

357 modal.animate([

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

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

360 ], {

361 duration: 100,

362 fill: 'both'

363 });

364 }

365 if(button) {

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

367 }

368 }

369}

370

371function update_tab_list() {

372 for(let item of node.modal_list) {

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

374 if(main) {

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

376 main.style.marginLeft = '0';

377 }

378 }

379 for(let item of node.tab_list) {

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

381 }

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

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

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

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

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

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

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

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

390 }

391 }

392}

393

394function update_fullscreen_option() {

395 let fullscreen = document.fullscreenElement;

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

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

398}

399

400function update_view_mode_option() {

401 let view_mode = state.option.view_mode;

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

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

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

405 if(view_mode) {

406 state.modal = null;

407 state.tab = null;

408 update_modal_list();

409 canvas.on_release(null, state);

410 canvas.clear_cursor();

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

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

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

414 node.hotbar.animate([

415 { opacity: 1 },

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

417 ], {

418 easing: 'ease-out',

419 duration: 250,

420 fill: 'both'

421 });

422 node.mainbar.animate([

423 { opacity: 1, marginTop: 0 },

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

425 ], {

426 easing: 'ease-out',

427 duration: 250,

428 fill: 'both'

429 });

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

431 node.sidebar.animate([

432 { opacity: 0 },

433 { opacity: 1 }

434 ], {

435 delay: 250,

436 easing: 'ease-out',

437 duration: 250,

438 fill: 'both'

439 });

440 }

441 else {

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

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

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

445 node.hotbar.animate([

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

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

448 ], {

449 easing: 'ease-out',

450 duration: 250,

451 fill: 'both'

452 });

453 node.mainbar.animate([

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

455 { opacity: 1, marginTop: 0 }

456 ], {

457 easing: 'ease-out',

458 duration: 250,

459 fill: 'both'

460 });

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

462 }

463}

464

465function update_undo_and_redo() {

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

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

468}

469

470function update_caption_list() {

471 for(let item of node.caption_list) {

472 let code = item.dataset.code;

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

474 }

475 for(let item of node.content_list) {

476 let code = item.dataset.code;

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

478 }

479}

480

481function on_pointerdown(event) {

482 // detect touch

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

484 state.device_list.touch = true;

485 if(state.device) {

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

487 }

488 else {

489 state.device = 'touch';

490 state.device_fallback = 'touch';

491 }

492 update_device_list();

493 }

494 // detect pen

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

496 state.device_list.pen = true;

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

498 if(!state.device) {

499 state.device = 'pen';

500 state.device_fallback = 'pen';

501 }

502 update_device_list();

503 }

504 // drawing

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

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

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

508 update_focus(false);

509 canvas.on_press(event, state);

510 update_undo_and_redo();

511 state.point = {

512 x: event.layerX,

513 y: event.layerY

514 };

515 canvas.draw_cursor(state);

516 }

517 }

518 }

519}

520

521function on_pointermove(event) {

522 // detect mouse

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

524 state.device_list.mouse = true;

525 if(state.device) {

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

527 }

528 else {

529 state.device = 'mouse';

530 state.device_fallback = 'mouse';

531 }

532 update_device_list();

533 }

534 // drawing

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

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

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

538 canvas.on_move(event, state);

539 }

540 state.point = {

541 x: event.layerX,

542 y: event.layerY

543 };

544 }

545 canvas.draw_cursor(state);

546 }

547}

548

549function on_pointerup(event) {

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

551 canvas.on_release(event, state);

552 update_undo_and_redo();

553 update_focus(true);

554 }

555}

556

557function on_pointerout(event) {

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

559 canvas.on_release(event, state);

560 update_undo_and_redo();

561 state.point = null;

562 canvas.draw_cursor(state);

563 }

564}

565

566function on_keydown(event) {

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

568 if(state.tab) {

569 state.tab = null;

570 update_tab_list();

571 return;

572 }

573 if(state.modal) {

574 state.modal = null;

575 update_modal_list();

576 return;

577 }

578 }

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

580 if(!state.option.view_mode) {

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

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

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

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

585 canvas.on_release(event, state);

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

587 state.index = i;

588 canvas.draw_cursor(state);

589 update_hotbar();

590 }

591 }

592 }

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

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

595 update_modal_list();

596 }

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

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

599 update_modal_list();

600 }

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

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

603 update_modal_list();

604 }

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

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

607 update_modal_list();

608 }

609 /*

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

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

612 update_modal_list();

613 }

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

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

616 update_modal_list();

617 }

618 */

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

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

621 update_modal_list();

622 }

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

624 canvas.on_release(event, state);

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

626 canvas.draw_cursor(state);

627 update_pencil_list();

628 }

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

630 canvas.on_release(event, state);

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

632 canvas.draw_cursor(state);

633 update_pencil_list();

634 }

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

636 canvas.on_release(event, state);

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

638 canvas.draw_cursor(state);

639 update_pencil_list();

640 }

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

642 if(!document.fullscreenElement) {

643 document.documentElement.requestFullscreen();

644 }

645 else if(document.exitFullscreen) {

646 document.exitFullscreen();

647 }

648 }

649 }

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

651 state.option.view_mode = !state.option.view_mode;

652 update_view_mode_option();

653 }

654 }

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

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

657 canvas.undo(state);

658 update_undo_and_redo();

659 }

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

661 canvas.redo(state);

662 update_undo_and_redo();

663 }

664 }

665}

666

667function on_wheel(event) {

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

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

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

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

672 canvas.on_release(event, state);

673 state.radius += y;

674 node.scale_input.value = state.radius;

675 canvas.draw_cursor(state);

676 }

677 }

678 }

679}

680

681function on_fullscreenchange(event) {

682 update_fullscreen_option();

683}

684

685function fill_hotbar_list() {

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

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

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

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

690 canvas.on_release(event, state);

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

692 state.index = i;

693 canvas.draw_cursor(state);

694 update_hotbar();

695 });

696 if(state.index == i) {

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

698 }

699 }

700 }

701}

702

703function fill_modal_color_list() {

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

705 if(COLORS.length > i) {

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

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

708 canvas.on_release(event, state);

709 state.color = i;

710 state.modal = null;

711 update_modal_list();

712 canvas.draw_cursor(state);

713 state.index = null;

714 update_hotbar();

715 });

716 }

717 }

718}

719

720function show_notification(message) {

721 let code = uuid();

722 state.timestamp = code;

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

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

725 node.notification.animate([

726 { opacity: 0 },

727 { opacity: 1 }

728 ], {

729 duration: 200,

730 fill: 'both'

731 });

732 setTimeout(function() {

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

734 node.notification.animate([

735 { opacity: 1 },

736 { opacity: 0 }

737 ], {

738 duration: 200,

739 fill: 'both'

740 });

741 }

742 }, 1000);

743}