Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
Nullify transforms (primarily rotation) in getBoundingClientRect?

I'm currently using this as a workout (just flat out removing the transform, getting the rect, and putting it back).

  useEffect(() => {
    if (typeof document !== undefined) {
      const ctn = document.getElementById(id);
      const parent = ctn.parentElement;
      const parentStyles = getComputedStyle(parent);
      const transform = parentStyles.transform;
      parent.style.transform = "rotate(0deg)";
      const box = parent.getBoundingClientRect(); 
      if (box.width > box.height * 0.5008) { // These numbers are specific to the aspect ratio of the phone frame, unrelated to question
        setHeight("100%");
        setWidth(box.height * 0.5008);
      } else {
        setHeight(box.width * 1.9888);
        setWidth("100%");
      }
      setBorderRadius(box.width * 0.125);
      parent.style.transform = transform;
    }
  }, [screen]);

The reason why I'm asking is because this is for a 'mock-up' component, i.e. you give it an image and it returns a nice mock-up. The general idea is that you just wrap it in whatever sized container you want, and it will auto-size everything with JS so the mock-up looks perfect. It's essentially 2 layers, the phone-frame img up top and the screen img below. I grab the width/height of the parent container and do a calculation to make the "screen" image line up perfectly with the phone.

No rotate, working good

If I don't completely remove the rotation transform before using getBoundingClientRect(), it calculates the width with the transform included, which screws up the element sizing. My current solution just feels sort of janky, and it makes you unable to change transforms on the parent container's CSS style sheet without a JS solution (because the transform just gets readded to inline-styles).

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda