J'ai un problème avec le dessin sur le canevas, j'ai un tableau d'informations d'image que je traverse et crée des objets image à partir de. Sur chaque événement images onload je dessine sur le canevas, mais cela ne fonctionnera qu'après un rafraîchissement (une fois que les images sont dans le cache, il semble).HTML 5 canvas DrawImage fonctionnant uniquement après l'actualisation
Voici un extrait de mon code
var image = new Image();
image.src = img.src; //got from elsewhere, need to draw a background image first
image.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
img.parentNode.replaceChild(canvas, img);
var context = canvas.getContext('2d');
context.drawImage(image, 0, 0, canvas.width, canvas.height);
context.globalCompositeOperation = 'source-over';
//looping through my image info
for (var index in anaglyphs){
var anaglyph = anaglyphs[index];
if (anaglyph === undefined) continue;
//If x, y, height and width values are set in Image then set in mask
//otherwise use defaults
var x = 0;
if (anaglyph.x !== undefined) x = anaglyph.x;
var y = 0;
if (anaglyph.y !== undefined) y = anaglyph.y;
var width = canvas.width;
if (anaglyph.width !== undefined) width = anaglyph.width;
var height = canvas.height;
if (anaglyph.height !== undefined) height = anaglyph.height;
var alpha = new Image();
//Use of an intimidate function to stop javascripts closure
//overriding the variables before the onload event fired. Creating
//temporary variables in the intimidate function, which executes
//immediately, and storing the desired values inside them for callback.
(function(){
var xPos = x;
var yPos = y;
var maskWidth = width;
var maskHeight = height;
alpha.onload = function() {
context.drawImage(this, xPos, yPos, maskHeight, maskWidth);
};
})();
alpha.src = "data:"+anaglyph.content+";base64,"+encode64(anaglyph.data);
}
};
Après un rafraîchissement, il fonctionne très bien et il continuera à fonctionner correctement si j'ouvre la page Web en utilisant à nouveau ce script, mais il échouera si j'effacer la cachez et rouvrez la page. Il suffit de travailler dans la dernière version de Firefox.
Merci
Hey, je fermeture dans une tentative d'avoir les images tirer lors du retour de l'événement onload, je besoin x , y, hauteur et largeur qui ont été remplacées lorsque la boucle s'est déplacée. Mais votre solution était meilleure, mais le même problème se posait toujours, je réussi à le corriger en ajoutant 'code' setTimeout (function() {rappel (images)}, 100) 'code' Je pense qu'il était un problème avec firefox le rendant car cela l'a parfaitement corrigé. À la votre – user1956149