Skip to content

<canvas> for HTML brax-viewer ignores specifying "height"Β #142

Closed

Description

@ymd-h

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.

def render(sys: brax.System, qps: List[brax.QP], height: int = 480) -> str:

brax/brax/io/html.py

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.

brax/js/viewer.js

Lines 168 to 170 in 661364e

window.onload = (evt) => this.setSize();
window.addEventListener('resize', (evt) => this.setSize(), false);
requestAnimationFrame(() => this.setSize());

brax/js/viewer.js

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.

brax/js/viewer.js

Lines 172 to 173 in 661364e

const resizeObserver = new ResizeObserver(() => this.resizeCanvasToDisplaySize());
resizeObserver.observe(this.domElement, {box: 'content-box'});

brax/js/viewer.js

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

  1. erikfrey commented on Jan 5, 2022

    @erikfrey
    Collaborator

    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?

  2. ymd-h commented on Jan 6, 2022

    @ymd-h
    ContributorAuthor

    @erikfrey
    Thank you for your reply!

    Would you like to open a PR?

    Yes, I made a PR. (#146)

    I couldn't find any test codes for JavaScript, however, at least I checked with following procedure;

    1. I manually bundled all the JavaScript codes and embedded into html template string.
    2. I ran it on Google Colab and checked it worked as I expected.
  3. erikfrey commented on Jan 7, 2022

    @erikfrey
    Collaborator

    OK, this is merged. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions