-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
163 lines (147 loc) · 4.42 KB
/
script.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
/**
* @type {HTMLVideoElement}
* */
const videoElem = document.getElementById("video");
const startElem = document.getElementById("start");
const stopElem = document.getElementById("stop");
const playElem = document.getElementById("play");
const downloadElem = document.getElementById("download");
/**
* @type {HTMLCanvasElement}
* */
const canvasElem = document.getElementById("canvas");
const ctxElem = canvasElem.getContext("2d");
ctxElem.imageSmoothingEnabled = true;
ctxElem.imageSmoothingQuality = "high";
canvas.width = videoElem.offsetWidth;
canvas.height = videoElem.offsetHeight;
canvas.style.width = `${videoElem.offsetWidth}px`;
canvas.style.height = `${videoElem.offsetHeight}px`;
const videoForGifOptions = {
fps: 50,
duration: 1000,
};
const frameDuration = 1e3 / videoForGifOptions.fps;
const displayMediaOptions = {
video: {
displaySurface: "window",
},
audio: false,
};
const main = function () {
const videoProcessor = {};
videoProcessor.doLoad = function () {
this.video = document.getElementById("video");
this.canvas = canvasElem;
this.width = this.video.videoWidth;
this.height = this.video.videoHeight;
this.gifFrames = {};
};
videoProcessor.doLoad();
videoProcessor.computeFrame = function () {
this.canvas.width = this.video.videoWidth;
this.canvas.height = this.video.videoHeight;
this.canvas.style.width = `${videoElem.offsetWidth}px`;
this.canvas.style.height = `${videoElem.offsetHeight}px`;
this.width = this.video.videoWidth;
this.height = this.video.videoHeight;
const aspectRatio = this.width / this.height;
const currentFrameIndex = Math.floor(this.video.currentTime * 1000);
ctxElem.drawImage(
this.video,
0,
0,
(this.width * aspectRatio) / 2,
(this.height * aspectRatio) / 2,
);
this.gifFrames[currentFrameIndex] = canvasElem.toDataURL("image/png");
};
videoProcessor.timerCallback = function () {
if (this.video.srcObject === null || this.video.srcObject.active !== true) {
return;
}
try {
videoProcessor.computeFrame();
} catch (err) {
console.log(err);
}
setTimeout(() => {
videoProcessor.timerCallback();
}, frameDuration);
};
videoProcessor.dumpOptionsInfo = function () {
const videoTrack = this.video.srcObject.getVideoTracks()[0];
ctxElem.drawImage(this.video, 0, 0, this.width, this.height);
console.log("Track settings:");
console.log(JSON.stringify(videoTrack.getSettings(), null, 2));
console.log("Track constraints:");
console.log(JSON.stringify(videoTrack.getConstraints(), null, 2));
};
videoProcessor.startCapture = async function () {
try {
this.video.srcObject =
await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
this.gifFrames = {};
videoProcessor.dumpOptionsInfo();
//this.video.paused = false;
videoProcessor.timerCallback();
} catch (err) {
console.error(err);
}
};
videoProcessor.stopCapture = function () {
this.video.srcObject = null;
};
videoProcessor.playFrameByFrame = function () {
const data = this.gifFrames;
if (data) {
Object.entries(data).map(([key, value]) => {
setTimeout(() => {
let image = new Image();
image.onload = function () {
ctxElem.drawImage(image, 0, 0, this.width, this.height);
};
image.src = value;
}, Number(key));
});
}
};
videoProcessor.download = function () {
const downloadFileType = "image/gif";
const downloadFileName = `download-gif-${new Date().toUTCString().replace(/(\s|,|:)/g, "")}.gif`;
console.log(downloadFileType, downloadFileName);
};
startElem.addEventListener(
"click",
() => {
console.log("startCapture");
videoProcessor.startCapture();
},
false,
);
stopElem.addEventListener(
"click",
() => {
console.log("stopCapture");
videoProcessor.stopCapture();
},
false,
);
playElem.addEventListener(
"click",
() => {
console.log("play");
videoProcessor.playFrameByFrame();
},
false,
);
downloadElem.addEventListener(
"click",
() => {
console.log("download");
videoProcessor.download();
},
false,
);
};
main();