so://phie

outline

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

blob: 49f3492b75b08647ce7cd53c3dbf48fb47b0eb29 (18815 bytes)[doc][blame][raw]

1import { COLORS, PRIORITY_MODAL, LOCALES } 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

9let node;

10let state = {

11 color: 54,

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

13 radius: 14,

14 index: 0,

15 device: null,

16 device_fallback: null,

17 device_list: {

18 mouse: false,

19 touch: false,

20 pen: false,

21 gamepad: false

22 },

23 pencil_list: [

24 'pen',

25 'marker',

26 'eraser'

27 ],

28 pencil: 'pen',

29 timestamp: null,

30 modal: null,

31 tab: null,

32 caption: null,

33 option: {

34 view_mode: false

35 },

36 history: [],

37 redo_history: []

38};

39

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

41

42function init() {

43

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

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

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

47

48 canvas.init();

49 locale.init('en');

50 locale.load('./asset/lang', LOCALES);

51 ripple.init();

52

53 node = query({

54 wrapper: '.wrapper',

55 controls: '.controls',

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

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

58 header: '.header-list',

59 hotbar: '.hotbar',

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

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

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

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

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

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

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

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

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

69 notification: '.notification',

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

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

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

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

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

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

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

77 colors_modal: '.colors-modal',

78 mainbar: '.mainbar',

79 sidebar: '.sidebar',

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

81 content: '.modal .content',

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

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

84 });

85

86 fill_hotbar_list();

87 fill_modal_color_list();

88

89 bind_events();

90

91 node.wrapper.animate([

92 { opacity: 1 },

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

94 ], {

95 delay: 100,

96 duration: 500,

97 fill: 'both'

98 });

99 node.hotbar.animate([

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

101 { opacity: 1 }

102 ], {

103 delay: 600,

104 easing: 'ease-out',

105 duration: 250,

106 fill: 'both'

107 });

108}

109

110function bind_events() {

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

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

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

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

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

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

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

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

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

120 if(event.ctrlKey) {

121 prevent(event);

122 }

123 }, { passive: false });

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

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

126 event.preventDefault();

127 }

128 }, { passive: false });

129

130 for(let item of node.language_list) {

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

132 let code = item.dataset.code;

133 locale.change(code);

134 update_language_list();

135 });

136 }

137 for(let item of node.device_list) {

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

139 let code = item.dataset.code;

140 if(state.device_list[code]) {

141 state.device = code;

142 update_device_list();

143 }

144 });

145 }

146 for(let item of node.pencil_list) {

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

148 state.pencil = item.dataset.code;

149 update_pencil_list();

150 state.modal = null;

151 update_modal_list();

152 });

153 }

154 for(let item of node.modal_event_list) {

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

156 let code = item.dataset.code;

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

158 if(match) {

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

160 switch(match[1]) {

161 case 'toggle':

162 if(code) {

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

164 update_modal_list();

165 }

166 break;

167 case 'open':

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

169 state.modal = code;

170 update_modal_list();

171 }

172 break;

173 case 'close':

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

175 state.modal = null;

176 update_modal_list();

177 }

178 break;

179 default:

180 break;

181 }

182 }

183 }

184 });

185 }

186 for(let item of node.tab_event_list) {

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

188 let code = item.dataset.code;

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

190 if(match) {

191 switch(match[1]) {

192 case 'open':

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

194 state.tab = code;

195 update_tab_list();

196 }

197 break;

198 case 'close':

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

200 state.tab = null;

201 update_tab_list();

202 }

203 break;

204 default:

205 break;

206 }

207 }

208 });

209 }

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

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

212 state.point = {

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

214 y: parseInt(window.innerHeight / 2)

215 };

216 canvas.draw_cursor(state);

217 });

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

219 canvas.clear(state);

220 update_undo_and_redo();

221 show_notification('notification.clear');

222 state.modal = null;

223 update_modal_list();

224 });

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

226 if(!document.fullscreenElement) {

227 document.documentElement.requestFullscreen();

228 }

229 else if(document.exitFullscreen) {

230 document.exitFullscreen();

231 }

232 });

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

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

235 state.option.view_mode = !state.option.view_mode;

236 update_view_mode_option();

