so://phie

outline

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

blob: 3fda7e90f64f7527db525f09028b1275a147e527 (21413 bytes)[doc][blame][raw]

1import { COLORS, PRIORITY_MODAL, LOCALES } from '../constant.js';

2import { uuid, query, prevent } from '../util.js';

3

4import locale from '../mod/locale.js';

5import canvas from './mod/canvas.js';

6

7let node;

8let state = {

9 color: 54,

10 color_list: [54, 2, 17, 27, 37, 12],

11 radius: 14,

12 region: [],

13 crop: false,

14 index: 0,

15 device: null,

16 device_fallback: null,

17 device_list: {

18 mouse: false,

19 touch: false,

20 pen: false,

21 gamepad: false

22 },

23 pencil_list: [

24 'pen',

25 'marker',

26 'eraser'

27 ],

28 pencil: 'pen',

29 timestamp: null,

30 modal: null,

31 tab: null,

32 caption: null,

33 option: {

34 view_mode: false,

35 crop_mode: false

36 },

37 history: [],

38 redo_history: []

39};

40

41export function init() {

42

43 locale.init('en');

44 locale.load('./asset/lang', LOCALES);

45 canvas.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 capture_event_list: { query: '[data-event*=".capture"]', 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 undo_tool: '[data-lang="action.undo"]',

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

72 colors_modal: '.colors-modal',

73 mainbar: '.mainbar',

74 sidebar: '.sidebar',

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

76 content: '.modal .content',

77 caption_list: { query: '[data-event="open.content"]' , all: true },

78 content_list: { query: '.content-item' , all: true },

79 download: '#download'

80 });

81

82 fill_hotbar_list();

83 fill_modal_color_list();

84

85 bind_events(locale);

86

87 node.wrapper.animate([

88 { opacity: 1 },

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

90 ], {

91 delay: 100,

92 duration: 500,

93 fill: 'both'

94 });

95 node.hotbar.animate([

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

97 { opacity: 1 }

98 ], {

99 delay: 600,

100 easing: 'ease-out',

101 duration: 250,

102 fill: 'both'

103 });

104}

105

106function bind_events(locale) {

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

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

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

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

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

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

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

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

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

116 if(event.ctrlKey) {

117 prevent(event);

118 }

119 }, { passive: false });

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

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

122 event.preventDefault();

123 }

124 }, { passive: false });

125

126 for(let item of node.language_list) {

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

128 let code = item.dataset.code;

129 locale.change(code);

130 update_language_list();

131 });

132 }

133 for(let item of node.device_list) {

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

135 let code = item.dataset.code;

136 if(state.device_list[code]) {

137 state.device = code;

138 update_device_list();

139 }

140 });

141 }

142 for(let item of node.pencil_list) {

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

144 state.pencil = item.dataset.code;

145 update_pencil_list();

146 state.modal = null;

147 update_modal_list();

148 });

149 }

150 for(let item of node.modal_event_list) {

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

152 let code = item.dataset.code;

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

154 if(match) {

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

156 switch(match[1]) {

157 case 'toggle':

158 if(code) {

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

160 update_modal_list();

161 }

162 break;

163 case 'open':

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

165 state.modal = code;

166 update_modal_list();

167 }

168 break;

169 case 'close':

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

171 state.modal = null;

172 update_modal_list();

173 }

174 break;

175 default:

176 break;

177 }

178 }

179 }

180 });

181 }

182 for(let item of node.tab_event_list) {

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

184 let code = item.dataset.code;

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

186 if(match) {

187 switch(match[1]) {

188 case 'open':

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

190 state.tab = code;

191 update_tab_list();

192 }

193 break;

194 case 'close':

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

196 state.tab = null;

197 update_tab_list();

198 }

199 break;

200 default:

201 break;

202 }

203 }

204 });

205 }

