有点被这个问题困扰,这就是为什么需要帮助。我使用 HTML5 和 JavaScript 在图像上插入文本并创建一个带有文本的新图像。如果我手动添加文本,一切都很好,但是如果我尝试将文本作为值传递给函数,则不会发生。下面是函数代码
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);
};
我通过
调用这个函数draw_text('Text 1','Text 2');
但是我要么 Canvas 根本不可见,要么它带有文本“未定义”。我究竟做错了什么?顺便说一句,如果有什么重要的话,我会在 codeigniter View 文件中执行此代码。
最佳答案
您没有正确使用“这个”。
替换:
var toptext = this.topt;
var bottomtext = this.bott;
与:
var toptext = topt;
var bottomtext = bott;
***此外,我刚刚注意到您对“draw_text()”进行了递归调用,而在draw_text的定义中没有参数,这也是一个问题。
关于JavaScript,将值传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3539535/