vasturiano / globe.gl

UI component for Globe Data Visualization using ThreeJS/WebGL
https://vasturiano.github.io/globe.gl/example/world-population/
MIT License
2.03k stars 301 forks source link

[questions]How to Centered/keep left the globe in the viewport? #55

Open moranno opened 3 years ago

moranno commented 3 years ago

The globe are always on the right of the canvas no matter how I adjust the canvas.

And you may discover the "count" error as I mentioned here: https://github.com/vasturiano/react-globe.gl/issues/59#issuecomment-864419796

snshn commented 3 years ago

Looks like #globeViz needs to have fixed width set. Currently it doesn't have overflow: hidden, hence the canvas element overflows it (being rendered oversized).

moranno commented 3 years ago

Looks like #globeViz needs to have fixed width set. Currently it doesn't have overflow: hidden, hence the canvas element overflows it (being rendered oversized).

Thanks, so how can I solve it, since it has to responsive?

snshn commented 3 years ago

You'd need to set width: 500px or width: 25% on one of the parent DIVs, before Globe() gets called. Hard to tell which one and what the exact value should be in your case, but that'll likely solve the problem.

moranno commented 3 years ago

You'd need to set width: 500px or width: 25% on one of the parent DIVs, before Globe() gets called. Hard to tell which one and what the exact value should be in your case, but that'll likely solve the problem.

<div style="float:left; width:500px;height:500px;">
<div id="globeViz" ></div>
</div>

Unfortunately... It doesn't work. It seems the canvas has a higher level than div.

div-can

snshn commented 3 years ago

Oh, that's weird. Seems like THREE.js doesn't have that functionality built-in (https://stackoverflow.com/questions/29884485/threejs-canvas-size-based-on-container), likely will have to set the canvas' width & height CSS properties manually. My current case is easier, since I just use globe.gl as a full-page/full-screen component.

You'll likely need to implement

window.addEventListener('resize', (event) => {
  world.width([event.target.innerWidth])
  world.height([event.target.innerHeight])
});

along with triggering the same world.width/world.height once the page loads, that way the canvas will fit perfect inside the parent DIV.

moranno commented 3 years ago

Oh, that's weird. Seems like THREE.js doesn't have that functionality built-in (https://stackoverflow.com/questions/29884485/threejs-canvas-size-based-on-container), likely will have to set the canvas' width & height CSS properties manually. My current case is easier, since I just use globe.gl as a full-page/full-screen component.

You'll likely need to implement

window.addEventListener('resize', (event) => {
  world.width([event.target.innerWidth])
  world.height([event.target.innerHeight])
});

along with triggering the same world.width/world.height once the page loads, that way the canvas will fit perfect inside the parent DIV.

Thanks! Any ideas how to write the code in DIV to read the world.width/world.height ?

snshn commented 3 years ago

This should do it: https://www.javascripttutorial.net/javascript-dom/javascript-width-height/

ralyodio commented 4 months ago

i get a lot of flashing of white when I resize. I tried setting html, body { background-color: #001;} but i style get a white flash when resizing.