简体   繁体   English

为什么我的背景不向右移动?

[英]Why does my background does not move to the right?

<canvas id="caneva" width="800" height="600"></canvas>
  <script>
    var canvas1 = document.querySelector("#caneva");
    var ctx  = canvas1.getContext("2d");
    var img = new Image();

        img.src = 'img/lvl1.jpg';
        var pattern = null;
        var posx = null;
        img.onload = function() {
          pattern = ctx.createPattern(img, 'repeat');
          ctx.fillStyle = pattern;
          ctx.fillRect(0, 0, 800, 600);
          posx = 0;
            function animate() {
                posx += 1;
                ctx.fillStyle = pattern;
                ctx.fillRect(posx, 0, 800, 600);
            }
            setInterval(animate, 10)
        };


      </script>

I Want to background image to move to the right.我想让背景图像向右移动。 It does not work.这没用。 I tried all and I didn't successfully to do it.我尝试了所有但我没有成功地做到这一点。

The pattern has a fixed transform and does not move with the rectangle.图案具有固定变换,不随矩形移动。

You can use the pattern's setTransform function or use the canvas transform.您可以使用模式的setTransform function 或使用 canvas 变换。

Changing the function to将 function 更改为

ctx.fillStyle = pattern;
function animate() {
   ctx.setTransform(1, 0, 0, 1, posx++, 0);  // last two are x, y of origin
   ctx.fillRect(0, 0, 800, 600);  // draw at origin
}

Will move the pattern.将移动模式。

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

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