2

estou tentando fazer uma animação com javascript puro, e estou usando style.transform = "rotate(" + ang + "deg)"; para fazer isso. Minha intenção é: quando ele chegar a uma posição x, ele volte em sentido anti-horário. Ele vai em sentido horário mas não muda na hora de voltar, tentei 1000 coisa mas n consegui.

Segue o código completo do arquivo html.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    #div{
    width: 100px;
    height: 100px;
    border: 1px solid black;
    background-color: blue;
    position: absolute;
    left: 0;
    top: 0;
}
</style>
<script>
    let jog, vel, dx, px, anima, parar, anima2, ang, rotacao;

    function inicia(){
        jog = document.getElementById("div");
        vel = 2;
        px = 0;
        dx = 1;
        ang = 0;
        parar = true;
        jog.addEventListener("click", moverParar);
        game();
    }

    function moverParar(){
        if(parar){
            parar = false;
            cancelAnimationFrame(anima);
        }else{
            parar = true;
            anima = requestAnimationFrame(game);
        }
    }

    function gira(){
        jog.style.transform = "rotate(" + ang + "deg)";
        rotacao = ang++;
        if(px > 500){
            dx = -1;
            ang = 360;
            rotacao = ang--;
        }
        else if(px < 0){
            dx = 1;
            ang = 0;
            rotacao = ang++;
        } 

        //anima2 = requestAnimationFrame(gira);
    }

    function game(){
        px += dx * vel;
        jog.style.left = px + "px";
        
        gira();

        anima = requestAnimationFrame(game);
    }

    window.addEventListener("load", inicia);
</script>
<body>
    <div id="div"></div>
</body>
</html>

1 Resposta 1

3

No método gira, onde sempre incrementava 1, alterei para que quando for maior que 500 decrementar 1.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    #div{
    width: 100px;
    height: 100px;
    border: 1px solid black;
    background-color: blue;
    position: absolute;
    left: 0;
    top: 0;
}
</style>
<script>
      let jog, vel, dx, px, anima, parar, anima2, ang, rotacao, direcao;

    function inicia(){
        jog = document.getElementById("div");
        vel = 2;
        px = 0;
        dx = 1;
        ang = 0;
		direcao=1;
        parar = true;
        jog.addEventListener("click", moverParar);
        game();
    }

    function moverParar(){
        if(parar){
            parar = false;
            cancelAnimationFrame(anima);
        }else{
            parar = true;
            anima = requestAnimationFrame(game);
        }
    }

    function gira(){
        jog.style.transform = "rotate(" + ang + "deg)";

		ang += direcao;
        rotacao = ang;
        if(px > 500){
            dx = -1;
            ang = 360;
            rotacao = ang--;
			direcao=-1;
        }
        else if(px < 0){
            dx = 1;
            ang = 0;
            rotacao = ang++;
			direcao=1;
        } 

        //anima2 = requestAnimationFrame(gira);
    }

    function game(){
        px += dx * vel;
        jog.style.left = px + "px";
        
        gira();

        anima = requestAnimationFrame(game);
    }

    window.addEventListener("load", inicia);
</script>
<body>
    <div id="div"></div>
</body>
</html>

2
  • Muito obrigado pela ajuda, abraços! 16/01/2019 às 20:43
  • Então eu nem precisava usar a variável rotacao né, bastava usar ang e direcao. 16/01/2019 às 21:20

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .