so://phie

outline

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

blob: 956f526f2f05e6154888ad236aaaf5116d303bfc (17106 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};

37

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

39

40function init() {

41

42 canvas.init();

43 locale.init('en');

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

45 ripple.init();

46

47 node = query({

48 wrapper: '.wrapper',

49 controls: '.controls',

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

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

52 hotbar: '.hotbar',

53 toolbar: '.toolbar',

54 actionbar: '.actionbar',

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

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

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

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

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

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

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

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

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

64 notification: '.notification',

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

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

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

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

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

70 });

71

72 fill_hotbar_list();

73 fill_modal_color_list();

74

75 bind_events();

76

77 node.wrapper.animate([

78 { opacity: 1 },

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

80 ], {

81 delay: 100,

82 duration: 500,

83 fill: 'both'

84 });

85 node.hotbar.animate([

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

87 { opacity: 1 }

88 ], {

89 delay: 600,

90 easing: 'ease-out',

91 duration: 250,

92 fill: 'both'

93 });

94}

95

96function bind_events() {

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

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

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

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

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

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

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

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

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

106 if(event.ctrlKey) {

107 prevent(event);

108 }

109 }, { passive: false });

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

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

112 event.preventDefault();

113 }

114 }, { passive: false });

115

116 for(let item of node.language_list) {

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

118 let code = item.dataset.code;

119 locale.change(code);

120 update_language_list();

121 });

122 }

123 for(let item of node.device_list) {

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

125 let code = item.dataset.code;

126 if(state.device_list[code]) {

127 state.device = code;

128 update_device_list();

129 }

130 });

131 }

132 for(let item of node.pencil_list) {

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

134 state.pencil = item.dataset.code;

135 update_pencil_list();

136 });

137 }

138 for(let item of node.modal_event_list) {

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

140 let code = item.dataset.code;

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

142 if(match) {

143 switch(match[1]) {

144 case 'toggle':

145 if(code) {

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

147 update_modal_list();

148 }

149 break;

150 case 'open':

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

152 state.modal = code;

153 update_modal_list();

154 }

155 break;

156 case 'close':

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

158 state.modal = null;

159 update_modal_list();

160 }

161 break;

162 default:

163 break;

164 }

165 }

166 });

167 }

168 for(let item of node.tab_event_list) {

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

170 let code = item.dataset.code;

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

172 if(match) {

173 switch(match[1]) {

174 case 'open':

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

176 state.tab = code;

177 update_tab_list();

178 }

179 break;

180 case 'close':

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

182 state.tab = null;

183 update_tab_list();

184 }

185 break;

186 default:

187 break;

188 }

189 }

190 });

191 }

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

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

194 state.point = {

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

196 y: parseInt(window.innerHeight / 2)

197 };

198 canvas.draw_cursor(state);

199 });

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

201 canvas.clear();

202 show_notification('notification.clear');

203 state.modal = null;

204 update_modal_list();

205 });

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

207 if(!document.fullscreenElement) {

208 document.documentElement.requestFullscreen();

209 }

210 else if(document.exitFullscreen) {

211 document.exitFullscreen();

212 }

213 });

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

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

216 state.option.view_mode = !state.option.view_mode;

217 update_view_mode_option();

218 });

219}

220

221function update_language_list() {

222 let lang = locale.get_current();

223 for(let item of node.language_list) {

224 let code = item.dataset.code;

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

226 }

227}

228

229function update_device_list() {

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

231 for(let item of node.device_list) {

232 let code = item.dataset.code;

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

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

235 }

236}

237

238function update_pencil_list() {

239 for(let item of node.pencil_list) {

240 let code = item.dataset.code;

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

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

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

244 }

245 }

246}

247

248function update_hotbar() {

249 canvas.on_release(event);

250 for(let item of node.hotbar_list) {

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

252 }

253}

254

255function update_focus(bool) {

256 for(let item of node.hotbar_list) {

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

258 }

259 for(let item of node.tool_list) {

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

261 }

262 for(let item of node.action_list) {

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

264 }

265}

266

267function update_modal_list() {

268 for(let item of node.modal_list) {

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

270 }

271 for(let item of node.modal_event_list) {

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

273 }

274 state.tab = null;

275 update_tab_list();

276 if(state.modal) {

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

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

279 if(modal) {

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

281 modal.animate([

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

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

284 ], {

285 duration: 100,

286 fill: 'both'

287 });

288 }

289 if(button) {

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

291 }

292 }

293}

294

295function update_tab_list() {

296 for(let item of node.modal_list) {

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

298 if(main) {

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

300 main.style.marginLeft = '0';

301 }

302 }

303 for(let item of node.tab_list) {

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

305 }

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

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

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

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

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

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

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

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

314 }

315 }

316}

317

318function update_fullscreen_option() {

319 let fullscreen = document.fullscreenElement;

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

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

322}

323

324function update_view_mode_option() {

325 let view_mode = state.option.view_mode;

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

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

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

329 if(view_mode) {

330 state.modal = null;

331 state.tab = null;

332 update_modal_list();

333 canvas.on_release();

334 canvas.clear_cursor();

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

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

337 node.toolbar.classList.add('inactive');

338 node.actionbar.classList.add('inactive');

339 node.hotbar.animate([

340 { opacity: 1 },

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

342 ], {

343 easing: 'ease-out',

344 duration: 250,

345 fill: 'both'

346 });

347 node.toolbar.animate([

348 { opacity: 1 },

349 { opacity: 0 }

350 ], {

351 easing: 'ease-out',

352 duration: 250,

353 fill: 'both'

354 });

355 node.actionbar.animate([

356 { opacity: 1 },

357 { opacity: 0 }

358 ], {

359 easing: 'ease-out',

360 duration: 250,

361 fill: 'both'

362 });

363 }

364 else {

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

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

367 node.toolbar.classList.remove('inactive');

368 node.actionbar.classList.remove('inactive');

369 node.hotbar.animate([

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

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

372 ], {

373 easing: 'ease-out',

374 duration: 250,

375 fill: 'both'

376 });

377 node.toolbar.animate([

378 { opacity: 0 },

379 { opacity: 1 }

380 ], {

381 easing: 'ease-out',

382 duration: 250,

383 fill: 'both'

384 });

385 node.actionbar.animate([

386 { opacity: 0 },

387 { opacity: 1 }

388 ], {

389 easing: 'ease-out',

390 duration: 250,

391 fill: 'both'

392 });

393 }

394}

