使画布图像重复向上滚动
因此,我有一个600x2400的图像,我正在尝试向上滚动(字符会在屏幕上自动自动)并重复。我有滚动,但没有重复。我的问题是,如何使图像从顶部重新绘制为第二个实例,并在到达该图的结尾时继续?到目前为止,当代码到达图像的末尾时,我只是对第一个实例滚动的底部进行了交错的重复。
JS
let canvas;
let context;
let secondsPassed;
let oldTimeStamp;
let fps;
let map = new Image();
let roadMovement = 0;
let increment = 2;
window.onload = init;
function init() {
canvas = document.getElementById('gameCanvas');
context = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
window.requestAnimationFrame(gameLoop);
}
function draw() {
map.src = "map.png";
}
function gameLoop(timeStamp) {
secondsPassed = (timeStamp - oldTimeStamp) / 1000;
oldTimeStamp = timeStamp;
fps = Math.round(1 / secondsPassed);
roadMovement += increment;
map.onload = function () {
context.drawImage(map, 0, roadMovement, 600, 600, 0, 0, canvas.width, canvas.height);
};
draw();
window.requestAnimationFrame(gameLoop);
}
CSS
body{
position: absolute;
width:100vw;
height:100vh;
margin:0;
padding:0;
display: flex;
justify-content: center;
align-items: center;
}
#gameCanvas{
position: relative;
left:0;
top:0;
margin:0;
padding:0;
}
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link rel="stylesheet" href="style.css">
<script src="jquery-3.6.0.min.js"></script>
</head>
<body>
<canvas id="gameCanvas">
</canvas>
<script src="script.js"></script>
</body>
</html>
编辑添加了以增量值,因此可以更改自动滚动速度。
So I've got a 600x2400 image I'm trying to make scroll upwards (character will be autorunning down the screen) and repeat. I've got the scrolling, but not the repeat. My question is, how do I make the image redraw for a second instance from the top and continue when I get to the end of this one? so far when the code gets to the end of the image, I just get a staggered repeat of the bottom section of the first instance scrolling.
JS
let canvas;
let context;
let secondsPassed;
let oldTimeStamp;
let fps;
let map = new Image();
let roadMovement = 0;
let increment = 2;
window.onload = init;
function init() {
canvas = document.getElementById('gameCanvas');
context = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
window.requestAnimationFrame(gameLoop);
}
function draw() {
map.src = "map.png";
}
function gameLoop(timeStamp) {
secondsPassed = (timeStamp - oldTimeStamp) / 1000;
oldTimeStamp = timeStamp;
fps = Math.round(1 / secondsPassed);
roadMovement += increment;
map.onload = function () {
context.drawImage(map, 0, roadMovement, 600, 600, 0, 0, canvas.width, canvas.height);
};
draw();
window.requestAnimationFrame(gameLoop);
}
CSS
body{
position: absolute;
width:100vw;
height:100vh;
margin:0;
padding:0;
display: flex;
justify-content: center;
align-items: center;
}
#gameCanvas{
position: relative;
left:0;
top:0;
margin:0;
padding:0;
}
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link rel="stylesheet" href="style.css">
<script src="jquery-3.6.0.min.js"></script>
</head>
<body>
<canvas id="gameCanvas">
</canvas>
<script src="script.js"></script>
</body>
</html>
Edited added let for increment value so autoscroll speed can be changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以两次绘制地图图像,并在其大于图像高度时重置
RoadMovement
。那是您知道整个图像在画布视图之外的时候。从技术上讲,您只需要在
ImageHeight -RoadMovement&lt; Canvasheight
,但优化可能不值得。You could draw the map image twice and reset the
roadMovement
any time it gets larger than the image height. That's when you know the whole image is outside of the canvas view.Technically, you only need to draw the second image when
imageHeight - roadMovement < canvasHeight
, but it might not be worth it to optimize.将图像直接放在CSS中的页面背景呢?
尝试以下操作:
What about placing the image as the background of the page directly in CSS?
Try this: