so://phie

outline

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

blob: 739c508e50be438667269bc1a4e28b54c3b42939 (17703 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 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="action.undo"]',

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

73 colors_modal: '.colors-modal',

74 mainbar: '.mainbar',

75 sidebar: '.sidebar',

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

77 });

78

79 fill_hotbar_list();

80 fill_modal_color_list();

81

82 bind_events();

83

84 node.wrapper.animate([

85 { opacity: 1 },

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

87 ], {

88 delay: 100,

89 duration: 500,

90 fill: 'both'

91 });

92 node.hotbar.animate([

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

94 { opacity: 1 }

95 ], {

96 delay: 600,

97 easing: 'ease-out',

98 duration: 250,

99 fill: 'both'

100 });

101}

102

103function bind_events() {

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

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

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

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

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

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

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

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

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

113 if(event.ctrlKey) {

114 prevent(event);

115 }

116 }, { passive: false });

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

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

119 event.preventDefault();

120 }

121 }, { passive: false });

122

123 for(let item of node.language_list) {

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

125 let code = item.dataset.code;

126 locale.change(code);

127 update_language_list();

128 });

129 }

130 for(let item of node.device_list) {

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

132 let code = item.dataset.code;

133 if(state.device_list[code]) {

134 state.device = code;

135 update_device_list();

136 }

137 });

138 }

139 for(let item of node.pencil_list) {

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

141 state.pencil = item.dataset.code;

142 update_pencil_list();

143 state.modal = null;

144 update_modal_list();

145 });

146 }

147 for(let item of node.modal_event_list) {

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

149 let code = item.dataset.code;

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

151 if(match) {

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

153 switch(match[1]) {

154 case 'toggle':

155 if(code) {

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

157 update_modal_list();

158 }

159 break;

160 case 'open':

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

162 state.modal = code;

163 update_modal_list();

164 }

165 break;

166 case 'close':

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

168 state.modal = null;

169 update_modal_list();

170 }

171 break;

172 default:

173 break;

174 }

175 }

176 }

177 });

178 }

179 for(let item of node.tab_event_list) {

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

181 let code = item.dataset.code;

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

183 if(match) {

184 switch(match[1]) {

185 case 'open':

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

187 state.tab = code;

188 update_tab_list();

189 }

190 break;

191 case 'close':

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

193 state.tab = null;

194 update_tab_list();

195 }

196 break;

197 default:

198 break;

199 }

200 }

201 });

202 }

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

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

205 state.point = {

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

207 y: parseInt(window.innerHeight / 2)

208 };

209 canvas.draw_cursor(state);

210 });

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

212 canvas.clear(state);

213 update_undo_and_redo();

214 show_notification('notification.clear');

215 state.modal = null;

216 update_modal_list();

217 });

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

219 if(!document.fullscreenElement) {

220 document.documentElement.requestFullscreen();

221 }

222 else if(document.exitFullscreen) {

223 document.exitFullscreen();

224 }

225 });

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

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

228 state.option.view_mode = !state.option.view_mode;

229 update_view_mode_option();

230 });

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

232 canvas.undo(state);

233 update_undo_and_redo();

234 });

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

236 canvas.redo(state);

237 update_undo_and_redo();

238 });

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

240 state.option.view_mode = false;

241 update_view_mode_option();

242 });

243}

244

245function update_language_list() {

246 let lang = locale.get_current();

247 for(let item of node.language_list) {

248 let code = item.dataset.code;

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

250 }

251}

252

253function update_device_list() {

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

255 for(let item of node.device_list) {

256 let code = item.dataset.code;

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

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

259 }

260}

261

262function update_pencil_list() {

263 for(let item of node.pencil_list) {

264 let code = item.dataset.code;

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

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

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

268 }

269 }

270}

271

272function update_hotbar() {

273 for(let item of node.hotbar_list) {

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

275 }

276}

277

278function update_focus(bool) {

279 for(let item of node.hotbar_list) {

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

281 }

282 for(let item of node.tool_list) {

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

284 }

285 for(let item of node.action_list) {

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

287 }

288}

289

290function update_modal_list() {

291 for(let item of node.modal_list) {

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

293 }

294 for(let item of node.modal_event_list) {

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

296 }

297 state.tab = null;

298 update_tab_list();

299 if(state.modal) {

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

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

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

303 if(modal) {

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

305 modal.animate([

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

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

308 ], {

309 duration: 100,

310 fill: 'both'

311 });

312 }

313 if(button) {

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

315 }

316 }

317}

318

319function update_tab_list() {

320 for(let item of node.modal_list) {

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

322 if(main) {

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

324 main.style.marginLeft = '0';

325 }

326 }

327 for(let item of node.tab_list) {

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

329 }

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

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

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

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

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

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

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

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

338 }

339 }

340}

341

342function update_fullscreen_option() {

343 let fullscreen = document.fullscreenElement;

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

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

346}

347

348function update_view_mode_option() {

349 let view_mode = state.option.view_mode;

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

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

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

353 if(view_mode) {

354 state.modal = null;

355 state.tab = null;

356 update_modal_list();

357 canvas.on_release(null, state);

358 canvas.clear_cursor();

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

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

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

362 node.hotbar.animate([

363 { opacity: 1 },

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

365 ], {

366 easing: 'ease-out',

367 duration: 250,

368 fill: 'both'

369 });

370 node.mainbar.animate([

371 { opacity: 1, marginTop: 0 },

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

373 ], {

374 easing: 'ease-out',

375 duration: 250,

376 fill: 'both'

377 });

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

379 node.sidebar.animate([

380 { opacity: 0 },

381 { opacity: 1 }

382 ], {

383 delay: 250,

384 easing: 'ease-out',

385 duration: 250,

386 fill: 'both'

387 });

388 }

389 else {

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

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

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

393 node.hotbar.animate([

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

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

396 ], {

397 easing: 'ease-out',

398 duration: 250,

399 fill: 'both'

400 });

401 node.mainbar.animate([

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

403 { opacity: 1, marginTop: 0 }

404 ], {

405 easing: 'ease-out',

406 duration: 250,

407 fill: 'both'

408 });

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

410 }

411}

412

413function update_undo_and_redo() {

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

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

416}

417

418function on_pointerdown(event) {

419 // detect touch

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

421 state.device_list.touch = true;

422 if(state.device) {

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

424 }

425 else {

426 state.device = 'touch';

427 state.device_fallback = 'touch';

428 }

429 update_device_list();

430 }

431 // detect pen

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

433 state.device_list.pen = true;

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

435 if(!state.device) {

436 state.device = 'pen';

437 state.device_fallback = 'pen';

438 }

439 update_device_list();

440 }

441 // drawing

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

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

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

445 update_focus(false);

446 canvas.on_press(event, state);

447 update_undo_and_redo();

448 state.point = {

449 x: event.layerX,

450 y: event.layerY

451 };

452 canvas.draw_cursor(state);

453 }

454 }

455 }

456}

457

458function on_pointermove(event) {

459 // detect mouse

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

461 state.device_list.mouse = true;

462 if(state.device) {

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

464 }

465 else {

466 state.device = 'mouse';

467 state.device_fallback = 'mouse';

468 }

469 update_device_list();

470 }

471 // drawing

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

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

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

475 canvas.on_move(event, state);

476 }

477 state.point = {

478 x: event.layerX,

479 y: event.layerY

480 };

481 }

482 else {

483 state.point = {

484 x: null,

485 y: null

486 };

487 }

488 canvas.draw_cursor(state);

489 }

490}

491

492function on_pointerup(event) {

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

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

495 canvas.on_release(event, state);

496 update_undo_and_redo();

497 update_focus(true);

498 }

499 }

500}

501

502function on_pointerout(event) {

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

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

505 canvas.on_release(event, state);

506 update_undo_and_redo();

507 state.point = null;

508 canvas.draw_cursor(state);

509 }

510 }

511}

512

513function on_keydown(event) {

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

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

516 if(state.tab) {

517 state.tab = null;

518 update_tab_list();

519 return;

520 }

521 if(state.modal) {

522 state.modal = null;

523 update_modal_list();

524 return;

525 }

526 }

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

528 if(!state.option.view_mode) {

529 if(!lock) {

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

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

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

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

534 canvas.on_release(event, state);

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

536 state.index = i;

537 canvas.draw_cursor(state);

538 update_hotbar();

539 }

540 }

541 }

542 }

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

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

545 update_modal_list();

546 }

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

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

549 update_modal_list();

550 }

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

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

553 update_modal_list();

554 }

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

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

557 update_modal_list();

558 }

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

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

561 update_modal_list();

562 }

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

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

565 update_modal_list();

566 }

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

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

569 update_modal_list();

570 }

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

572 canvas.undo(state);

573 update_undo_and_redo();

574 }

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

576 canvas.redo(state);

577 update_undo_and_redo();

578 }

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

580 canvas.on_release(event, state);

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

582 canvas.draw_cursor(state);

583 update_pencil_list();

584 }

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

586 canvas.on_release(event, state);

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

588 canvas.draw_cursor(state);

589 update_pencil_list();

590 }

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

592 canvas.on_release(event, state);

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

594 canvas.draw_cursor(state);

595 update_pencil_list();

596 }

597 }

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

599 state.option.view_mode = !state.option.view_mode;

600 update_view_mode_option();

601 }

602 }

603}

604

605function on_wheel(event) {

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

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

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

609 canvas.on_release(event, state);

610 state.radius += y;

611 node.scale_input.value = state.radius;

612 canvas.draw_cursor(state);

613 }

614 }

615}

616

617function on_fullscreenchange(event) {

618 update_fullscreen_option();

619}

620

621function fill_hotbar_list() {

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

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

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

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

626 canvas.on_release(event, state);

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

628 state.index = i;

629 canvas.draw_cursor(state);

630 update_hotbar();

631 });

632 if(state.index == i) {

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

634 }

635 }

636 }

637}

638

639function fill_modal_color_list() {

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

641 if(COLORS.length > i) {

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

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

644 canvas.on_release(event, state);

645 state.color = i;

646 state.modal = null;

647 update_modal_list();

648 canvas.draw_cursor(state);

649 state.index = null;

650 update_hotbar();

651 });

652 }

653 }

654}

655

656function show_notification(message) {

657 let code = uuid();

658 state.timestamp = code;

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

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

661 node.notification.animate([

662 { opacity: 0 },

663 { opacity: 1 }

664 ], {

665 duration: 200,

666 fill: 'both'

667 });

668 setTimeout(function() {

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

670 node.notification.animate([

671 { opacity: 1 },

672 { opacity: 0 }

673 ], {

674 duration: 200,

675 fill: 'both'

676 });

677 }

678 }, 1000);

679}