javascript - 具有可变背景宽度的 PIXI tilingSprite

标签 javascript pixi.js

我的 PIXI.js 遇到问题。有一个 tilingSprite 使用 1200x1200 图像作为纹理。然而,舞台几乎从来没有 1200 像素宽,而是响应屏幕宽度。 我很难弄清楚如何使 tilingSprite 始终为舞台的宽度。

这是我生成 tilingSprite 的代码。

var background,
    backgroundTexture = new PIXI.Texture.fromImage('modules/foo/bar/texture.jpg');

  //backgroundTexture.baseTexture.width = stageWidth;
  //backgroundTexture.update();

  background = new PIXI.extras.TilingSprite(backgroundTexture, stageWidth, stageHeight);

  return  background;

我已经尝试过应用scale.x和scale.y,设置纹理和tilingSprite的宽度,摆弄baseTexture。

如果有任何提示,我将不胜感激。 使用的PIXI版本是3.0.10。

最佳答案

我计算了纹理大小和背景大小之间的比率。需要注意的重要一点是,WebGL 渲染器要求背景纹理分辨率为 2 的幂。 2、4、8、16、256、512。此外,如果纹理大于屏幕尺寸(例如,纹理高度为 1024,但屏幕高度仅为 960),则侧面会出现黑条。我猜它不擅长缩小纹理,只能放大它们。

下面的代码始终拉伸(stretch)纹理以适应屏幕的高度,并沿宽度拉伸(stretch)图 block :

var bg, WIDTH, HEIGHT,
    preload = function () {
         bg = game.add.tileSprite(0, 0, WIDTH, HEIGHT, 'background');
    },

    resizeBg = function () {
        // Determine which screen dimension is most constrained
        //var ratio = Math.max(WIDTH / bg.texture.width, HEIGHT / bg.texture.height);

        // always favor height, tile width
        var ratio = HEIGHT / bg.texture.height;

        // Scale the view appropriately to fill that dimension
        bg.scale.x = bg.scale.y = ratio;

        bg.scale.setTo(ratio, ratio);
    },

    resize = function (width, height) {

        //  If the game container is resized this function will be called automatically.
        //  You can use it to align sprites that should be fixed in place and other responsive display things.

        WIDTH = game.width || 540;
        HEIGHT =  game.height || 960;

        if (bg) {
            resizeBg();
        }
    };

来自http://www.rocketshipgames.com/blogs/tjkopena/2015/09/basic-scaling-animation-and-parallax-in-pixi-js-v3/

关于javascript - 具有可变背景宽度的 PIXI tilingSprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39766008/

相关文章:

javascript - 使用新的谷歌异步分析片段实现“silktide cookie 同意”

javascript - Backbone.history.navigate 不保存参数

javascript - Canvas 获取透视点

webgl - requestAnimationFrame 和 requestAnimFrame 之间的区别

javascript - mocha 错误 : No test files found: "test/" npm ERR! 测试失败

javascript - 如何获得正确的列索引和行索引?

java - 在 selenium WebDriver 中返回时出现 StaleElementReferenceException

javascript - 如何绘制箭头以通过 ngraph.pixi 与 PIXI.js 与 webgl 链接?

javascript - 将整个 Canvas 分成相等的列

javascript - 在 PIXI.js 中正确制作简单图形的动画