so://phie

outline

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

blob: 4ce222a819a16b6e3f7d4fe562c6990f0d163221 (16734 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 switch(match[1]) {

145 case 'toggle':

146 if(code) {

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

148 update_modal_list();

149 }

150 break;

151 case 'open':

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

153 state.modal = code;

154 update_modal_list();

155 }

156 break;

157 case 'close':

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

159 state.modal = null;

160 update_modal_list();

161 }

162 break;

163 default:

164 break;

165 }

166 }

167 });

168 }

169 for(let item of node.tab_event_list) {

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

171 let code = item.dataset.code;

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

173 if(match) {

174 switch(match[1]) {

175 case 'open':

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

177 state.tab = code;

178 update_tab_list();

179 }

180 break;

181 case 'close':

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

183 state.tab = null;

184 update_tab_list();

185 }

186 break;

187 default:

188 break;

189 }

190 }

191 });

192 }

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

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

195 state.point = {

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

197 y: parseInt(window.innerHeight / 2)

198 };

199 canvas.draw_cursor(state);

200 });

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

202 canvas.clear();

203 show_notification('notification.clear');

204 state.modal = null;

205 update_modal_list();

206 });

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

208 if(!document.fullscreenElement) {

209 document.documentElement.requestFullscreen();

210 }

211 else if(document.exitFullscreen) {

212 document.exitFullscreen();

213 }

214 });

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

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

217 state.option.view_mode = !state.option.view_mode;

218 update_view_mode_option();

219 });

220}

221

222function update_language_list() {

223 let lang = locale.get_current();

224 for(let item of node.language_list) {

225 let code = item.dataset.code;

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

227 }

228}

229

230function update_device_list() {

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

232 for(let item of node.device_list) {

233 let code = item.dataset.code;

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

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

236 }

237}

238

239function update_pencil_list() {

240 for(let item of node.pencil_list) {

241 let code = item.dataset.code;

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

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

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

245 }

246 }

247}

248

249function update_hotbar() {

250 canvas.on_release(event);

251 for(let item of node.hotbar_list) {

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

253 }

254}

255

256function update_focus(bool) {

257 for(let item of node.hotbar_list) {

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

259 }

260 for(let item of node.tool_list) {

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

262 }

263 for(let item of node.action_list) {

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

265 }

266}

267

268function update_modal_list() {

269 for(let item of node.modal_list) {

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

271 }

272 for(let item of node.modal_event_list) {

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

274 }

275 state.tab = null;

276 update_tab_list();

277 if(state.modal) {

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

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

280 if(modal) {

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

282 modal.animate([

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

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

285 ], {

286 duration: 100,

287 fill: 'both'

288 });

289 }

290 if(button) {

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

292 }

293 }

294}

295

296function update_tab_list() {

297 for(let item of node.modal_list) {

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

299 if(main) {

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

301 main.style.marginLeft = '0';

302 }

303 }

304 for(let item of node.tab_list) {

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

306 }

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

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

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

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

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

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

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

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

315 }

316 }

317}

318

319function update_fullscreen_option() {

320 let fullscreen = document.fullscreenElement;

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

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

323}

324

325function update_view_mode_option() {

326 let view_mode = state.option.view_mode;

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

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

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

330 if(view_mode) {

331 state.modal = null;

332 state.tab = null;

333 update_modal_list();

334 canvas.on_release();

335 canvas.clear_cursor();

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

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

338 node.header.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.header.animate([

348 { opacity: 1 },

349 { opacity: 0 }

350 ], {

351 easing: 'ease-out',

352 duration: 250,

353 fill: 'both'

354 });

355 }

356 else {

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

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

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

360 node.hotbar.animate([

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

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

363 ], {

364 easing: 'ease-out',

365 duration: 250,

366 fill: 'both'

367 });

368 node.header.animate([

369 { opacity: 0 },

370 { opacity: 1 }

371 ], {

372 easing: 'ease-out',

373 duration: 250,

374 fill: 'both'

375 });

376 }

377}

378

379function on_pointerdown(event) {

380 // detect touch

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

382 state.device_list.touch = true;

383 if(state.device) {

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

385 }

386 else {

387 state.device = 'touch';

388 state.device_fallback = 'touch';

389 }

390 update_device_list();

391 }

392 // detect pen

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

394 state.device_list.pen = true;

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

396 if(!state.device) {

397 state.device = 'pen';

398 state.device_fallback = 'pen';

399 }

400 update_device_list();

401 }

