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