-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQRSocket.js
217 lines (173 loc) · 5.82 KB
/
QRSocket.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import QRious from 'qrious'
import { SocketBase } from './lib/handler.js';
const blank = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNgYAAAAAMAASsJTYQAAAAASUVORK5CYII='
const className = "✨" + Math.random().toString(36).slice(2)
const template = new DOMParser().parseFromString(`
<section class="${className}">
<figure class="qr">
<img src="${blank}"/>
<figcaption>[]</figcaption>
</figure>
<figure class="video">
<canvas width="300" height="300"></canvas>
<figcaption>[]</figcaption>
</figure>
<style>
.${className} {
display: flex;
align-items: center;
justify-items: top;
width: 90vmin;
height: 90vmin;
margin:auto;
}
.${className} img {
image-rendering: pixelated;
height: 50vmin;
width: 50vmin;
aspect-ratio: 1 / 1;
}
.${className} canvas {
height: 25vmin;
width: 25vmin;
object-fit:cover;
}
.${className} figcaption {
width: 25vmin;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.${className} .qr figcaption{
width: 50vmin;
}
@media (orientation: landscape) {
.${className} {
flex-direction: row;
}
}
@media (orientation: portrait) {
.${className} {
flex-direction: column;
}
}
</style>
</section>
`, "text/html").body.firstChild;
const detector = new BarcodeDetector({
formats: ["qr_code"],
});
const chunking = true;
export class QRSocket extends EventTarget {
constructor(options = {}) {
super();
this.sock = new SocketBase(document.location.href)
window.q = this
this.element = template.cloneNode(true)
this.queue = []
this.txi = 0;
this.rxi = -1;
this.options = options;
this.currenChunk = '';
(options.target || document.body).appendChild(this.element);
this.start()
}
async send(data) {
setTimeout(() => { this.showLatest() }, 10)
return this.sock.enqueueMessage(data)
}
showLatest() {
this.setQR('rx', this.sock.current);
}
async start() {
this.setQR('rx', this.sock.current);
const stream = this.stream = await navigator.mediaDevices.getUserMedia({
video: {
facingMode: 'user'
},
audio: false
});
const canvas = this.element.querySelector('canvas');
const ctx = canvas.getContext('2d')
// const video = this.element.querySelector('video');
const video = document.createElement('video');
video.srcObject = stream;
// trying to get ios not going fullscreen
video.autoplay = true;
video.playsinline = true;
video.muted = true; // Mute the video
video.setAttribute('playsinline', ''); // Set playsinline attribute
video.addEventListener('canplay', () => {
canvas.width = video.videoWidth || 100;
canvas.height = video.videoHeight || 100;
ctx.drawImage(video, 0, 0, canvas.width, canvas.height)
})
try {
await video.play()
} catch (e) {
console.log("adding play on click")
document.body.addEventListener('touchstart', (e) => {
e.preventDefault()
video.play()
}, { once: true })
}
this.running = true;
const check = async () => {
await new Promise(re => setTimeout(re, 2000))
while (this.running) {
try {
const codes = await detector.detect(video)
ctx.drawImage(video, 0, 0, canvas.width, canvas.height)
for (const code of codes) {
const completed = this.sock.handleObservation(code.rawValue);
if (completed) {
this.dispatchEvent(
new MessageEvent('message', { data: completed })
)
}
this.element.querySelector('.video figcaption').innerText = code.rawValue
// console.log(code)
ctx.beginPath()
const firstPoint = code.cornerPoints[0];
ctx.moveTo(firstPoint.x, firstPoint.y); // Set the starting point
for (const { x, y } of code.cornerPoints) {
ctx.lineTo(x, y)
console.log(x, y)
}
ctx.closePath()
ctx.fillStyle = '#f088'
ctx.strokeWidth = 20
ctx.fill()
}
if (codes.length > 0) {
this.setQR('rx', this.sock.current);
}
// avoid when tab is hidden
await new Promise(requestAnimationFrame)
} catch (e) {// video not ready?
console.error(e)
await new Promise(re => setTimeout(re, 1000))
}
}
}
check()
}
setQR(name, value) {
console.log("setQR:", name, value)
new QRious({
element: this.element.querySelector(`img`),
value
});
this.element.querySelector('.qr figcaption').innerText = value
}
stop(discardStream = true) {
this.running = false;
const video = this.element.querySelector('video')
if (discardStream) {
video.srcObject.getTracks().forEach(function (track) {
track.stop();
});
}
this.element.remove()
}
}