简体   繁体   English

HTML5 canvas:如何为上下文设置动画?

[英]HTML5 canvas: How to animate context?

Is there any way how to animate context in the HTML5 canvas 2d? 有什么方法可以在HTML5 canvas 2d中设置上下文动画吗?

I would like to create huge context but to show only small part of it on the screen. 我想创建一个巨大的上下文,但只在屏幕上显示一小部分。 Then I would like to apply translate animation to scroll the context to show other parts of the context. 然后,我想应用translate动画来滚动上下文以显示上下文的其他部分。

How should I do it? 我该怎么办?

Create one canvas with a context that contains the entire thing you want to display, you can do this by making it onscreen if you need to so you can see what it looks like. 创建一个具有包含要显示的整个内容的上下文的画布,可以通过在需要时将其显示在屏幕上来做到这一点,以便可以看到它的外观。

Then take what you have for making that and enclose it within a function or some kind of closure. 然后,将要实现的功能包含在函数或某种闭包中。 Then use document.createElement('canvas'), and use it's context for the code you created for the display context. 然后使用document.createElement('canvas'),并将其上下文用作您为显示上下文创建的代码。

You can then use .drawImage() when within the main context of your visible canvas, to draw the other context to the screen, and animate it upwards in your animation loop. 然后,当您在可见画布的主上下文中时,可以使用.drawImage()将另一个上下文绘制到屏幕上,并在动画循环中向上对其进行动画处理。

I wish I could've made that a little easier to understand, it's easier to understand by showing an example. 我希望我可以使它更容易理解,并通过显示一个示例使其更容易理解。

Off-screen rendering is essentially what I'm trying to explain. 屏幕外渲染本质上就是我要解释的内容。

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

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