395

396function on_pointerdown(event) {

397 // detect touch

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

399 state.device_list.touch = true;

400 if(state.device) {

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

402 }

403 else {

404 state.device = 'touch';

405 state.device_fallback = 'touch';

406 }

407 update_device_list();

408 }

409 // detect pen

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

411 state.device_list.pen = true;

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

413 if(!state.device) {

414 state.device = 'pen';

415 state.device_fallback = 'pen';

416 }

417 update_device_list();

418 }

419 // drawing

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

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

422 update_focus(false);

423 canvas.on_press(event, state);

424 state.point = {

425 x: event.layerX,

426 y: event.layerY

427 };

428 canvas.draw_cursor(state);

429 }

430 }

431}

432

433function on_pointermove(event) {

434 // detect mouse

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

436 state.device_list.mouse = true;

437 if(state.device) {

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

439 }

440 else {

441 state.device = 'mouse';

442 state.device_fallback = 'mouse';

443 }

444 update_device_list();

445 }

446 // drawing

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

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

449 canvas.on_move(event, state);

450 state.point = {

451 x: event.layerX,

452 y: event.layerY

453 };

454 canvas.draw_cursor(state);

455 }

456 }

457}

458

459function on_pointerup(event) {

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

461 canvas.on_release(event);

462 canvas.draw_cursor(state);

463 update_focus(true);

464 }

465}

466

467function on_pointerout(event) {

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

469 canvas.on_release(event);

470 state.point = null;

471 canvas.draw_cursor(state);

472 }

473}

474

475function on_keydown(event) {

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

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

478 if(state.tab) {

479 state.tab = null;

480 update_tab_list();

481 return;

482 }

483 if(state.modal) {

484 state.modal = null;

485 update_modal_list();

486 return;

487 }

488 }

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

490 if(!state.option.view_mode) {

491 if(!lock) {

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

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

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

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

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

497 state.index = i;

498 canvas.draw_cursor(state);

499 update_hotbar();

500 }

501 }

502 }

503 }

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

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

506 update_modal_list();

507 }

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

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

510 update_modal_list();

511 }

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

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

514 update_modal_list();

515 }

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

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

518 update_modal_list();

519 }

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

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

522 update_modal_list();

523 }

524 }

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

526 state.option.view_mode = !state.option.view_mode;

527 update_view_mode_option();

528 }

529 }

530 // alt

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

532 if(!state.option.view_mode) {

533 if(!lock) {

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

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

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

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

538 canvas.on_release(event);

539 canvas.draw_cursor(state);

540 update_pencil_list();

541 }

542 }

543 }

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

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

546 update_modal_list();

547 }

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

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

550 update_modal_list();

551 }

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

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

554 update_modal_list();

555 }

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

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

558 state.modal = 'settings';

559 update_modal_list();

560 state.tab = 'help';

561 update_tab_list();

562 }

563 else {

564 state.modal = null;

565 update_modal_list();

566 }

567 }

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

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

570 state.modal = 'settings';

571 update_modal_list();

572 state.tab = 'language';

573 update_tab_list();

574 }

575 else {

576 state.modal = null;

577 update_modal_list();

578 }

579 }

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

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

582 state.modal = 'settings';

583 update_modal_list();

584 state.tab = 'device';

585 update_tab_list();

586 }

587 else {

588 state.modal = null;

589 update_modal_list();

590 }

591 }

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

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

594 state.modal = 'settings';

595 update_modal_list();

596 state.tab = 'appearance';

597 update_tab_list();

598 }

599 else {

600 state.modal = null;

601 update_modal_list();

602 }

603 }

604 }

605 }

606}

607

608function on_wheel(event) {

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

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

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

612 canvas.on_release(event);

613 state.radius += y;

614 node.scale_input.value = state.radius;

615 canvas.draw_cursor(state);

616 }

617 }

618}

619

620function on_fullscreenchange(event) {

621 update_fullscreen_option();

622}

623

624function fill_hotbar_list() {

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

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

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

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

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

630 state.index = i;

631 canvas.draw_cursor(state);

632 update_hotbar();

633 });

634 if(state.index == i) {

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

636 }

637 }

638 }

639}

640

641function fill_modal_color_list() {

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

643 if(COLORS.length > i) {

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

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

646 state.color = i;

647 state.modal = null;

648 update_modal_list();

649 canvas.draw_cursor(state);

650 state.index = null;

651 update_hotbar();

652 });

653 }

654 }

655}

656

657function show_notification(message) {

658 let code = uuid();

659 state.timestamp = code;

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

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

662 node.notification.animate([

663 { opacity: 0 },

664 { opacity: 1 }

665 ], {

666 duration: 200,

667 fill: 'both'

668 });

669 setTimeout(function() {

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

671 node.notification.animate([

672 { opacity: 1 },

673 { opacity: 0 }

674 ], {

675 duration: 200,

676 fill: 'both'

677 });

678 }

679 }, 2000);

680}