【问题标题】:PIXI getting image width,heightPIXI获取图像宽度,高度
【发布时间】:2015-07-01 10:56:12
【问题描述】:

在调用自动​​渲染器之前,我需要获取刚刚添加到舞台的图像大小并将其设置为 defaultX 和 defaultY,但我似乎无法获取大小。 我如何抓住尺寸? 另外,从我目前得到的信息来看,我认为设置大小的最佳方法是使用自动渲染器。但是,如果我稍后 resize() 会有什么影响? 任何帮助将不胜感激。谢谢。

我的代码:

this.stage = new PIXI.Container();
var texture = PIXI.Texture.fromImage('background.png'); //1610x640
var spr = new PIXI.Sprite(texture);
this.stage.addChild(spr);

defaultX = texture.width;
defaultY = texture.height;

this.renderer = PIXI.autoDetectRenderer(defaultX, defaultY); //640x690

this.renderer.resize(x,y); // I know this works with other values 
                           // eg if I hardcode with 1610,640
                           // but texture.width doesn't work and
                           // I don't want to hardcode it

【问题讨论】:

    标签: pixi.js


    【解决方案1】:

    调用texture.width时无法加载图片。

    我使用下一个构造:

    this.stage = new PIXI.Container();
    this.renderer = PIXI.autoDetectRenderer(640, 690); //640x690
    
    var baseTexture = new PIXI.BaseTexture('background.png');
    
    // can be loaded from cache
    if (baseTexture .hasLoaded) {
        createSprite(baseTexture);
    } else {       
        baseTexture .on('loaded', function() {
            createSprite(baseTexture); // or this
        });
    }
    
    function createSprite(baseTexture) {
        var texture = new PIXI.Texture(baseTexture);
        var spr = new PIXI.Sprite(texture);
        this.stage.addChild(spr);
    
        defaultX = texture.width;
        defaultY = texture.height;
    
        this.renderer.resize(defaultX,defaultY);
    }
    

    【讨论】:

    • 感谢您的回复,但它不起作用。我只得到一个默认大小的绿色矩形。你能详细说明你的第一句话吗?
    • 我在 pixi2 中加载图像时遇到问题,所以我开始在 pixi 中使用之前加载图像。这样的代码对我有用,但后来我开始对图像使用浏览器 onload 事件,例如:
    • var src = 'background.png'; var img = new Image() img.onload = function() { PIXI.utils.BaseTextureCache[src] = new PIXI.BaseTexture(this); } 图像.src = src;所以加载后你可以从缓存中获取基础纹理 var baseTexture = PIXI.utils.BaseTextureCache['background.png']
    猜你喜欢
    • 2013-06-19
    • 2010-11-18
    • 1970-01-01
    • 2017-05-23
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    相关资源
    最近更新 更多