I would want to implement a fadeOut effect followed by a fadeIn effect.
I wrote something similar to this:
function fadeOut(content) {
console.log('fade out');
alpha -= delta;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.globalAlpha = alpha;
content();
if (alpha > 0) {
requestAnimationFrame(fadeOut.bind(this, content));
}
else {
alpha = 1;
ctx.globalAlpha = alpha;
}
}
function fadeIn(content) {
console.log('fade in');
alpha += delta;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.globalAlpha = alpha;
content();
if (alpha < 1) {
requestAnimationFrame(fadeIn.bind(this, content));
}
else {
alpha = 1;
ctx.globalAlpha = alpha;
}
}
Calling each of these 2 functions would be fine.
However if I wanted to call them in a sequence they will happen to run simultaneously.
requestAnimationFrame(fadeOut.bind(this, drawMap.bind(this, MAP1)));
requestAnimationFrame(fadeIn.bind(this, drawMap.bind(this, MAP1)));
How can I solve this question
solve the question
p p p p
2
Answers
If you want the fade in function to run sequentially after the fade out function, then you should have the fade out function call the fade in function when it’s done.
You don’t want two concurrent calls to
requestAnimationFrame
, because that’s what’s making your fade in/out animations run at the same time.The example below does what I believe you’re describing, using the similar function
setTimeout
:I’d suggest using some kind of generalized approach that can handle arbitrary sequence of transitions.