nitro-renderer/src/pixi-proxy/TextureUtils.ts

59 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-08-03 17:54:33 +02:00
import { SCALE_MODES } from '@pixi/constants';
import { AbstractRenderer, Renderer, RenderTexture, Resource, Texture } from '@pixi/core';
import { DisplayObject } from '@pixi/display';
2021-07-13 22:34:41 +02:00
import { Extract } from '@pixi/extract';
2021-08-03 17:54:33 +02:00
import { Rectangle } from '@pixi/math';
2022-10-30 05:02:19 +01:00
import { PixiApplicationProxy } from './PixiApplicationProxy';
2021-03-17 03:02:09 +01:00
export class TextureUtils
{
public static generateTexture(displayObject: DisplayObject, region: Rectangle = null, scaleMode: number = SCALE_MODES.NEAREST, resolution: number = 1): RenderTexture
{
2022-10-30 05:02:19 +01:00
if (!displayObject) return null;
2021-03-17 03:02:09 +01:00
2021-08-03 17:54:33 +02:00
return this.getRenderer().generateTexture(displayObject, {
2021-08-03 02:08:22 +02:00
scaleMode,
resolution,
region
});
2021-03-17 03:02:09 +01:00
}
2021-07-14 09:10:54 +02:00
public static generateTextureFromImage(image: HTMLImageElement): Texture<Resource>
2021-03-17 03:02:09 +01:00
{
2022-10-30 05:02:19 +01:00
if (!image) return null;
2021-03-17 03:02:09 +01:00
return Texture.from(image);
}
public static generateImage(target: DisplayObject | RenderTexture): HTMLImageElement
{
2022-10-30 05:02:19 +01:00
if (!target) return null;
2021-03-17 03:02:09 +01:00
2021-08-03 17:54:33 +02:00
return this.getExtractor().image(target);
2021-03-17 03:02:09 +01:00
}
public static generateImageUrl(target: DisplayObject | RenderTexture): string
{
2022-10-30 05:02:19 +01:00
if (!target) return null;
2021-03-17 03:02:09 +01:00
2021-08-03 17:54:33 +02:00
return this.getExtractor().base64(target);
2021-07-13 22:34:41 +02:00
}
public static generateCanvas(target: DisplayObject | RenderTexture): HTMLCanvasElement
{
2022-10-30 05:02:19 +01:00
if (!target) return null;
2021-07-13 22:34:41 +02:00
2021-08-03 17:54:33 +02:00
return this.getExtractor().canvas(target);
2021-03-17 03:02:09 +01:00
}
2021-07-13 22:34:41 +02:00
public static getRenderer(): Renderer | AbstractRenderer
2021-03-17 03:02:09 +01:00
{
2022-10-30 05:02:19 +01:00
return PixiApplicationProxy.instance.renderer;
2021-03-17 03:02:09 +01:00
}
2021-08-03 17:54:33 +02:00
public static getExtractor(): Extract
2021-03-17 03:02:09 +01:00
{
2021-08-03 17:54:33 +02:00
return (this.getRenderer().plugins.extract as Extract);
2021-03-17 03:02:09 +01:00
}
}