Skip to content

Commit 7fa7446

Browse files
author
joeyklee
committed
Merge branch 'main' into joeyklee.adds-callcallback-tests
2 parents 8a18de7 + af1e447 commit 7fa7446

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

.all-contributorsrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,17 @@
12971297
"code",
12981298
"example"
12991299
]
1300+
},
1301+
{
1302+
"login": "lindapaiste",
1303+
"name": "lindapaiste",
1304+
"avatar_url": "https://avatars.githubusercontent.com/u/28965286?v=4",
1305+
"profile": "http://lindapaiste.com",
1306+
"contributions": [
1307+
"code",
1308+
"ideas",
1309+
"bug"
1310+
]
13001311
}
13011312
],
13021313
"contributorsPerLine": 7,

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
338338
</tr>
339339
<tr>
340340
<td align="center"><a href="https://github.com/asvsfs"><img src="https://avatars.githubusercontent.com/u/119840?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Amir</b></sub></a><br /><a href="https://github.com/ml5js/ml5-library/commits?author=asvsfs" title="Code">💻</a> <a href="#example-asvsfs" title="Examples">💡</a></td>
341+
<td align="center"><a href="http://lindapaiste.com"><img src="https://avatars.githubusercontent.com/u/28965286?v=4?s=100" width="100px;" alt=""/><br /><sub><b>lindapaiste</b></sub></a><br /><a href="https://github.com/ml5js/ml5-library/commits?author=lindapaiste" title="Code">💻</a> <a href="#ideas-lindapaiste" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/ml5js/ml5-library/issues?q=author%3Alindapaiste" title="Bug reports">🐛</a></td>
341342
</tr>
342343
</table>
343344

src/StyleTransfer/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class StyleTransfer extends Video {
5454
async load(model) {
5555
if (this.videoElt) {
5656
await this.loadVideo();
57-
this.videoReady = true;
5857
}
5958
await this.loadCheckpoints(model);
6059
return this;

src/utils/Video.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,51 @@ Image and Video base class
88
*/
99

1010
class Video {
11+
/**
12+
* @property {HTMLVideoElement} [video]
13+
*/
14+
15+
/**
16+
* @param {HTMLVideoElement | p5.Video | null | undefined} [video] - Can pass a video
17+
* into the constructor of the model in order to run the model on every frame of the video.
18+
* @param {number | null | undefined} [size] - The size expected by the underlying model.
19+
* NOT the size of the current video. The size will be used to resize the current video.
20+
*/
1121
constructor(video, size) {
22+
/**
23+
* @type {HTMLVideoElement | null}
24+
*/
1225
this.videoElt = null;
26+
/**
27+
* @type {number | null | undefined}
28+
*/
1329
this.size = size;
30+
/**
31+
* @type {boolean}
32+
*/
1433
this.videoReady = false;
1534

16-
if (video instanceof HTMLVideoElement) {
17-
this.videoElt = video;
18-
} else if (video !== null && typeof video === 'object' && video.elt instanceof HTMLVideoElement) {
19-
// Handle p5.js video element
20-
this.videoElt = video.elt;
35+
if (typeof HTMLVideoElement !== 'undefined') {
36+
if (video instanceof HTMLVideoElement) {
37+
this.videoElt = video;
38+
} else if (video !== null && typeof video === 'object' && video.elt instanceof HTMLVideoElement) {
39+
// Handle p5.js video element
40+
this.videoElt = video.elt;
41+
}
2142
}
2243
}
2344

45+
/**
46+
* Copies the stream from the source video into a new video element.
47+
* The copied element is set to property `this.video` and is also returned by the function.
48+
* @returns {Promise<HTMLVideoElement>}
49+
*/
2450
async loadVideo() {
2551
let stream;
26-
return new Promise((resolve) => {
52+
return new Promise((resolve, reject) => {
53+
if (!this.videoElt) {
54+
reject(new Error('No video was passed to the constructor.'));
55+
}
2756
this.video = document.createElement('video');
2857
const sUsrAg = navigator.userAgent;
2958
if (sUsrAg.indexOf('Firefox') > -1) {
@@ -32,14 +61,17 @@ class Video {
3261
stream = this.videoElt.captureStream();
3362
}
3463
this.video.srcObject = stream;
35-
this.video.width = this.size;
36-
this.video.height = this.size;
64+
if (this.size) {
65+
this.video.width = this.size;
66+
this.video.height = this.size;
67+
}
3768
this.video.autoplay = true;
3869
this.video.playsinline = true;
3970
this.video.muted = true;
4071
const playPromise = this.video.play();
4172
if (playPromise !== undefined) {
4273
playPromise.then(() => {
74+
this.videoReady = true;
4375
resolve(this.video);
4476
});
4577
}

0 commit comments

Comments
 (0)