so://phie

outline

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

blob: 6ccfc1823bf8ae2f6334ef628939d5496d03b514 (18497 bytes)[doc][blame][raw]

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

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

3

4import locale from '../mod/locale.js';

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

6

7let node;

8let state = {

9 color: 54,

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

11 radius: 14,

12 index: 0,

13 device: null,

14 device_fallback: null,

15 device_list: {

16 mouse: false,

17 touch: false,

18 pen: false,

19 gamepad: false

20 },

21 pencil_list: [

22 'pen',

23 'marker',

24 'eraser'

25 ],

26 pencil: 'pen',

27 timestamp: null,

28 modal: null,

29 tab: null,

30 caption: null,

31 option: {

32 view_mode: false

33 },

34 history: [],

35 redo_history: []

36};

37

38export function init() {

39

40 locale.init('en');

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

42 canvas.init();

43

44 node = query({

45 wrapper: '.wrapper',

46 controls: '.controls',

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

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

49 header: '.header-list',

50 hotbar: '.hotbar',

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

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

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

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

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

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

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

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

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

60 notification: '.notification',

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

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

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

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

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

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

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

68 colors_modal: '.colors-modal',

69 mainbar: '.mainbar',

70 sidebar: '.sidebar',

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

72 content: '.modal .content',

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

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

75 });

76

77 fill_hotbar_list();

78 fill_modal_color_list();

79

80 bind_events(locale);

81

82 node.wrapper.animate([

83 { opacity: 1 },

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

85 ], {

86 delay: 100,

87 duration: 500,

88 fill: 'both'

89 });

90 node.hotbar.animate([

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

92 { opacity: 1 }

93 ], {

94 delay: 600,

95 easing: 'ease-out',

96 duration: 250,

97 fill: 'both'

98 });

99}

100

101function bind_events(locale) {

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

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

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

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

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

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

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

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

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

111 if(event.ctrlKey) {

112 prevent(event);

113 }

114 }, { passive: false });

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

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

117 event.preventDefault();

118 }

119 }, { passive: false });

120

121 for(let item of node.language_list) {

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

123 let code = item.dataset.code;

124 locale.change(code);

125 update_language_list();

126 });

127 }

128 for(let item of node.device_list) {

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

130 let code = item.dataset.code;

131 if(state.device_list[code]) {

132 state.device = code;

133 update_device_list();

134 }

135 });

136 }

137 for(let item of node.pencil_list) {

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

139 state.pencil = item.dataset.code;

140 update_pencil_list();

141 state.modal = null;

142 update_modal_list();

143 });

144 }

145 for(let item of node.modal_event_list) {

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

147 let code = item.dataset.code;

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

149 if(match) {

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

151 switch(match[1]) {

152 case 'toggle':

153 if(code) {

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

155 update_modal_list();

156 }

157 break;

158 case 'open':

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

160 state.modal = code;

161 update_modal_list();

162 }

163 break;

164 case 'close':

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

166 state.modal = null;

167 update_modal_list();

168 }

169 break;

170 default:

171 break;

172 }

173 }

174 }

175 });

176 }

177 for(let item of node.tab_event_list) {

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

179 let code = item.dataset.code;

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

181 if(match) {

182 switch(match[1]) {

183 case 'open':

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

185 state.tab = code;

186 update_tab_list();

187 }

188 break;

189 case 'close':

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

191 state.tab = null;

192 update_tab_list();

193 }

194 break;

195 default:

196 break;

197 }

198 }

199 });

200 }

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

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

203 state.point = {

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

205 y: parseInt(window.innerHeight / 2)

206 };

207 canvas.draw_cursor(state);

208 });

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

210 canvas.clear(state);

211 update_undo_and_redo();

212 show_notification('notification.clear');

213 state.modal = null;

214 update_modal_list();

215 });

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

217 if(!document.fullscreenElement) {

218 document.documentElement.requestFullscreen();

219 }

220 else if(document.exitFullscreen) {

221 document.exitFullscreen();

222 }

223 });

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

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

226 state.option.view_mode = !state.option.view_mode;

227 update_view_mode_option();

228 });

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

230 canvas.undo(state);

231 update_undo_and_redo();

