如何使用纯 JavaScript 为游戏制作循环画布背景? 运动模糊演示CPU/GPU 负载非常重

如何解决如何使用纯 JavaScript 为游戏制作循环画布背景? 运动模糊演示CPU/GPU 负载非常重

这让我发疯了哈哈。我正在制作一个简单的侧跑游戏,并希望让背景中的草在循环中真正快速地放大(草是一个可以拉伸画布宽度的精灵)。我明白如何实现我需要的所有精灵功能,但我似乎无法让这个细节发挥作用。我正在尝试只使用基本的 JavaScript。任何想法将不胜感激。

解决方法

无休止地滚动重复图像

要绘制无限滚动的背景,您需要一个重复的图像。

以下基本演示中使用的重复草图像 256 x 72 像素的示例。

enter image description here

当您将图像移动到与其宽度相等的距离时向一侧移动图像时,您将其向相反方向移动相同的宽度。

使用余数运算符 %

最容易做到这一点
const worldSpeed = -20;  // images moves from right to left
var w = image.width;
var worldPos = w;

然后为每次更新移动图像位置并绘制图像

worldPos = ((worldPos + worldSpeed) % w + w) % w;
ctx.drawImage(image,worldPos,ctx.canvas.height - image.height);

图片的宽度可能不足以覆盖整个显示屏的宽度。要解决只需通过向左和向右绘制副本来重复图像,以便它们始终覆盖所有显示(或使用图案)

无尽的滚动演示

  • 该示例将草纹理显示为无限滚动。

  • 图像被绘制 3 次以适合整个画布。

  • 使用键盘和鼠标点击画布聚焦,使用左右箭头移动。

  • 在没有键盘的情况下等待 5 秒,它就会启动。

requestAnimationFrame(renderLoop);
const W = canvas.width,H = canvas.height;
const ctx = canvas.getContext("2d");
const grass = new Image;
grass.src = "https://i.stack.imgur.com/X8GdJ.png";
const sky = new Image;
sky.src = "https://i.stack.imgur.com/MfmBf.png";
const maxSpeed = 100;
var worldPos = 0,speed = 0,acceleration = 0.5;
var skyPos = 0

function drawImageRepeat(x,img) {
    const ih = img.naturalHeight
    ctx.drawImage(img,x,H - ih);
    ctx.drawImage(img,x + 256,x - 256,H - ih);
}

function move() {
    if (grass.complete && sky.complete) {
        if (keys.left) { speed-= acceleration }
        if (keys.right) { speed+= acceleration }
        speed = speed< -maxSpeed ? -maxSpeed : speed > maxSpeed ? maxSpeed : speed;
        speed *= 0.99;
        worldPos = ((worldPos + speed) % 256 + 256) % 256;   
        skyPos = ((skyPos + speed / 16) % 256 + 256) % 256; 
        drawImageRepeat(skyPos,sky);
        drawImageRepeat(worldPos,grass);
    }
}
function renderLoop() {
    ctx.setTransform(1,1,0);
    ctx.clearRect(0,W,H);
    move();
    speed  && (info.textContent = "Speed: " + (speed * 60).toFixed(0) + "Px per second");
    requestAnimationFrame(renderLoop);
}


const K = {ArrowLeft: "left",ArrowRight: "right"};
const keys = {left: false,right: false};
const listen = (cb,...ns) => ns.forEach(n => addEventListener(n,cb));
const keyEvent = (e,n = K[e.code]) => n && (keys[n] = (e.preventDefault(),e.type === "keydown")); 
listen(keyEvent,"keydown","keyup");
setTimeout(() => speed === 0 && (keys.left = true),5000);
#info {
    position: absolute;
    top: 10px;
    left: 10px;
    font-family: arial;
    font-size: 24;
}
canvas { border: 1px solid black; }
<div id="info">Click to focuse then Left Right Arrow keys to move</div>
<canvas id="canvas" width="400" height="150"></canvas>

问题

当速度开始变得非常高时,滚动图像看起来不自然,如果允许继续加速,它甚至会开始看起来变慢。

运动模糊

为了避免这种效果,我们需要引入一些运动模糊。

下面的演示与上面相同,但不是每帧绘制一次重复的图像,而是为它在帧之间移动的每个像素绘制一次。例如,如果速度为每帧 100 像素,则每次绘制将绘制 100 次移动 1 个像素。

必须对每个步骤做出贡献的图像数量(如果速度为每帧 100 像素)将约为第 100。我们使用 alpha 来减少图像每帧添加的数量。

这个方法还有一些其他问题,演示通过改变模糊的传播并在它变慢时停止它来克服。

运动模糊演示

注意天空没有应用运动模糊。

const W = canvas.width,H = canvas.height;
requestAnimationFrame(renderLoop);
const ctx = canvas.getContext("2d");
const grass = new Image;
grass.src = "https://i.stack.imgur.com/X8GdJ.png";
const sky = new Image;
sky.src = "https://i.stack.imgur.com/MfmBf.png";

const maxSpeed = 100;
var worldPos = 256,skyPos = 256,acceleration = 0.5

function drawImageRepeat(x,H - ih);
}

function move() {
    if (grass.complete && sky.complete) {
        if (keys.left) { speed -= acceleration }
        if (keys.right) { speed += acceleration }
        speed = speed < -maxSpeed ? -maxSpeed : speed > maxSpeed ? maxSpeed : speed;
        speed *= 0.99;
        worldPos = ((worldPos + speed) % 256 + 256) % 256;
        skyPos = ((skyPos + speed / 16) % 256 + 256) % 256; 
        var dir = Math.sign(speed);
        var i = Math.abs(speed) + 1 | 0;
        drawImageRepeat(skyPos,sky);
        if (i < 2) {
            drawImageRepeat(worldPos,grass);
        } else {
            if (i < 20) {
                ctx.globalAlpha = 1 - (i / 20);
                dir *= i / 20;
                drawImageRepeat((worldPos + i * dir) % 256,grass);
            }
            ctx.globalAlpha = (1 / i) ** 0.95;
            while (i--) { drawImageRepeat((worldPos + i * dir) % 256,grass) }
            ctx.globalAlpha = 1;
        }
    }
}
function renderLoop() {
    ctx.setTransform(1,H);
    move();
    speed && (info.textContent = "Speed: " + (speed * 60).toFixed(0) + "Px per second");
    requestAnimationFrame(renderLoop);
}
const K = {ArrowLeft: "left",5000);
#info {
    position: absolute;
    top: 10px;
    left: 10px;
    font-family: arial;
    font-size: 24;
}
canvas { border: 1px solid black; }
<div id="info">Click to focuse then Left Right Arrow keys to move</div>
<canvas id="canvas" width="400" height="150"></canvas>

CPU/GPU 负载非常重

这显然是非常昂贵的 CPU/GPU(以 100 的速度需要绘制 300 次图像)。

如果您以几种不同的速度渲染带有运动模糊的图像(使用 photoshop、blender 或使用上面的演示代码通过画布),这可以克服

然后,不是为移动的每个像素绘制图像,而是随着速度的增加混合预渲染的运动模糊图像。然后,您最多只需要渲染每个图像两次(从一个预渲染的模糊混合到下一个),而不是 100 次。

,

如果您的问题是移动背景,您可以使用 CSS。

例如:

background_sprite.animate([

    {"left": "0px"},{"left": "100px"}

],{

    "duration": 7000 // In milliseconds

})

为了让它移动得更快,你可以减少持续时间。 这种方法的唯一缺点是 left 位置在动画完成后重置。 这可以通过异步运行超时函数来绕过。 例如:

setTimeout(() => {
    background_sprite.style.left = "100px"
},7000)

所以我们完整的运动代码可能看起来像这样

background_sprite.animate([
    {"left": "0px"},{"left": background_sprite.style.width}
],{
    "duration": 500 // In milliseconds
})
setTimeout(() => {
    background_sprite.style.left = background_sprite.style.width
},500)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res