so://phie

outline

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

blob: 690b0645158c28a3b849deb07674e105625068bc (16809 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 header: '.header-list',

53 hotbar: '.hotbar',

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

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

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

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

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

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

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

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

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

63 notification: '.notification',

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

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

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

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

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

69 });

70

71 fill_hotbar_list();

72 fill_modal_color_list();

73

74 bind_events();

75

76 node.wrapper.animate([

77 { opacity: 1 },

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

79 ], {

80 delay: 100,

81 duration: 500,

82 fill: 'both'

83 });

84 node.hotbar.animate([

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

86 { opacity: 1 }

87 ], {

88 delay: 600,

89 easing: 'ease-out',

90 duration: 250,

91 fill: 'both'

92 });

93}

94

95function bind_events() {

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

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

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

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

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

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

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

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

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

105 if(event.ctrlKey) {

106 prevent(event);

107 }

108 }, { passive: false });

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

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

111 event.preventDefault();

112 }

113 }, { passive: false });

114

115 for(let item of node.language_list) {

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

117 let code = item.dataset.code;

118 locale.change(code);

119 update_language_list();

120 });

121 }

122 for(let item of node.device_list) {

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

124 let code = item.dataset.code;

125 if(state.device_list[code]) {

126 state.device = code;

127 update_device_list();

128 }

129 });

130 }

131 for(let item of node.pencil_list) {

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

133 state.pencil = item.dataset.code;

134 update_pencil_list();

135 state.modal = null;

136 update_modal_list();

137 });

138 }

139 for(let item of node.modal_event_list) {

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

141 let code = item.dataset.code;

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

143 if(match) {

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

145 switch(match[1]) {

146 case 'toggle':

147 if(code) {

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

149 update_modal_list();

150 }

151 break;

152 case 'open':

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

154 state.modal = code;

155 update_modal_list();

156 }

157 break;

158 case 'close':

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

160 state.modal = null;

161 update_modal_list();

162 }

163 break;

164 default:

165 break;

166 }

167 }

168 }

169 });

170 }

171 for(let item of node.tab_event_list) {

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

173 let code = item.dataset.code;

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

175 if(match) {

176 switch(match[1]) {

177 case 'open':

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

179 state.tab = code;

180 update_tab_list();

181 }

182 break;

183 case 'close':

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

185 state.tab = null;

186 update_tab_list();

187 }

188 break;

189 default:

190 break;

191 }

192 }

193 });

194 }

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

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

197 state.point = {

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

199 y: parseInt(window.innerHeight / 2)

200 };

201 canvas.draw_cursor(state);

202 });

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

204 canvas.clear();

205 show_notification('notification.clear');

206 state.modal = null;

207 update_modal_list();

208 });

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

210 if(!document.fullscreenElement) {

211 document.documentElement.requestFullscreen();

212 }

213 else if(document.exitFullscreen) {

214 document.exitFullscreen();

215 }

216 });

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

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

219 state.option.view_mode = !state.option.view_mode;

220 update_view_mode_option();

221 });

222}

223

224function update_language_list() {

225 let lang = locale.get_current();

226 for(let item of node.language_list) {

227 let code = item.dataset.code;

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

229 }

230}

231

232function update_device_list() {

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

234 for(let item of node.device_list) {

235 let code = item.dataset.code;

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

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

238 }

239}

240

241function update_pencil_list() {

242 for(let item of node.pencil_list) {

243 let code = item.dataset.code;

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

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

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

247 }

248 }

249}

250

251function update_hotbar() {

252 canvas.on_release(event);

253 for(let item of node.hotbar_list) {

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

255 }

256}

257

258function update_focus(bool) {

259 for(let item of node.hotbar_list) {

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

261 }

262 for(let item of node.tool_list) {

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

264 }

265 for(let item of node.action_list) {

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

267 }

268}

269

270function update_modal_list() {

271 for(let item of node.modal_list) {

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

273 }

274 for(let item of node.modal_event_list) {

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

276 }

277 state.tab = null;

278 update_tab_list();

279 if(state.modal) {

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

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

282 if(modal) {

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

284 modal.animate([

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

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

287 ], {

288 duration: 100,

289 fill: 'both'

290 });

291 }

292 if(button) {

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

294 }

295 }

296}

297

298function update_tab_list() {

299 for(let item of node.modal_list) {

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

301 if(main) {

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

303 main.style.marginLeft = '0';

304 }

305 }

306 for(let item of node.tab_list) {

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

308 }

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

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

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

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

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

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

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

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

317 }

318 }

319}

320

321function update_fullscreen_option() {

322 let fullscreen = document.fullscreenElement;

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

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

325}

326

327function update_view_mode_option() {

328 let view_mode = state.option.view_mode;

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

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

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

332 if(view_mode) {

333 state.modal = null;

334 state.tab = null;

335 update_modal_list();

336 canvas.on_release();

337 canvas.clear_cursor();

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

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

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

341 node.hotbar.animate([

342 { opacity: 1 },

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

344 ], {

345 easing: 'ease-out',

346 duration: 250,

347 fill: 'both'

348 });

349 node.header.animate([

350 { opacity: 1 },

351 { opacity: 0 }

352 ], {

353 easing: 'ease-out',

354 duration: 250,

355 fill: 'both'

356 });

357 }

358 else {

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

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

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

362 node.hotbar.animate([

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

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

365 ], {

366 easing: 'ease-out',

367 duration: 250,

368 fill: 'both'

369 });

370 node.header.animate([

371 { opacity: 0 },

372 { opacity: 1 }

373 ], {

374 easing: 'ease-out',

375 duration: 250,

376 fill: 'both'

377 });

378 }

379}

