I’m having trouble with a JavaScript animation loop. I’m using Raphael.js to create an explosion effect when a button is clicked. The initial animation works fine, but the loop I’ve set up to repeat the animation isn’t doing anything.
Here’s what I’m trying to do:
- Show an explosion shape
- Animate it to expand
- Loop between two states (expanded and original) a few times
I’ve tried two approaches:
- Using functions for each animation state:
function boom1() {
star.animate({path: 'M 250,350 l 20,60 l 50,-20...', fill: 'red'}, 100, 'linear');
}
function boom2() {
star.animate({path: 'M 250,330 l 10,80 l 70,-20...', fill: 'orange'}, 100, 'linear');
}
button.click(function() {
star.show();
star.animate({path: 'M 250,330 l 10,80 l 70,-20...', fill: 'orange'}, 100, 'linear');
for(var i = 0; i < 4; i++) {
if (i % 2 === 0) {
boom1();
} else {
boom2();
}
}
});
- Putting animations directly in the loop:
button.click(function() {
star.show();
star.animate({path: 'M 250,330 l 10,80 l 70,-20...', fill: 'orange'}, 100, 'linear');
for(var i = 0; i < 5; i++) {
if (i % 2 === 0) {
star.animate({path: 'M 250,350 l 20,60 l 50,-20...', fill: 'red'}, 100, 'linear');
} else {
star.animate({path: 'M 250,330 l 10,80 l 70,-20...', fill: 'orange'}, 100, 'linear');
}
}
});
Neither approach is working. The initial animation plays, but the loop doesn’t seem to run at all. Any ideas on what I’m doing wrong?