Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS random animation

My idea is to make an image to dismember into little parts that will scale down, while they fly away.

I've managed to do that with a couple of CSS animations -scale + translate3d-(the results aren't great but it's a start).

Now, the problem is that I would like the translations to be random. As far as I've understood, there is a simple way involving JS/Jquery/GSAP, and a little more complicated way involving SCSS/Sass...

I'm unfamiliar with all of them.

I've found a code that uses javascript to randomize a rotation, and I have adapted it to my translation.

The code was posted here as an answer.

// search the CSSOM for a specific -webkit-keyframe rule
function findKeyframesRule(rule)
{
    // gather all stylesheets into an array
    var ss = document.styleSheets;

    // loop through the stylesheets
    for (var i = 0; i < ss.length; ++i) {

        // loop through all the rules
        for (var j = 0; j < ss[i].cssRules.length; ++j) {

            // find the -webkit-keyframe rule whose name matches our passed       over parameter and return that rule
            if (ss[i].cssRules[j].type == window.CSSRule.WEBKIT_KEYFRAMES_RULE && ss[i].cssRules[j].name == rule)
                return ss[i].cssRules[j];
        }
    }

    // rule not found
    return null;
}

// remove old keyframes and add new ones
function change(anim)
{
    // find our -webkit-keyframe rule
    var keyframes = findKeyframesRule(anim);
    // remove the existing 38% and 39% rules
    keyframes.deleteRule("38%");
    keyframes.deleteRule("39%");
    // create new 38% and 39% rules with random numbers
    keyframes.insertRule("38% { -webkit-transform: translate3d("+randomFromTo(-100,100)+"vw,"+randomFromTo(-100,100)+"vw,0vw); }");
    keyframes.insertRule("39% { -webkit-transform: translate3d("+randomFromTo(-100,100)+"vw,"+randomFromTo(-100,100)+"vw,0vw); }");
    // assign the animation to our element (which will cause the animation to run)
    document.getElementById('onet').style.webkitAnimationName = anim;
}

// begin the new animation process
function startChange()
{
    // remove the old animation from our object
    document.getElementById('onet').style.webkitAnimationName = "none";
    // call the change method, which will update the keyframe animation
    setTimeout(function(){change("translate3d");}, 0);
}

// get a random number integer between two low/high extremes
function randomFromTo(from, to){
   return Math.floor(Math.random() * (to - from + 1) + from);
}

So finally, there is this part:

$(function() {
    $('#update-box').bind('click',function(e) {
        e.preventDefault();
        startChange();        
    });
});

Which I'm not sure but I guess it's function is to trigger the function startChange.

Now. In my case, I would like a function to auto trigger and, as the animation has to continue playing, it would have to loop indefinitely..

Any ideas how to do that? I guess I could use onAnimationEnd.. But obviously I do not know how to write it...

like image 257
Sourcerer Avatar asked Aug 23 '15 02:08

Sourcerer


People also ask

Can I use random in CSS?

External JavaScript Let me get that out of the way first — CSS has no built-in “random” function, no Math. random() equivalent and no way to generate a random number or a random color at all.

Can you do animations with CSS?

CSS animations make it possible to animate transitions from one CSS style configuration to another. Animations consist of two components, a style describing the CSS animation and a set of keyframes that indicate the start and end states of the animation's style, as well as possible intermediate waypoints.

What triggers a CSS animation?

CSS animations enable web elements to transition from one CSS style configuration to another. The browser can start defined animations on load, but event triggered CSS animations rely on adding and removing classes. AMP supports both animation types.


1 Answers

The inbuilt JavaScript function setTimeout(functionName, time) calls the function named functionName after time milliseconds. Remove the $('#update-box').bind... part, and replace with a function that is called every 1000ms or so. For example:

$(function() {
    function callStartChange() {
        startChange();
        setTimeout(callStartChange, 1000);
    }
    // And now start the process:
    setTimeout(callStartChange, 1000);
});

This will call startChange every second (1000ms).

like image 179
Dodo Avatar answered Oct 24 '22 02:10

Dodo