Repository navigation
Description
Thank you for providing a great project!
I noticed HTML viewer (brax@v0.0.10/js/viewer.js) ignored specified height and became as big as possible.
(Moreover, sometimes the viewer overwraps other outputs.)
Reproducible Code on Google Colab
!pip install brax
from IPython.display import HTML
from brax.io import html
from brax import envs
import brax.jumpy as jp
env = envs.create("ant")
state = env.reset(jp.random_prngkey(0))
display("This message triggers size increase.")
display(HTML(html.render(env.sys, [state.qp], height=10)))
display("This message is overwrapped by viewer.")
html.save_html("test.html", env.sys, [state.qp]) # Download "test.html" and view with browser.Tested
- Brax 0.0.10
- Google Chrome
- macOS Big Sur 11.6
- Environment
- Google Colab
- Local html file (written by
brax.io.html.save_html)
Backgound
brax.io.html.render function takes height parameter and creates <div> element with fixed height.
I expected the output viewer had fixed height, however, it is not true.
Line 40 in 661364e
| def render(sys: brax.System, qps: List[brax.QP], height: int = 480) -> str: |
Lines 64 to 68 in 661364e
| #brax-viewer { | |
| margin: 0; | |
| padding: 0; | |
| height: <!-- viewer height goes here -->; | |
| } |
(Possible) Cause
At some events, the viewer is resized without specifying explicit height (and width). If height parameter (h) is undefined, window.innerHeight is used instead of the parent <div#brax-viewer>'s height.
Lines 168 to 170 in 661364e
| window.onload = (evt) => this.setSize(); | |
| window.addEventListener('resize', (evt) => this.setSize(), false); | |
| requestAnimationFrame(() => this.setSize()); |
Lines 183 to 189 in 661364e
| setSize(w, h) { | |
| if (w === undefined) { | |
| w = this.domElement.offsetWidth; | |
| } | |
| if (h === undefined) { | |
| h = window.innerHeight; | |
| } |
(Candidate) Fix
I think there are (at leaset) two candidate fixes.
Candidate 1
The first fix is replacing the default fallback value of h in setSize() by this.domElement.clientHeight.
if (h === undefined) {
h = this.domElement.clientHeight;
} Candidate 2
The other fix is using resizeCanvasToDisplaySize() as ResizeObserver does.
Lines 172 to 173 in 661364e
| const resizeObserver = new ResizeObserver(() => this.resizeCanvasToDisplaySize()); | |
| resizeObserver.observe(this.domElement, {box: 'content-box'}); |
Lines 201 to 206 in 661364e
| resizeCanvasToDisplaySize() { | |
| //look up canvas size | |
| const width = this.domElement.clientWidth; | |
| const height = this.domElement.clientHeight; | |
| this.setSize(width, height); | |
| } |
Activity
erikfrey commentedon Jan 5, 2022
Hi @ymd-h - thanks for pointing this out! It looks like Candidate 1 most directly addresses the bug, that looks like a good solution to me. Would you like to open a PR?
Fix: Use clientHeight as default height parameter in SetSize()
ymd-h commentedon Jan 6, 2022
@erikfrey
Thank you for your reply!
Yes, I made a PR. (#146)
I couldn't find any test codes for JavaScript, however, at least I checked with following procedure;
Fix: Use clientHeight as default height parameter in SetSize() (#146)
erikfrey commentedon Jan 7, 2022
OK, this is merged. Thanks!