File size: 1,457 Bytes
75fefa7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck -- TODO: fix this

import { Application, Assets, Sprite, Texture } from "pixi.js";

export const isDestroyed = (app: Application) => {
  if (!app.ticker || !app.renderer || !app.stage || !app.renderer.gl)
    return true;

  return app.renderer.gl.isContextLost();
};

export const generateTexture = (app: Application, graphic: any) => {
  const renderer = app.renderer;

  if (!isDestroyed(app)) {
    return renderer.generateTexture(graphic);
  }

  return Texture.WHITE;
};

export const degreesToRadians = (degrees: number) => {
  return degrees * (Math.PI / 180);
};

export const imageToSprite = async (app: Application, path: string) => {
  let texture;

  if (Assets.cache.has(path)) {
    texture = Assets.cache.get(path);
  } else {
    texture = await Assets.load(path);
  }

  const sprite = Sprite.from(texture);

  return sprite;
};

export const createRenderWithFPS = (app: Application, fps: number) => {
  let lastUpdateTime = 0;

  return () => {
    const currentTime = performance.now();
    const timeSinceLastUpdate = currentTime - lastUpdateTime;

    if (timeSinceLastUpdate >= 1000 / fps) {
      app.ticker.update();
      app.render();
      lastUpdateTime = currentTime;
    }
  };
};

export const waitUntilPixiIsReady = (app: Application) => {
  return new Promise((resolve) => {
    app.canvas.addEventListener("pixi-initialized", resolve);
  });
};