Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
Performance-heavy function which adds an event listener on every polygon within a very large SVG

I've got a large map made up of hexagons in one large SVG. I've created this function in which as the user hovers over the SVG, it adds an event listener to every hexagon which in turn performs a function that compares the distance between the mouse and each hexagon, and in turn gets those within a distance of 30px and adds CSS styling to them which makes them proportionally scale down in size in regards to the mouse position.

This works well enough but it can get pretty choppy when hovering over a lot of hexagons and or increasing the distance detection. I'm an amateur when it comes to all of this so I might not be aware of much easier and probably more optimal ways of going about this.

Here's the code:

function hexagonHover() {
    for (i = 0; i < poly.length; i++) {
        poly[i].addEventListener('mouseover', function (event) {
            for (i = 0; i < poly.length; i++) {

                var pos = poly[i].getBoundingClientRect();

                let _x = Math.round(pos.left + pos.width / 2);
                let _y = Math.round(pos.top + pos.height / 2);
                
                var mouseX = Math.round(event.clientX);
                var mouseY = Math.round(event.clientY);

                var a = _x - mouseX;
                var b = _y - mouseY;

                var dist = Math.round(Math.sqrt(a * a + b * b));

                if (dist <= 30) {

                    let v = mapRange(dist, 0, 30, 1.2, 0.2);

                    poly[i].style.webkitTransform = poly[i].style.transform =
                        "scale(" + v + ")";
                    poly[i].style.webkitTransform = poly[i].style.transformOrigin =
                        "center";
                    poly[i].style.webkitTransform = poly[i].style.transformBox = "fill-box";
                    poly[i].style.transition = "transform ease 1s";
                } else {
                    poly[i].style.webkitTransform = poly[i].style.transform = "scale(1.0)";
                }
            }
        });
    }
}

Here's a codepen which demonstrates the effect albeit on a much smaller scale

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda