简体   繁体   English

如果条件在画布中无法正常工作

[英]If condition not working properly in canvas

Actually i have a set of points which when connected forms like pipes connected horizontally and vertically. 实际上,我有一组要点,这些要点在连接时就像水平和垂直连接的管道一样。 My aim is to move the circle through the points / pipes.The if condition is also not working properly. 我的目的是通过点/管道移动圆。if条件也无法正常工作。

When the begin function is called it takes the first and second points and the circle moves from the first to the second point through the first if condition. 调用begin函数时,它将获取第一点和第二点,圆通过第一if条件从第一点移至第二点。

Then when it reaches the second point it should go to the next if condition where the current animation stops and a new animation starts from the callBegin function. 然后,当到达第二点时,如果当前动画停止并且从callBegin函数开始一个新动画的条件下,它应该转到下一个。

The code goes like below 代码如下

    var ParticleGen = function() {
        var particle = this;
        this.begin = function(){
            var pipeBegin = points[pipeIndex];
            var pipeEnds = points[pipeIndex + 1];
            nx = pipeBegin.x;
            ny = pipeBegin.y;
            if(pipeBegin.x == pipeEnds.x ){
                if( pipeBegin.y > pipeEnds.y){
                    // endpoint y greater than start point y moving upwards
                    d = "up";
                    function animloop(){
                        if(ny > pipeEnds.y) {
                            ctx.clearRect(0, 0, w, h);
                            drawCircle(nx, ny);
                            ny--;
                            nx = nx;
                        }
                        requestAnimFrame(animloop);
                    }
                    animloop();
                }else if( pipeBegin.y < pipeEnds.y ){
                    d = "down";
                    function animloop1(){
                        if(ny < pipeEnds.y) {
                            ctx.clearRect(0, 0, w, h);
                            drawCircle(nx, ny);
                            ny++;
                            nx = nx;
                        }
                        requestAnimFrame(animloop1);
                    }
                    animloop1();

                }else if(ny == pipeEnds.y){
                    cancelAnimationFrame(animloop1);
                    particle.callBegin();
                }           
            }else if(pipeBegin.y == pipeEnds.y ){
                if(pipeBegin.x < pipeEnds.x){
                    // start.x < end.x right movement
                    d = "right";
                    function animloop2(){
                        if(nx < pipeEnds.x) {
                            ctx.clearRect(0, 0, w, h);
                            drawCircle(nx, ny);
                            nx++;
                            ny = ny;
                        }
                        requestAnimFrame(animloop2);
                    }
                    animloop2();
                }else if(pipeBegin.x > pipeEnds.x) {
                    d = "left";
                    function animloop3(){
                        if(nx > pipeEnds.x) {
                            ctx.clearRect(0, 0, w, h);
                            drawCircle(nx, ny);
                            nx--;
                            ny = ny;
                        }
                        requestAnimFrame(animloop3);
                    }
                    animloop3();

                }else if(nx == pipeEnds.x){
                    cancelAnimationFrame(animloop2);
                    particle.callBegin();
                }   
            }
        }
        this.callBegin = function(){
            if(pipeIndex <= 3){
                pipeIndex++;
            }
            particle.begin();
        }
    };

function drawCircle(cx, cy){
    ctx.beginPath();
    ctx.arc(cx, cy, 2, 0, 2 * Math.PI, false);
    ctx.fillStyle = "black";
    ctx.fill(); 
    ctx.closePath();
}

var newObj = new ParticleGen();
newObj.begin();

I am new to this.thats y... I have the fiddle here.. Thank you. 我是新来的。那是...我在这里摆弄小提琴谢谢。

I think that your method, with all those ifs, is too complex for what you're trying to achieve. 我认为您的方法以及所有这些ifs对于您要实现的目标来说太复杂了。 It could be done with considerably less code. 可以用更少的代码来完成。

