aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/scan/+page.svelte
blob: b21a745840786a59077850eaf0febe0a8f208375 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<script>
  import { tick } from "svelte";
  import Icon from "$comp/Icon.svelte";
  import QrScanner from "qr-scanner";
  import { onMount, onDestroy } from "svelte";
  import { back } from "$lib/utils";
  import { goto } from "$app/navigation";
  import { BarcodeDetector } from "barcode-detector/ponyfill";

  let scanner,
    vid = $state(),
    resizing;

  let resize = () => {
    if (resizing) clearTimeout(resizing);
    resizing = setTimeout(initialize, 1000);
  };

  let initialize = async () => {
    if (scanner) {
      scanner.stop();
      await new Promise((r) => setTimeout(r, 1000));
    }

    let options = {
      highlightScanRegion: true,
      highlightCodeOutline: true,
      qrEngine: BarcodeDetector,
    };
    let cb = ({ data }) =>
      scanner.stop() || goto(`/send/${encodeURIComponent(data)}`);
    scanner = new QrScanner(vid, cb, options);
    await tick();
    scanner.start();
  };

  onMount(initialize);
  onDestroy(() => scanner?.stop());
</script>

<svelte:window onresize={resize} />

<div class="flex w-full mb-4 p-4">
  <div class="mx-auto rounded-3xl">
    <video
      bind:this={vid}
      class="border-4 rounded-3xl border-black max-h-[calc(100vh*0.7)]"
    ></video>
  </div>
</div>

<div class="flex justify-center">
  <button class="btn !w-auto" onclick={back}>Cancel</button>
</div>

<style>
  video {
    transition: transform 0.3s ease-out;
    transform: scale(1);
  }
</style>