如何为js乌龟分形制作动画?

How to animate js turtle fractals?

我需要用js画分形turtle library so, the fractal is animated until it is drawn totally like the python turtle example here

我试过命令 here。文档中有一个名为 animate 的函数,但我无法使用它,它只会等待一段时间,然后在没有任何动画的情况下绘制分形。

我输入命令输入animate(demo, 1000)

有什么建议吗?

如果我不能在 js turtle 中使用动画有没有另一个快速简单的库可以完成绘制分形的工作?!

您没有正确使用 animate()。您不能只将它应用于已完成的程序并期望其行为发生变化。相反,您需要将其合并到程序中。应该有一个函数在每次调用时绘制动画的一部分。然后让 animate() 一遍又一遍地调用它。重写您的示例:

function square(side) {
    repeat(4, function () {
        forward(side);
        right(90);
    });
}

var s = 100

function draw() {
    square(s);
    right(36)

    s -= 10

    if (s < 0) {
        s = 100
        clear()
    }
}

function demo() {
    hideTurtle();
    colour(0, 0, 255, 1);
    animate(draw, 500);
}

通过 demo() 调用它,不要对其调用 animate()。它的基本动画单位是正方形。如果你想看到正在绘制的正方形,那么你需要重新设计代码,使基本动画单元成为正方形的一侧(即 line.)