在 Pixi.js 中计算精灵纹理的分辨率

Calculate resolution of sprite texture in Pixi.js

如何计算 PIXI.js 中生成的纹理的分辨率。 generateTexture(...) 的文档需要一个数字作为分辨率。

文档:http://pixijs.github.io/docs/PIXI.Graphics.html#generateTexture

在下面的示例中,将绘制一个具有特定宽度和高度的矩形。我认为分辨率是宽度和高度相乘的像素数。

this._graphics.lineStyle(1, 0x012489, 1.0);
this._graphics.beginFill(0x696969, this.1.0);
this._graphics.drawRect(-(width/2.0), -(height/2.0), width, height);
this._graphics.endFill();

this.texture = this._graphics.generateTexture(width * height, PIXI.SCALE_MODES.DEFAULT);

从代码看来,分辨率设置的不是分辨率,而是相对分辨率,实际上是这样的:https://pixijs.github.io/docs/PIXI.BaseTexture.html#resolution,所以它说:"the resolution of the texture for devices with different pixel ratios".

默认为 1,这对于桌面设备是正常的,移动设备上的像素比例可能会有所不同。您应该能够使用此函数 (getPixelRatio) 获得像素比: https://github.com/Hachitus/FlaTWorld/blob/master/src/components/map/core/utils/utils.js#L195 ,但该函数并未经过真正的实战测试,尽管到目前为止在测试中也未显示任何问题。