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

529
Visualizações
Rotate circle to vector (js)

I'm probably drastically overthinking this. But I have a circle I am dynamically generating with slices of PI. I would like to have the ability to click the circle and rotate it. Whatever point on the circle I click, That point on the circle is the one I would like to follow the mouse.

I've been playing around with it for awhile. And I can get it to work on one side of the circle. But the other side of the circle does the opposite of what I expect. Heres basically the setup

        const currentDegrees = piCircle.current;
        const rect = canvas.getBoundingClientRect();
        const x = state.xy[0] - rect.left;
        const y = state.xy[1] - rect.top;
        
        const dx = state.delta[0];
        const dy = state.delta[1];

x and y are the mouse positions on the circle, dx and dy is the normalized motion the mouse is moving in(typically 1/0/-1). currentDegrees is the current rotation of the circle.

My current, somewhat working approach was this
const degrees = toDegrees(Math.atan2(y + dy, x + dx));

That approach doesnt seem to respect the rotation of the circle. on the right side of the circle it works fine, on the left the motion is inverted.

To simplify the issue, I realize I can phrase it more like this. I have two vectors, mouse position(x&y) and delta(x&y). By adding those two together i would have a third vector which is the destination I would like my initial position to end up at when I apply some degree of rotation.

    const x = state.xy[0] - rect.left;
    const y = state.xy[1] - rect.top;
    const dx = x + state.delta[0];
    const dy = y + state.delta[1];

I drew an awful thing to also help visualize my problementer image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is my best guess for how to do this. Its not perfect. The circle rotates slightly slower than the pointer. I'm not sure how to improve it. I'm open to suggestions.

        const x = state.xy[0] - rect.left;
        const y = state.xy[1] - rect.top;
        
        const dx = x + state.delta[0];
        const dy = y + state.delta[1];

        const startAngle = Math.atan2(y - centerY , x - centerX);
        const endAngle = Math.atan2(dy - centerY, dx - centerX)

        const cx = centerX + radius * Math.cos(startAngle);
        const cy = centerY + radius * Math.sin(startAngle);

        const dcx = centerX + radius * Math.cos(endAngle);
        const dcy = centerY + radius * Math.sin(endAngle);

        const rsa = Math.atan2(centerY - dcy, centerX - dcx);
        const rea = Math.atan2(centerY - cy, centerX - cx);

        const angle = toDegrees(rsa - rea)

about 4 years ago · Juan Pablo Isaza Relatório
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