I have the following function, which, among other parameters, receives a parameter called pack_choice (which is a number). Depending on the number, I want to assign a text to var, and later print this var on canvas.
Here's the full function:
function create_voucher(visitor_name, unique_number, pack_choice)
{
var canvas = $("#canvas_voucher")[0];
var str_pack_name;
alert(pack_choice);
switch (pack_choice)
{
case 1:
str_pack_name = 'text 1';
alert(str_pack_name);
break;
case 2:
str_pack_name = 'text 2';
alert(str_pack_name);
break;
case 3:
str_pack_name = 'text 3';
alert(str_pack_name);
break;
}
alert(str_pack_name);
if (canvas.getContext)
{
var ctx = canvas.getContext('2d');
// Draw shapes
var img = new Image();
img.src = 'images/voucher.jpg';
img.onload = function(){
ctx.drawImage(img,0,0);
ctx.fillStyle="#000000";
ctx.font="bold 25px monospace";
ctx.fillText(str_pack_name, 600, 120);
...draw name...
...draw number...
draw_voucher_img();
...jQuery functions...
}
} else {
alert('Please update your browser');
}
}
The alert(pack_choice);
does give me the passed number. But alert(str_pack_name);
gives me undefined
message in the alert and draws it on the canvas.
Why is it happening and how do I fix it?