Answer the question
In order to leave comments, you need to log in
Where is the error in the Javascript loop code?
Hi everybody! I am a beginner learning Javascript. Please help me find the error.
I want to display consecutive pictures in Canvas (without sprites).
I have an array of image urls. In the following function, I shove them as src of new Image objects:
function loadImg(linkArr, draw){
var imgs = [];
linkArr.forEach(function(link){
var img = new Image();
img.src = link
imgs.push(img); // Получаем массив с картнками и их урлами
})
draw(imgs)
};
function draw(imgs){ // Принимаем массив
var step = 0; // Шаг - координата по У, с изминением которой следующая картинка будет дальше
imgs.forEach(function(src){ // Бегу по массив
con.drawImage(src, 0, step, 200 , 150) // Рисую картинки
step += 30; // добавляю шаг
})
}
window.onload = function(){
loadImg(arr, draw) // Если добавить setInterval, то отобразиться первая картинка, потом на том же месте последня..
}
Answer the question
In order to leave comments, you need to log in
First, put in semicolons.
Then, before executing draw(imgs) , you need to wait for all images to load with img.onload.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question