
Without wasting any seconds, he wrote down 4 formulas for 4 different resizers (he has changed his formula for the bottom-right resizer to use the same method as other resizers. His laughter stopped when he realized that he can use the same formula for resizing the height of the element. “Huge success!” - He laughed loudly in the middle of the night like a mad scientist.
#JS RESPONSIVE RESIZE EFFECT CODE#
“First, the white square on the black background” - He typed in those first lines of css code into the file body, html He’s actually using Sass for his project, but to facilitate major readers, I’ll write it in plain css. One hand on the keyboard, his other hand quickly controls the mouse to switch to the css file. Closed his eyes, everything was clear in his mind, a white rectangle on a black background with 4 white circle resizer at 4 of its corners. “That should do it” - He laid back to his pillow before moving on to the css code. “It should be a square with 4 resizers at its 4 corners” - His thought went through his naive brain and passed down to his hand then finally command all 8 of his 10 fingers to type the correct letter for the html structure. Like every other start, he first created a html structure for the “resizable div”. His arrogant when up, “can’t be that hard” - He said with a smile on his face without knowing that the problem he’s about to encounter is going to take a great deal of his knowledge and experience to solve. As he was scrolling aimlessly through a long list of JavaScript functions, he stumbled on an empty function named makeResizableDiv(div). “Perfect time for programming” - Hung said. It was a quiet night with a perfect temperature for the brain to give 100%.
#JS RESPONSIVE RESIZE EFFECT TRIAL#
You can just find these numbers by trial and error based on your scene scale, or use some heuristic to compute it.Making a resizable div in JS is not easy as you think So this is a generic implementation of that example. We like the behavior of 1/2.Ĭonst camH = Math.tan((myFov/2))Ĭonst newFov = (Math.atan(newH)) * 2 So the aspect of 1/2 is ideal and aligns with the image with no overflow or gaps. But when we go to 1500px x 500px we see gaps. Now when we apply the usual resize logic: cam.aspect = aspectĪnd we resize the screen to 500px x 500px we get your result. There is only one ever position of the camera that would render this mesh to fit a 1000px x 500px screen: = heightHalf / Math.tan( (myFov/2))


If the image was sitting on a mesh, const image = new THREE.Mesh(new THREE.PlaneGeometry(1000,500))Īnd if you picked some constant fov: const myFov = 45 This gives us a height of 750px, and we can only fit 500px. We want the 1000px of the image width to fit 1500px, so we will scale the image up by 1.5x. The aspect ratio of that is 3:1, and the entire thing is 2:1. We want the image to “fill the entire window”, so it needs to cover the 1500 x 500. Ok we don’t want that, so we need to do something to the image. You’d see the entire image and 250px x 500px gaps on both sides. You’re doing that in your code and you experience what would be seen as a gap in the example demo.

Let’s resize it to be wider than the image, 1500px x 500px. This is cool and it’s how THREE.PerspectiveCamera behaves “by default”. So now at 500px x 500px we have overflow in the horizontal axis. Cool, lets make the window narrower first, 500px. When you set your window to be 1000px x 500px it exactly aligns with the image, no overflow no gaps on either side. Let’s say the image has an aspect ratio of 2:1. You could use a bounding box, geometric checks and whatnot, but the easiest thing would be to just pick some world size rectangle, and apply the same logic you would to an image/quad.

What you need is something that relates to both that camera.fov and your subject, you need to frame it somehow. You don’t have that, and only have height basically by setting camera.fov. They have the width and height available to them and can apply it to logic. The example you’ve posted seems to be working with an image, you seem to be working with a mesh.
