so://phie

outline

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

blob: a79469cc7e81daf59a1727126562dbfe58a45aef (23920 bytes)[doc][blame][raw]

1import { FIREBASE, 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';

6import cookie from './mod/cookie.js';

7

8let node;

9let state = {

10 color: 54,

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

12 radius: 14,

13 region: [],

14 crop: false,

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 crop_mode: false,

37 dark_mode: false

38 },

39 history: [],

40 redo_history: [],

41 key: {}

42};

43

44export async function init() {

45

46 locale.init();

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

48 canvas.init();

49

50 node = query({

51 wrapper: '.wrapper',

52 controls: '.controls',

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

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

55 header: '.header-list',

56 hotbar: '.hotbar',

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

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

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

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

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

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

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

64 capture_event_list: { query: '[data-event*=".capture"]', all: true },

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

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

67 notification: '.notification',

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

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

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

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

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

73 option_event_dark_mode: '[data-event="toggle.dark-mode"]',

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

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

76 colors_modal: '.colors-modal',

77 mainbar: '.mainbar',

78 sidebar: '.sidebar',

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

80 content: '.modal .content',

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

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

83 download: '#download',

84 cookie: '.cookie',

85 accept: '.button.accept'

86 });

87

88 update_language_list();

89

90 fill_hotbar_list();

91 fill_modal_color_list();

92

93 bind_events(locale);

94

95 node.wrapper.animate([

96 { opacity: 1 },

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

98 ], {

99 delay: 100,

100 duration: 500,

101 fill: 'both'

102 });

103 node.hotbar.animate([

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

105 { opacity: 1 }

106 ], {

107 delay: 600,

108 easing: 'ease-out',

109 duration: 250,

110 fill: 'both'

111 });

112

113 setTimeout(function() {

114 node.wrapper.classList.add('hidden');

115 }.bind(this), 600);

116

117 if(!cookie.hasAccepted()) {

118 node.cookie.classList.remove('hidden');

119 cookie.requestPermission(node.accept)

120 .then(function() {

121 node.cookie.classList.add('hidden');

122 load_resources();

123 });

124 }

125 else {

126 load_resources();

127 }

128}

129

130function load_resources() {

131 let dark_mode = localStorage.getItem('outline.custom.dark-mode');

132 if(dark_mode && dark_mode === 'true') {

133 document.body.classList.add('theme-dark');

134 state.option.dark_mode = true;

135 update_dark_mode_option({ passive: true });

136 }

137 let lang = localStorage.getItem('outline.custom.lang');

138 if(lang && LOCALES.includes(lang)) {

139 locale.change(lang);

140 update_language_list();

141 }

142 try {

143 firebase.initializeApp(FIREBASE);

144 firebase.analytics();

145 }

146 catch(error) {

147 console.warn('Unable to load Google Analytics!');

148 }

149}

150

151function bind_events(locale) {

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

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

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

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

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

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

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

159 document.addEventListener('keyup', on_keyup);

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

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

162 if(event.ctrlKey) {

163 prevent(event);

164 }

165 }, { passive: false });

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

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

168 event.preventDefault();

169 }

170 }, { passive: false });

171

172 for(let item of node.language_list) {

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

174 let code = item.dataset.code;

175 locale.change(code);

176 update_language_list();

177 });

178 }

179 for(let item of node.device_list) {

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

181 let code = item.dataset.code;

182 if(state.device_list[code]) {

183 state.device = code;

184 update_device_list();

185 }

186 });

187 }

188 for(let item of node.pencil_list) {

189 item.addEventListener('click', function() {

190 state.pencil = item.dataset.code;

191 update_pencil_list();

192 state.modal = null;

193 update_modal_list();

194 });

195 }

196 for(let item of node.modal_event_list) {

197 item.addEventListener('click', function() {

198 let code = item.dataset.code;

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

200 if(match) {

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

202 switch(match[1]) {

203 case 'toggle':

204 if(code) {

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

206 update_modal_list();

207 }

208 break;

209 case 'open':

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

211 state.modal = code;

212 update_modal_list();

213 }

214 break;

215 case 'close':

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

217 state.modal = null;

218 update_modal_list();

219 }

220 break;

221 default:

222 break;

223 }

224 }

225 }

226 });

227 }

228 for(let item of node.tab_event_list) {

229 item.addEventListener('click', function() {

230 let code = item.dataset.code;

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

232 if(match) {

233 switch(match[1]) {

234 case 'open':

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

236 state.tab = code;

237 update_tab_list();

238 }

239 break;

240 case 'close':

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

242 state.tab = null;

243 update_tab_list();

244 }

245 break;

246 default:

247 break;

248 }

249 }

250 });

251 }

252 for(let item of node.capture_event_list) {

253 item.addEventListener('click', function() {

254 let code = item.dataset.code;

255 if(code === 'download') {

256 if(state.option.crop_mode) {

257 state.option.crop_mode = false;

258 canvas.draw_crop(state);

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

260 }

261 let data = canvas.create_capture(0, 0, window.innerWidth, window.innerHeight, 60);

262 if(data) {

263 node.download.download = `outline-${uuid()}.png`;

264 node.download.href = data;

265 node.download.click();

266 }

267 else {

268 console.warn('Unable to create capture from current state!');

269 }

270 }

271 if(code === 'crop') {

272 state.option.crop_mode = true;

273 state.region = [];

274 canvas.draw_crop(state);

275 canvas.get_canvas().style.cursor = 'crosshair';

276 }

277 state.modal = null;

278 state.tab = null;

279 update_modal_list();

280 })

281 }

282 node.scale_input.addEventListener('input', function() {

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

284 state.point = {

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

286 y: parseInt(window.innerHeight / 2)

287 };

288 canvas.draw_cursor(state);

289 });

290 node.clear.addEventListener('click', function() {

291 canvas.clear(state);

292 update_undo_and_redo();

293 show_notification('notification.clear');

294 state.modal = null;

295 update_modal_list();

296 });

297 node.option_event_fullscreen.addEventListener('click', function() {

298 if(!document.fullscreenElement) {

299 document.documentElement.requestFullscreen();

300 }

301 else if(document.exitFullscreen) {

302 document.exitFullscreen();

303 }

304 });

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

306 node.option_event_view_mode.addEventListener('click', function() {

307 state.option.view_mode = !state.option.view_mode;

308 update_view_mode_option();

309 });

310 node.option_event_dark_mode.addEventListener('click', function() {

311 state.option.dark_mode = !state.option.dark_mode;

312 update_dark_mode_option();

313 });

314 node.undo_tool.addEventListener('click', function() {

315 canvas.undo(state);

316 update_undo_and_redo();

317 });

318 node.redo_tool.addEventListener('click', function() {

319 canvas.redo(state);

320 update_undo_and_redo();

321 });

322 node.edit_mode.addEventListener('click', function() {

323 state.option.view_mode = false;

324 update_view_mode_option();

325 });

326 for(let item of node.caption_list) {

327 item.addEventListener('click', function() {

328 state.caption = item.dataset.code;

329 update_caption_list();

330 });

331 }

332

333 window.addEventListener('beforeunload', function() {

334 // ...

335 });

336}

337

338function update_language_list() {

339 let lang = locale.get_current();

340 for(let item of node.language_list) {

341 let code = item.dataset.code;

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

343 }

344 if(cookie.hasAccepted()) {

345 localStorage.setItem('outline.custom.lang', lang);

346 }

347}

348

349function update_device_list() {

350 if(!state.option.view_mode && !state.option.crop_mode) {

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

352 }

353 for(let item of node.device_list) {

354 let code = item.dataset.code;

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

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

357 }

358}

359

360function update_pencil_list() {

361 for(let item of node.pencil_list) {

362 let code = item.dataset.code;

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

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

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

366 }

367 }

368}

369

370function update_hotbar() {

371 for(let item of node.hotbar_list) {

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

373 }

374}

375

376function update_focus(bool) {

377 for(let item of node.hotbar_list) {

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

379 }

380 for(let item of node.tool_list) {

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

382 }

383 for(let item of node.action_list) {

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

385 }

386}

387

388function update_modal_list() {

389 for(let item of node.modal_list) {

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

391 }

392 for(let item of node.modal_event_list) {

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

394 }

395 state.tab = null;

396 update_tab_list();

397 state.caption = 'controls';

398 update_caption_list();

399 if(state.modal) {

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

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

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

403 if(modal) {

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

405 modal.animate([

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

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

408 ], {

409 duration: 100,

410 fill: 'both'

411 });

412 }

413 if(button) {

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

415 }

416 }

417}

418

419function update_tab_list() {

420 for(let item of node.modal_list) {

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

422 if(main) {

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

424 main.style.marginLeft = '0';

425 }

426 }

427 for(let item of node.tab_list) {

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

429 }

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

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

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

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

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

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

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

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

438 }

439 }

440}

441

442function update_fullscreen_option() {

443 let fullscreen = document.fullscreenElement;

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

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

446}

447

448function update_view_mode_option() {

449 let view_mode = state.option.view_mode;

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

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

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

453 state.modal = null;

454 state.tab = null;

455 update_modal_list();

456 if(view_mode) {

457 canvas.on_release(null, state);

458 canvas.clear_cursor();

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

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

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

462 node.hotbar.animate([

463 { opacity: 1 },

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

465 ], {

466 easing: 'ease-out',

467 duration: 250,

468 fill: 'both'

469 });

470 node.mainbar.animate([

471 { opacity: 1, marginTop: 0 },

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

473 ], {

474 easing: 'ease-out',

475 duration: 250,

476 fill: 'both'

477 });

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

479 node.sidebar.animate([

480 { opacity: 0 },

481 { opacity: 1 }

482 ], {

483 delay: 250,

484 easing: 'ease-out',

485 duration: 250,

486 fill: 'both'

487 });

488 }

489 else {

490 state.option.crop_mode = false;

491 canvas.draw_crop(state);

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

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

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

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

496 node.hotbar.animate([

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

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

499 ], {

500 easing: 'ease-out',

501 duration: 250,

502 fill: 'both'

503 });

504 node.mainbar.animate([

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

506 { opacity: 1, marginTop: 0 }

507 ], {

508 easing: 'ease-out',

509 duration: 250,

510 fill: 'both'

511 });

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

513 }

514}

515

516function update_dark_mode_option(args) {

517 let { passive = false } = args || {};

518 let dark_mode = state.option.dark_mode;

519 if(!passive) {

520 show_notification(`notification.dark-mode.${dark_mode ? 'on' : 'off'}`);

521 }

522 node.option_event_dark_mode.classList[dark_mode ? 'add' : 'remove']('active');

523 node.option_event_dark_mode.querySelector('.ico').textContent = dark_mode ? 'toggle_on' : 'toggle_off';

524 document.body.classList[dark_mode ? 'add' : 'remove']('theme-dark');

525 if(cookie.hasAccepted()) {

526 localStorage.setItem('outline.custom.dark-mode', dark_mode);

527 }

528}

529

530function update_undo_and_redo() {

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

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

533}

534

535function update_caption_list() {

536 for(let item of node.caption_list) {

537 let code = item.dataset.code;

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

539 }

540 for(let item of node.content_list) {

541 let code = item.dataset.code;

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

543 }

544}

545

546function on_pointerdown(event) {

547 // detect touch

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

549 state.device_list.touch = true;

550 if(state.device) {

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

552 }

553 else {

554 state.device = 'touch';

555 state.device_fallback = 'touch';

556 }

557 update_device_list();

558 }

559 // detect pen

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

561 state.device_list.pen = true;

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

563 if(!state.device) {

564 state.device = 'pen';

565 state.device_fallback = 'pen';

566 }

567 update_device_list();

568 }

569 // drawing

570 if(event.button == 0) {

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

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

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

574 update_focus(false);

575 canvas.on_press(event, state);

576 update_undo_and_redo();

577 state.point = {

578 x: event.layerX,

579 y: event.layerY

580 };

581 canvas.draw_cursor(state);

582 }

583 }

584 }

585 if(state.option.view_mode && state.option.crop_mode) {

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

587 state.crop = true;

588 state.region = [];

589 state.region[0] = { x: event.layerX, y: event.layerY };

590 canvas.draw_crop(state);

591 }

592 }

593 }

594}

595

596function on_pointermove(event) {

597 // detect mouse

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

599 state.device_list.mouse = true;

600 if(state.device) {

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

602 }

603 else {

604 state.device = 'mouse';

605 state.device_fallback = 'mouse';

606 }

607 update_device_list();

608 }

609 // drawing

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

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

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

613 canvas.on_move(event, state);

614 }

615 state.point = {

616 x: event.layerX,

617 y: event.layerY

618 };

619 }

620 canvas.draw_cursor(state);

621 }

622 if(state.option.view_mode && state.option.crop_mode) {

623 if(state.crop && event.target === canvas.get_canvas()) {

624 state.region[1] = { x: event.layerX, y: event.layerY };

625 canvas.draw_crop(state);

626 }

627 }

628}

629

630function on_pointerup(event) {

631 if(event.button == 0) {

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

633 canvas.on_release(event, state);

634 update_undo_and_redo();

635 update_focus(true);

636 }

637 if(state.option.view_mode && state.option.crop_mode) {

638 if(state.crop && event.target === canvas.get_canvas()) {

639 state.crop = false;

640 state.region[1] = { x: event.layerX, y: event.layerY };

641 canvas.draw_crop(state);

642 let [ begin, end ] = state.region;

643 let delta = {

644 x: end.x - begin.x,

645 y: end.y - begin.y

646 };

647 let x = begin.x < end.x ? begin.x : end.x;

648 let y = begin.y < end.y ? begin.y : end.y;

649 if(delta.x < 0) {

650 delta.x *= -1;

651 }

652 if(delta.y < 0) {

653 delta.y *= -1;

654 }

655 if(delta.x != 0 && delta.y != 0) {

656 let data = canvas.create_capture(x, y, delta.x, delta.y);

657 if(data) {

658 node.download.download = `outline-${uuid()}.png`;

659 node.download.href = data;

660 node.download.click();

661 state.option.crop_mode = false;

662 canvas.draw_crop(state);

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

664 }

665 else {

666 console.warn('Unable to create capture from current state!');

667 }

668 }

669 }

670 }

671 }