Apart from that, your problem was that you were checking whether to stop the animation and start a new segment in the wrong place. 除此之外,您的问题是您正在检查是否要停止动画并在错误的位置开始新的片段。 You were doing it in the main code of begin(), but this method is only called once at the start of each segment. 您在begin()的主要代码中进行了此操作,但是此方法在每个段的开头仅被调用一次。 When the dot reaches the end, you don't call begin() again, so you never check whether to stop the animation and start a new one. 当点到达末尾时,您不会再次调用begin(),因此您永远不会检查是否停止动画并开始新的动画。 In fact, your current animation remains active forever but it just doesn't do anything because of one of these ifs: 实际上,您当前的动画将永远保持活动状态,但由于以下其中一种情况,它什么也没做:

if(ny > pipeEnds.y) {
    ctx.clearRect(0, 0, w, h);
    drawCircle(nx, ny);
    ny--;
    nx = nx;
}

And it is in these ifs, where you check whether the dot has reached the end of its path, wher you should call cancelAnimFrame and start a new sequence. 在这些ifs中,您将检查点是否已到达其路径的末尾,然后应调用cancelAnimFrame并开始一个新序列。 I've updated your fiddle and copy here the relevant code (begin() and callBegin() functions). 我已经更新了您的小提琴,并在此处复制了相关代码(begin()和callBegin()函数)。

this.begin = function () {
    var pipeBegin = points[pipeIndex];
    var pipeEnds = points[pipeIndex + 1];
    nx = pipeBegin.x;
    ny = pipeBegin.y;
    if (pipeBegin.x == pipeEnds.x) {
        if (pipeBegin.y > pipeEnds.y) {
            // endpoint y greater than start point y moving upwards
            d = "up";

            function animloop() {
                if (ny > pipeEnds.y) {
                    ctx.clearRect(0, 0, w, h);
                    drawCircle(nx, ny);
                    ny--;
                    nx = nx;
                    requestAnimFrame(animloop);
                }else if (ny == pipeEnds.y) {
                    cancelAnimationFrame(animloop);
                    particle.callBegin();
                }

            }
            animloop();
        } else if (pipeBegin.y < pipeEnds.y) {
            d = "down";

            function animloop1() {
                if (ny < pipeEnds.y) {
                    ctx.clearRect(0, 0, w, h);
                    drawCircle(nx, ny);
                    ny++;
                    nx = nx;
                    requestAnimFrame(animloop1);
                } else if (ny == pipeEnds.y) {
                    cancelAnimationFrame(animloop1);
                    particle.callBegin();
                }

            }
            animloop1();

        } 
    } else if (pipeBegin.y == pipeEnds.y) {
        if (pipeBegin.x < pipeEnds.x) {
            // start.x < end.x right movement
            d = "right";

            function animloop2() {
                if (nx < pipeEnds.x) {
                    ctx.clearRect(0, 0, w, h);
                    drawCircle(nx, ny);
                    nx++;
                    ny = ny;
                    requestAnimFrame(animloop2);
                } else if (ny == pipeEnds.y) {
                    cancelAnimationFrame(animloop2);
                    particle.callBegin();
                }

            }
            animloop2();
        } else if (pipeBegin.x > pipeEnds.x) {
            d = "left";

            function animloop3() {
                if (nx > pipeEnds.x) {
                    ctx.clearRect(0, 0, w, h);
                    drawCircle(nx, ny);
                    nx--;
                    ny = ny;
                    requestAnimFrame(animloop3);
                } else if (ny == pipeEnds.y) {
                    cancelAnimationFrame(animloop3);
                    particle.callBegin();
                }

            }
            animloop3();

        } else if (nx == pipeEnds.x) {
            cancelAnimationFrame(animloop2);
            particle.callBegin();
        }
    }
}
this.callBegin = function () {
    if (pipeIndex <= 3) {
        pipeIndex++;
        console.log(pipeIndex)
        particle.begin();
    }

}

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

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