237 });

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

239 canvas.undo(state);

240 update_undo_and_redo();

241 });

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

243 canvas.redo(state);

244 update_undo_and_redo();

245 });

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

247 state.option.view_mode = false;

248 update_view_mode_option();

249 });

250 for(let item of node.caption_list) {

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

252 state.caption = item.dataset.code;

253 update_caption_list();

254 });

255 }

256

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

258 // ...

259 });

260}

261

262function update_language_list() {

263 let lang = locale.get_current();

264 for(let item of node.language_list) {

265 let code = item.dataset.code;

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

267 }

268}

269

270function update_device_list() {

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

272 for(let item of node.device_list) {

273 let code = item.dataset.code;

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

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

276 }

277}

278

279function update_pencil_list() {

280 for(let item of node.pencil_list) {

281 let code = item.dataset.code;

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

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

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

285 }

286 }

287}

288

289function update_hotbar() {

290 for(let item of node.hotbar_list) {

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

292 }

293}

294

295function update_focus(bool) {

296 for(let item of node.hotbar_list) {

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

298 }

299 for(let item of node.tool_list) {

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

301 }

302 for(let item of node.action_list) {

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

304 }

305}

306

307function update_modal_list() {

308 for(let item of node.modal_list) {

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

310 }

311 for(let item of node.modal_event_list) {

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

313 }

314 state.tab = null;

315 update_tab_list();

316 state.caption = 'controls';

317 update_caption_list();

318 if(state.modal) {

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

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

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

322 if(modal) {

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

324 modal.animate([

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

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

327 ], {

328 duration: 100,

329 fill: 'both'

330 });

331 }

332 if(button) {

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

334 }

335 }

336}

337

338function update_tab_list() {

339 for(let item of node.modal_list) {

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

341 if(main) {

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

343 main.style.marginLeft = '0';

344 }

345 }

346 for(let item of node.tab_list) {

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

348 }

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

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

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

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

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

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

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

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

357 }

358 }

359}

360

361function update_fullscreen_option() {

362 let fullscreen = document.fullscreenElement;

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

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

365}

366

367function update_view_mode_option() {

368 let view_mode = state.option.view_mode;

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

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

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

372 if(view_mode) {

373 state.modal = null;

374 state.tab = null;

375 update_modal_list();

376 canvas.on_release(null, state);

377 canvas.clear_cursor();

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

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

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

381 node.hotbar.animate([

382 { opacity: 1 },

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

384 ], {

385 easing: 'ease-out',

386 duration: 250,

387 fill: 'both'

388 });

389 node.mainbar.animate([

390 { opacity: 1, marginTop: 0 },

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

392 ], {

393 easing: 'ease-out',

394 duration: 250,

395 fill: 'both'

396 });

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

398 node.sidebar.animate([

399 { opacity: 0 },

400 { opacity: 1 }

401 ], {

402 delay: 250,

403 easing: 'ease-out',

404 duration: 250,

405 fill: 'both'

406 });

407 }

408 else {

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

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

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

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

413 node.hotbar.animate([

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

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

416 ], {

417 easing: 'ease-out',

418 duration: 250,

419 fill: 'both'

420 });

421 node.mainbar.animate([

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

423 { opacity: 1, marginTop: 0 }

424 ], {

425 easing: 'ease-out',

426 duration: 250,

427 fill: 'both'

428 });

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

430 }

431}

432

433function update_undo_and_redo() {

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

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

436}

437

438function update_caption_list() {

439 for(let item of node.caption_list) {

440 let code = item.dataset.code;

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

442 }

443 for(let item of node.content_list) {

444 let code = item.dataset.code;

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

446 }

447}

448

449function on_pointerdown(event) {

450 // detect touch

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

452 state.device_list.touch = true;

453 if(state.device) {

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

455 }

456 else {

457 state.device = 'touch';

458 state.device_fallback = 'touch';

459 }

460 update_device_list();

461 }

462 // detect pen

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

464 state.device_list.pen = true;

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

466 if(!state.device) {

467 state.device = 'pen';

468 state.device_fallback = 'pen';

469 }

470 update_device_list();

471 }

472 // drawing

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

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

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

476 update_focus(false);