232 });

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

234 canvas.redo(state);

235 update_undo_and_redo();

236 });

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

238 state.option.view_mode = false;

239 update_view_mode_option();

240 });

241 for(let item of node.caption_list) {

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

243 state.caption = item.dataset.code;

244 update_caption_list();

245 });

246 }

247

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

249 // ...

250 });

251}

252

253function update_language_list() {

254 let lang = locale.get_current();

255 for(let item of node.language_list) {

256 let code = item.dataset.code;

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

258 }

259}

260

261function update_device_list() {

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

263 for(let item of node.device_list) {

264 let code = item.dataset.code;

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

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

267 }

268}

269

270function update_pencil_list() {

271 for(let item of node.pencil_list) {

272 let code = item.dataset.code;

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

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

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

276 }

277 }

278}

279

280function update_hotbar() {

281 for(let item of node.hotbar_list) {

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

283 }

284}

285

286function update_focus(bool) {

287 for(let item of node.hotbar_list) {

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

289 }

290 for(let item of node.tool_list) {

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

292 }

293 for(let item of node.action_list) {

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

295 }

296}

297

298function update_modal_list() {

299 for(let item of node.modal_list) {

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

301 }

302 for(let item of node.modal_event_list) {

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

304 }

305 state.tab = null;

306 update_tab_list();

307 state.caption = 'controls';

308 update_caption_list();

309 if(state.modal) {

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

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

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

313 if(modal) {

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

315 modal.animate([

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

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

318 ], {

319 duration: 100,

320 fill: 'both'

321 });

322 }

323 if(button) {

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

325 }

326 }

327}

328

329function update_tab_list() {

330 for(let item of node.modal_list) {

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

332 if(main) {

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

334 main.style.marginLeft = '0';

335 }

336 }

337 for(let item of node.tab_list) {

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

339 }

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

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

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

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

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

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

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

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

348 }

349 }

350}

351

352function update_fullscreen_option() {

353 let fullscreen = document.fullscreenElement;

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

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

356}

357

358function update_view_mode_option() {

359 let view_mode = state.option.view_mode;

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

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

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

363 if(view_mode) {

364 state.modal = null;

365 state.tab = null;

366 update_modal_list();

367 canvas.on_release(null, state);

368 canvas.clear_cursor();

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

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

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

372 node.hotbar.animate([

373 { opacity: 1 },

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

375 ], {

376 easing: 'ease-out',

377 duration: 250,

378 fill: 'both'

379 });

380 node.mainbar.animate([

381 { opacity: 1, marginTop: 0 },

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

383 ], {

384 easing: 'ease-out',

385 duration: 250,

386 fill: 'both'

387 });

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

389 node.sidebar.animate([

390 { opacity: 0 },

391 { opacity: 1 }

392 ], {

393 delay: 250,

394 easing: 'ease-out',

395 duration: 250,

396 fill: 'both'

397 });

398 }

399 else {

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

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

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

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

404 node.hotbar.animate([

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

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

407 ], {

408 easing: 'ease-out',

409 duration: 250,

410 fill: 'both'

411 });

412 node.mainbar.animate([

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

414 { opacity: 1, marginTop: 0 }

415 ], {

416 easing: 'ease-out',

417 duration: 250,

418 fill: 'both'

419 });

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

421 }

422}

423

424function update_undo_and_redo() {

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

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

427}

428

429function update_caption_list() {

430 for(let item of node.caption_list) {

431 let code = item.dataset.code;

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

433 }

434 for(let item of node.content_list) {

435 let code = item.dataset.code;

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

437 }

438}

439

440function on_pointerdown(event) {

441 // detect touch

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

443 state.device_list.touch = true;

444 if(state.device) {

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

446 }

447 else {

448 state.device = 'touch';

449 state.device_fallback = 'touch';

450 }

451 update_device_list();

452 }

453 // detect pen

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

455 state.device_list.pen = true;

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

457 if(!state.device) {

458 state.device = 'pen';

459 state.device_fallback = 'pen';

460 }

461 update_device_list();

462 }

463 // drawing

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

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

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

467 update_focus(false);

468 canvas.on_press(event, state);

