-
Notifications
You must be signed in to change notification settings - Fork 0
/
QRScan.svelte
118 lines (98 loc) 路 3.24 KB
/
QRScan.svelte
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<script lang="ts">
import { onMount } from 'svelte';
import { Html5Qrcode } from 'html5-qrcode';
import { goto } from '$app/navigation';
let html5Qrcode
onMount(init)
function init() {
html5Qrcode = new Html5Qrcode('reader');
html5Qrcode.start(
{ facingMode: 'environment' },
{
fps: 10,
qrbox: { width: 250, height: 250 },
aspectRatio: 0.5625
},
onScanSuccess,
onScanFailure
);
}
async function onScanSuccess(decodedText, decodedResult) {
try {
const url = new URL(decodedText);
if (url.host != window.location.host) return;
const path = url.pathname;
if (!path.startsWith('/events/join/')) return;
const eventId = path.substring(12);
html5Qrcode.stop();
goto(path);
} catch {
// ignore, not a URL that was scanned
alert('error url ')
}
}
function onScanFailure(error) {
console.warn(`Code scan error = ${error}`)
}
let canvas: HTMLCanvasElement | undefined;
let video: HTMLVideoElement | undefined;
let loading = false;
let videoWidth = 0;
let videoHeight = 0;
const requestVideoCamera = async () => {
try {
loading = true;
const stream = await window.navigator.mediaDevices.getUserMedia({
audio: false,
video: {
facingMode: 'environment',
aspectRatio: 1.3333333333,
width: { min: 2560 }
}
});
video.srcObject = stream;
const settings = stream.getTracks()[0].getSettings();
videoHeight = settings.height;
videoWidth = settings.width;
video.play();
loading = false;
} catch (error) {
alert(error);
console.log(error);
}
};
onMount(requestVideoCamera);
</script>
<style>
reader {
position: absolute !important;
}
</style>
<main class="h-full w-full relative flex flex-col justify-between items-center">
<reader id="reader" class="absolute w-full h-full bg-black object-cover pointer-events-none -z-20" />
<!-- svelte-ignore a11y-media-has-caption -->
<video
bind:this={video}
class="h-full w-full absolute bg-black object-cover pointer-events-none -z-10"
playsinline
/>
<section class="text-white py-8 text-center">
<h1 class="font-bold text-4xl">ReLive</h1>
<div class="">Scan QR code to<br>join an event</div>
</section>
<section class="w-56 h-56 p-4 relative">
<img src="/images/qr-code-placeholder.png" alt="QR code" class="opacity-25 object-cover"/>
<div class="absolute border-t-2 border-l-2 top-5 left-5 w-10 h-10 border-gray-400"></div>
<div class="absolute border-t-2 border-r-2 top-5 right-5 w-10 h-10 border-gray-400"></div>
<div class="absolute border-b-2 border-l-2 bottom-5 left-5 w-10 h-10 border-gray-400"></div>
<div class="absolute border-b-2 border-r-2 bottom-5 right-5 w-10 h-10 border-gray-400"></div>
</section>
<section class="flex flex-row w-full justify-end my-8">
<a href="/events/create" type="submit" class="flex justify-center items-center ml-auto text-white font-bold w-16 h-16 rounded-full mr-8 bg-accent">
<i class="fa fa-plus text-2xl mx-auto"></i>
</a>
</section>
</main>
<div class="w-0 h-0 overflow-hidden">
<canvas id="canvas" bind:this={canvas} class="opacity-0" />
</div>