I
I
imagance2021-09-25 23:00:32
JavaScript
imagance, 2021-09-25 23:00:32

Why does the image stretch when animating in canvas?

For some reason, when animating a picture, it stretches. What to do?

var cvs = document.getElementById("canvas");
var ctx = cvs.getContext("2d");

var bird = new Image();
var stop = new Image();

bird.src = "hero.png";
stop.src = "stopped.png";

var xPos = 100;
var yPos = 0;


function draw() {
  
  ctx.drawImage(stop, 300, 0);
  ctx.drawImage(bird, xPos, 0);
  
  xPos += 1;

  requestAnimationFrame(draw);
}

setTimeout(draw, 200)

<canvas id="canvas" width="1088px" height="912px"></canvas>
<script src="script.js"></script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2021-09-25
@imagance

The draw function does not overwrite what has been drawn on the canvas.
Make the canvas clear before drawing pictures, for example, ctx.clearRect(0, 0, cvs.width, cvs.height);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question