469 update_undo_and_redo();

470 state.point = {

471 x: event.layerX,

472 y: event.layerY

473 };

474 canvas.draw_cursor(state);

475 }

476 }

477 }

478}

479

480function on_pointermove(event) {

481 // detect mouse

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

483 state.device_list.mouse = true;

484 if(state.device) {

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

486 }

487 else {

488 state.device = 'mouse';

489 state.device_fallback = 'mouse';

490 }

491 update_device_list();

492 }

493 // drawing

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

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

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

497 canvas.on_move(event, state);

498 }

499 state.point = {

500 x: event.layerX,

501 y: event.layerY

502 };

503 }

504 canvas.draw_cursor(state);

505 }

506}

507

508function on_pointerup(event) {

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

510 canvas.on_release(event, state);

511 update_undo_and_redo();

512 update_focus(true);

513 }

514}

515

516function on_pointerout(event) {

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

518 canvas.on_release(event, state);

519 update_undo_and_redo();

520 state.point = null;

521 canvas.draw_cursor(state);

522 }

523}

524

525function on_keydown(event) {

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

527 if(state.tab) {

528 state.tab = null;

529 update_tab_list();

530 return;

531 }

532 if(state.modal) {

533 state.modal = null;

534 update_modal_list();

535 return;

536 }

537 }

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

539 if(!state.option.view_mode) {

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

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

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

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

544 canvas.on_release(event, state);

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

546 state.index = i;

547 canvas.draw_cursor(state);

548 update_hotbar();

549 }

550 }

551 }

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

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

554 update_modal_list();

555 }

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

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

558 update_modal_list();

559 }

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

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

562 update_modal_list();

563 }

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

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

566 update_modal_list();

567 }

568 /*

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

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

571 update_modal_list();

572 }

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

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

575 update_modal_list();

576 }

577 */

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

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

580 update_modal_list();

581 }

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

583 canvas.on_release(event, state);

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

585 canvas.draw_cursor(state);

586 update_pencil_list();

587 }

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

589 canvas.on_release(event, state);

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

591 canvas.draw_cursor(state);

592 update_pencil_list();

593 }

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

595 canvas.on_release(event, state);

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

597 canvas.draw_cursor(state);

598 update_pencil_list();

599 }

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

601 if(!document.fullscreenElement) {

602 document.documentElement.requestFullscreen();

603 }

604 else if(document.exitFullscreen) {

605 document.exitFullscreen();

606 }

607 }

608 }

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

610 state.option.view_mode = !state.option.view_mode;

611 update_view_mode_option();

612 }

613 }

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

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

616 canvas.undo(state);

617 update_undo_and_redo();

618 }

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

620 canvas.redo(state);

621 update_undo_and_redo();

622 }

623 }

624}

625

626function on_wheel(event) {

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

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

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

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

631 canvas.on_release(event, state);

632 state.radius += y;

633 node.scale_input.value = state.radius;

634 canvas.draw_cursor(state);

635 }

636 }

637 }

638}

639

640function on_fullscreenchange(event) {

641 update_fullscreen_option();

642}

643

644function fill_hotbar_list() {

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

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

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

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

649 canvas.on_release(event, state);

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

651 state.index = i;

652 canvas.draw_cursor(state);

653 update_hotbar();

654 });

655 if(state.index == i) {

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

657 }

658 }

659 }

660}

661

662function fill_modal_color_list() {

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

664 if(COLORS.length > i) {

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

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

667 canvas.on_release(event, state);

668 state.color = i;

669 state.modal = null;

670 update_modal_list();

671 canvas.draw_cursor(state);

672 state.index = null;

673 update_hotbar();

674 });

675 }

676 }

677}

678

679function show_notification(message) {

680 let code = uuid();

681 state.timestamp = code;

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

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

684 node.notification.animate([

685 { opacity: 0 },

686 { opacity: 1 }

687 ], {

688 duration: 200,

689 fill: 'both'

690 });

691 setTimeout(function() {

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

693 node.notification.animate([

694 { opacity: 1 },

695 { opacity: 0 }

696 ], {

697 duration: 200,

698 fill: 'both'

699 });

700 }

701 }, 1000);

702}