4
The question is this, I was able to develop the exercise to a certain extent, after that, as I do not know much of javascript, if I send the square back diagonally, the square comes back earlier due to the coordinates that have already been used, it is complex to explain, but is giving conflict in the coordinates because they have been used previously in the path.
The point is, how do I get the square to go back diagonally as the image shows? (The square leaves from the left, making the route) 90% of the exercise is done, that’s all I need.
Thank you
var canvas = document.getElementById('minha-tela');
var ctx = canvas.getContext('2d');
//definir o ínicio do desenho
var x = 0
var y = 150;
var l = 0;
//a função gameloop é chamada aqui
requestAnimationFrame(gameloop);
function gameloop() {
if(x<=700 && y==150)
x = x + 10;
if (x>=700 && y>=0)
y=y-10;
if (x>=350 && y<=0)
x=x-10;
desenharQuadrado(x,y);
//incrementar a variável x indicando o deslocamento para a direita
//chama novamente o ciclo da animação
requestAnimationFrame(gameloop);
}
function desenharQuadrado(pX,pY) {
ctx.clearRect(0, 0, 800, 400); //antes de fazer o desenho é preciso limpar o canvas
ctx.fillStyle = '#00F';
ctx.fillRect(pX, pY, 100, 100);
}
<canvas id="minha-tela" width="800" height="400" style="border: #F00 solid 1px;"> </canvas>
Douglaslb, do not remove information from the question that makes it meaningless for the reader (such as the figure that shows the desired path and part of the explanation of the problem), even if the question has already been answered. In this way, other people can also learn using the code of both the question and the answer.
– Victor Stafusa