pixi-fusion 2.0.1 → 2.2.0

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.
Files changed (44) hide show
  1. package/dist/src/assets-manager/AssetsManager.js +2 -2
  2. package/dist/src/camera/Camera.js +8 -8
  3. package/dist/src/game-context/Game.context.d.ts +1 -0
  4. package/dist/src/game-context/Game.context.js +17 -13
  5. package/dist/src/game-objects/usePhysicalObject.js +1 -1
  6. package/dist/src/game-objects/usePhysicalObjectFromConfig.d.ts +1 -1
  7. package/dist/src/game-objects/usePhysicalObjectFromConfig.js +1 -1
  8. package/dist/src/game-objects/useWalls.js +2 -2
  9. package/dist/src/hooks/useAnimatedSprite.js +4 -4
  10. package/dist/src/hooks/useCollisionDetection.js +1 -1
  11. package/dist/src/hooks/useObject.js +10 -10
  12. package/dist/src/hooks/useSprite.js +1 -1
  13. package/dist/src/hooks/useTexture.js +1 -1
  14. package/dist/src/hooks/useTilingSprite.js +2 -2
  15. package/dist/src/index.d.ts +0 -1
  16. package/dist/src/index.js +0 -1
  17. package/dist/src/layer/Layer.js +4 -4
  18. package/dist/src/physics/MatterPhysics.context.js +11 -11
  19. package/dist/src/physics/useCollisionEventHandler.js +1 -1
  20. package/dist/src/physics/usePhysicsEngineEventHandler.js +1 -1
  21. package/dist/src/physics/usePhysicsTickerCallback.js +1 -1
  22. package/dist/src/stage/Stage.js +3 -3
  23. package/dist/src/world/World.js +31 -34
  24. package/package.json +27 -14
  25. package/src/assets-manager/AssetsManager.tsx +14 -17
  26. package/src/camera/Camera.tsx +16 -28
  27. package/src/game-context/Game.context.tsx +18 -13
  28. package/src/game-objects/usePhysicalObject.ts +1 -1
  29. package/src/game-objects/usePhysicalObjectFromConfig.ts +2 -2
  30. package/src/game-objects/useWalls.ts +2 -2
  31. package/src/hooks/useAnimatedSprite.ts +4 -4
  32. package/src/hooks/useCollisionDetection.ts +1 -1
  33. package/src/hooks/useObject.ts +10 -10
  34. package/src/hooks/useSprite.ts +1 -1
  35. package/src/hooks/useTexture.ts +1 -1
  36. package/src/hooks/useTilingSprite.ts +2 -2
  37. package/src/index.ts +0 -1
  38. package/src/layer/Layer.tsx +4 -4
  39. package/src/physics/MatterPhysics.context.tsx +23 -17
  40. package/src/physics/useCollisionEventHandler.ts +1 -1
  41. package/src/physics/usePhysicsEngineEventHandler.ts +1 -1
  42. package/src/physics/usePhysicsTickerCallback.ts +1 -1
  43. package/src/stage/Stage.tsx +7 -13
  44. package/src/world/World.tsx +37 -37
@@ -1,26 +1,20 @@
1
1
  import React, { PropsWithChildren, useCallback, useEffect, useMemo, useState } from "react";
2
2
  import { Container } from "pixi.js";
3
- import { StageContext, StageContextValue } from "./Stage.context";
4
3
  import { useWorld } from "../hooks";
5
4
  import { Layer } from "../layer";
5
+ import { StageContext, StageContextValue } from "./Stage.context";
6
6
 