477 canvas.on_press(event, state);

478 update_undo_and_redo();

479 state.point = {

480 x: event.layerX,

481 y: event.layerY

482 };

483 canvas.draw_cursor(state);

484 }

485 }

486 }

487}

488

489function on_pointermove(event) {

490 // detect mouse

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

492 state.device_list.mouse = true;

493 if(state.device) {

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

495 }

496 else {

497 state.device = 'mouse';

498 state.device_fallback = 'mouse';

499 }

500 update_device_list();

501 }

502 // drawing

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

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

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

506 canvas.on_move(event, state);

507 }

508 state.point = {

509 x: event.layerX,

510 y: event.layerY

511 };

512 }

513 canvas.draw_cursor(state);

514 }

515}

516

517function on_pointerup(event) {

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

519 canvas.on_release(event, state);

520 update_undo_and_redo();

521 update_focus(true);

522 }

523}

524

525function on_pointerout(event) {

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

527 canvas.on_release(event, state);

528 update_undo_and_redo();

529 state.point = null;

530 canvas.draw_cursor(state);

531 }

532}

533

534function on_keydown(event) {

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

536 if(state.tab) {

537 state.tab = null;

538 update_tab_list();

539 return;

540 }

541 if(state.modal) {

542 state.modal = null;

543 update_modal_list();

544 return;

545 }

546 }

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

548 if(!state.option.view_mode) {

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

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

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

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

553 canvas.on_release(event, state);

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

555 state.index = i;

556 canvas.draw_cursor(state);

557 update_hotbar();

558 }

559 }

560 }

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

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

563 update_modal_list();

564 }

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

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

567 update_modal_list();

568 }

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

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

571 update_modal_list();

572 }

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

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

575 update_modal_list();

576 }

577 /*

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

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

580 update_modal_list();

581 }

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

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

584 update_modal_list();

585 }

586 */

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

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

589 update_modal_list();

590 }

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

592 canvas.on_release(event, state);

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

594 canvas.draw_cursor(state);

595 update_pencil_list();

596 }

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

598 canvas.on_release(event, state);

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

600 canvas.draw_cursor(state);

601 update_pencil_list();

602 }

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

604 canvas.on_release(event, state);

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

606 canvas.draw_cursor(state);

607 update_pencil_list();

608 }

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

610 if(!document.fullscreenElement) {

611 document.documentElement.requestFullscreen();

612 }

613 else if(document.exitFullscreen) {

614 document.exitFullscreen();

615 }

616 }

617 }

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

619 state.option.view_mode = !state.option.view_mode;

620 update_view_mode_option();

621 }

622 }

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

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

625 canvas.undo(state);

626 update_undo_and_redo();

627 }

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

629 canvas.redo(state);

630 update_undo_and_redo();

631 }

632 }

633}

634

635function on_wheel(event) {

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

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

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

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

640 canvas.on_release(event, state);

641 state.radius += y;

642 node.scale_input.value = state.radius;

643 canvas.draw_cursor(state);

644 }

645 }

646 }

647}

648

649function on_fullscreenchange(event) {

650 update_fullscreen_option();

651}

652

653function fill_hotbar_list() {

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

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

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

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

658 canvas.on_release(event, state);

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

660 state.index = i;

661 canvas.draw_cursor(state);

662 update_hotbar();

663 });

664 if(state.index == i) {

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

666 }

667 }

668 }

669}

670

671function fill_modal_color_list() {

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

673 if(COLORS.length > i) {

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

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

676 canvas.on_release(event, state);

677 state.color = i;

678 state.modal = null;

679 update_modal_list();

680 canvas.draw_cursor(state);

681 state.index = null;

682 update_hotbar();

683 });

684 }

685 }

686}

687

688function show_notification(message) {

689 let code = uuid();

690 state.timestamp = code;

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

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

693 node.notification.animate([

694 { opacity: 0 },

695 { opacity: 1 }

696 ], {

697 duration: 200,

698 fill: 'both'

699 });

700 setTimeout(function() {

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

702 node.notification.animate([

703 { opacity: 1 },

704 { opacity: 0 }

705 ], {

706 duration: 200,

707 fill: 'both'

708 });

709 }

710 }, 1000);

711}