简体   繁体   English

HTML5 / JS大量隐藏的画布

[英]HTML5/JS large amount of hidden canvases

I want to render a large randomized world (1600x24000 pixels in size) and draw it onto the screen. 我想渲染一个大型的随机世界(大小为1600x24000像素)并将其绘制到屏幕上。 The point is not for me to see the whole map at once though, so this is very laggy even when the majority of the image goes offscreen. 我的意思不是立即看到整个地图,所以即使大部分图像不在屏幕上也很不方便。 I have the idea of splitting the world into 24 different hidden canvases, each 1600x1000 in size. 我的想法是将世界分成24个不同的隐藏画布,每个画布的大小为1600x1000。 These would be generated when the application is loaded and saved into a hidden canvas., where only one is drawn at a time 这些将在应用程序加载并保存到隐藏的画布中时生成。一次只能绘制一个画布。

Remember that this is a randomly generated image using ctx graphics, not a static image. 请记住,这是使用ctx图形随机生成的图像,而不是静态图像。 I have not yet been able to try it out. 我还无法尝试。 What should I expect? 我应该期待什么? Is this efficient? 这样有效吗? How could I do it better? 我怎样才能做得更好?

I would like to add that the current way I am handling this is by drawing only the visible part each and every frame. 我想补充一点,我当前的处理方式是在每一帧中仅绘制可见部分。 However, there are usually 80-120 images onscreen at a time and having to render all these in real time can get rather laggy on mobile. 但是,通常一次只能显示80-120张图像,而必须实时渲染所有这些图像在移动设备上会变得很滞后。 I would hope that pre-rendering a large section and drawing it as a single image would speed things up. 我希望预先渲染一个较大的部分并将其绘制为单个图像可以加快处理速度。

You will probably gain performance on mobile devides by combining your tiles into pre-rendered images. 通过将图块组合到预渲染的图像中,您可能会在移动设备上获得性能。

Of course, there is the possibility that your game map is simply too large to be rendered responsively on mobile devices. 当然,您的游戏地图可能太大而无法在移动设备上进行响应渲染。 Therefore, you will need to do performance testing. 因此,您将需要进行性能测试。

Here's code for you to test & experiment with performance: http://jsfiddle.net/m1erickson/Lgcot78L/ 这是供您测试和试验性能的代码: http : //jsfiddle.net/m1erickson/Lgcot78L/

It creates 24 images of 1000 width x 600 height. 它创建24张宽1000 x高600的图像。

These images are panned across a 360px wide canvas by using a negative X offset. 通过使用负X偏移,可以在360px宽的画布上平移这些图像。

When necessary, there are 2 images drawn on the canvas. 必要时,画布上会绘制2张图像。 This occurs when the lefthand image is not wide enough to cover the entire canvas. 当左侧图像的宽度不足以覆盖整个画布时,就会发生这种情况。 Then the next image in sequence is drawn to fill the gap. 然后绘制顺序的下一张图像以填补空白。

This is the heart of the code--the draw() function that draws the image(s) on the canvas: 这是代码的核心-draw()函数在画布上绘制图像:

function draw(){

    // calc which of the 24 images goes on the left side
    var index=parseInt(offsetX/imgWidth);

    // calc how far left to offset that image
    var drawOffsetX=index*imgWidth-offsetX;

    // draw the image at that offset
    ctx.drawImage(canvases[index],drawOffsetX,0);  

    // if the left image doesn't cover the whole canvas
    // then draw the next image to fill the gap
    if(drawOffsetX<(canvasWidth-imgWidth)){
        index++;
        drawOffsetX+=imgWidth;
        ctx.drawImage(canvases[index],drawOffsetX,0)
    }
}

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

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