对由 KineticJS 框架创建的 HTML5 画布进行动画调整大小

发布于 2024-12-29 08:31:33 字数 858 浏览 0 评论 0 原文

我正在尝试更改由 KineticJS框架——也就是说,不是画布内的对象,而是元素的大小。

由于 jQuery .animate 函数不能用于此事(它更改 CSS,我希望它更改元素的实际属性),我不得不开发 我自己的函数,使用内部 stage.setSize(width, height) KineticJS API 提供的函数。 我根本没有编写动画函数的经验,所以我可能完全错误地处理这种情况。

问题: 它依赖于性能,因此通常不够快(感谢 setInterval)。更不用说它仅部分适用于移动设备(iPhone 4S iOS 5.0.1 测试)。任何解决方案都必须或多或少完美地工作,即使在移动设备上也是如此。

我正在寻找不同的方法来改进这个功能。射击。

(对于那些没有看到我的代码的链接;http://jsfiddle.net/G4nuH/ animateResize 是相关函数。)

I'm trying to change the size of an HTML5 canvas element created by the KineticJS framework--that is, not the objects inside the canvas, but the element's size.

Since the jQuery .animate function can't be used in this matter (it changes CSS, I want it to change the actual attributes of the element), I had to develop my own function that uses the internal stage.setSize(width, height) function provided by the KineticJS API.
I have no experience at writing animation functions at all, so I might be approaching the situation completely wrong.

The problem:
It is performance dependent, therefore often not fast enough (thanks to setInterval). Not to mention that it only partially works on mobile devices (iPhone 4S iOS 5.0.1 tested). Any solution has to work more or less flawlessly, even on mobile devices.

I'm looking for different ways to improve this function. Shoot.

(For those who didn't get see the link to my code; http://jsfiddle.net/G4nuH/animateResize is the relevant function.)

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

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

发布评论

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

评论(3

清旖 2025-01-05 08:31:33

在不知道最终应用程序的具体情况的情况下,我建议如果可以的话避免使用画布尺寸的动画。您可能知道,如果更改画布元素的大小,存储在其上的所有内容都会被清除。这意味着动画尺寸需要您增量调整宽度和/或高度,同时在每次迭代时重新绘制整个画布。对于台式机来说,这可能不是一个大问题。不过,移动设备将陷入困境。

相反,我建议您通过增加容器元素的大小(带有边框、背景颜色等,以便动画清晰可见)来伪造动画。然后,当动画完成时,将当前画布数据保存到临时对象,增加画布的大小,并将旧内容印回其上。

如果您希望对画布尺寸进行动画处理以显示更大的理论画布上已经存在的内容(即用户有一个正在裁剪整个画布的小窗口),那么最好使用 CSS width< /code> 和 height 以及 overflow: hide; 属性。使用这种方法,您将在所有绘制操作期间编辑整个画布,但动画视口的大小将变得简单而流畅。

Without knowing the specifics of your final application, I would recommend avoiding animation of the canvas size if you can. As you probably know, if you change the size of a canvas element, everything stored on it is wiped clean. This means that animating the dimensions requires you to adjust the width and/or height incrementally while re-drawing the entire canvas at each iteration. For a desktop, this probably isn't a huge issue. Mobile devices will struggle, though.

Instead, I would suggest that you fake the animation by increasing the size of a container element (with a border, background color, etc. so the animation is apparent). Then, when the animation is done, save your current canvas data to a temporary object, increase the size of your canvas, and stamp the old content back on it.

If you are looking to animate the canvas dimensions to reveal content that is already present on a larger theoretical canvas (i.e. the user has a small window that's cropping the full canvas), you would be better off playing with your CSS width and height along with the overflow: hidden; property. With this approach you would be editing your full canvas during all draw operations, but animating the size of your viewport would be simple and smooth.

娇纵 2025-01-05 08:31:33

画布是图形的视口,它可以是文档的整个大小。您可以使用 context.clip() 函数来定义您想要显示的区域,而不是调整画布大小,这需要更新 html 框模型。 (你的性能问题!)

ctx.moveTo(162, 20);
ctx.lineTo(162, 320);
ctx.lineTo(300, 320);
ctx.lineTo(300, 20);
ctx.clip();

The canvas is a view port for graphics, it can be the entire size of your document. You can use the context.clip() function to define the area you wish to display instead of resizing the canvas which requires the html box model to be updated. (your performance problem!)

ctx.moveTo(162, 20);
ctx.lineTo(162, 320);
ctx.lineTo(300, 320);
ctx.lineTo(300, 20);
ctx.clip();
风吹雨成花 2025-01-05 08:31:33

经过一番研究,我找到了自己的解决方案。使用 jQuery.animate 的示例。必须对它们进行动画处理,因为 KineticJS 有几个背景层。

    //Gather all canvases
    var canvases = document.getElementsByTagName("canvas");

    //Animate their size
    for(var c in canvases) {
       $(canvases[c]).animate({
          'width': "100px",
          'height': "100px"
       }, 500);
    }

After some research, I found my own hacky-solution. An example using jQuery.animate. Had to animate them all, since KineticJS has several background layers.

    //Gather all canvases
    var canvases = document.getElementsByTagName("canvas");

    //Animate their size
    for(var c in canvases) {
       $(canvases[c]).animate({
          'width': "100px",
          'height': "100px"
       }, 500);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文