简体   繁体   English

html5画布缓存磁贴

[英]html5 canvas caching tiles

I'm making an MMO game in HTML5 Canvas . 我正在用HTML5 Canvas制作MMO游戏。 It's isometric and it has tiles and walls . 它是等轴测图 ,有瓷砖和墙壁

So I need to optimise the game for slow computers/mobile devices . 因此,我需要针对速度较慢的计算机/移动设备优化游戏。
For that I want to cache my tiles and walls: 为此,我想缓存我的瓷砖和墙壁:

Once they are drawn, they are static/not animated 绘制后,它们是静态/不动画的

so I don't need to redraw them each time. 因此我不需要每次都重新绘制它们。


I also don't want to use two (or more) <canvas> objects so I want to use the same <canvas> element that I'm using to draw with requestAnimationFrame my game items/entities. 我也不想使用两个(或更多) <canvas>对象,所以我想使用与requestAnimationFrame绘制游戏项目/实体时使用的相同的<canvas>元素。

I've alerdy tried to draw the tiles and the walls to an offscreen canvas with the same width/height as the visible canvas and then draw it each time on the visible canvas, 我一直努力尝试将瓷砖和墙壁绘制为宽度/高度与可见画布相同的屏幕外画布,然后每次在可见画布上进行绘制,

but the visible canvas uses the resolution of the page so it's pretty the same CPU usage with or without cache on high resolutions (like my current screen: 1920x1080 ) 但可见的画布使用页面的分辨率,因此在高分辨率下(无论是否带有高速缓存),CPU使用率都几乎相同(例如我当前的屏幕: 1920x1080

How should I do this properly so I can reduce CPU usage and use memory instead ? 如何正确执行此操作,以便减少CPU使用率并改为使用内存? Is it impossible without using two canvases? 不使用两个画布就不可能吗?

You could try saving the static elements as an image after you draw the map for the first time and then call the image. 您可以在第一次绘制地图后尝试将静态元素另存为图像,然后调用该图像。 It will be faster if you have a lot of elements involved into drawing the map. 如果您在绘制地图时涉及很多元素,它将更快。

If nothing overdraws these static elements during the gameplay they never have to be drawn again. 如果在游戏过程中没有任何东西会覆盖这些静态元素,则无需再次绘制它们。 Keep that in mind 记住这一点

EDIT: Alternatively you can draw smaller object and scale them ( refrence ) 编辑:或者,您可以绘制较小的对象并缩放它们( 参考

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.strokeRect(5,5,25,15);
ctx.scale(2,2);
ctx.strokeRect(5,5,25,15);

The second rectangle is twice as high and wide. 第二个矩形的高度和宽度是其两倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM