so://phie

outline

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

blob: 17cbfcdb53e582c7a8d2b95959bc3ccb9f1bc076 (17773 bytes)[doc][blame][raw]

1import { COLORS } 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 option: {

34 view_mode: false

35 },

36 history: [],

37 redo_history: []

38};

39

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

41

42function init() {

43

44 canvas.init();

45 locale.init('en');

46 locale.load('./asset/lang', ['en', 'de']);

47 ripple.init();

48

49 node = query({

50 wrapper: '.wrapper',

51 controls: '.controls',

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

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

54 header: '.header-list',

55 hotbar: '.hotbar',

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

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

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

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

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

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

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

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

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

65 notification: '.notification',

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

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

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

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

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

71 undo_tool: '[data-lang="tool.undo"]',

72 redo_tool: '[data-lang="tool.redo"]'

73 });

74

75 fill_hotbar_list();

76 fill_modal_color_list();

77

78 bind_events();

79

80 node.wrapper.animate([

81 { opacity: 1 },

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

83 ], {

84 delay: 100,

85 duration: 500,

86 fill: 'both'

87 });

88 node.hotbar.animate([

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

90 { opacity: 1 }

91 ], {

92 delay: 600,

93 easing: 'ease-out',

94 duration: 250,

95 fill: 'both'

96 });

97}

98

99function bind_events() {

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

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

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

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

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

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

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

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

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

109 if(event.ctrlKey) {

110 prevent(event);

111 }

112 }, { passive: false });

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

114 if(event.target !== node.scale_input) {

115 event.preventDefault();

116 }

117 }, { passive: false });

118

119 for(let item of node.language_list) {

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

121 let code = item.dataset.code;

122 locale.change(code);

123 update_language_list();

124 });

125 }

126 for(let item of node.device_list) {

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

128 let code = item.dataset.code;

129 if(state.device_list[code]) {

130 state.device = code;

131 update_device_list();

132 }

133 });

134 }

135 for(let item of node.pencil_list) {

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

137 state.pencil = item.dataset.code;

138 update_pencil_list();

139 state.modal = null;

140 update_modal_list();

141 });

142 }

143 for(let item of node.modal_event_list) {

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

145 let code = item.dataset.code;

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

147 if(match) {

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

149 switch(match[1]) {

150 case 'toggle':

151 if(code) {

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

153 update_modal_list();

154 }

155 break;

156 case 'open':

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

158 state.modal = code;

159 update_modal_list();

160 }

161 break;

162 case 'close':

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

164 state.modal = null;

165 update_modal_list();

166 }

167 break;

168 default:

169 break;

170 }

171 }

172 }

173 });

174 }

175 for(let item of node.tab_event_list) {

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

177 let code = item.dataset.code;

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

179 if(match) {

180 switch(match[1]) {

181 case 'open':

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

183 state.tab = code;

184 update_tab_list();

185 }

186 break;

187 case 'close':

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

189 state.tab = null;

190 update_tab_list();

191 }

192 break;

193 default:

194 break;

195 }

196 }

197 });

198 }

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

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

201 state.point = {

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

203 y: parseInt(window.innerHeight / 2)

204 };

205 canvas.draw_cursor(state);

206 });

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

208 canvas.clear(state);

209 update_undo_and_redo();

210 show_notification('notification.clear');

211 state.modal = null;

212 update_modal_list();

213 });

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

215 if(!document.fullscreenElement) {

216 document.documentElement.requestFullscreen();

217 }

218 else if(document.exitFullscreen) {

219 document.exitFullscreen();

220 }

221 });

222 document.addEventListener('fullscreenchange', on_fullscreenchange);

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

224 state.option.view_mode = !state.option.view_mode;

225 update_view_mode_option();

226 });

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

228 canvas.undo(state);

229 update_undo_and_redo();

230 });

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

232 canvas.redo(state);

233 update_undo_and_redo();

234 });

235}

236

237function update_language_list() {

238 let lang = locale.get_current();

239 for(let item of node.language_list) {

240 let code = item.dataset.code;

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

242 }

243}

244

245function update_device_list() {

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

247 for(let item of node.device_list) {

248 let code = item.dataset.code;

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

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

251 }

252}

253