402 // drawing

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

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

405 update_focus(false);

406 canvas.on_press(event, state);

407 state.point = {

408 x: event.layerX,

409 y: event.layerY

410 };

411 canvas.draw_cursor(state);

412 }

413 }

414}

415

416function on_pointermove(event) {

417 // detect mouse

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

419 state.device_list.mouse = true;

420 if(state.device) {

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

422 }

423 else {

424 state.device = 'mouse';

425 state.device_fallback = 'mouse';

426 }

427 update_device_list();

428 }

429 // drawing

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

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

432 canvas.on_move(event, state);

433 state.point = {

434 x: event.layerX,

435 y: event.layerY

436 };

437 canvas.draw_cursor(state);

438 }

439 }

440}

441

442function on_pointerup(event) {

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

444 canvas.on_release(event);

445 canvas.draw_cursor(state);

446 update_focus(true);

447 }

448}

449

450function on_pointerout(event) {

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

452 canvas.on_release(event);

453 state.point = null;

454 canvas.draw_cursor(state);

455 }

456}

457

458function on_keydown(event) {

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

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

461 if(state.tab) {

462 state.tab = null;

463 update_tab_list();

464 return;

465 }

466 if(state.modal) {

467 state.modal = null;

468 update_modal_list();

469 return;

470 }

471 }

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

473 if(!state.option.view_mode) {

474 if(!lock) {

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

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

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

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

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

480 state.index = i;

481 canvas.draw_cursor(state);

482 update_hotbar();

483 }

484 }

485 }

486 }

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

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

489 update_modal_list();

490 }

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

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

493 update_modal_list();

494 }

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

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

497 update_modal_list();

498 }

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

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

501 update_modal_list();

502 }

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

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

505 update_modal_list();

506 }

507 }

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

509 state.option.view_mode = !state.option.view_mode;

510 update_view_mode_option();

511 }

512 }

513 // alt

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

515 if(!state.option.view_mode) {

516 if(!lock) {

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

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

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

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

521 canvas.on_release(event);

522 canvas.draw_cursor(state);

523 update_pencil_list();

524 }

525 }

526 }

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

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

529 update_modal_list();

530 }

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

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

533 update_modal_list();

534 }

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

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

537 update_modal_list();

538 }

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

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

541 state.modal = 'more';

542 update_modal_list();

543 state.tab = 'help';

544 update_tab_list();

545 }

546 else {

547 state.modal = null;

548 update_modal_list();

549 }

550 }

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

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

553 state.modal = 'more';

554 update_modal_list();

555 state.tab = 'language';

556 update_tab_list();

557 }

558 else {

559 state.modal = null;

560 update_modal_list();

561 }

562 }

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

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

565 state.modal = 'more';

566 update_modal_list();

567 state.tab = 'device';

568 update_tab_list();

569 }

570 else {

571 state.modal = null;

572 update_modal_list();

573 }

574 }

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

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

577 state.modal = 'more';

578 update_modal_list();

579 state.tab = 'appearance';

580 update_tab_list();

581 }

582 else {

583 state.modal = null;

584 update_modal_list();

585 }

586 }

587 }

588 }

589}

590

591function on_wheel(event) {

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

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

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

595 canvas.on_release(event);

596 state.radius += y;

597 node.scale_input.value = state.radius;

598 canvas.draw_cursor(state);

599 }

600 }

601}

602

603function on_fullscreenchange(event) {

604 update_fullscreen_option();

605}

606

607function fill_hotbar_list() {

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

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

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

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

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

613 state.index = i;

614 canvas.draw_cursor(state);

615 update_hotbar();

616 });

617 if(state.index == i) {

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

619 }

620 }

621 }

622}

623

624function fill_modal_color_list() {

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

626 if(COLORS.length > i) {

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

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

629 state.color = i;

630 state.modal = null;

631 update_modal_list();

632 canvas.draw_cursor(state);

633 state.index = null;

634 update_hotbar();

635 });

636 }

637 }

638}

639

640function show_notification(message) {

641 let code = uuid();

642 state.timestamp = code;

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

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

645 node.notification.animate([

646 { opacity: 0 },

647 { opacity: 1 }

648 ], {

649 duration: 200,

650 fill: 'both'

651 });

652 setTimeout(function() {

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

654 node.notification.animate([

655 { opacity: 1 },

656 { opacity: 0 }

657 ], {

658 duration: 200,

659 fill: 'both'

660 });

661 }

662 }, 2000);

663}