7
7
  export const Stage: React.FC<PropsWithChildren> = ({ children }) => {
8
8
  const { application } = useWorld();
9
9
  const [things, setThings] = useState<Container[]>([]);
10
10
 
11
- const addObject = useCallback(
12
- (thing: Container) => {
13
- setThings((oldThings) => [...oldThings, thing]);
14
- },
15
- [things]
16
- );
11
+ const addObject = useCallback((thing: Container) => {
12
+ setThings((oldThings) => [...oldThings, thing]);
13
+ }, []);
17
14
 
18
- const removeObject = useCallback(
19
- (thing: Container) => {
20
- setThings((oldThings) => oldThings.filter(({ uid }) => uid === thing.uid));
21
- },
22
- [application]
23
- );
15
+ const removeObject = useCallback((thing: Container) => {
16
+ setThings((oldThings) => oldThings.filter(({ uid }) => uid === thing.uid));
17
+ }, []);
24
18
 
25
19
  const conextValue = useMemo<StageContextValue>(
26
20
  () => ({
@@ -1,8 +1,8 @@
1
- import React, { PropsWithChildren, createRef, useEffect, useMemo, useState } from "react";
1
+ import React, { PropsWithChildren, createRef, useCallback, useEffect, useLayoutEffect, useMemo, useState } from "react";
2
2
  import { Application, EventMode } from "pixi.js";
3
- import { WorldContextValue, WorldContext } from "./World.context";
4
3
  import { Stage } from "../stage";
5
4
  import { AssetsManager } from "../assets-manager";
5
+ import { WorldContextValue, WorldContext } from "./World.context";
6
6
 
7
7
  type WorldProps = {
8
8
  eventMode?: EventMode;
@@ -17,17 +17,38 @@ export const World: React.FC<PropsWithChildren & WorldProps> = ({ children, even
17
17
 
18
18
  const [applicationRef, setApplicationRef] = useState<Application | null>(null);
19
19
 
20
- const worldSize = useMemo(() => {
21
- if (size) {
22
- return { ...size };
23
- }
20
+ const [canvasSize, setCanvasSize] = useState<null | { width: number; height: number }>(null);
24
21
 
22
+ const onCanvasRef = useCallback(
23
+ (ref: HTMLDivElement) => {
24
+ const application = new Application();
25
+ application.init({ eventMode }).then(() => {
26
+ canvasRef.current = ref;
27
+ application.resizeTo = canvasRef.current;
28
+ application.resize();
29
+ setApplicationRef(application);
30
+ });
31
+ },
32
+ [canvasRef, eventMode]
33
+ );
34
+
35
+ useLayoutEffect(() => {
25
36
  if (canvasRef?.current) {
26
37
  const { width, height } = canvasRef.current.getBoundingClientRect();
27
- return {
38
+ setCanvasSize({
28
39
  width,
29
40
  height
30
- };
41
+ });
42
+ }
43
+ }, [canvasRef]);
44
+
45
+ const worldSize = useMemo(() => {
46
+ if (size) {
47
+ return { ...size };
48
+ }
49
+
50
+ if (canvasSize) {
51
+ return canvasSize;
31
52
  }
32
53
 
33
54
  if (!applicationRef) {
@@ -38,26 +59,10 @@ export const World: React.FC<PropsWithChildren & WorldProps> = ({ children, even
38
59
  }
39
60
 
40
61
  return {
41
- width: applicationRef.screen.width,
42
- height: applicationRef.screen.height
62
+ width: applicationRef?.screen.width,
63
+ height: applicationRef?.screen.height
43
64
  };
44
- }, [size, canvasRef.current, applicationRef]);
45
-
46
- useEffect(() => {
47
- if (!applicationRef) {
48
- const application = new Application();
49
- application.init().then(() => {
50
- setApplicationRef(application);
51
- });
52
- }
53
- }, []);
54
-
55
- useEffect(() => {
56
- if (applicationRef && canvasRef.current) {
57
- applicationRef.resizeTo = canvasRef.current;
58
- applicationRef.resize();
59
- }
60
- }, [canvasRef.current, applicationRef]);
65
+ }, [size, canvasSize, applicationRef]);
61
66
 
62
67
  const conextValue = useMemo<WorldContextValue>(
63
68
  () => ({
@@ -71,26 +76,21 @@ export const World: React.FC<PropsWithChildren & WorldProps> = ({ children, even
71
76
  if (!applicationRef) {
72
77
  return;
73
78
  }
74
- canvasRef.current?.appendChild(applicationRef.canvas);
79
+ const canvas = canvasRef.current;
80
+ canvas?.appendChild(applicationRef.canvas);
75
81
 
76
82
  return () => {
77
83
  if (applicationRef) {
78
- canvasRef.current?.removeChild(applicationRef.canvas);
84
+ canvas?.removeChild(applicationRef.canvas);
79
85
  }
80
86
  };
81
- }, [canvasRef.current, applicationRef]);
82
-
83
- useEffect(() => {
84
- if (applicationRef) {
85
- applicationRef.stage.eventMode = eventMode;
86
- }
87
- }, [eventMode, applicationRef]);
87
+ }, [canvasRef, applicationRef]);
88
88
 
89
89
  return (
90
90
  <WorldContext.Provider value={conextValue}>
91
91
  <AssetsManager>
92
92
  <Stage>{children}</Stage>
93
- <div style={{ width: "100%", height: "100%" }} ref={canvasRef} />
93
+ <div style={{ width: "100%", height: "100%" }} ref={onCanvasRef} />
94
94
  </AssetsManager>
95
95
  </WorldContext.Provider>
96
96
  );