pixi-fusion 1.0.1 → 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.
- package/dist/src/camera/Camera.js +9 -9
- package/dist/src/hooks/useTickerCallback.js +3 -3
- package/dist/src/stage/Stage.js +3 -3
- package/dist/src/world/World.context.d.ts +1 -2
- package/dist/src/world/World.js +27 -32
- package/package.json +1 -1
- package/src/camera/Camera.tsx +9 -9
- package/src/hooks/useTickerCallback.ts +3 -3
- package/src/stage/Stage.tsx +3 -3
- package/src/world/World.context.tsx +1 -2
- package/src/world/World.tsx +29 -34
|
@@ -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,
|
|
11
|
+
const { application, size } = useWorld();
|
|
12
12
|
const { addObject: addObjectToStage, removeObject: removeObjectFromStage } = useStage();
|
|
13
13
|
const [things, setThings] = useState([]);
|
|
14
|
-
const viewport = useMemo(() =>
|
|
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
|
|
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
|
|
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,
|
|
47
|
+
}, [size, viewport?.uid, application]);
|
|
48
48
|
useEffect(() => {
|
|
49
|
-
if (!
|
|
49
|
+
if (!application || !viewport) {
|
|
50
50
|
return;
|
|
51
51
|
}
|
|
52
52
|
viewport?.moveCenter(0, 0);
|
|
53
|
-
}, [viewport, size,
|
|
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
|
-
}, [
|
|
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
|
|
4
|
+
const { application } = useWorld();
|
|
5
5
|
useEffect(() => {
|
|
6
|
-
if (!
|
|
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,
|
|
13
|
+
}, [isEnabled, application, callback]);
|
|
14
14
|
};
|
package/dist/src/stage/Stage.js
CHANGED
|
@@ -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
|
|
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 (!
|
|
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,
|
|
26
|
+
}, [things, application]);
|
|
27
27
|
return (React.createElement(StageContext.Provider, { value: conextValue },
|
|
28
28
|
React.createElement(Layer, null, children)));
|
|
29
29
|
};
|
package/dist/src/world/World.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import React, { createRef, useEffect, useMemo,
|
|
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
|
|
8
|
+
const [applicationRef, setApplicationRef] = useState(null);
|
|
10
9
|
const worldSize = useMemo(() => {
|
|
11
10
|
if (size) {
|
|
12
11
|
return { ...size };
|
|
@@ -18,55 +17,51 @@ export const World = ({ children, eventMode, size }) => {
|
|
|
18
17
|
height
|
|
19
18
|
};
|
|
20
19
|
}
|
|
21
|
-
if (!
|
|
20
|
+
if (!applicationRef) {
|
|
22
21
|
return {
|
|
23
22
|
width: 0,
|
|
24
23
|
height: 0
|
|
25
24
|
};
|
|
26
25
|
}
|
|
27
26
|
return {
|
|
28
|
-
width:
|
|
29
|
-
height:
|
|
27
|
+
width: applicationRef.screen.width,
|
|
28
|
+
height: applicationRef.screen.height
|
|
30
29
|
};
|
|
31
|
-
}, [size, canvasRef,
|
|
30
|
+
}, [size, canvasRef.current, applicationRef]);
|
|
32
31
|
useEffect(() => {
|
|
33
|
-
if (!
|
|
34
|
-
application
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
setIsInitialized(true);
|
|
38
|
-
})
|
|
39
|
-
.catch((e) => {
|
|
40
|
-
setIsInitialized(true);
|
|
41
|
-
console.error(e);
|
|
32
|
+
if (!applicationRef) {
|
|
33
|
+
const application = new Application();
|
|
34
|
+
application.init().then(() => {
|
|
35
|
+
setApplicationRef(application);
|
|
42
36
|
});
|
|
43
37
|
}
|
|
44
|
-
}, [
|
|
38
|
+
}, []);
|
|
45
39
|
useEffect(() => {
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
40
|
+
if (applicationRef && canvasRef.current) {
|
|
41
|
+
applicationRef.resizeTo = canvasRef.current;
|
|
42
|
+
applicationRef.resize();
|
|
49
43
|
}
|
|
50
|
-
}, [canvasRef.current,
|
|
44
|
+
}, [canvasRef.current, applicationRef]);
|
|
51
45
|
const conextValue = useMemo(() => ({
|
|
52
|
-
application:
|
|
53
|
-
size: worldSize
|
|
54
|
-
|
|
55
|
-
}), [isInitialized, worldSize]);
|
|
46
|
+
application: applicationRef,
|
|
47
|
+
size: worldSize
|
|
48
|
+
}), [applicationRef, worldSize]);
|
|
56
49
|
useEffect(() => {
|
|
57
|
-
if (!
|
|
50
|
+
if (!applicationRef) {
|
|
58
51
|
return;
|
|
59
52
|
}
|
|
60
|
-
canvasRef.current?.appendChild(
|
|
53
|
+
canvasRef.current?.appendChild(applicationRef.canvas);
|
|
61
54
|
return () => {
|
|
62
|
-
|
|
55
|
+
if (applicationRef) {
|
|
56
|
+
canvasRef.current?.removeChild(applicationRef.canvas);
|
|
57
|
+
}
|
|
63
58
|
};
|
|
64
|
-
}, [canvasRef.current,
|
|
59
|
+
}, [canvasRef.current, applicationRef]);
|
|
65
60
|
useEffect(() => {
|
|
66
|
-
if (
|
|
67
|
-
|
|
61
|
+
if (applicationRef) {
|
|
62
|
+
applicationRef.stage.eventMode = eventMode;
|
|
68
63
|
}
|
|
69
|
-
}, [eventMode,
|
|
64
|
+
}, [eventMode, applicationRef]);
|
|
70
65
|
return (React.createElement(WorldContext.Provider, { value: conextValue },
|
|
71
66
|
React.createElement(AssetsManager, null,
|
|
72
67
|
React.createElement(Stage, null, children),
|
package/package.json
CHANGED
package/src/camera/Camera.tsx
CHANGED
|
@@ -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,
|
|
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
|
-
|
|
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
|
|
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
|
|
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,
|
|
72
|
+
}, [size, viewport?.uid, application]);
|
|
73
73
|
|
|
74
74
|
useEffect(() => {
|
|
75
|
-
if (!
|
|
75
|
+
if (!application || !viewport) {
|
|
76
76
|
return;
|
|
77
77
|
}
|
|
78
78
|
viewport?.moveCenter(0, 0);
|
|
79
|
-
}, [viewport, size,
|
|
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
|
-
}, [
|
|
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
|
|
13
|
+
const { application } = useWorld();
|
|
14
14
|
|
|
15
15
|
useEffect(() => {
|
|
16
|
-
if (!
|
|
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,
|
|
24
|
+
}, [isEnabled, application, callback]);
|
|
25
25
|
};
|
package/src/stage/Stage.tsx
CHANGED
|
@@ -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
|
|
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 (!
|
|
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,
|
|
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
|
-
|
|
10
|
-
readonly isInitialized: boolean;
|
|
9
|
+
application: Application | null;
|
|
11
10
|
};
|
|
12
11
|
|
|
13
12
|
export const WorldContext = createContext<WorldContextValue>({} as unknown as WorldContextValue);
|
package/src/world/World.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { PropsWithChildren, createRef, useEffect, useMemo,
|
|
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
|
|
18
|
+
const [applicationRef, setApplicationRef] = useState<Application | null>(null);
|
|
20
19
|
|
|
21
20
|
const worldSize = useMemo(() => {
|
|
22
21
|
if (size) {
|
|
@@ -31,65 +30,61 @@ export const World: React.FC<PropsWithChildren & WorldProps> = ({ children, even
|
|
|
31
30
|
};
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
if (!
|
|
33
|
+
if (!applicationRef) {
|
|
35
34
|
return {
|
|
36
35
|
width: 0,
|
|
37
36
|
height: 0
|
|
38
37
|
};
|
|
39
38
|
}
|
|
39
|
+
|
|
40
40
|
return {
|
|
41
|
-
width:
|
|
42
|
-
height:
|
|
41
|
+
width: applicationRef.screen.width,
|
|
42
|
+
height: applicationRef.screen.height
|
|
43
43
|
};
|
|
44
|
-
}, [size, canvasRef,
|
|
44
|
+
}, [size, canvasRef.current, applicationRef]);
|
|
45
45
|
|
|
46
46
|
useEffect(() => {
|
|
47
|
-
if (!
|
|
48
|
-
application
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
})
|
|
53
|
-
.catch((e) => {
|
|
54
|
-
setIsInitialized(true);
|
|
55
|
-
console.error(e);
|
|
56
|
-
});
|
|
47
|
+
if (!applicationRef) {
|
|
48
|
+
const application = new Application();
|
|
49
|
+
application.init().then(() => {
|
|
50
|
+
setApplicationRef(application);
|
|
51
|
+
});
|
|
57
52
|
}
|
|
58
|
-
}, [
|
|
53
|
+
}, []);
|
|
59
54
|
|
|
60
55
|
useEffect(() => {
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
if (applicationRef && canvasRef.current) {
|
|
57
|
+
applicationRef.resizeTo = canvasRef.current;
|
|
58
|
+
applicationRef.resize();
|
|
64
59
|
}
|
|
65
|
-
}, [canvasRef.current,
|
|
60
|
+
}, [canvasRef.current, applicationRef]);
|
|
66
61
|
|
|
67
62
|
const conextValue = useMemo<WorldContextValue>(
|
|
68
63
|
() => ({
|
|
69
|
-
application:
|
|
70
|
-
size: worldSize
|
|
71
|
-
isInitialized
|
|
64
|
+
application: applicationRef,
|
|
65
|
+
size: worldSize
|
|
72
66
|
}),
|
|
73
|
-
[
|
|
67
|
+
[applicationRef, worldSize]
|
|
74
68
|
);
|
|
75
69
|
|
|
76
70
|
useEffect(() => {
|
|
77
|
-
if (!
|
|
71
|
+
if (!applicationRef) {
|
|
78
72
|
return;
|
|
79
73
|
}
|
|
80
|
-
|
|
81
|
-
canvasRef.current?.appendChild(application.current.canvas);
|
|
74
|
+
canvasRef.current?.appendChild(applicationRef.canvas);
|
|
82
75
|
|
|
83
76
|
return () => {
|
|
84
|
-
|
|
77
|
+
if (applicationRef) {
|
|
78
|
+
canvasRef.current?.removeChild(applicationRef.canvas);
|
|
79
|
+
}
|
|
85
80
|
};
|
|
86
|
-
}, [canvasRef.current,
|
|
81
|
+
}, [canvasRef.current, applicationRef]);
|
|
87
82
|
|
|
88
83
|
useEffect(() => {
|
|
89
|
-
if (
|
|
90
|
-
|
|
84
|
+
if (applicationRef) {
|
|
85
|
+
applicationRef.stage.eventMode = eventMode;
|
|
91
86
|
}
|
|
92
|
-
}, [eventMode,
|
|
87
|
+
}, [eventMode, applicationRef]);
|
|
93
88
|
|
|
94
89
|
return (
|
|
95
90
|
<WorldContext.Provider value={conextValue}>
|