I am writing a little app using canvas.
In app have a button "next" when i click next canvas draw another text.
I use clearRect to clear canvas before redraw text. But the first text draw never remove.
This is my code:
ctx = $("canvas").get("0").getContext('2d');
ctx.canvas.width = 300;
ctx.canvas.height = 100;
var fontSize = 300/4;
ctx.font= fontSize + "px Times New Roman";
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.fillStyle = "rgb(255, 0, 0)";
ctx.fillText(VietBasic.mainWord[0],300/2,(100 - fontSize*1.4)/2);
$("#next").bind("click.next", function(){
k++;
if(k >= VietBasic.mainWord.length){
k=0;
}
ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);
ctx.font= fontSize + "px Times New Roman";
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.fillStyle = "rgb(255, 0, 0)";
ctx.fillText(VietBasic.mainWord[k],300/2,(100 - fontSize*1.4)/2);
$("#imgSample").html(VietBasic.mainWord[k]);
$("#wordSample").html(VietBasic.keyWord[k]);
$("#mainImg").attr("src",VietBasic.imageSample[k]);
});
This is screenshot:
Please help me, i am newbie... Thanks for reading! :)