【发布时间】:2020-03-09 19:50:19
【问题描述】:
我正在尝试想出一种方法,让图像在屏幕内外切换几秒钟。 (进入屏幕并停留约 5 秒钟,然后转出并保持隐藏)。我已经有了转入的代码,我的问题是让它转出。
这是代码中的转换。
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">
【问题讨论】:
-
您的标题有点误导...您确实知道如何“取消显示”或清除画布中的某些内容,并且您正在使用它:
ctx.clearRect(0, 0, canvas.width, canvas.height);...我猜您只是需要帮助带有过渡/动画
标签: canvas html5-canvas canvasjs