Skip to content

Commit

Permalink
✨ classicwebcam: Support configured aspect ratio on video
Browse files Browse the repository at this point in the history
Also make sure we don't go beyond a 1:1 aspect ration on rotation.

Finally get rid of some CSS issues caused by a loss of flexbox mode
in deeper divs.

Implements #4837
  • Loading branch information
foosel committed Aug 10, 2023
1 parent 415f912 commit 97f54f2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 35 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions src/octoprint/plugins/classicwebcam/static/js/classicwebcam.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ $(function () {
}
});

self.webcamClass = ko.pureComputed(function () {
if (self.settings.rotate90()) {
return "webcam_rotated " + self.webcamRatioClass();
} else {
return "webcam_unrotated " + self.webcamRatioClass();
}
});

self.onBeforeBinding = function () {
// Subscribe to rotation event to ensure we update calculations.
// We need to wait for the CSS to be updated by KO, thus we use a timeout to
Expand Down Expand Up @@ -289,11 +297,10 @@ $(function () {
// accommodate the rotation). The target is centered in the container and
// rotated around its center, so after we manually resized the container
// everything will layout nicely.
if (rotationContainer && player.videoWidth && player.videoHeight) {
// Calculate the height the video will have in the UI, based on the
// video width and the aspect ratio.
var aspectRatio = player.videoWidth / player.videoHeight;
var height = aspectRatio * rotationContainer.offsetWidth;
if (rotationContainer) {
// we'll go with an aspect ration of 1:1 for rotated videos, same as
// for mjpg streams
var height = rotationContainer.offsetWidth;

// Enforce the height on the rotation container and the rotation target.
// Width of the container will be 100%, height will be calculated
Expand Down
66 changes: 38 additions & 28 deletions src/octoprint/plugins/classicwebcam/static/less/classicwebcam.less
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
.classicwebcam_aspect_ratio_mixin {
aspect-ratio: 1;

&.ratio43 {
aspect-ratio: 4 / 3;
}

&.ratio169 {
aspect-ratio: 16 / 9;
}

&.ratio1610 {
aspect-ratio: 16 / 10;
}
}

.classicwebcam_flexbox_mixin {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}

#webcam_video_container {
width: 100%;
position: relative;
Expand All @@ -23,31 +46,30 @@
height: 100%;
}

.webcam_rotated {
.webcam_rotated,
.webcam_unrotated {
position: relative;
width: 100%;
padding-bottom: 56.25%;
display: flex;
justify-content: center;
align-items: center;
margin: auto;
.classicwebcam_flexbox_mixin();
.classicwebcam_aspect_ratio_mixin();

.rotation_target {
.classicwebcam_flexbox_mixin();
div {
.classicwebcam_flexbox_mixin();
}
}
}

.webcam_rotated {
aspect-ratio: 1;
.rotation_target {
position: absolute;
transform: rotate(-90deg);
width: 100%;

div {
width: 100%;
height: 100%;
}
}
}

.webcam_unrotated {
position: relative;
width: 100%;
}
}

#webcam_img_container {
Expand Down Expand Up @@ -141,19 +163,7 @@
width: 100%;
pointer-events: none;

padding-bottom: 100%;

&.ratio43 {
padding-bottom: 75%;
}

&.ratio169 {
padding-bottom: 56.25%;
}

&.ratio1610 {
padding-bottom: 62.5%;
}
.classicwebcam_aspect_ratio_mixin();

position: relative;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<!-- /ko -->
</div>
</div>
<div data-bind="css: { webcam_rotated: settings.rotate90(), webcam_unrotated: !settings.rotate90() }">
<div data-bind="css: webcamClass">
<div class="rotation_target">
<div data-bind="css: { flipH: settings.flipH(), flipV: settings.flipV() }">
<video id="webcam_webrtc" muted autoplay playsinline style="width: 100%" data-bind="visible: webcamWebRTCEnabled()"></video>
Expand Down

1 comment on commit 97f54f2

@GitIssueBot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on OctoPrint Community Forum. There might be relevant details there:

https://community.octoprint.org/t/letterbox-camera-image-in-ui-customizer-since-update-1-10-0rc1/56681/21

Please sign in to comment.