380

381function on_pointerdown(event) {

382 // detect touch

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

384 state.device_list.touch = true;

385 if(state.device) {

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

387 }

388 else {

389 state.device = 'touch';

390 state.device_fallback = 'touch';

391 }

392 update_device_list();

393 }

394 // detect pen

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

396 state.device_list.pen = true;

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

398 if(!state.device) {

399 state.device = 'pen';

400 state.device_fallback = 'pen';

401 }

402 update_device_list();

403 }

404 // drawing

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

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

407 update_focus(false);

408 canvas.on_press(event, state);

409 state.point = {

410 x: event.layerX,

411 y: event.layerY

412 };

413 canvas.draw_cursor(state);

414 }

415 }

416}

417

418function on_pointermove(event) {

419 // detect mouse

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

421 state.device_list.mouse = true;

422 if(state.device) {

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

424 }

425 else {

426 state.device = 'mouse';

427 state.device_fallback = 'mouse';

428 }

429 update_device_list();

430 }

431 // drawing

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

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

434 canvas.on_move(event, state);

435 state.point = {

436 x: event.layerX,

437 y: event.layerY

438 };

439 canvas.draw_cursor(state);

440 }

441 }

442}

443

444function on_pointerup(event) {

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

446 canvas.on_release(event);

447 canvas.draw_cursor(state);

448 update_focus(true);

449 }

450}

451

452function on_pointerout(event) {

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

454 canvas.on_release(event);

455 state.point = null;

456 canvas.draw_cursor(state);

457 }

458}

459

460function on_keydown(event) {

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

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

463 if(state.tab) {

464 state.tab = null;

465 update_tab_list();

466 return;

467 }

468 if(state.modal) {

469 state.modal = null;

470 update_modal_list();

471 return;

472 }

473 }

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

475 if(!state.option.view_mode) {

476 if(!lock) {

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

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

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

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

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

482 state.index = i;

483 canvas.draw_cursor(state);

484 update_hotbar();

485 }

486 }

487 }

488 }

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

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

491 update_modal_list();

492 }

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

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

495 update_modal_list();

496 }

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

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

499 update_modal_list();

500 }

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

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

503 update_modal_list();

504 }

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

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

507 update_modal_list();

508 }

509 }

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

511 state.option.view_mode = !state.option.view_mode;

512 update_view_mode_option();

513 }

514 }

515 // alt

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

517 if(!state.option.view_mode) {

518 if(!lock) {

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

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

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

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

523 canvas.on_release(event);

524 canvas.draw_cursor(state);

525 update_pencil_list();

526 }

527 }

528 }

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

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

531 update_modal_list();

532 }

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

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

535 update_modal_list();

536 }

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

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

539 update_modal_list();

540 }

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

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

543 state.modal = 'more';

544 update_modal_list();

545 state.tab = 'help';

546 update_tab_list();

547 }

548 else {

549 state.modal = null;

550 update_modal_list();

551 }

552 }

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

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

555 state.modal = 'more';

556 update_modal_list();

557 state.tab = 'language';

558 update_tab_list();

559 }

560 else {

561 state.modal = null;

562 update_modal_list();

563 }

564 }

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

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

567 state.modal = 'more';

568 update_modal_list();

569 state.tab = 'device';

570 update_tab_list();

571 }

572 else {

573 state.modal = null;

574 update_modal_list();

575 }

576 }

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

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

579 state.modal = 'more';

580 update_modal_list();

581 state.tab = 'appearance';

582 update_tab_list();

583 }

584 else {

585 state.modal = null;

586 update_modal_list();

587 }

588 }

589 }

590 }

591}

592

593function on_wheel(event) {

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

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

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

597 canvas.on_release(event);

598 state.radius += y;

599 node.scale_input.value = state.radius;

600 canvas.draw_cursor(state);

601 }

602 }

603}

604

605function on_fullscreenchange(event) {

606 update_fullscreen_option();

607}

608

609function fill_hotbar_list() {

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

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

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

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

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

615 state.index = i;

616 canvas.draw_cursor(state);

617 update_hotbar();

618 });

619 if(state.index == i) {

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

621 }

622 }

623 }

624}

625

626function fill_modal_color_list() {

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

628 if(COLORS.length > i) {

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

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

631 state.color = i;

632 state.modal = null;

633 update_modal_list();

634 canvas.draw_cursor(state);

635 state.index = null;

636 update_hotbar();

637 });

638 }

639 }

640}

641

642function show_notification(message) {

643 let code = uuid();

644 state.timestamp = code;

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

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

647 node.notification.animate([

648 { opacity: 0 },

649 { opacity: 1 }

650 ], {

651 duration: 200,

652 fill: 'both'

653 });

654 setTimeout(function() {

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

656 node.notification.animate([

657 { opacity: 1 },

658 { opacity: 0 }

659 ], {

660 duration: 200,

661 fill: 'both'

662 });

663 }

664 }, 2000);

665}