简体   繁体   English

背景代理奇数HTML5画布

[英]Background acting odd html5 canvas

I have an 800 by 100 image for the background and I am trying to pull sections from it (like a sprite sheet) to generate a background because I am under the impression that it is an efficient way of doing it(also seems like one that I can toy with and learn to generate backgrounds in the future!) 我有一个800 x 100的背景图像,我试图从中拉出一些部分(如sprite表)以生成背景,因为我印象中这是一种有效的处理方法(似乎也可以我可以玩耍并学习将来的背景知识!)

It is not working though, int he first animation frame ( I found this from slowing it down) it shows half of the entire background image(not the sections that I want to use) then it moves it down presumably 800 pixels and shows it and by the third frame it is gone! 不过,它不起作用,在第一个动画帧(我是通过放慢速度找到它的)中,它显示了整个背景图像的一半(不是我要使用的部分),然后将其向下移动了800像素,并显示了第三帧消失了!

please help :/ thank you! 请帮助:/谢谢!

    var bricks = [0, 1, 1, 1, 1, 2];

    function createBackground() {
        for(var i = 0; i < bricks.length; i++) {
            drawBackground(bricks[i]);
        }

    }
    var bg = new Image();
    bg.src = 'bgsheet2.png';
    var srcX, srcY = 0,srcW = 100,srcH = 100,destX = 0,destY = canvas.height-100,destW = 100,destH = 100;

    function drawBackground(type) {

        switch(type) {
            case 1:
                srcX = 0;
                ctx.drawImage(bg,srcX,srcY,srcW,srcH,destX,destY,destW,destH);
                destX+=100;
                break;
            case 2:
                srcX = 100;
                ctx.drawImage(bg,srcX,srcY,srcW,srcH,destX,destY,destW,destH);
                destX+=100;
                break;
            case 3:
                srcX = 200;
                destX+=100;
                ctx.drawImage(bg,srcX,srcY,srcW,srcH,destX,destY,destW,destH);
                break;
            default:
                srcX = 300;
                ctx.drawImage(bg,srcX,srcY,srcW,srcH,destX,destY,destW,destH);
                destX+=100;
                break;
        }
    }

     //this is in my main animation loop
    createBackground();

For the efficiency, you have the following common solutions. 为了提高效率,您有以下常用解决方案。

  1. If the background is static (no moving parts like water or any interaction with it) then you can draw it to an on-screen canvas (a visible canvas) just the one time, but don't clear that canvas. 如果背景是静态的(没有可移动的部分,如水或与水没有任何相互作用),则可以一次将其绘制到屏幕上的画布(可见的画布)上,但不要清除该画布。 That way your tiles are only processed and drawn the once. 这样,您的图块仅被处理并绘制一次。

  2. The second method, a little more difficult than the first, is to draw sections to an offscreen canvas with the goal of limiting your (re)draws calls. 第二种方法比第一种困难一些,它是将部分绘制到屏幕外的画布上,目的是限制您的(重新)绘制调用。

  3. You can also get in to using "dirty rectangles" which is basically the practice of only updating the changed parts of your display. 您还可以使用“脏矩形”,这基本上是仅更新显示更改部分的一种做法。 This is by far the hardest solution for efficiency. 到目前为止,这是提高效率的最困难的解决方案。

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

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