Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resize to Renderable.js #374

Closed

Conversation

akaszynski
Copy link
Contributor

@akaszynski akaszynski commented Apr 20, 2022

Automatically resizes the pythreejs Renderer.

Resolves #362.

@akaszynski akaszynski marked this pull request as draft April 20, 2022 05:56
@codecov-commenter
Copy link

codecov-commenter commented Apr 20, 2022

Codecov Report

Merging #374 (671f610) into master (a78cb57) will increase coverage by 0.10%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #374      +/-   ##
==========================================
+ Coverage   70.86%   70.96%   +0.10%     
==========================================
  Files          23       23              
  Lines         834      837       +3     
==========================================
+ Hits          591      594       +3     
  Misses        243      243              
Impacted Files Coverage Δ
pythreejs/_base/renderable.py 85.96% <100.00%> (+0.25%) ⬆️
pythreejs/core/Renderer.py 82.97% <100.00%> (+0.37%) ⬆️
pythreejs/renderers/WebGLRenderer.py 100.00% <100.00%> (ø)

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@akaszynski akaszynski marked this pull request as ready for review April 20, 2022 16:07
@akaszynski
Copy link
Contributor Author

Hi @vidartf,

This is ready for review.

This repo has been a bit light as of late in regards to commits and PR merges. I'm at a point that I'd like to take up maintenance of this repo if possible.

Copy link
Member

@vidartf vidartf left a comment

Choose a reason for hiding this comment

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

Thanks for this! I've added some comments below about specific parts, but the large points of the logic looks correct 👍

scene = Instance(Scene).tag(sync=True, **widget_serialization)
camera = Instance(Camera).tag(sync=True, **widget_serialization)
controls = List(Instance(Controls)).tag(sync=True, **widget_serialization)
#effect = Instance(Effect, allow_none=True).tag(sync=True, **widget_serialization)
background = Color('black', allow_none=True).tag(sync=True)
background_opacity = Float(1.0, min=0.0, max=1.0).tag(sync=True)

def __init__(self, scene, camera, controls=None, antialias=False, alpha=False, webgl_version=2, **kwargs):
def __init__(self, scene, camera, controls=None, antialias=False, alpha=False, webgl_version=2, auto_resize=False, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def __init__(self, scene, camera, controls=None, antialias=False, alpha=False, webgl_version=2, auto_resize=False, **kwargs):

The users can pass autoResize here, and it will be passed along to the super's init method via the kwargs. The other args that are listed here explicitly are there because:

  • They are required (scene and camera)
  • They are hidden traits (name start with an underscore).

The autoResize trait does not need the same logic.

super(Renderer, self).__init__(
scene=scene,
camera=camera,
controls=controls or [],
_antialias=antialias,
_alpha=alpha,
_webgl_version=webgl_version,
auto_resize=False,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
auto_resize=False,

**kwargs)
link((self, 'width'), (self, '_width'))
link((self, 'height'), (self, '_height'))
self.autoResize = auto_resize
Copy link
Member

Choose a reason for hiding this comment

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

If we pass along the trait to the super's init, we won't need to set it here.

Suggested change
self.autoResize = auto_resize

@@ -18,6 +18,7 @@ class WebGLRenderer(RenderableWidget):

width = CInt(200)
height = CInt(200)
autoResize = Bool(False)
Copy link
Member

Choose a reason for hiding this comment

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

This is already declared on the base class, so should not need to be redeclared here.

Suggested change
autoResize = Bool(False)

@@ -2,6 +2,7 @@ var _ = require('underscore');
var widgets = require('@jupyter-widgets/base');
var $ = require('jquery');
var Promise = require('bluebird');
var THREE = require('three');
Copy link
Member

Choose a reason for hiding this comment

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

This import isn't used, so is probably not needed here.

Suggested change
var THREE = require('three');

// Get the size of the container, or if that doesn't exist, the window
getSize: function() {
try {
const { width, height } = this.el.getBoundingClientRect();
Copy link
Member

Choose a reason for hiding this comment

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

I would also use window.getComputedStyle() for both the container (this.el) and the rendered element (this.renderer.domElement and or this.$frozenRenderer if this.isFrozen is true) to substract the margins and paddings from these sizes. That should hopefully avoid the magic "4.4" below, as the actual size of this padding will depend on the context (e.g. the margin is a CSS variable set by the theme in JupyterLab).

if (width === 0){
return;
}
this.renderer.setSize(width, height - 4.4); // Seems to grow by 4.4 px on each resize
Copy link
Member

Choose a reason for hiding this comment

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

If you set the size on the model (this.model.set({"_width": width, "_height": height})), then the update would happen automatically via the existing updateSize method, and the sizes would also persist through a freeze/thaw cycle.

console.log("bounding client size");
return [width, height];
} catch (error) {
// Not using a container for the renderer
Copy link
Member

Choose a reason for hiding this comment

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

I'm curious about this. When does this happen?

@akaszynski akaszynski closed this Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Autoresize Renderer
3 participants