so://phie

outline

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

blob: b6aff0c2c79c58c91f360112fe7f9edf947eb615 (16058 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 timestamp: null,

25 modal: null,

26 tab: null,

27 option: {

28 view_mode: false

29 }

30};

31

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

33

34function init() {

35

36 canvas.init();

37 locale.init('en');

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

39 ripple.init();

40

41 node = query({

42 wrapper: '.wrapper',

43 controls: '.controls',

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

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

46 hotbar: '.hotbar',

47 toolbar: '.toolbar',

48 actionbar: '.actionbar',

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

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

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

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

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

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

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

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

57 notification: '.notification',

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

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

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

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

62 });

63

64 fill_hotbar_list();

65 fill_modal_color_list();

66

67 bind_events();

68

69 node.wrapper.animate([

70 { opacity: 1 },

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

72 ], {

73 delay: 100,

74 duration: 500,

75 fill: 'both'

76 });

77 node.hotbar.animate([

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

79 { opacity: 1 }

80 ], {

81 delay: 600,

82 easing: 'ease-out',

83 duration: 250,

84 fill: 'both'

85 });

86}

87

88function bind_events() {

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

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

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

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

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

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

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

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

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

98 if(event.ctrlKey) {

99 prevent(event);

100 }

101 }, { passive: false });

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

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

104 event.preventDefault();

105 }

106 }, { passive: false });

107

108 for(let item of node.language_list) {

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

110 let code = item.dataset.code;

111 locale.change(code);

112 update_language_list();

113 });

114 }

115 for(let item of node.device_list) {

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

117 let code = item.dataset.code;

118 if(state.device_list[code]) {

119 state.device = code;

120 update_device_list();

121 }

122 });

123 }

124 for(let item of node.modal_event_list) {

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

126 let code = item.dataset.code;

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

128 if(match) {

129 switch(match[1]) {

130 case 'toggle':

131 if(code) {

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

133 update_modal_list();

134 }

135 break;

136 case 'open':

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

138 state.modal = code;

139 update_modal_list();

140 }

141 break;

142 case 'close':

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

144 state.modal = null;

145 update_modal_list();

146 }

147 break;

148 default:

149 break;

150 }

151 }

152 });

153 }

154 for(let item of node.tab_event_list) {

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

156 let code = item.dataset.code;

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

158 if(match) {

159 switch(match[1]) {

160 case 'open':

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

162 state.tab = code;

163 update_tab_list();

164 }

165 break;

166 case 'close':

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

168 state.tab = null;

169 update_tab_list();

170 }

171 break;

172 default:

173 break;

174 }

175 }

176 });

177 }

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

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

180 state.point = {

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

182 y: parseInt(window.innerHeight / 2)

183 };

184 canvas.draw_cursor(state);

185 });

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

187 canvas.clear();

188 show_notification('notification.clear');

189 state.modal = null;

190 update_modal_list();

191 });

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

193 if(!document.fullscreenElement) {

194 document.documentElement.requestFullscreen();

195 }

196 else if(document.exitFullscreen) {

197 document.exitFullscreen();

198 }

199 });

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

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

202 state.option.view_mode = !state.option.view_mode;

203 update_view_mode_option();

204 });

205}

206

207function update_language_list() {

208 let lang = locale.get_current();

209 for(let item of node.language_list) {

210 let code = item.dataset.code;

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

212 }

213}

214

215function update_device_list() {

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

217 for(let item of node.device_list) {

218 let code = item.dataset.code;

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

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

221 }

222}

223

224function update_hotbar() {

225 canvas.on_release(event);

226 for(let item of node.hotbar_list) {

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

228 }

229}

230

231function update_focus(bool) {

232 for(let item of node.hotbar_list) {

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

234 }

235 for(let item of node.tool_list) {

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

237 }

238 for(let item of node.action_list) {

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

240 }

241}

242

243function update_modal_list() {

244 for(let item of node.modal_list) {

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

246 }

247 for(let item of node.tool_list) {

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

249 }

250 for(let item of node.action_list) {

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

252 }

253 state.tab = null;

254 update_tab_list();

255 if(state.modal) {

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

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

258 if(modal) {

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

260 modal.animate([

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

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

263 ], {

264 duration: 100,

265 fill: 'both'

266 });

267 }

268 if(button) {

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

270 }

271 }

272}

273

274function update_tab_list() {

275 for(let item of node.modal_list) {

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

277 if(main) {

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

279 main.style.marginLeft = '0';

280 }

281 }

282 for(let item of node.tab_list) {

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

284 }

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

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

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

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

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

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

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

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

293 }

294 }

295}

296

297function update_fullscreen_option() {

298 let fullscreen = document.fullscreenElement;

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

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

301}

302