672}

673

674function on_pointerout(event) {

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

676 canvas.on_release(event, state);

677 update_undo_and_redo();

678 state.point = null;

679 canvas.draw_cursor(state);

680 }

681 if(state.option.view_mode && state.option.crop_mode) {

682 if(state.crop && event.target === canvas.get_canvas()) {

683 state.crop = false;

684 state.region = [];

685 canvas.draw_crop(state);

686 }

687 }

688}

689

690function on_keydown(event) {

691 if(!state.key[event.code]) {

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

693 if(state.tab) {

694 state.tab = null;

695 update_tab_list();

696 return;

697 }

698 if(state.modal) {

699 state.modal = null;

700 update_modal_list();

701 return;

702 }

703 if(state.option.view_mode && state.option.crop_mode) {

704 state.option.crop_mode = false;

705 canvas.draw_crop(state);

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

707 }

708 }

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

710 if(!state.option.view_mode) {

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

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

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

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

715 canvas.on_release(event, state);

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

717 state.index = i;

718 canvas.draw_cursor(state);

719 update_hotbar();

720 }

721 }

722 }

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

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

725 update_modal_list();

726 }

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

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

729 update_modal_list();

730 }

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

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

733 update_modal_list();

734 }

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

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

737 update_modal_list();

738 }

739 /*

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

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

742 update_modal_list();

743 }

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

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

746 update_modal_list();

747 }

748 */

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

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

751 update_modal_list();

752 }

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

754 canvas.on_release(event, state);

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

756 canvas.draw_cursor(state);

757 update_pencil_list();

758 }

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

760 canvas.on_release(event, state);

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

762 canvas.draw_cursor(state);

763 update_pencil_list();

764 }

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

766 canvas.on_release(event, state);

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

768 canvas.draw_cursor(state);

769 update_pencil_list();

770 }

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

772 if(!document.fullscreenElement) {

773 document.documentElement.requestFullscreen();

774 }

775 else if(document.exitFullscreen) {

776 document.exitFullscreen();

777 }

778 }

779 }

780 if(state.option.view_mode && !state.option.crop_mode) {

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

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

783 update_modal_list();

784 }

785 }

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

787 state.option.view_mode = !state.option.view_mode;

788 update_view_mode_option();

789 }

790 }

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

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

793 canvas.undo(state);

794 update_undo_and_redo();

795 }

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

797 canvas.redo(state);

798 update_undo_and_redo();

799 }

800 }

801 if(event.code === 'Enter') {

802 if(state.modal === 'clear') {

803 node.clear.click();

804 }

805 }

806 state.key[event.code] = true;

807 }

808}

809

810function on_keyup(event) {

811 state.key[event.code] = false;

812}

813

814function on_wheel(event) {

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

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

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

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

819 canvas.on_release(event, state);

820 state.radius += y;

821 node.scale_input.value = state.radius;

822 canvas.draw_cursor(state);

823 }

824 }

825 }

826}

827

828function on_fullscreenchange() {

829 update_fullscreen_option();

830}

831

832function fill_hotbar_list() {

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

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

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

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

837 canvas.on_release(event, state);

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

839 state.index = i;

840 canvas.draw_cursor(state);

841 update_hotbar();

842 });

843 if(state.index == i) {

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

845 }

846 }

847 }

848}

849

850function fill_modal_color_list() {

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

852 if(COLORS.length > i) {

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

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

855 canvas.on_release(event, state);

856 state.color = i;

857 state.modal = null;

858 update_modal_list();

859 canvas.draw_cursor(state);

860 state.index = null;

861 update_hotbar();

862 });

863 }

864 }

865}

866

867function show_notification(message) {

868 let code = uuid();

869 state.timestamp = code;

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

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

872 node.notification.animate([

873 { opacity: 0 },

874 { opacity: 1 }

875 ], {

876 duration: 200,

877 fill: 'both'

878 });

879 setTimeout(function() {

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

881 node.notification.animate([

882 { opacity: 1 },

883 { opacity: 0 }

884 ], {

885 duration: 200,

886 fill: 'both'

887 });

888 }

889 }, 1000);

890}