Answer the question
In order to leave comments, you need to log in
Why doesn't canvas accept dynamic src's?
Why canvas doesn't accept dynamic src, there is code like this
var canvas = document.getElementById("myCanvas"),
context = canvas.getContext("2d");
contextT = canvas.getContext("2d");
var img = new Image();
var imgflag_1 = new Image();
var imgflag_2 = new Image();
var imgflag_3 = new Image();
var imgflag_4 = new Image();
var imgUSER = new Image();
var loaded = 0;
img.src = "img/testimage.png";
var testingURL = "minCountry/"+arr[a[0]]+".png";
imgflag_1.src = testingURL;
console.log("link "+testingURL); // тут выводится как надо, но фото не отображается на холсте
imgflag_2.src = "minCountry/American.png";
imgflag_3.src = "minCountry/American.png";
imgflag_4.src = "minCountry/American.png";
imgUSER.src = "img-user.jpg";
imgflag_1.onload = function(){
if(++loaded == 2)
draw();
}
imgflag_2.onload = function(){
if(++loaded == 2)
draw();
}
imgflag_3.onload = function(){
if(++loaded == 2)
draw();
}
imgflag_4.onload = function(){
if(++loaded == 2)
draw();
}
imgUSER.onload = function(){
if(++loaded == 2)
draw();
}
img.onload = function() {
if(++loaded == 2)
draw();
};
function draw(){
context.drawImage(img, 0, 0);
context.drawImage(imgflag_1, 515, 162,110,110);
context.drawImage(imgflag_2, 515, 275,110,110);
context.drawImage(imgflag_3, 515, 385,110,110);
context.drawImage(imgflag_4, 515, 500,110,110);
context.drawImage(imgUSER, 15, 160,400,400);
}
imgflag_1.src = "minCountry/American.png";
var testingURL = "minCountry/"+arr[a[0]]+".png";
imgflag_1.src = testingURL;
Answer the question
In order to leave comments, you need to log in
Once upon a time I wrote
function loadResources(arr,func){//функция загрузки внешних ресурсов(картинок) arr - массив ссылок, func - функция которая вызовется после загрузки всех ресурсов
var loadStatus = false;
loadStatus = {count: arr.length, loaded: 0, percent: 0};//count - общее количество ресурсов, loaded - сколько загружено ресурсов, percent сколько загружено в процентах
for(var i = 0; i < arr.length; i++){
var tmp = new Image();
tmp.src = arr[i];
tmp.onload = function(){
loadStatus.loaded++;
loadStatus.percent = (loadStatus.loaded * 100)/loadStatus.count;
if(loadStatus.loaded >= loadStatus.count)
func();
}
}
return loadStatus;
}
loadResources(["/img/1.jpg","/img/2.jpg"],draw)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question