6

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...

Community
  • 1
  • 1
Sourcerer
  • 139
  • 9
  • My answer should answer your question. If you need anything else explained I'll add it to my answer. – Dodo Aug 23 '15 at 02:47

1 Answers1

3

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).

Dodo
  • 323
  • 1
  • 8