小编rob*_*uan的帖子

使用requestAnimationFrame控制fps?

这似乎requestAnimationFrame是现在制作动画的事实上的方式.在大多数情况下,它对我来说效果很好,但是现在我正在尝试做一些画布动画,我想知道:有没有办法确保它以某个fps运行?我知道rAF的目的是为了一贯平滑的动画,我可能冒着使我的动画不稳定的风险,但是现在看起来它的速度几乎是任意的,并且我想知道是否有办法打击不知何故.

我使用setInterval但我想要rAF提供的优化(特别是当选项卡处于焦点时自动停止).

如果有人想查看我的代码,它几乎是:

animateFlash: function() {
    ctx_fg.clearRect(0,0,canvasWidth,canvasHeight);
    ctx_fg.fillStyle = 'rgba(177,39,116,1)';
    ctx_fg.strokeStyle = 'none';
    ctx_fg.beginPath();
    for(var i in nodes) {
        nodes[i].drawFlash();
    }
    ctx_fg.fill();
    ctx_fg.closePath();
    var instance = this;
    var rafID = requestAnimationFrame(function(){
        instance.animateFlash();
    })

    var unfinishedNodes = nodes.filter(function(elem){
        return elem.timer < timerMax;
    });

    if(unfinishedNodes.length === 0) {
        console.log("done");
        cancelAnimationFrame(rafID);
        instance.animate();
    }
}
Run Code Online (Sandbox Code Playgroud)

其中Node.drawFlash()只是一些代码,它根据计数器变量确定半径,然后绘制一个圆.

javascript performance animation canvas requestanimationframe

111
推荐指数
8
解决办法
9万
查看次数

Jinja2和Flask:将变量传递给父模板而不将其传递给子模板

假设我有一个带有标题的基本模板,并且该标题的内容需要传递到模板中.

<header>
  You are logged in as {{ name }}
</header>
Run Code Online (Sandbox Code Playgroud)

此基本模板扩展了许多页面.如何传递该变量而不将其传递给每个孩子?例如,我不想这样做:

render_template("child1.html", name=user.name)
render_template("child2.html", name=user.name)
render_template("child3.html", name=user.name)
etc...
Run Code Online (Sandbox Code Playgroud)

因为谁知道我可能有多少个子页面.它感觉不够干燥.

我从来没有实际渲染基本模板,只有它的子项,但我不知道如何传递数据.

有没有办法做到这一点?我不应该使用继承吗?

python variables templates jinja2 flask

5
推荐指数
1
解决办法
6914
查看次数