Skip to content

Commit 88e5845

Browse files
committed
Add automatic fixed-size resizing of the video and canvas elements based on the available page width
1 parent ac81b6f commit 88e5845

File tree

4 files changed

+77
-18
lines changed

4 files changed

+77
-18
lines changed

README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,8 @@ Additionally, the [Mesh](https://github.com/Valkryst/Jellron/blob/master/js/mesh
3434
### Styling
3535

3636
You _must_ specify a fixed size for the `video` element VIA CSS. There are a number of issues in implementing dynamic
37-
sizing, in a way that works across many browsers, so it is currently unsupported.
38-
39-
```css
40-
video {
41-
height: 480px;
42-
width: 640px;
43-
}
44-
```
37+
sizing, in a way that works across many browsers, so it is currently unsupported. See [styles.css](https://github.com/Valkryst/Jellron/blob/master/css/styles.css)
38+
for an example of how to implement various fixed sizes depending on the available width.
4539

4640
### Keypoint Rendering
4741

css/styles.css

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ html {
88
& > canvas, & > video {
99
position: absolute;
1010
}
11-
12-
& > video {
13-
height: 480px;
14-
width: 640px;
15-
}
1611
}
1712
}
1813
}
@@ -29,3 +24,65 @@ table, th, td {
2924
#webcam-settings, #keypoint-settings {
3025
grid-template-columns: 1fr 1fr;
3126
}
27+
28+
#jellron-display {
29+
& > canvas, & > video {
30+
position: absolute;
31+
32+
transform-origin: top left;
33+
transform: scale(-1, 1) translateX(-100%);
34+
}
35+
}
36+
37+
@media only screen and (min-width: 320px) {
38+
#jellron-display {
39+
& > video {
40+
height: 240px;
41+
width: 320px;
42+
}
43+
}
44+
}
45+
46+
@media only screen and (min-width: 640px) {
47+
#jellron-display {
48+
& > video {
49+
height: 480px;
50+
width: 640px;
51+
}
52+
}
53+
}
54+
55+
@media only screen and (min-width: 800px) {
56+
#jellron-display > video {
57+
height: 600px;
58+
width: 800px;
59+
}
60+
}
61+
62+
@media only screen and (min-width: 1024px) {
63+
#jellron-display > video {
64+
height: 768px;
65+
width: 1024px;
66+
}
67+
}
68+
69+
@media only screen and (min-width: 1280px) {
70+
#jellron-display > video {
71+
height: 960px;
72+
width: 1280px;
73+
}
74+
}
75+
76+
@media only screen and (min-width: 1600px) {
77+
#jellron-display > video {
78+
height: 1200px;
79+
width: 1600px;
80+
}
81+
}
82+
83+
@media only screen and (min-width: 1920px) {
84+
#jellron-display > video {
85+
height: 1440px;
86+
width: 1920px;
87+
}
88+
}

index.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,13 @@
223223
</fieldset>
224224
</form>
225225

226-
<video autoplay playsinline>
227-
This page requires a browser that supports the <em>video</em> element.
228-
</video>
226+
<div id="jellron-display">
227+
<video autoplay playsinline>
228+
This page requires a browser that supports the <em>video</em> element.
229+
</video>
229230

230-
<canvas></canvas>
231+
<canvas></canvas>
232+
</div>
231233
</main>
232234
</body>
233235

js/detector/body_detector.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ export class BodyDetector extends Detector {
4040
this.intervalId = setInterval(async () => {
4141
const currentTime = performance.now();
4242

43+
let frame = null;
4344
let rawBodies = [];
4445
try {
45-
rawBodies = await this.detector.estimatePoses(videoElement);
46+
frame = tf.browser.fromPixels(videoElement);
47+
frame = tf.image.resizeBilinear(frame, [videoElement.height, videoElement.width]);
48+
49+
rawBodies = await this.detector.estimatePoses(frame);
4650
} catch (e) {
4751
/*
4852
* Depending on the state of the video element, this can throw a "Requested texture size [0x0] is
4953
* invalid." error. It doesn't seem to cause any issues, so we ignore it.
5054
*/
55+
} finally {
56+
frame?.dispose();
5157
}
5258

5359
if (rawBodies.length === 0) {

0 commit comments

Comments
 (0)