soiz1 commited on
Commit
9da37fd
·
verified ·
1 Parent(s): 04a63e9

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +14 -12
index.html CHANGED
@@ -22,32 +22,34 @@
22
  const video = document.getElementById("video");
23
  const canvas = document.getElementById("canvas");
24
  const ctx = canvas.getContext("2d");
 
 
25
 
26
  async function startCapture() {
27
  try {
28
- const pipWindow = await documentPictureInPicture.requestWindow();
29
  const stream = await navigator.mediaDevices.getDisplayMedia({ video: true });
30
  video.srcObject = stream;
31
- video.onloadedmetadata = async () => {
32
  canvas.width = video.videoWidth;
33
  canvas.height = video.videoHeight;
34
- captureFrame(pipWindow);
 
 
 
 
 
35
  };
36
  } catch (err) {
37
  console.error("エラー: ", err);
38
  }
39
  }
40
 
41
- async function captureFrame(pipWindow) {
 
42
  ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
43
- canvas.toBlob(async (blob) => {
44
- const img = document.createElement("img");
45
- img.src = URL.createObjectURL(blob);
46
- pipWindow.document.body.appendChild(img);
47
- img.style.width = "100%";
48
- img.style.height = "100%";
49
- img.style.objectFit = "cover";
50
- }, "image/png");
51
  }
52
 
53
  startButton.addEventListener("click", startCapture);
 
22
  const video = document.getElementById("video");
23
  const canvas = document.getElementById("canvas");
24
  const ctx = canvas.getContext("2d");
25
+ let pipWindow;
26
+ let img;
27
 
28
  async function startCapture() {
29
  try {
30
+ pipWindow = await documentPictureInPicture.requestWindow();
31
  const stream = await navigator.mediaDevices.getDisplayMedia({ video: true });
32
  video.srcObject = stream;
33
+ video.onloadedmetadata = () => {
34
  canvas.width = video.videoWidth;
35
  canvas.height = video.videoHeight;
36
+ img = document.createElement("img");
37
+ img.style.width = "100%";
38
+ img.style.height = "100%";
39
+ img.style.objectFit = "cover";
40
+ pipWindow.document.body.appendChild(img);
41
+ updateFrame();
42
  };
43
  } catch (err) {
44
  console.error("エラー: ", err);
45
  }
46
  }
47
 
48
+ function updateFrame() {
49
+ if (!pipWindow || pipWindow.closed) return;
50
  ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
51
+ img.src = canvas.toDataURL("image/png");
52
+ requestAnimationFrame(updateFrame);
 
 
 
 
 
 
53
  }
54
 
55
  startButton.addEventListener("click", startCapture);