MCPcopy Create free account
hub / github.com/GoogleChrome/samples / startDrawing

Function startDrawing

requestvideoframecallback/script.js:1–41  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1const startDrawing = () => {
2 const button = document.querySelector("button");
3 const video = document.querySelector("video");
4 const canvas = document.querySelector("canvas");
5 const ctx = canvas.getContext("2d");
6 const fpsInfo = document.querySelector("#fps-info");
7 const metadataInfo = document.querySelector("#metadata-info");
8
9 button.addEventListener('click', () => video.paused ? video.play() : video.pause());
10
11 video.addEventListener('play', () => {
12 if (!('requestVideoFrameCallback' in HTMLVideoElement.prototype)) {
13 return alert('Your browser does not support the `Video.requestVideoFrameCallback()` API.');
14 }
15 });
16
17 let width = canvas.width;
18 let height = canvas.height;
19
20 let paintCount = 0;
21 let startTime = 0.0;
22
23 const updateCanvas = (now, metadata) => {
24 if (startTime === 0.0) {
25 startTime = now;
26 }
27
28 ctx.drawImage(video, 0, 0, width, height);
29
30 const elapsed = (now - startTime) / 1000.0;
31 const fps = (++paintCount / elapsed).toFixed(3);
32 fpsInfo.innerText = !isFinite(fps) ? 0 : fps;
33 metadataInfo.innerText = JSON.stringify(metadata, null, 2);
34
35 video.requestVideoFrameCallback(updateCanvas);
36 };
37
38 video.src =
39 "https://cdn.glitch.com/c162fc32-0a96-4954-83c2-90d4cdb149fc%2FBig_Buck_Bunny_360_10s_20MB.mp4?v=1587545460302";
40 video.requestVideoFrameCallback(updateCanvas);
41};
42
43window.addEventListener('load', startDrawing);

Callers

nothing calls this directly

Calls 1

pauseMethod · 0.45

Tested by

no test coverage detected