Here my code in my head
section:
<script src="jquery-1.10.1.min.js"></script>
<script src="metro.js"></script>
<script src="draw.js"></script>
<script>
$(document).ready(function() {
var im = $('<img />')
.attr({id: 'metro', src:'metro.png'})
.appendTo($('body'))
.hide()
.on('load', function() {
okIm = true;
drawIfOk();
});
var bg = $('<img />')
.attr({id: 'back', src:'back.png'})
.appendTo($('body'))
.hide()
.on('load', function() {
okBg = true;
drawIfOk();
});
});
</script>
This code should download dynamically 2 images and in the drawIfOk();
function, i test if both images are downloaded. If so, then I try to create canvas based on the width & height of the image, like this:
function drawIfOk() {
if (!okIm) {
return;
}
if (!okBg) {
return;
}
initTest();
}
Simple eh?
Now the initTest();
function is very simple:
function initTest() {
gImgMetro = $('#metro')[0];
var bg = $('#back')[0];
/* Création du canvas */
gImgCanvas = $('<canvas />').attr({
width: bg.width,
height: bg.height
})[0];
gImgCtx = gImgCanvas.getContext('2d');
if(!gImgCtx) {
displayError("Impossible de récupérer le context du canvas");
return;
}
}
Then IE gives me an error because bg.width
and bg.height
are 0 whereas it works on Safari, Chrome, Firefox and Maxthon! [Trying not to grumble about IE]
How to circumvent this?