简体   繁体   English

使用clearRect()覆盖画布上的文本输出?

[英]using clearRect() to overwrite text output on canvas?

I'm making a little game on jsbin and everything so far is going well but I'm have a slight problem. 我正在jsbin上做一些小游戏,到目前为止一切进展顺利,但是我有一个小问题。 The goal of the game is to click the randomly appearing circle as many times as possible in one minute. 游戏的目的是在一分钟内尽可能多地单击随机出现的圆圈。 I want it to output the time left and the score in the corners, and I have done so. 我希望它输出剩余时间和弯道得分,而我已经做到了。 The problem is that they are overwriting each other. 问题在于它们彼此覆盖。 This is because to prevent flickering I decided not to use 这是因为为了防止闪烁,我决定不使用

c.clearRect(0,0,canvas.width,canvas.height);

instead drawing a clearRect just over the circle when its clicked. 而是在单击圆时在圆上绘制一个clearRect。 I want to do a similar thing with text. 我想对文本做类似的事情。 I used this line: 我用了这一行:

c.clearRect(0,fontSize,c.measureText(timeLeft),fontSize);

this should work but it has no effect. 这应该可以,但是没有效果。 I've tried everything, but I don't know what's wrong with this line. 我已经尝试了一切,但是我不知道这条线出了什么问题。 My only other theroy is that is in the wrong spot in the code, but I haven't found a problem with that. 我唯一的理论是代码中的错误位置,但是我没有发现任何问题。

Here is the link to the current version I'm working on: http://jsbin.com/touchgame/10/edit 这是我正在使用的当前版本的链接: http : //jsbin.com/touchgame/10/edit

Thanks! 谢谢!

measureText() returns an object with a width property, so you need to use c.measureText(timeLeft).width. measureText()返回具有width属性的对象,因此您需要使用c.measureText(timeLeft).width。

Also, you decreased the timeLeft and then called clearRect, which will clear a rectangle based on the new width for timeLeft. 另外,您减小了timeLeft, 然后调用clearRect,它将根据timeLeft的宽度清除一个矩形。 You want to clear based on the width for the "old" timeLeft value and then decrease the timeLeft: 您要根据“旧” timeLeft值的宽度清除, 然后减小timeLeft:

if (timeLeft < 1){
  c.clearRect(0,fontSize,c.measureText(timeLeft).width + 5,fontSize*1.5);

  timeLeft--;
  //clear over time output

  console.log(c.measureText(timeLeft));
}

This should work. 这应该工作。 Because the way drawing text works, it's not trivial to know exactly the bounding box of the text, so that's why we clear a larger area than fontSize. 因为绘制文本的方式有效,所以确切地知道文本的边界框并非易事,因此这就是我们清除比fontSize大的区域的原因。 You could use c.textBaseline = 'top', which would place the text with the top coordinate at the y you specify instead of the baseline of the text at y. 您可以使用c.textBaseline ='top',它将具有顶部坐标的文本放置在您指定的y处,而不是将文本的基线放置在y处。

Finally, I think it's an easier approach to clear the canvas completely and redraw everything when you want to update the graphics. 最后,我认为这是一种更简单的方法,可以在需要更新图形时完全清除画布并重新绘制所有内容。 If you clear the canvas and then immediately redraw evrything you won't get any flickering. 如果清除画布,然后立即重新绘制evrything,则不会有任何闪烁。 The performance hit of redrawing everything is usually neglible in most cases, and it makes things a lot simpler. 在大多数情况下,重绘所有内容的性能影响通常可以忽略不计,这使事情变得简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM