pixi-fusion 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,10 +8,10 @@ export const Camera = ({ children, clampZoom }) => {
8
8
  const [isReady, setIsReady] = useState(false);
9
9
  const [followContainer, setFollowContainer] = useState(null);
10
10
  const [ensureVisibleOptions, setEnsureVisibleOptions] = useState(null);
11
- const { application, isInitialized, size } = useWorld();
11
+ const { application, size } = useWorld();
12
12
  const { addObject: addObjectToStage, removeObject: removeObjectFromStage } = useStage();
13
13
  const [things, setThings] = useState([]);
14
- const viewport = useMemo(() => isInitialized
14
+ const viewport = useMemo(() => application
15
15
  ? new ViewportContainer({
16
16
  worldHeight: size.height,
17
17
  worldWidth: size.width,
@@ -19,7 +19,7 @@ export const Camera = ({ children, clampZoom }) => {
19
19
  screenWidth: application.canvas.width,
20
20
  events: application?.renderer?.events
21
21
  })
22
- : null, [size, application, isInitialized]);
22
+ : null, [size, application]);
23
23
  const addObject = useCallback((thing) => {
24
24
  setThings((oldThings) => [...oldThings, thing]);
25
25
  }, [things]);
@@ -31,10 +31,10 @@ export const Camera = ({ children, clampZoom }) => {
31
31
  removeObject
32
32
  }), [addObject, removeObject]);
33
33
  useEffect(() => {
34
- if (viewport && clampZoom) {
34
+ if (viewport && clampZoom && application) {
35
35
  viewport.clampZoom(clampZoom);
36
36
  }
37
- }, [clampZoom, isInitialized]);
37
+ }, [clampZoom]);
38
38
  useEffect(() => {
39
39
  if (viewport) {
40
40
  if (size.width) {
@@ -44,13 +44,13 @@ export const Camera = ({ children, clampZoom }) => {
44
44
  viewport.height = size.height;
45
45
  }
46
46
  }
47
- }, [size, viewport?.uid, isInitialized]);
47
+ }, [size, viewport?.uid, application]);
48
48
  useEffect(() => {
49
- if (!isInitialized || !viewport) {
49
+ if (!application || !viewport) {
50
50
  return;
51
51
  }
52
52
  viewport?.moveCenter(0, 0);
53
- }, [viewport, size, isInitialized]);
53
+ }, [viewport, size, application]);
54
54
  useEffect(() => {
55
55
  if (!viewport) {
56
56
  return;
@@ -61,7 +61,7 @@ export const Camera = ({ children, clampZoom }) => {
61
61
  removeObjectFromStage(viewport);
62
62
  setIsReady(false);
63
63
  };
64
- }, [application, isInitialized]);
64
+ }, [viewport?.uid]);
65
65
  useEffect(() => {
66
66
  if (!viewport || !isReady) {
67
67
  return;
@@ -1,14 +1,14 @@
1
1
  import { useEffect } from "react";
2
2
  import { useWorld } from "../hooks";
3
3
  export const useTickerCallback = ({ isEnabled = true, callback }) => {
4
- const { application, isInitialized } = useWorld();
4
+ const { application } = useWorld();
5
5
  useEffect(() => {
6
- if (!isInitialized || !isEnabled) {
6
+ if (!application || !isEnabled) {
7
7
  return;
8
8
  }
9
9
  application.ticker.add(callback);
10
10
  return () => {
11
11
  application.ticker.remove(callback);
12
12
  };
13
- }, [isEnabled, application, isInitialized, callback]);
13
+ }, [isEnabled, application, callback]);
14
14
  };
@@ -3,7 +3,7 @@ import { StageContext } from "./Stage.context";
3
3
  import { useWorld } from "../hooks";
4
4
  import { Layer } from "../layer";
5
5
  export const Stage = ({ children }) => {
6
- const { application, isInitialized } = useWorld();
6
+ const { application } = useWorld();
7
7
  const [things, setThings] = useState([]);
8
8
  const addObject = useCallback((thing) => {
9
9
  setThings((oldThings) => [...oldThings, thing]);
@@ -16,14 +16,14 @@ export const Stage = ({ children }) => {
16
16
  removeObject
17
17
  }), [addObject, removeObject]);
18
18
  useEffect(() => {
19
- if (!isInitialized) {
19
+ if (!application) {
20
20
  return;
21
21
  }
22
22
  application.stage.addChild(...things);
23
23
  return () => {
24
24
  application.stage.removeChild(...things);
25
25
  };
26
- }, [things, isInitialized]);
26
+ }, [things, application]);
27
27
  return (React.createElement(StageContext.Provider, { value: conextValue },
28
28
  React.createElement(Layer, null, children)));
29
29
  };
@@ -4,7 +4,6 @@ export type WorldContextValue = {
4
4
  width: number;
5
5
  height: number;
6
6
  };
7
- readonly application: Application;
8
- readonly isInitialized: boolean;
7
+ application: Application | null;
9
8
  };
10
9
  export declare const WorldContext: import("react").Context<WorldContextValue>;
@@ -1,12 +1,11 @@
1
- import React, { createRef, useEffect, useMemo, useRef, useState } from "react";
1
+ import React, { createRef, useEffect, useMemo, useState } from "react";
2
2
  import { Application } from "pixi.js";
3
3
  import { WorldContext } from "./World.context";
4
4
  import { Stage } from "../stage";
5
5
  import { AssetsManager } from "../assets-manager";
6
6
  export const World = ({ children, eventMode, size }) => {
7
- const [isInitialized, setIsInitialized] = useState(false);
8
7
  const canvasRef = createRef();
9
- const application = useRef(new Application());
8
+ const [applicationRef, setApplicationRef] = useState(null);
10
9
  const worldSize = useMemo(() => {
11
10
  if (size) {
12
11
  return { ...size };
@@ -18,47 +17,51 @@ export const World = ({ children, eventMode, size }) => {
18
17
  height
19
18
  };
20
19
  }
21
- if (!isInitialized) {
20
+ if (!applicationRef) {
22
21
  return {
23
22
  width: 0,
24
23
  height: 0
25
24
  };
26
25
  }
27
26
  return {
28
- width: application.current.screen.width,
29
- height: application.current.screen.height
27
+ width: applicationRef.screen.width,
28
+ height: applicationRef.screen.height
30
29
  };
31
- }, [size, canvasRef, isInitialized]);
30
+ }, [size, canvasRef.current, applicationRef]);
32
31
  useEffect(() => {
33
- if (canvasRef.current && !isInitialized) {
34
- application.current
35
- .init({
36
- resizeTo: canvasRef.current
37
- })
38
- .then(() => {
39
- setIsInitialized(true);
32
+ if (!applicationRef) {
33
+ const application = new Application();
34
+ application.init().then(() => {
35
+ setApplicationRef(application);
40
36
  });
41
37
  }
42
- }, [canvasRef.current, isInitialized]);
38
+ }, []);
39
+ useEffect(() => {
40
+ if (applicationRef && canvasRef.current) {
41
+ applicationRef.resizeTo = canvasRef.current;
42
+ applicationRef.resize();
43
+ }
44
+ }, [canvasRef.current, applicationRef]);
43
45
  const conextValue = useMemo(() => ({
44
- application: application.current,
45
- size: worldSize,
46
- isInitialized
47
- }), [isInitialized, worldSize]);
46
+ application: applicationRef,
47
+ size: worldSize
48
+ }), [applicationRef, worldSize]);
48
49
  useEffect(() => {
49
- if (!isInitialized) {
50
+ if (!applicationRef) {
50
51
  return;
51
52
  }
52
- canvasRef.current?.appendChild(application.current.canvas);
53
+ canvasRef.current?.appendChild(applicationRef.canvas);
53
54
  return () => {
54
- canvasRef.current?.removeChild(application.current.canvas);
55
+ if (applicationRef) {
56
+ canvasRef.current?.removeChild(applicationRef.canvas);
57
+ }
55
58
  };
56
- }, [canvasRef.current, isInitialized]);
59
+ }, [canvasRef.current, applicationRef]);
57
60
  useEffect(() => {
58
- if (application.current) {
59
- application.current.stage.eventMode = eventMode;
61
+ if (applicationRef) {
62
+ applicationRef.stage.eventMode = eventMode;
60
63
  }
61
- }, [eventMode, application.current, isInitialized]);
64
+ }, [eventMode, applicationRef]);
62
65
  return (React.createElement(WorldContext.Provider, { value: conextValue },
63
66
  React.createElement(AssetsManager, null,
64
67
  React.createElement(Stage, null, children),
package/package.json CHANGED
@@ -47,6 +47,6 @@
47
47
  "build": "tsc",
48
48
  "build:dev": "tsc -w"
49
49
  },
50
- "version": "1.0.0",
50
+ "version": "1.0.2",
51
51
  "webpack": "./src/index.ts"
52
52
  }
@@ -15,12 +15,12 @@ export const Camera: React.FC<CameraProps & PropsWithChildren> = ({ children, cl
15
15
  const [isReady, setIsReady] = useState(false);
16
16
  const [followContainer, setFollowContainer] = useState<Container | null>(null);
17
17
  const [ensureVisibleOptions, setEnsureVisibleOptions] = useState<EnsureVisibleOptions | null>(null);
18
- const { application, isInitialized, size } = useWorld();
18
+ const { application, size } = useWorld();
19
19
  const { addObject: addObjectToStage, removeObject: removeObjectFromStage } = useStage();
20
20
  const [things, setThings] = useState<Container[]>([]);
21
21
  const viewport = useMemo(
22
22
  () =>
23
- isInitialized
23
+ application
24
24
  ? new ViewportContainer({
25
25
  worldHeight: size.height,
26
26
  worldWidth: size.width,
@@ -29,7 +29,7 @@ export const Camera: React.FC<CameraProps & PropsWithChildren> = ({ children, cl
29
29
  events: application?.renderer?.events
30
30
  })
31
31
  : null,
32
- [size, application, isInitialized]
32
+ [size, application]
33
33
  );
34
34
 
35
35
  const addObject = useCallback(
@@ -55,10 +55,10 @@ export const Camera: React.FC<CameraProps & PropsWithChildren> = ({ children, cl
55
55
  );
56
56
 
57
57
  useEffect(() => {
58
- if (viewport && clampZoom) {
58
+ if (viewport && clampZoom && application) {
59
59
  viewport.clampZoom(clampZoom);
60
60
  }
61
- }, [clampZoom, isInitialized]);
61
+ }, [clampZoom]);
62
62
 
63
63
  useEffect(() => {
64
64
  if (viewport) {
@@ -69,14 +69,14 @@ export const Camera: React.FC<CameraProps & PropsWithChildren> = ({ children, cl
69
69
  viewport.height = size.height;
70
70
  }
71
71
  }
72
- }, [size, viewport?.uid, isInitialized]);
72
+ }, [size, viewport?.uid, application]);
73
73
 
74
74
  useEffect(() => {
75
- if (!isInitialized || !viewport) {
75
+ if (!application || !viewport) {
76
76
  return;
77
77
  }
78
78
  viewport?.moveCenter(0, 0);
79
- }, [viewport, size, isInitialized]);
79
+ }, [viewport, size, application]);
80
80
 
81
81
  useEffect(() => {
82
82
  if (!viewport) {
@@ -90,7 +90,7 @@ export const Camera: React.FC<CameraProps & PropsWithChildren> = ({ children, cl
90
90
  removeObjectFromStage(viewport);
91
91
  setIsReady(false);
92
92
  };
93
- }, [application, isInitialized]);
93
+ }, [viewport?.uid]);
94
94
 
95
95
  useEffect(() => {
96
96
  if (!viewport || !isReady) {
@@ -10,10 +10,10 @@ export const useTickerCallback = <T = unknown>({
10
10
  isEnabled?: boolean;
11
11
  callback: TickerCallback<T>;
12
12
  }) => {
13
- const { application, isInitialized } = useWorld();
13
+ const { application } = useWorld();
14
14
 
15
15
  useEffect(() => {
16
- if (!isInitialized || !isEnabled) {
16
+ if (!application || !isEnabled) {
17
17
  return;
18
18
  }
19
19
 
@@ -21,5 +21,5 @@ export const useTickerCallback = <T = unknown>({
21
21
  return () => {
22
22
  application.ticker.remove(callback);
23
23
  };
24
- }, [isEnabled, application, isInitialized, callback]);
24
+ }, [isEnabled, application, callback]);
25
25
  };
@@ -5,7 +5,7 @@ import { useWorld } from "../hooks";
5
5
  import { Layer } from "../layer";
6
6
 
7
7
  export const Stage: React.FC<PropsWithChildren> = ({ children }) => {
8
- const { application, isInitialized } = useWorld();
8
+ const { application } = useWorld();
9
9
  const [things, setThings] = useState<Container[]>([]);
10
10
 
11
11
  const addObject = useCallback(
@@ -31,7 +31,7 @@ export const Stage: React.FC<PropsWithChildren> = ({ children }) => {
31
31
  );
32
32
 
33
33
  useEffect(() => {
34
- if (!isInitialized) {
34
+ if (!application) {
35
35
  return;
36
36
  }
37
37
 
@@ -40,7 +40,7 @@ export const Stage: React.FC<PropsWithChildren> = ({ children }) => {
40
40
  return () => {
41
41
  application.stage.removeChild(...things);
42
42
  };
43
- }, [things, isInitialized]);
43
+ }, [things, application]);
44
44
 
45
45
  return (
46
46
  <StageContext.Provider value={conextValue}>
@@ -6,8 +6,7 @@ export type WorldContextValue = {
6
6
  width: number;
7
7
  height: number;
8
8
  };
9
- readonly application: Application;
10
- readonly isInitialized: boolean;
9
+ application: Application | null;
11
10
  };
12
11
 
13
12
  export const WorldContext = createContext<WorldContextValue>({} as unknown as WorldContextValue);
@@ -1,4 +1,4 @@
1
- import React, { PropsWithChildren, createRef, useEffect, useMemo, useRef, useState } from "react";
1
+ import React, { PropsWithChildren, createRef, useEffect, useMemo, useState } from "react";
2
2
  import { Application, EventMode } from "pixi.js";
3
3
  import { WorldContextValue, WorldContext } from "./World.context";
4
4
  import { Stage } from "../stage";
@@ -13,10 +13,9 @@ type WorldProps = {
13
13
  };
14
14
 
15
15
  export const World: React.FC<PropsWithChildren & WorldProps> = ({ children, eventMode, size }) => {
16
- const [isInitialized, setIsInitialized] = useState(false);
17
16
  const canvasRef = createRef<HTMLDivElement>();
18
17
 
19
- const application = useRef(new Application());
18
+ const [applicationRef, setApplicationRef] = useState<Application | null>(null);
20
19
 
21
20
  const worldSize = useMemo(() => {
22
21
  if (size) {
@@ -31,56 +30,61 @@ export const World: React.FC<PropsWithChildren & WorldProps> = ({ children, even
31
30
  };
32
31
  }
33
32
 
34
- if (!isInitialized) {
33
+ if (!applicationRef) {
35
34
  return {
36
35
  width: 0,
37
36
  height: 0
38
37
  };
39
38
  }
39
+
40
40
  return {
41
- width: application.current.screen.width,
42
- height: application.current.screen.height
41
+ width: applicationRef.screen.width,
42
+ height: applicationRef.screen.height
43
43
  };
44
- }, [size, canvasRef, isInitialized]);
44
+ }, [size, canvasRef.current, applicationRef]);
45
45
 
46
46
  useEffect(() => {
47
- if (canvasRef.current && !isInitialized) {
48
- application.current
49
- .init({
50
- resizeTo: canvasRef.current
51
- })
52
- .then(() => {
53
- setIsInitialized(true);
54
- });
47
+ if (!applicationRef) {
48
+ const application = new Application();
49
+ application.init().then(() => {
50
+ setApplicationRef(application);
51
+ });
55
52
  }
56
- }, [canvasRef.current, isInitialized]);
53
+ }, []);
54
+
55
+ useEffect(() => {
56
+ if (applicationRef && canvasRef.current) {
57
+ applicationRef.resizeTo = canvasRef.current;
58
+ applicationRef.resize();
59
+ }
60
+ }, [canvasRef.current, applicationRef]);
57
61
 
58
62
  const conextValue = useMemo<WorldContextValue>(
59
63
  () => ({
60
- application: application.current,
61
- size: worldSize,
62
- isInitialized
64
+ application: applicationRef,
65
+ size: worldSize
63
66
  }),
64
- [isInitialized, worldSize]
67
+ [applicationRef, worldSize]
65
68
  );
66
69
 
67
70
  useEffect(() => {
68
- if (!isInitialized) {
71
+ if (!applicationRef) {
69
72
  return;
70
73
  }
71
-
72
- canvasRef.current?.appendChild(application.current.canvas);
74
+ canvasRef.current?.appendChild(applicationRef.canvas);
73
75
 
74
76
  return () => {
75
- canvasRef.current?.removeChild(application.current.canvas);
77
+ if (applicationRef) {
78
+ canvasRef.current?.removeChild(applicationRef.canvas);
79
+ }
76
80
  };
77
- }, [canvasRef.current, isInitialized]);
81
+ }, [canvasRef.current, applicationRef]);
78
82
 
79
83
  useEffect(() => {
80
- if (application.current) {
81
- application.current.stage.eventMode = eventMode;
84
+ if (applicationRef) {
85
+ applicationRef.stage.eventMode = eventMode;
82
86
  }
83
- }, [eventMode, application.current, isInitialized]);
87
+ }, [eventMode, applicationRef]);
84
88
 
85
89
  return (
86
90
  <WorldContext.Provider value={conextValue}>