在画布上显示一个字符串,只有几秒钟

时间:2015-01-07 06:11:38

标签: javascript jquery html html5 canvas

我已经编写了一个代码,在单击开始按钮的情况下,我的单词应显示几秒钟。我该怎么做?

function start()
    {

      block.x = 0;
      for (var i = 0; i < boxes.length; i++) 
      {
          resetbox(boxes[i]);


      }
    if (!continueAnimating) 
    {
        continueAnimating = true;
        animate();
    }
    }

我想显示这个让我们说5秒:var text = randomLetter(nu);

See Demo

2 个答案:

答案 0 :(得分:2)

我可以给你一个简单的演示,用于在按钮点击:

1秒内在画布上书写和清除文本

DEMO

代码:

var c=document.getElementById("canvas");
var ctx=c.getContext("2d");

function clear(){
     ctx.clearRect(0,0,c.width,c.height);
}

function write()
{
    var c=document.getElementById("canvas");
    var ctx=c.getContext("2d");

    ctx.font="20px Georgia";
    ctx.fillText("Hello World!",10,50);

    ctx.font="30px Verdana";
    // Create gradient
    var gradient=ctx.createLinearGradient(0,0,c.width,0);
    gradient.addColorStop("0","magenta");
    gradient.addColorStop("0.5","blue");
    gradient.addColorStop("1.0","red");
    // Fill with gradient
    ctx.fillStyle=gradient;
    ctx.fillText("Big smile!",10,90);
}

function start()
    {
        write();
        setTimeout(function(){
            clear();
        },1000);
    }

答案 1 :(得分:0)

尝试使用以下代码 - 您必须使用setTimeout()

<强> Click Here to See Working Demo

<强> HTML

<div id="someDiv">Your Text Here</div>
<input id="SomeButton" type="button" value="Start">

<强> Jquery的

$(document).ready(function(){
       $("#someDiv").hide();
    $("#SomeButton").click(function(){            
        start();
    });        
});

function start()
{
    $("#someDiv").show();
    setTimeout(function () {
                    $("#someDiv").fadeOut(1000, function () { $(this).hide(); });
                }, 2000);
}