206 for(let item of node.capture_event_list) {

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

208 let code = item.dataset.code;

209 if(code === 'download') {

210 if(state.option.crop_mode) {

211 state.option.crop_mode = false;

212 canvas.draw_crop(state);

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

214 }

215 let data = canvas.create_capture(0, 0, window.innerWidth, window.innerHeight, 60);

216 if(data) {

217 node.download.download = `outline-${uuid()}.png`;

218 node.download.href = data;

219 node.download.click();

220 }

221 else {

222 console.warn('Unable to create capture from current state!');

223 }

224 }

225 if(code === 'crop') {

226 state.option.crop_mode = true;

227 state.region = [];

228 canvas.draw_crop(state);

229 canvas.get_canvas().style.cursor = 'crosshair';

230 }

231 state.modal = null;

232 state.tab = null;

233 update_modal_list();

234 })

235 }

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

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

238 state.point = {

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

240 y: parseInt(window.innerHeight / 2)

241 };

242 canvas.draw_cursor(state);

243 });

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

245 canvas.clear(state);

246 update_undo_and_redo();

247 show_notification('notification.clear');

248 state.modal = null;

249 update_modal_list();

250 });

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

252 if(!document.fullscreenElement) {

253 document.documentElement.requestFullscreen();

254 }

255 else if(document.exitFullscreen) {

256 document.exitFullscreen();

257 }

258 });

259 window.addEventListener('fullscreenchange', on_fullscreenchange);

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

261 state.option.view_mode = !state.option.view_mode;

262 update_view_mode_option();

263 });

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

265 canvas.undo(state);

266 update_undo_and_redo();

267 });

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

269 canvas.redo(state);

270 update_undo_and_redo();

271 });

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

273 state.option.view_mode = false;

274 update_view_mode_option();

275 });

276 for(let item of node.caption_list) {

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

278 state.caption = item.dataset.code;

279 update_caption_list();

280 });

281 }

282

283 window.addEventListener('beforeunload', function(event) {

284 // ...

285 });

286}

287

288function update_language_list() {

289 let lang = locale.get_current();

290 for(let item of node.language_list) {

291 let code = item.dataset.code;

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

293 }

294}

295

296function update_device_list() {

297 if(!state.option.view_mode && !state.option.crop_mode) {

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

299 }

300 for(let item of node.device_list) {

301 let code = item.dataset.code;

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

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

304 }

305}

306

307function update_pencil_list() {

308 for(let item of node.pencil_list) {

309 let code = item.dataset.code;

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

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

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

313 }

314 }

315}

316

317function update_hotbar() {

318 for(let item of node.hotbar_list) {

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

320 }

321}

322

323function update_focus(bool) {

324 for(let item of node.hotbar_list) {

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

326 }

327 for(let item of node.tool_list) {

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

329 }

330 for(let item of node.action_list) {

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

332 }

333}

334

335function update_modal_list() {

336 for(let item of node.modal_list) {

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

338 }

339 for(let item of node.modal_event_list) {

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

341 }

342 state.tab = null;

343 update_tab_list();

344 state.caption = 'controls';

345 update_caption_list();

346 if(state.modal) {

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

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

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

350 if(modal) {

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

352 modal.animate([

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

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

355 ], {

356 duration: 100,

357 fill: 'both'

358 });

359 }

360 if(button) {

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

362 }

363 }

364}

365

366function update_tab_list() {

367 for(let item of node.modal_list) {

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

369 if(main) {

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

371 main.style.marginLeft = '0';

372 }

373 }

374 for(let item of node.tab_list) {

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

376 }

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

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

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

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

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

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

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

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

385 }

386 }

387}

388

389function update_fullscreen_option() {

390 let fullscreen = document.fullscreenElement;

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

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

393}

394

