简体   繁体   English

无法从HTML5画布清除行

[英]Line not clearing from HTML5 Canvas

I'm trying to solve an odd artifact that is occurring in my HTML5 Canvas animation. 我正在尝试解决HTML5 Canvas动画中出现的异常工件。 I'm trying to surround the text with lines that follow each other. 我试图用彼此相邻的行包围文本。 When I get to clearing the third line, there is a line still there as if clearRect() didn't work correctly. 当我要清除第三行时,仍然有一行,好像clearRect()不能正常工作。
I've already googled and my draw functions do indeed use beginPath() and closePath() respectively, so you can imagine my frustration when this thing doesn't clear. 我已经用beginPath()搜索过,并且我的绘制函数确实确实分别使用了beginPath()closePath() ,所以当这个问题不清楚时,您可以想象我的沮丧。

I'm hoping someone can help me see where I've gone wrong with this animation. 我希望有人可以帮助我看看这个动画出了什么问题。

Here's an example of the draw code (because stack overflow is forcing me to put code in here if I link to JSFiddle) 这是绘制代码的示例(因为如果我链接到JSFiddle,则堆栈溢出会迫使我将代码放入此处)

function drawArc(xPos, yPos,
  radius,
  startAngle, endAngle,
  anticlockwise,
  lineColor, fillColor) {
  var startAngle = startAngle * (Math.PI / 180);
  var endAngle = endAngle * (Math.PI / 180);

  var radius = radius;

  context.strokeStyle = lineColor;
  context.fillStyle = fillColor;
  context.lineWidth = 1;

  context.beginPath();
  context.arc(xPos, yPos,
    radius,
    startAngle, endAngle,
    anticlockwise);
  context.fill();
  context.closePath();
}

function drawLine(xStartPos, yStartPos, xEndPos, yEndPos, width, color) {
  context.stokeStyle = color;
  context.lineWidth = width;

  context.beginPath();
  context.moveTo(xStartPos, yStartPos);
  context.lineTo(xEndPos, yEndPos);
  context.stroke();
  context.closePath();
}

JSFiddle: https://jsfiddle.net/xtkbnxx5/9/ JSFiddle: https ://jsfiddle.net/xtkbnxx5/9/

With this version, it looks like the animation is stopping in the wrong place. 使用此版本,动画似乎停止在错误的位置。 However, If you comment out line 126 in the javascript and run it again, you will see that the line is just there and never gets cleared... 但是,如果您注释掉javascript中的第126行并再次运行它,您将看到该行就在那里并且永远不会被清除...

Any help would be greatly appreciated. 任何帮助将不胜感激。

Your context.clearRect(...) statements are not clearing the entire canvas. 您的context.clearRect(...)语句不会清除整个画布。

When I change you clearRect statements to 当我将您的clearRect语句更改为

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

Then the second line eventually disappears. 然后第二行最终消失。

https://jsfiddle.net/xtkbnxx5/10/ https://jsfiddle.net/xtkbnxx5/10/

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

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