app.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { startViewer, stopViewer, recenter, setJoystick } from './viewer.js';
  2. const STAGES = [
  3. ['frames', 'Klatki z wideo'],
  4. ['colmap', 'Pozycje kamer (COLMAP)'],
  5. ['reconstruct', 'Rekonstrukcja 3D (GenRecon)'],
  6. ['glb', 'Składanie sceny (GLB)'],
  7. ];
  8. const $ = (id) => document.getElementById(id);
  9. let poll = null;
  10. function show(view) {
  11. for (const v of document.querySelectorAll('.view')) v.classList.remove('active');
  12. $(view).classList.add('active');
  13. }
  14. function buildStages() {
  15. $('stages').innerHTML = STAGES
  16. .map(([k, label]) => `<li data-stage="${k}"><span class="dot"></span>${label}</li>`).join('');
  17. }
  18. function updateStages(current, status) {
  19. const order = STAGES.map((s) => s[0]);
  20. const idx = order.indexOf(current);
  21. document.querySelectorAll('#stages li').forEach((li, i) => {
  22. li.classList.toggle('done', status === 'done' || i < idx);
  23. li.classList.toggle('active', status !== 'done' && i === idx);
  24. });
  25. }
  26. async function refresh(id) {
  27. let st;
  28. try { st = await (await fetch(`/api/jobs/${id}`)).json(); } catch { return; }
  29. $('bar-fill').style.width = (st.progress || 0) + '%';
  30. $('progress-msg').textContent = st.message || st.status || '';
  31. $('progress-msg').classList.toggle('error', st.status === 'error');
  32. updateStages(st.stage, st.status);
  33. try {
  34. const log = await (await fetch(`/api/jobs/${id}/log`)).text();
  35. const el = $('log'); el.textContent = log; el.scrollTop = el.scrollHeight;
  36. } catch { /* no log yet */ }
  37. if (st.status === 'done') {
  38. clearInterval(poll); poll = null;
  39. openViewer(`/api/jobs/${id}/scene.glb`);
  40. } else if (st.status === 'error') {
  41. clearInterval(poll); poll = null;
  42. $('progress-msg').textContent = '⚠ ' + (st.error || 'błąd rekonstrukcji');
  43. }
  44. }
  45. async function upload(file) {
  46. if (!file) return;
  47. buildStages();
  48. $('bar-fill').style.width = '0'; $('progress-msg').textContent = 'Wysyłanie filmu…'; $('log').textContent = '';
  49. show('view-progress');
  50. const fd = new FormData(); fd.append('video', file);
  51. let res;
  52. try { res = await (await fetch('/api/jobs', { method: 'POST', body: fd })).json(); }
  53. catch (e) { $('progress-msg').classList.add('error'); $('progress-msg').textContent = 'Nie udało się wysłać: ' + e; return; }
  54. if (res.error) { $('progress-msg').classList.add('error'); $('progress-msg').textContent = res.error; return; }
  55. poll = setInterval(() => refresh(res.id), 1500);
  56. refresh(res.id);
  57. }
  58. function openViewer(url) {
  59. show('view-viewer');
  60. document.body.classList.toggle('touch', matchMedia('(pointer: coarse)').matches);
  61. startViewer(url).catch((e) => {
  62. show('view-upload');
  63. alert('Nie udało się załadować sceny 3D: ' + e);
  64. });
  65. }
  66. function backToUpload() {
  67. if (poll) { clearInterval(poll); poll = null; }
  68. stopViewer();
  69. show('view-upload');
  70. }
  71. // --- mobile joystick ---
  72. function wireJoystick() {
  73. const stick = $('joystick'), knob = $('joystick-knob');
  74. let active = null, cx = 0, cy = 0, R = 45;
  75. const set = (dx, dy) => {
  76. const len = Math.hypot(dx, dy) || 1, cl = Math.min(len, R);
  77. dx = dx / len * cl; dy = dy / len * cl;
  78. knob.style.transform = `translate(${dx}px,${dy}px)`;
  79. setJoystick(dx / R, -dy / R); // up = forward
  80. };
  81. stick.addEventListener('pointerdown', (e) => {
  82. active = e.pointerId; const r = stick.getBoundingClientRect();
  83. cx = r.left + r.width / 2; cy = r.top + r.height / 2;
  84. stick.setPointerCapture(e.pointerId); set(e.clientX - cx, e.clientY - cy);
  85. });
  86. stick.addEventListener('pointermove', (e) => { if (e.pointerId === active) set(e.clientX - cx, e.clientY - cy); });
  87. const end = (e) => { if (e.pointerId === active) { active = null; knob.style.transform = ''; setJoystick(0, 0); } };
  88. stick.addEventListener('pointerup', end);
  89. stick.addEventListener('pointercancel', end);
  90. }
  91. function init() {
  92. const drop = $('drop'), input = $('file-input');
  93. input.addEventListener('change', () => upload(input.files[0]));
  94. ['dragenter', 'dragover'].forEach((ev) => drop.addEventListener(ev, (e) => { e.preventDefault(); drop.classList.add('drag'); }));
  95. ['dragleave', 'drop'].forEach((ev) => drop.addEventListener(ev, (e) => { e.preventDefault(); drop.classList.remove('drag'); }));
  96. drop.addEventListener('drop', (e) => upload(e.dataTransfer.files[0]));
  97. $('btn-demo').addEventListener('click', () => openViewer('./demo.glb'));
  98. $('btn-cancel').addEventListener('click', backToUpload);
  99. $('btn-new').addEventListener('click', backToUpload);
  100. $('btn-recenter').addEventListener('click', recenter);
  101. wireJoystick();
  102. if ('serviceWorker' in navigator) navigator.serviceWorker.register('./sw.js').catch(() => {});
  103. // Deep-link: /?job=<id> resumes (or opens) a specific reconstruction, so a
  104. // finished scene has a shareable URL and progress survives a reload.
  105. const jobId = new URLSearchParams(location.search).get('job');
  106. if (jobId) {
  107. buildStages();
  108. show('view-progress');
  109. poll = setInterval(() => refresh(jobId), 1500);
  110. refresh(jobId);
  111. }
  112. }
  113. init();