Skip to content

Commit

Permalink
improve initial scene resize
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Aug 30, 2024
1 parent c67ac32 commit b87eafd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion nicegui/elements/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function texture_material(texture) {

export default {
template: `
<div style="position:relative">
<div style="position:relative" data-initializing>
<canvas style="position:relative"></canvas>
<div style="position:absolute;pointer-events:none;top:0"></div>
<div style="position:absolute;pointer-events:none;top:0"></div>
Expand Down Expand Up @@ -213,6 +213,7 @@ export default {

beforeDestroy() {
window.removeEventListener("resize", this.resize);
window.removeEventListener("DOMContentLoaded", this.resize);
},

methods: {
Expand Down Expand Up @@ -463,6 +464,8 @@ export default {
this.camera.updateProjectionMatrix();
},
init_objects(data) {
this.resize();
this.$el.removeAttribute("data-initializing");
this.is_initialized = true;
for (const [
type,
Expand Down
1 change: 1 addition & 0 deletions nicegui/elements/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def __init__(self,
self.on('dragstart', self._handle_drag)
self.on('dragend', self._handle_drag)
self._props['drag_constraints'] = drag_constraints
self._classes.append('nicegui-scene')

def on_click(self, callback: Callable[..., Any]) -> Self:
"""Add a callback to be invoked when a 3D object is clicked."""
Expand Down
7 changes: 6 additions & 1 deletion nicegui/elements/scene_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "tween";

export default {
template: `
<div style="position:relative">
<div style="position:relative" data-initializing>
<canvas style="position:relative"></canvas>
</div>`,

Expand Down Expand Up @@ -96,9 +96,14 @@ export default {

beforeDestroy() {
window.removeEventListener("resize", this.resize);
window.removeEventListener("DOMContentLoaded", this.resize);
},

methods: {
init() {
this.resize();
this.$el.removeAttribute("data-initializing");
},
move_camera(x, y, z, look_at_x, look_at_y, look_at_z, up_x, up_y, up_z, duration) {
if (this.camera_tween) this.camera_tween.stop();
this.camera_tween = new TWEEN.Tween([
Expand Down
2 changes: 2 additions & 0 deletions nicegui/elements/scene_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self,
self._click_handlers = [on_click] if on_click else []
self.on('init', self._handle_init)
self.on('click3d', self._handle_click)
self._classes.append('nicegui-scene-view')

def on_click(self, callback: Callable[..., Any]) -> Self:
"""Add a callback to be invoked when a 3D object is clicked."""
Expand All @@ -55,6 +56,7 @@ def on_click(self, callback: Callable[..., Any]) -> Self:
def _handle_init(self, e: GenericEventArguments) -> None:
with self.client.individual_target(e.args['socket_id']):
self.move_camera(duration=0)
self.run_method('init')

async def initialized(self) -> None:
"""Wait until the scene is initialized."""
Expand Down
4 changes: 4 additions & 0 deletions nicegui/static/nicegui.css
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ h6.q-timeline__title {
height: 10em;
background-color: AliceBlue;
}
.nicegui-scene[data-initializing],
.nicegui-scene-view[data-initializing] {
visibility: hidden;
}

/* CodeMirror */
.nicegui-codemirror .cm-editor.cm-focused {
Expand Down

0 comments on commit b87eafd

Please sign in to comment.