Resizing browser, image positioning & performance #1016
-
Hi, So I got this issue where my Image with a shaderMaterial won't perfectly center on the screen when resizing the browser. Above it I have a hidden img-element for accessability-purposes. Please give it a try and resize the browser back and forth. Sometimes the Image get stuck either to the left or right of the hidden img-element. Resizing the browser with just a pixel makes it go back in place. I think this would be a cool idea to further work on (with the hidden img-element on top) only if my mesh for the image would follow the element perfectly. I have created a simplified sandbox out of one of Pauls sandboxes (Thank you, I've been learning a lot of you). Best, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
This seemed to lag behind each resize (resizing correctly, but one resize too late — race condition maybe?). https://codesandbox.io/s/r3f-contact-shadow-forked-8bw74?file=/src/Image.js Edit: It seems that your Image component is reliant on an image in the dom. I've added its props as dependencies in a const { width, height, top, left } = primaryImage.getBoundingClientRect()
// There's a better way to do this, right?
const [scaleX, setScaleX] = useState()
const [scaleY, setScaleY] = useState()
const [posX, setPosX] = useState()
const [posY, setPosY] = useState()
useEffect(() => {
setScaleX((width / window.innerWidth) * viewport.width)
setScaleY((height / window.innerHeight) * viewport.height)
setPosX(((width / window.innerWidth) * viewport.width) / 2 - viewport.width / 2 + (left / window.innerWidth) * viewport.width)
setPosY(-((height / window.innerHeight) * viewport.height) / 2 + viewport.height / 2 - (top / window.innerHeight) * viewport.height)
}, [width, height, top, left, viewport.width, viewport.height]) |
Beta Was this translation helpful? Give feedback.
This seemed to lag behind each resize (resizing correctly, but one resize too late — race condition maybe?).
https://codesandbox.io/s/r3f-contact-shadow-forked-8bw74?file=/src/Image.js
Edit: It seems that your Image component is reliant on an image in the dom. I've added its props as dependencies in a
useEffect
hook and it will update in sync.