简体   繁体   English

Canvas clearRect 无法使用 Animate

[英]Canvas clearRect not working using Animate

I am using a animate drawing lines technique to create lines between each of my points.我正在使用动画绘制线条技术在我的每个点之间创建线条。 Once a point is activated I want an old point to be removed.激活一个点后,我希望删除一个旧点。 I set clearRect outside of my interval but its not removing the old line.我在间隔之外设置了 clearRect 但它没有删除旧行。 In my previous code (before the setInterval was created) worked flawlessly, since adding the setinterval, not so much.在我之前的代码中(在创建 setInterval 之前)工作完美,因为添加了 setinterval,并没有那么多。

Here is the script for just the clear / creation.这是用于清除/创建的脚本。 Thank you for your help.谢谢您的帮助。

function draw(st,en){
            var c = document.getElementById("map_id");
            var ctx = c.getContext("2d");
            ctx.clearRect(0, 0, c.width, c.height);
            ctx.translate(0.5, 0.5);
            var amount = 0;
  
            setInterval(function() {
                amount += 0.05; // change to alter duration
                  if (amount > 1) amount = 1;
                  ctx.clearRect(0, 0, c.width, c.height);
                  ctx.strokeStyle = "#ccc";
                  ctx.moveTo(st['x'], st['y']);
                  ctx.lineWidth = 5;
                  // lerp : a  + (b - a) * f
                  ctx.lineTo(st['x'] + (en['x'] - st['x']) * amount, st['y'] + (en['y'] - st['y']) * amount);
                  ctx.stroke();
            }, 30);
        }

UPDATE **** I found a post regarding my issue.更新 **** 我发现了一篇关于我的问题的帖子。 It says I need to include beginPath.它说我需要包含 beginPath。 I did so, but now I am getting flicker of my lines which means the setInterval is not stopping once it is complete.我这样做了,但现在我的线条闪烁,这意味着 setInterval 一旦完成就不会停止。

So checking for completion of drawn line to unset the interval is the solution.因此,检查绘制线是否完成以取消设置间隔是解决方案。 Almost there.差不多好了。

Have the solution to my issue.有我的问题的解决方案。

I tracked the var amount to determine when it has reached 1 with if ( amount == 1) window.clearInterval(map_int)我使用 if ( amount == 1) window.clearInterval(map_int) 跟踪 var 数量以确定它何时达到 1

Problem solved!问题解决了! See solution below请参阅下面的解决方案

function draw(st,en){
            var c = document.getElementById("map_id");
            var ctx = c.getContext("2d");
            ctx.clearRect(0, 0, c.width, c.height);
            ctx.translate(0.5, 0.5);
            var amount = 0;
  
            let map_int = setInterval(function() {
                amount += 0.05; // change to alter duration
                  if (amount > 1) amount = 1;
                  ctx.beginPath();
                  ctx.clearRect(0, 0, c.width, c.height);
                  ctx.strokeStyle = "#ccc";
                  ctx.moveTo(st['x'], st['y']);
                  ctx.lineWidth = 5;
                  // lerp : a  + (b - a) * f
                  ctx.lineTo(st['x'] + (en['x'] - st['x']) * amount, st['y'] + (en['y'] - st['y']) * amount);
                  ctx.stroke();
                  if(amount == 1) window.clearInterval(map_int);
            }, 15);
        }

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

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