I'm trying to come up with a way where an image transitions in and out of the screen for a few seconds each. (Into the screen and stays for about 5 seconds then transitions out and stays hidden). I already have code for transitioning in, my problem is getting it to transition out.
This is the transition in code.
let canvas = null;
let ctx = null;
window.onload = onAllAssetsLoaded;
function onAllAssetsLoaded() {
// stopAndHide the webpage loading message
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
/* Step 1 of 3 */
/* Start the animations */
setTimeout(startImageAnimation, 2000);
renderCanvas();
}
/* Step 2 of 3 */
/* Each animation needs its own code */
/******************************************************************************/
/* These three are ALWAYS needed */
let imageAnimationInterval = null;
const IMAGE_FRAMERATE = 5; // change to suit the animation frameRate in milliseconds. Smaller numbers give a faster animation */
let imageAnimationIsDisplayed = false;
/* These variables depend on the animation */
let image = new Image();
image.src = "http://i.stack.imgur.com/UFBxY.png";
let imageX = 0;
let imageY = 0;
let size = 0;
function startImageAnimation() {
imageAnimationIsDisplayed = true;
imageAnimationInterval = setInterval(updateImageAnimation, IMAGE_FRAMERATE);
}
function stopImageAnimation() {
imageAnimationIsDisplayed = true;
clearInterval(imageAnimationInterval);
imageAnimationInterval = null; // set to null when not running
}
function stopAndHideImageAnimation() {
stopImageAnimation();
imageAnimationIsDisplayed = false;
}
function updateImageAnimation() {
size++;
if (size === canvas.width) {
stopImageAnimation();
}
}
function renderImageAnimation() {
if (imageAnimationIsDisplayed) {
ctx.drawImage(image, imageX, imageY, size, size);
}
}
/******************************************************************************/
/******************************************************************************/
function renderCanvas() {
requestAnimationFrame(renderCanvas);
ctx.clearRect(0, 0, canvas.width, canvas.height);
/* Step 3 of 3 */
/* Drawn the animations */
renderImageAnimation();
}
<canvas id="canvas">