254function update_pencil_list() {

255 for(let item of node.pencil_list) {

256 let code = item.dataset.code;

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

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

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

260 }

261 }

262}

263

264function update_hotbar() {

265 for(let item of node.hotbar_list) {

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

267 }

268}

269

270function update_focus(bool) {

271 for(let item of node.hotbar_list) {

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

273 }

274 for(let item of node.tool_list) {

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

276 }

277 for(let item of node.action_list) {

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

279 }

280}

281

282function update_modal_list() {

283 for(let item of node.modal_list) {

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

285 }

286 for(let item of node.modal_event_list) {

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

288 }

289 state.tab = null;

290 update_tab_list();

291 if(state.modal) {

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

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

294 if(modal) {

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

296 modal.animate([

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

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

299 ], {

300 duration: 100,

301 fill: 'both'

302 });

303 }

304 if(button) {

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

306 }

307 }

308}

309

310function update_tab_list() {

311 for(let item of node.modal_list) {

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

313 if(main) {

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

315 main.style.marginLeft = '0';

316 }

317 }

318 for(let item of node.tab_list) {

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

320 }

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

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

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

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

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

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

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

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

329 }

330 }

331}

332

333function update_fullscreen_option() {

334 let fullscreen = document.fullscreenElement;

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

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

337}

338

339function update_view_mode_option() {

340 let view_mode = state.option.view_mode;

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

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

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

344 if(view_mode) {

345 state.modal = null;

346 state.tab = null;

347 update_modal_list();

348 canvas.on_release(null, state);

349 canvas.clear_cursor();

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

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

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

353 node.hotbar.animate([

354 { opacity: 1 },

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

356 ], {

357 easing: 'ease-out',

358 duration: 250,

359 fill: 'both'

360 });

361 node.header.animate([

362 { opacity: 1 },

363 { opacity: 0 }

364 ], {

365 easing: 'ease-out',

366 duration: 250,

367 fill: 'both'

368 });

369 }

370 else {

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

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

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

374 node.hotbar.animate([

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

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

377 ], {

378 easing: 'ease-out',

379 duration: 250,

380 fill: 'both'

381 });

382 node.header.animate([

383 { opacity: 0 },

384 { opacity: 1 }

385 ], {

386 easing: 'ease-out',

387 duration: 250,

388 fill: 'both'

389 });

390 }

391}

392

393function update_undo_and_redo() {

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

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

396}

397

398function on_pointerdown(event) {

399 // detect touch

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

401 state.device_list.touch = true;

402 if(state.device) {

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

404 }

405 else {

406 state.device = 'touch';

407 state.device_fallback = 'touch';

408 }

409 update_device_list();

410 }

411 // detect pen

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

413 state.device_list.pen = true;

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

415 if(!state.device) {

416 state.device = 'pen';

417 state.device_fallback = 'pen';

418 }

419 update_device_list();

420 }

421 // drawing

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

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

424 update_focus(false);

425 canvas.on_press(event, state);

426 update_undo_and_redo();

427 state.point = {

428 x: event.layerX,

429 y: event.layerY

430 };

431 canvas.draw_cursor(state);

432 }

433 }

434}

435

436function on_pointermove(event) {

437 // detect mouse

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

439 state.device_list.mouse = true;

440 if(state.device) {

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

442 }

443 else {

444 state.device = 'mouse';

445 state.device_fallback = 'mouse';

446 }

447 update_device_list();

448 }

449 // drawing

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

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

452 canvas.on_move(event, state);

453 state.point = {

454 x: event.layerX,

455 y: event.layerY

456 };

457 canvas.draw_cursor(state);

458 }

459 }

460}

461

462function on_pointerup(event) {

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

464 canvas.on_release(event, state);

465 update_undo_and_redo();

466 canvas.draw_cursor(state);

467 update_focus(true);

468 }

469}

470

471function on_pointerout(event) {

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

473 canvas.on_release(event, state);

474 update_undo_and_redo();

475 state.point = null;

476 canvas.draw_cursor(state);

477 }

478}

479

480function on_keydown(event) {

481 let lock = event.getModifierState("CapsLock");

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

483 if(state.tab) {

484 state.tab = null;

485 update_tab_list();

486 return;

487 }

488 if(state.modal) {

489 state.modal = null;

490 update_modal_list();

491 return;

492 }

493 }

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

495 if(!state.option.view_mode) {

496 if(!lock) {

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

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

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

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

501 canvas.on_release(event, state);

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

503 state.index = i;

504 canvas.draw_cursor(state);

505 update_hotbar();

506 }

507 }

508 }

509 }

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

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

512 update_modal_list();

513 }

514 if(event.code === 'KeyS') {

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

516 update_modal_list();

517 }

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

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

520 update_modal_list();

521 }

522 if(event.code === 'KeyD') {

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

524 update_modal_list();

525 }

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

527 state.modal = state.modal !== 'capture' ? 'capture' : null;

528 update_modal_list();

529 }

530 }

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

532 state.option.view_mode = !state.option.view_mode;

533 update_view_mode_option();

534 }

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

536 canvas.undo(state);

537 update_undo_and_redo();

538 }

539 if(event.code === 'ArrowRight') {

540 canvas.redo(state);

541 update_undo_and_redo();

542 }

543 }

544 // alt

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

546 if(!state.option.view_mode) {

547 if(!lock) {

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

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

550 if(i >= 0 && i < state.pencil_list.length) {

551 canvas.on_release(event, state);

552 state.pencil = state.pencil_list[i];

553 canvas.draw_cursor(state);

554 update_pencil_list();

555 }

556 }

557 }

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

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

560 update_modal_list();

561 }

562 if(event.code === 'KeyP') {

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

564 update_modal_list();

565 }

566 if(event.code === 'KeyS') {

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

568 update_modal_list();

569 }

570 if(event.code === 'KeyH') {

571 if(state.tab !== 'help') {

572 state.modal = 'more';

573 update_modal_list();

574 state.tab = 'help';

575 update_tab_list();

576 }

577 else {

578 state.modal = null;

579 update_modal_list();

580 }

581 }

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

583 if(state.tab !== 'language') {

584 state.modal = 'more';

585 update_modal_list();

586 state.tab = 'language';

587 update_tab_list();

588 }

589 else {

590 state.modal = null;

591 update_modal_list();

592 }

593 }

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

595 if(state.tab !== 'device') {

596 state.modal = 'more';

597 update_modal_list();

598 state.tab = 'device';

599 update_tab_list();

600 }

601 else {

602 state.modal = null;

603 update_modal_list();

604 }

605 }

606 if(event.code === 'KeyA') {

607 if(state.tab !== 'appearance') {

608 state.modal = 'more';

609 update_modal_list();

610 state.tab = 'appearance';

611 update_tab_list();

612 }

613 else {

614 state.modal = null;

615 update_modal_list();

616 }

617 }

618 }

619 }

620}

621

622function on_wheel(event) {

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

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

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

626 canvas.on_release(event, state);

627 state.radius += y;

628 node.scale_input.value = state.radius;

629 canvas.draw_cursor(state);

630 }

631 }

632}

633

634function on_fullscreenchange(event) {

635 update_fullscreen_option();

636}

637

638function fill_hotbar_list() {

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

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

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

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

643 canvas.on_release(event, state);

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

645 state.index = i;

646 canvas.draw_cursor(state);

647 update_hotbar();

648 });

649 if(state.index == i) {

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

651 }

652 }

653 }

654}

655

656function fill_modal_color_list() {

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

658 if(COLORS.length > i) {

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

660 item.addEventListener('pointerdown', function(event) {

661 canvas.on_release(event, state);

662 state.color = i;

663 state.modal = null;

664 update_modal_list();

665 canvas.draw_cursor(state);

666 state.index = null;

667 update_hotbar();

668 });

669 }

670 }

671}

672

673function show_notification(message) {

674 let code = uuid();

675 state.timestamp = code;

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

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

678 node.notification.animate([

679 { opacity: 0 },

680 { opacity: 1 }

681 ], {

682 duration: 200,

683 fill: 'both'

684 });

685 setTimeout(function() {

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

687 node.notification.animate([

688 { opacity: 1 },

689 { opacity: 0 }

690 ], {

691 duration: 200,

692 fill: 'both'

693 });

694 }

695 }, 2000);

696}