303function update_view_mode_option() {

304 let view_mode = state.option.view_mode;

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

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

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

308 if(view_mode) {

309 state.modal = null;

310 state.tab = null;

311 update_modal_list();

312 canvas.on_release();

313 canvas.clear_cursor();

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

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

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

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

318 node.hotbar.animate([

319 { opacity: 1 },

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

321 ], {

322 easing: 'ease-out',

323 duration: 250,

324 fill: 'both'

325 });

326 node.toolbar.animate([

327 { opacity: 1 },

328 { opacity: 0 }

329 ], {

330 easing: 'ease-out',

331 duration: 250,

332 fill: 'both'

333 });

334 node.actionbar.animate([

335 { opacity: 1 },

336 { opacity: 0 }

337 ], {

338 easing: 'ease-out',

339 duration: 250,

340 fill: 'both'

341 });

342 }

343 else {

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

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

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

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

348 node.hotbar.animate([

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

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

351 ], {

352 easing: 'ease-out',

353 duration: 250,

354 fill: 'both'

355 });

356 node.toolbar.animate([

357 { opacity: 0 },

358 { opacity: 1 }

359 ], {

360 easing: 'ease-out',

361 duration: 250,

362 fill: 'both'

363 });

364 node.actionbar.animate([

365 { opacity: 0 },

366 { opacity: 1 }

367 ], {

368 easing: 'ease-out',

369 duration: 250,

370 fill: 'both'

371 });

372 }

373}

374

375function on_pointerdown(event) {

376 // detect touch

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

378 state.device_list.touch = true;

379 if(state.device) {

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

381 }

382 else {

383 state.device = 'touch';

384 state.device_fallback = 'touch';

385 }

386 update_device_list();

387 }

388 // detect pen

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

390 state.device_list.pen = true;

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

392 if(!state.device) {

393 state.device = 'pen';

394 state.device_fallback = 'pen';

395 }

396 update_device_list();

397 }

398 // drawing

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

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

401 update_focus(false);

402 canvas.on_press(event, state);

403 state.point = {

404 x: event.layerX,

405 y: event.layerY

406 };

407 canvas.draw_cursor(state);

408 }

409 }

410}

411

412function on_pointermove(event) {

413 // detect mouse

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

415 state.device_list.mouse = true;

416 if(state.device) {

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

418 }

419 else {

420 state.device = 'mouse';

421 state.device_fallback = 'mouse';

422 }

423 update_device_list();

424 }

425 // drawing

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

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

428 canvas.on_move(event, state);

429 state.point = {

430 x: event.layerX,

431 y: event.layerY

432 };

433 canvas.draw_cursor(state);

434 }

435 }

436}

437

438function on_pointerup(event) {

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

440 canvas.on_release(event);

441 canvas.draw_cursor(state);

442 update_focus(true);

443 }

444}

445

446function on_pointerout(event) {

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

448 canvas.on_release(event);

449 state.point = null;

450 canvas.draw_cursor(state);

451 }

452}

453

454function on_keydown(event) {

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

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

457 if(state.tab) {

458 state.tab = null;

459 update_tab_list();

460 return;

461 }

462 if(state.modal) {

463 state.modal = null;

464 update_modal_list();

465 return;

466 }

467 }

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

469 if(!state.option.view_mode && !lock) {

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

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

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

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

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

475 state.index = i;

476 canvas.draw_cursor(state);

477 update_hotbar();

478 }

479 }

480 }

481 }

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

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

484 update_modal_list();

485 }

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

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

488 update_modal_list();

489 }

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

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

492 update_modal_list();

493 }

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

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

496 update_modal_list();

497 }

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

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

500 update_modal_list();

501 }

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

503 state.option.view_mode = !state.option.view_mode;

504 update_view_mode_option();

505 }

506 }

507 // alt

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

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

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

511 update_modal_list();

512 }

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

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

515 update_modal_list();

516 }

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

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

519 update_modal_list();

520 }

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

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

523 state.modal = 'settings';

524 update_modal_list();

525 state.tab = 'help';

526 update_tab_list();

527 }

528 else {

529 state.modal = null;

530 update_modal_list();

531 }

532 }

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

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

535 state.modal = 'settings';

536 update_modal_list();

537 state.tab = 'language';

538 update_tab_list();

539 }

540 else {

541 state.modal = null;

542 update_modal_list();

543 }

544 }

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

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

547 state.modal = 'settings';

548 update_modal_list();

549 state.tab = 'device';

550 update_tab_list();

551 }

552 else {

553 state.modal = null;

554 update_modal_list();

555 }

556 }

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

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

559 state.modal = 'settings';

560 update_modal_list();

561 state.tab = 'appearance';

562 update_tab_list();

563 }

564 else {

565 state.modal = null;

566 update_modal_list();

567 }

568 }

569 }

570}

571

572function on_wheel(event) {

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

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

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

576 canvas.on_release(event);

577 state.radius += y;

578 node.scale_input.value = state.radius;

579 canvas.draw_cursor(state);

580 }

581 }

582}

583

584function on_fullscreenchange(event) {

585 update_fullscreen_option();

586}

587

588function fill_hotbar_list() {

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

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

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

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

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

594 state.index = i;

595 canvas.draw_cursor(state);

596 update_hotbar();

597 });

598 if(state.index == i) {

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

600 }

601 }

602 }

603}

604

605function fill_modal_color_list() {

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

607 if(COLORS.length > i) {

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

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

610 state.color = i;

611 state.modal = null;

612 update_modal_list();

613 canvas.draw_cursor(state);

614 state.index = null;

615 update_hotbar();

616 });

617 }

618 }

619}

620

621function show_notification(message) {

622 let code = uuid();

623 state.timestamp = code;

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

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

626 node.notification.animate([

627 { opacity: 0 },

628 { opacity: 1 }

629 ], {

630 duration: 200,

631 fill: 'both'

632 });

633 setTimeout(function() {

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

635 node.notification.animate([

636 { opacity: 1 },

637 { opacity: 0 }

638 ], {

639 duration: 200,

640 fill: 'both'

641 });

642 }

643 }, 2000);

644}