将连续帧绘制到 HTML5 Canvas 仅显示最后一帧
我有一个复杂的循环,可以计算我想要在画布元素中显示的内容的各种静止帧。每次计算帧时,都会显示它,我调用一个计时器并等到我清除它,然后显示下一个帧,依此类推。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用
setTimeout
而不是循环。另外,我宁愿使用
context.clearRect
(请参阅 MDC )而不是调整大小,没有任何性能差异。按照这些思路应该可以做到这一点:
永远不要尝试在 JavaScript 中模拟
sleep
,您将阻止整个浏览器在您sleeping
时执行任何操作。You have to use
setTimeout
instead of your loop.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:
Never try to emulate
sleep
in JavaScript you will block the whole Browser from doing anything while you'resleeping
.