简体   繁体   English

播放/暂停任何画布动画

[英]Play/Pause any Canvas animation

I'm building a tool where I have an IFRAME inside of which a user can put any HTML containing a canvas animation.我正在构建一个工具,其中有一个 IFRAME,用户可以在其中放置任何包含画布动画的 HTML。

The purpose is to let them choose if they want to use createJS, Adobe Edge Animate or any other tool they prefer.目的是让他们选择是否要使用 createJS、Adobe Edge Animate 或任何其他他们喜欢的工具。

Despite this I need to be able to play and pause this canvas animation no matter which tool they used.尽管如此,无论他们使用哪种工具,我都需要能够播放和暂停这个画布动画。

Do you think this is possible?你认为这可能吗? Or do you think I will be tied to the framework they used?或者你认为我会被他们使用的框架所束缚?

I've tried clearing the request animation frame of the page but it didn't work well.我尝试清除页面的请求动画框架,但效果不佳。

iframe.contentWindow.cancelAnimationFrame(<don't know what to put in here without accessing the animation framework>)

Do you have any suggestion?你有什么建议吗?

Thank you!谢谢!

Andrea安德烈亚

EDIT: In my case scenario the iframe is a sort of sand-box where the user can put whatever it wants, even the javascript for the functioning of the framework he used编辑:在我的情况下,iframe 是一种沙盒,用户可以在其中放置任何想要的东西,甚至是他使用的框架功能的 javascript

Supporting different Html5 Canvas libraries支持不同的 Html5 Canvas 库

It is theoretically possible because while most libraries have their own built-in animation methods, you can certainly just use their drawing methods and then use your own animation loop to animate their drawings.这在理论上是可能的,因为虽然大多数库都有自己的内置动画方法,但您当然可以只使用他们的绘图方法,然后使用自己的动画循环来为他们的绘图设置动画。

But, Wow!但是,哇! This would be a huge task.这将是一项艰巨的任务。 Just off the top of my head you would have to:就在我的头顶上,你必须:

  1. Load only the code of the users selected library - eg.仅加载用户所选库的代码 - 例如。 Easel.js . Easel.js

  2. Create a canvas DOM element which any library must use to display drawings.创建一个画布 DOM 元素,任何库都必须使用它来显示绘图。

  3. Create a hook to let them set up their particular libraries environment.创建一个钩子让他们设置他们特定的库环境。 For example, this is where the EaselJS user would create their stage: var stage = new createjs.Stage("theRequiredCanvas");例如,这是 EaselJS 用户创建他们的舞台的地方: var stage = new createjs.Stage("theRequiredCanvas");

  4. Create a hook to let them run their brand of code inside your animation loop.创建一个钩子,让他们在你的动画循环中运行他们的代码。

  5. To hook their code into your framework, you would have to require them to put all their code into a .js file that is loaded with your framework.要将他们的代码挂钩到您的框架中,您必须要求他们将所有代码放入一个随您的框架一起加载的.js文件中。

Stopping...!停下来……!

I'm going to stop reasoning out a solution here because this would be more work for the user than just using their own library.我将停止在这里推理出解决方案,因为这对用户来说比仅使用他们自己的库更重要。

The easy part of your question: Pausing & Continuing an animation问题的简单部分:暂停和继续动画

You can set a flag that stops the animation loop.您可以设置一个标志来停止动画循环。

When you want to continue the animation you clear that flag and request the animation loop.当您想继续动画时,您可以清除该标志并请求动画循环。

Example code:示例代码:

 var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); ctx.fillStyle='skyblue'; ctx.strokeStyle='lightgray'; ctx.lineWidth=3; var cw=canvas.width; var ch=canvas.height; // animation will pause when paused==true var paused=false; // testing, a rotation angle for the animated rect var angle=0; // pause the animation on #pause click $('#pause').on('click',function(){ paused=true; }); // continue the animation on #continue click $('#continue').on('click',function(){ paused=false; requestAnimationFrame(animate); }); // start the animation loop requestAnimationFrame(animate); function animate(time){ if(paused){return;} // animate anything ctx.clearRect(0,0,cw,ch); ctx.translate(cw/2,ch/2); ctx.rotate(angle); ctx.fillRect(-50,-50,100,100); ctx.strokeRect(-50,-50,100,100); ctx.setTransform(1,0,0,1,0,0); // increase the angle for the next loop angle+=Math.PI/60; // request another animation loop requestAnimationFrame(animate); }
 body{ background-color: ivory; } #canvas{border:1px solid red; margin:0 auto; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <button id='pause'>Pause</button> <button id='continue'>Continue</button> <br> <canvas id="canvas" width=300 height=300></canvas>

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

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