gpt4 book ai didi

javascript - 使用 HTML 5 Canvas 自动滚动

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:56 25 4
gpt4 key购买 nike

我遇到了与 HTML 5 Canvas 动画相关的问题。
我刚刚写了这整个代码,它所做的是一个盒子不断向下移动并超过网页的高度,因为滚动选项出现了,因此我必须向下滚动才能看到盒子在哪里。
所以我的问题是:是否有任何方法可以在框向下移动时自动帮助用户仅通过该动画向下滚动?这意味着当盒子向下移动时它也会向下滚动,这有助于用户显示盒子移动的位置。如果它涉及到背景,那么它将在那里,它只是一个示例。

fiddle 在这里: http://jsfiddle.net/gamealchemist/MLHQK/我尝试使用背景滚动,但那也不起作用...!

代码是:

CSS

  body {
margin: 0px;
padding: 0px;
}

html

<canvas id="myCanvas" width="578" height="1000"></canvas>

JavaScript

window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();

function drawRectangle(myRectangle, context) {
context.beginPath();
context.rect(myRectangle.x, myRectangle.y, myRectangle.width, myRectangle.height);
context.fillStyle = '#8ED6FF';
context.fill();
context.lineWidth = myRectangle.borderWidth;
context.strokeStyle = 'black';
context.stroke();
}

function animate(myRectangle, canvas, context, startTime) {
// update
var time = Date.now() - startTime;

// pixels / second^2
var gravity = 200;

myRectangle.y = 0.5 * gravity * Math.pow(time / 1000, 2);

if (myRectangle.y > canvas.height - myRectangle.height - myRectangle.borderWidth / 2) {
myRectangle.y = canvas.height - myRectangle.height - myRectangle.borderWidth / 2;
}
lastTime = time;

// clear
context.clearRect(0, 0, canvas.width, canvas.height);

// draw
drawRectangle(myRectangle, context);

// request new frame
requestAnimFrame(function () {
animate(myRectangle, canvas, context, startTime);
});
}
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');

var myRectangle = {
x: 239,
y: 0,
width: 100,
height: 50,
borderWidth: 5
};

drawRectangle(myRectangle, context);

// wait one second before dropping rectangle
setTimeout(function () {
var startTime = (new Date()).getTime();
animate(myRectangle, canvas, context, startTime);
}, 2000);

最佳答案

将此添加到您的animate 函数中可以解决问题

delay = 100;
// scroll
if( myRectangle.y - delay > 0 )
window.scrollTo( 0, myRectangle.y - delay );


这是我根据您的代码制作的示例,我还添加了背景图片,因此您可以看到盒子正确移动。

<!DOCTYPE html>
<html>
<head>
<script>
window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();

function drawRectangle(myRectangle, context) {
context.beginPath();
context.rect(myRectangle.x, myRectangle.y, myRectangle.width, myRectangle.height);
context.fillStyle = '#8ED6FF';
context.fill();
context.lineWidth = myRectangle.borderWidth;
context.strokeStyle = 'black';
context.stroke();
}

function animate(myRectangle, canvas, context, startTime) {
// update
var time = Date.now() - startTime;

// pixels / second^2
var gravity = 200;

myRectangle.y = 0.5 * gravity * Math.pow(time / 1000, 2);

if (myRectangle.y > canvas.height - myRectangle.height - myRectangle.borderWidth / 2) {
myRectangle.y = canvas.height - myRectangle.height - myRectangle.borderWidth / 2;
}
lastTime = time;

// clear
context.clearRect(0, 0, canvas.width, canvas.height);

// draw
drawRectangle(myRectangle, context);

delay = 100;
// scroll
if( myRectangle.y - delay > 0 )
window.scrollTo( 0, myRectangle.y - delay );

// request new frame
requestAnimFrame(function () {
animate(myRectangle, canvas, context, startTime);
});
}

function load(){
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');

var myRectangle = {
x: 239,
y: 0,
width: 100,
height: 50,
borderWidth: 5
};

drawRectangle(myRectangle, context);

// wait one second before dropping rectangle
setTimeout(function () {
var startTime = (new Date()).getTime();
animate(myRectangle, canvas, context, startTime);
}, 2000);
}
</script>
</head>
<body background="http://upload.wikimedia.org/wikipedia/commons/1/16/Appearance_of_sky_for_weather_forecast,_Dhaka,_Bangladesh.JPG" style="margin: 0px;padding: 0px;" onload="load();">
<canvas id="myCanvas" width="578" height="5000"></canvas>
</body>
</html>

关于javascript - 使用 HTML 5 Canvas 自动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23646971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com