-
Notifications
You must be signed in to change notification settings - Fork 2
/
2-OffscreenCanvas.html
105 lines (93 loc) · 3.68 KB
/
2-OffscreenCanvas.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>OffscreenCanvas Capture</title>
</head>
<body>
<span class="error"></span>
<br>
<label for="interval">Time between images in seconds: </label>
<input id="interval" type="text" value="1" min="1" pattern="\d+">
<br>
<button id="start">Start</button>
<br>
<label for="hideVid">Hide video element: </label>
<input id="hideVid" type="checkbox">
<video autoplay muted playsinline></video>
<p>
Images captured: <span id="image_count">0</span>
</p>
<br>
<button id="stop" disabled>Stop & Show Images</button>
<div id="images"></div>
<script>
// show an error if not supported
if(typeof OffscreenCanvas === 'undefined'){
const supportError = "OffscreenCanvas is not supported by your browser";
const errorSpan = document.querySelector('span.error');
errorSpan.style = "color:red;font-size:larger;font-weight:bold";
errorSpan.innerText = supportError;
console.error(supportError);
}
const startBtn = document.querySelector('button#start');
const stopBtn = document.querySelector('button#stop');
const hideVid = document.querySelector('input#hideVid')
const intervalSec = document.querySelector('input#interval');
const videoElem = document.querySelector('video');
const imageCountSpan = document.querySelector('span#image_count');
const imagesDiv = document.querySelector('div#images');
const storage = []; // Use this array as our database
let stream, captureInterval;
hideVid.onclick = () => videoElem.hidden = hideVid.checked;
startBtn.onclick = async () => {
startBtn.disabled = true;
stream = await navigator.mediaDevices.getUserMedia({video: true});
videoElem.onplaying = () => console.log("video playing stream:", videoElem.srcObject);
videoElem.srcObject = stream;
const canvas = new OffscreenCanvas(1,1); // needs an initial size
const ctx = canvas.getContext("2d");
// (data check on the interval value) * that value is in seconds * frame timestamps are in microseconds
const interval = (parseInt(intervalSec.value) >= 1 ? intervalSec.value * 1 : 1) * 1000;
captureInterval = setInterval(async () => {
// I am not assuming the source video has fixed dimensions
canvas.height = videoElem.videoHeight;
canvas.width = videoElem.videoWidth;
ctx.drawImage(videoElem, 0, 0);
const bitmap = canvas.transferToImageBitmap();
console.log(bitmap);
storage.push(bitmap);
imageCountSpan.innerText++;
}, interval);
stopBtn.disabled = false;
}
stopBtn.onclick = () => {
// stop capture
clearInterval(captureInterval);
// close the camera
stream.getTracks().forEach(track => track.stop());
// Display each image
async function showImages() {
const bitmap = storage.shift();
const width = bitmap.width;
const height = bitmap.height;
console.log(bitmap);
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
// Common method
// const ctx = canvas.getContext("2d");
// ctx.drawImage(frame, 0, 0);
// less resource intensive method but no Safari support
const ctx = canvas.getContext("bitmaprenderer");
ctx.transferFromImageBitmap(bitmap);
imagesDiv.appendChild(canvas);
if (storage.length > 0)
await showImages();
}
console.log("stored images");
showImages();
}
</script>
</body>
</html>