395function update_view_mode_option() {

396 let view_mode = state.option.view_mode;

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

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

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

400 state.modal = null;

401 state.tab = null;

402 update_modal_list();

403 if(view_mode) {

404 canvas.on_release(null, state);

405 canvas.clear_cursor();

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

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

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

409 node.hotbar.animate([

410 { opacity: 1 },

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

412 ], {

413 easing: 'ease-out',

414 duration: 250,

415 fill: 'both'

416 });

417 node.mainbar.animate([

418 { opacity: 1, marginTop: 0 },

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

420 ], {

421 easing: 'ease-out',

422 duration: 250,

423 fill: 'both'

424 });

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

426 node.sidebar.animate([

427 { opacity: 0 },

428 { opacity: 1 }

429 ], {

430 delay: 250,

431 easing: 'ease-out',

432 duration: 250,

433 fill: 'both'

434 });

435 }

436 else {

437 state.option.crop_mode = false;

438 canvas.draw_crop(state);

439 canvas.on_release(null, state, true);

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

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

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

443 node.hotbar.animate([

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

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

446 ], {

447 easing: 'ease-out',

448 duration: 250,

449 fill: 'both'

450 });

451 node.mainbar.animate([

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

453 { opacity: 1, marginTop: 0 }

454 ], {

455 easing: 'ease-out',

456 duration: 250,

457 fill: 'both'

458 });

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

460 }

461}

462

463function update_undo_and_redo() {

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

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

466}

467

468function update_caption_list() {

469 for(let item of node.caption_list) {

470 let code = item.dataset.code;

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

472 }

473 for(let item of node.content_list) {

474 let code = item.dataset.code;

475 item.classList[code === state.caption ? 'remove' : 'add']('hidden');

476 }

477}

478

479function on_pointerdown(event) {

480 // detect touch

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

482 state.device_list.touch = true;

483 if(state.device) {

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

485 }

486 else {

487 state.device = 'touch';

488 state.device_fallback = 'touch';

489 }

490 update_device_list();

491 }

492 // detect pen

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

494 state.device_list.pen = true;

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

496 if(!state.device) {

497 state.device = 'pen';

498 state.device_fallback = 'pen';

499 }

500 update_device_list();

501 }

502 // drawing

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

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

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

506 update_focus(false);

507 canvas.on_press(event, state);

508 update_undo_and_redo();

509 state.point = {

510 x: event.layerX,

511 y: event.layerY

512 };

513 canvas.draw_cursor(state);

514 }

515 }

516 }

517 if(state.option.view_mode && state.option.crop_mode) {

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

519 state.crop = true;

520 state.region = [];

521 state.region[0] = { x: event.layerX, y: event.layerY };

522 canvas.draw_crop(state);

523 }

524 }

525}

526

527function on_pointermove(event) {

528 // detect mouse

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

530 state.device_list.mouse = true;

531 if(state.device) {

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

533 }

534 else {

535 state.device = 'mouse';

536 state.device_fallback = 'mouse';

537 }

538 update_device_list();

539 }

540 // drawing

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

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

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

544 canvas.on_move(event, state);

545 }

546 state.point = {

547 x: event.layerX,

548 y: event.layerY

549 };

550 }

551 canvas.draw_cursor(state);

552 }

553 if(state.option.view_mode && state.option.crop_mode) {

554 if(state.crop && event.target === canvas.get_canvas()) {

555 state.region[1] = { x: event.layerX, y: event.layerY };

556 canvas.draw_crop(state);

557 }

558 }

559}

560

561function on_pointerup(event) {

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

563 canvas.on_release(event, state);

564 update_undo_and_redo();

565 update_focus(true);

566 }

567 if(state.option.view_mode && state.option.crop_mode) {

568 if(state.crop && event.target === canvas.get_canvas()) {

569 state.crop = false;

570 state.region[1] = { x: event.layerX, y: event.layerY };

571 canvas.draw_crop(state);

572 let [ begin, end ] = state.region;

573 let delta = {

574 x: end.x - begin.x,

575 y: end.y - begin.y

576 };

577 let x = begin.x < end.x ? begin.x : end.x;

578 let y = begin.y < end.y ? begin.y : end.y;

579 if(delta.x < 0) {

580 delta.x *= -1;

581 }

582 if(delta.y < 0) {

583 delta.y *= -1;

584 }

585 if(delta.x != 0 && delta.y != 0) {

586 let data = canvas.create_capture(x, y, delta.x, delta.y);

587 if(data) {

588 node.download.download = `outline-${uuid()}.png`;

589 node.download.href = data;

590 node.download.click();

591 state.option.crop_mode = false;

592 canvas.draw_crop(state);

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

594 }

595 else {

596 console.warn('Unable to create capture from current state!');

597 }

598 }

599 }

600 }

601}

602

603function on_pointerout(event) {

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

605 canvas.on_release(event, state);

606 update_undo_and_redo();

607 state.point = null;

608 canvas.draw_cursor(state);

609 }

610 if(state.option.view_mode && state.option.crop_mode) {

611 if(state.crop && event.target === canvas.get_canvas()) {

612 state.crop = false;

613 state.region = [];

614 canvas.draw_crop(state);

615 }

616 }

617}

618

619function on_keydown(event) {

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

621 if(state.tab) {

622 state.tab = null;

623 update_tab_list();

624 return;

625 }

626 if(state.modal) {

627 state.modal = null;

628 update_modal_list();

629 return;

630 }

631 if(state.option.view_mode && state.option.crop_mode) {

632 state.option.crop_mode = false;

633 canvas.draw_crop(state);

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

635 }

636 }

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

638 if(!state.option.view_mode) {

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

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

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

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

643 canvas.on_release(event, state);

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

645 state.index = i;

646 canvas.draw_cursor(state);

647 update_hotbar();

648 }

649 }

650 }

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

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

653 update_modal_list();

654 }

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

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

657 update_modal_list();

658 }

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

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

661 update_modal_list();

662 }

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

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

665 update_modal_list();

666 }

667 /*

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

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

670 update_modal_list();

671 }

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

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

674 update_modal_list();

675 }

676 */

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

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

679 update_modal_list();

680 }

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

682 canvas.on_release(event, state);

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

684 canvas.draw_cursor(state);

685 update_pencil_list();

686 }

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

688 canvas.on_release(event, state);

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

690 canvas.draw_cursor(state);

691 update_pencil_list();

692 }

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

694 canvas.on_release(event, state);

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

696 canvas.draw_cursor(state);

697 update_pencil_list();

698 }

699 if(event.code === 'KeyF') {

700 if(!document.fullscreenElement) {

701 document.documentElement.requestFullscreen();

702 }

703 else if(document.exitFullscreen) {

704 document.exitFullscreen();

705 }

706 }

707 }

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

709 state.option.view_mode = !state.option.view_mode;

710 update_view_mode_option();

711 }

712 }

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

714 if(event.code === 'KeyY') {

715 canvas.undo(state);

716 update_undo_and_redo();

717 }

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

719 canvas.redo(state);

720 update_undo_and_redo();

721 }

722 }

723}

724

725function on_wheel(event) {

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

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

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

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

730 canvas.on_release(event, state);

731 state.radius += y;

732 node.scale_input.value = state.radius;

733 canvas.draw_cursor(state);

734 }

735 }

736 }

737}

738

739function on_fullscreenchange(event) {

740 update_fullscreen_option();

741}

742

743function fill_hotbar_list() {

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

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

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

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

748 canvas.on_release(event, state);

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

750 state.index = i;

751 canvas.draw_cursor(state);

752 update_hotbar();

753 });

754 if(state.index == i) {

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

756 }

757 }

758 }

759}

760

761function fill_modal_color_list() {

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

763 if(COLORS.length > i) {

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

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

766 canvas.on_release(event, state);

767 state.color = i;

768 state.modal = null;

769 update_modal_list();

770 canvas.draw_cursor(state);

771 state.index = null;

772 update_hotbar();

773 });

774 }

775 }

776}

777

778function show_notification(message) {

779 let code = uuid();

780 state.timestamp = code;

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

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

783 node.notification.animate([

784 { opacity: 0 },

785 { opacity: 1 }

786 ], {

787 duration: 200,

788 fill: 'both'

789 });

790 setTimeout(function() {

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

792 node.notification.animate([

793 { opacity: 1 },

794 { opacity: 0 }

795 ], {

796 duration: 200,

797 fill: 'both'

798 });

799 }

800 }, 1000);

801}