简体   繁体   English

如何改善动画的移动性能?

[英]How can I improve my animation's mobile performance?

So, I built this little interactive roulette: 因此,我建立了这个互动小轮盘:

http://techgoldmine.com/roulette/ http://techgoldmine.com/roulette/

I need it to work on mobile as well as desktop. 我需要它既可以在移动设备上也可以在台式机上工作。 Originally I handled interaction by having the user interact with an SVG circle overlapping the image, however for testing purposes I have removed that. 最初,我通过让用户与覆盖图像的SVG圈进行交互来处理交互,但是出于测试目的,我已将其删除。

Currently the animation is carried out using an event with a setInterval function: 当前,使用带有setInterval函数的事件来执行动画:

        $('.roulette').bind('touchstart', function () {
            if (inMotion == true) {
                cleanUp();
            }
            intervalvar = setInterval(spinWheel, 24);
            // spinWheel();
            $(document).bind('touchend', function () {
                count = Math.abs(force)
                mouseup = 1;
            });
        });

After calculations are carried out, I rotate the roulette with this function: 计算完成后,我使用以下功能旋转轮盘:

        function rotate(a) {
            roulette.css({
                '-webkit-transform': 'rotate(' + a + 'deg)',
                '-ms-transform': 'rotate(' + a + 'deg)',
                '-o-transform': 'rotate(' + a + 'deg)',
                '-moz-transform': 'rotate(' + a + 'deg)',
                'transform': 'rotate(' + a + 'deg)'
            });
        }

It works perfectly well within the browser, however when it comes to mobile it lags up very much. 它在浏览器中可以很好地工作,但是在移动方面却滞后很多。 Any ideas on how performance may be improved? 关于如何提高性能的任何想法?

Do not use setInterval for the animation. 不要将setInterval用于动画。 Mobile devices have CSS animations capabilities, use CSS for your animations. 移动设备具有CSS动画功能,请将CSS用于动画。 However if you really need to do a JS animation use requestAnimationFrame instead of setInterval and sync your frames with the browser refresh. 但是,如果您确实需要执行JS动画,请使用requestAnimationFrame而不是setInterval并通过浏览器刷新来同步帧。

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

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