Created
February 15, 2017 13:20
-
-
Save unknownuser88/8dcf4124c607e2da963c93ae58e12f03 to your computer and use it in GitHub Desktop.
coin animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function animateCoins(from, to, cb) { | |
var fromTop, fromLeft, toTop, toLeft; | |
var cxTime = 400, | |
cxRadius = 30, | |
goMaxTime = 1400, | |
goMinTime = 700, | |
coins = []; | |
if (typeof from == "object") { | |
fromLeft = from.x; fromTop = from.y; | |
} | |
if (typeof to == "object") { | |
toLeft = to.x; toTop = to.y; | |
} | |
if (typeof from == "string" || from instanceof $) { | |
var f = $(from)[0].getBoundingClientRect(); | |
fromLeft = f.left; fromTop = f.top; | |
} | |
if (typeof to == "string" || to instanceof $) { | |
var t = $(to)[0].getBoundingClientRect(); | |
toLeft = t.left; toTop = t.top; | |
} | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
for (var i = 0; i <= 14; i++) { | |
(function(index) { | |
coins.push($('<img>', { | |
"src": "coin.png", | |
"class": 'fly_coin', | |
"id": "coin_" + index | |
})); | |
coins[index] | |
.appendTo('body') | |
.css({ | |
"top": fromTop , | |
"left": fromLeft | |
}) | |
.animate({ | |
"left": "+=" + getRandomInt(-cxRadius, cxRadius), | |
"top": "+=" + getRandomInt(-cxRadius, cxRadius), | |
leaveTransforms: true | |
}, cxTime, function() { | |
$(this).animate({ | |
"top": toTop, | |
"left": toLeft, | |
leaveTransforms: true | |
}, getRandomInt(goMinTime, goMaxTime), function() { | |
coins[index].remove(); | |
}); | |
}) | |
})(i); | |
} | |
// navsyaki hanum enq mekel tesar mite lrvav mnac | |
setTimeout(function() { | |
$('.fly_coin').remove(); | |
cb && cb(); | |
}, cxTime + goMaxTime); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment