views:

58

answers:

1

Kind of stuck with this thing, that's why need help please. I am using HTML5 and JavaScript to insert text on an image and create a new image with the text on it. Everything is fine if I am adding the text manually, however if I am trying to pass the text to the function as a value, its not happening. Here's the function code below

 function draw_text(topt,bott){
  var canvas = document.getElementById("e");
  var context = canvas.getContext("2d");
    var img = new Image();
    img.src = "testimage.jpg";
    img.onload = function() 
    {
    context.drawImage(img, 0, 0);
    draw_text();
    };
    var toptext =  this.topt;
    var bottomtext =  this.bott;
    context.font = "12px Arial";
    context.fillStyle = "white"; 
    context.strokeStyle = 'black';
    context.fillText(toptext, (img.width-context.measureText(toptext).width)/2, 40);
    context.strokeText(toptext, (img.width-context.measureText(toptext).width)/2, 40);
    context.fillText(bottomtext, (img.width-context.measureText(bottomtext).width)/2, 350);
    };

And I am calling this function by

draw_text('Text 1','Text 2');

But I either the canvas is not visible at all or it comes with the text 'Undefined'. What am I doing wrong? By the way, if it's any important I am doing this code in a codeigniter view file.

+3  A: 

You are not using 'this' correctly.

Replace:

var toptext =  this.topt;
var bottomtext =  this.bott;

With:

var toptext =  topt;
var bottomtext =  bott;

***Also, I just noticed you have a recursive call to "draw_text()" with no parameters in the definition of draw_text, which is also a problem.

jdc0589
Actually this is how I tried the very first time. Didn't work! Both ways I am getting the same result. But if I assign values to topt and bott manually and code the function like function draw_text() it works.
well, using 'this' the way you were definitely wont work. If calling the function by "draw_text('asd','asd')" works but not "draw_text(somevar, somevar2)", then one of the two variables you are passing in is simply null or undefined.
jdc0589
Also, check the edit to my original answer
jdc0589
Sorry if I was not clear in my last comment. I meant that I tried it just the way you commented the first time and it didnt work. Then I tried using "this" and that too didn't work. If I don't pass any variable at all and write the function like draw_text() and manually pass the values in the function itself it works. Don't know if it has got to do anything with codeigniter ...but somehow I am not able to pass the value to the function irrespective of if I use 'this' or not. Removed the recursive function too .. same result. Still checking though!
Problem solved! Thanks to you, I took a close look at the recursive call and that solved it. No, removing the recursive call to draw_text didn't solve it. But if I pass the arguments to that recursive call also, it works. No idea why it happens though, I am no JS expert. But it works this way!
Because you were calling the method without parameters, so the value of topt and bott in that call are (the arguments of the current call dont get automatically passed on or anything)
jdc0589