将连续帧绘制到 HTML5 Canvas 仅显示最后一帧

发布于 2024-10-11 02:38:09 字数 741 浏览 1 评论 0原文

我有一个复杂的循环,可以计算我想要在画布元素中显示的内容的各种静止帧。每次计算帧时,都会显示它,我调用一个计时器并等到我清除它,然后显示下一个帧,依此类推。

    drawing(transform(alone, Box, canvasx.width, canvasx.height), false, "00f", canvasx);
    drawing(transform(Lines, Box, canvasx.width, canvasx.height), false, "ff0000", canvasx);

    var date = new Date();
    var curDate = null;

    do {
        curDate = new Date();
    }
    while (curDate - date < 550);

    if (alone.length > 0) {
        canvasx.width = canvasx.width;
    } 

如果我在 var 日期行中放置一个断点并每次按播放,则会显示每个单独的帧,但是当我让它运行时,画布在运行时为空,最后显示最后一帧。

现在,如果我删除 canvasx.width = canvasx.width; 我仍然得到相同的行为,只是显然在最后我得到了一个在另一个之上绘制的所有框架。

显然它不是动画,所以我不能在 setinterval 中调用绘图。

有谁知道为什么吗?

I have this complicated loop that calculates various still frames of what I want to show in a canvas element. Each time the frame is calculated it gets displayed I call a timer and wait till I clear it and then the next frames is displayed and so on.

    drawing(transform(alone, Box, canvasx.width, canvasx.height), false, "00f", canvasx);
    drawing(transform(Lines, Box, canvasx.width, canvasx.height), false, "ff0000", canvasx);

    var date = new Date();
    var curDate = null;

    do {
        curDate = new Date();
    }
    while (curDate - date < 550);

    if (alone.length > 0) {
        canvasx.width = canvasx.width;
    } 

if i put a break point in var date line and press play each time, every individual frame get displayed but when I let it run through the canvas is empty while it runs and at the end it displays the last frame.

now if i delete the canvasx.width = canvasx.width; I still get the same behavior only obviously at the end i get all frames drawn one on top of the other.

Obviously its not an animation so i cant call drawing in a setinterval.

does anyone has any idea why;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别再吹冷风 2024-10-18 02:38:10

必须使用setTimeout而不是循环。

// JavaScript is single thread, and this BLOCKS the re-rendering and display of the canvas
do {
    curDate = new Date();
}
while (curDate - date < 550);

另外,我宁愿使用 context.clearRect (请参阅 MDC )而不是调整大小,没有任何性能差异。

按照这些思路应该可以做到这一点:

function drawCanvas() {
    if (alone.length > 0) {
        // clear canvas
    } 
    drawing(transform(alone, Box, canvasx.width, canvasx.height), false, "00f", canvasx);
    drawing(transform(Lines, Box, canvasx.width, canvasx.height), false, "ff0000", canvasx);
    setTimeout(drawCanvas, 550);
}

永远不要尝试在 JavaScript 中模拟 sleep ,您将阻止整个浏览器在您 sleeping 时执行任何操作。

You have to use setTimeout instead of your loop.

// JavaScript is single thread, and this BLOCKS the re-rendering and display of the canvas
do {
    curDate = new Date();
}
while (curDate - date < 550);

Also I'd rather use context.clearRect (See MDC) instead of the resizing, there's no performance difference whatsoever.

Something along these lines should do it:

function drawCanvas() {
    if (alone.length > 0) {
        // clear canvas
    } 
    drawing(transform(alone, Box, canvasx.width, canvasx.height), false, "00f", canvasx);
    drawing(transform(Lines, Box, canvasx.width, canvasx.height), false, "ff0000", canvasx);
    setTimeout(drawCanvas, 550);
}

Never try to emulate sleep in JavaScript you will block the whole Browser from doing anything while you're sleeping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文