0
在這裏,我想通過這個函數「launch」傳遞一個參數,參數是tntLoader。然而,它通過成功,並執行包括計時器(間隔)功能運行得很好,問題是它不顯示在畫布上的整個文本。它只是顯示一條小線。是的畫布對象正確加載,一切都在html文件中定義好和好。所以它應該是這樣的:launch(tntLoader);
。JavaScript沒有在畫布上正確繪製文本
var surface;
var timer;
var counter = 0;
var tntLoader= {
canvas:"canvas",
speed:30,
x:0,
y:0,
textSize:16,
canvasElement:0, /* Do not modify this variable */
surface:0, /* Do not modify this variable */
font:"Arial",
color:"#00000",
text:"Sample Text",
type:"fill"
};
function launch(feg){
counter=0;
feg.canvasElement = document.querySelector(feg.canvas);
feg.surface = feg.canvasElement.getContext("2d");
timer = window.setInterval(function(){drawText(feg);},feg.speed);
}
function drawText(drawingObject){
if(counter>drawingObject.text.length){
window.clearInterval(timer);
}
drawingObject.surface.font=drawingObject.textSize+"px "+drawingObject.font;
if(drawingObject.type=="fill"){
drawingObject.surface.fillText(drawingObject.text.charAt(counter),drawingObject.x+30*counter,drawingObject.y);
counter++;
}else{
drawingObject.surface.strokeText(drawingObject.text.charAt(counter),drawingObject.x+30*counter,drawingObject.y);
counter++;
}
}