reacthub-pixitweenjs 0.2.6 → 0.2.8

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.
@@ -0,0 +1,7 @@
1
+ export * from "./PixiImageSprite";
2
+ export * from "./PixiSVGSprite";
3
+ export * from "./PixiAudioSprite";
4
+ export * from "./PixiGifSprite";
5
+ export * from "./PixiTextSprite";
6
+ export * from "./PixiVideoSprite";
7
+ export * from "./PixiAnimatedSprite";
@@ -0,0 +1,199 @@
1
+ import * as React from "react";
2
+ import { useContext, useRef, useState } from "react";
3
+ import gsap from "gsap";
4
+
5
+ import { Container, withFilters } from "@pixi/react";
6
+ import { AdjustmentFilter } from "@pixi/filter-adjustment";
7
+ import * as PIXI from "pixi.js";
8
+ import { Effects } from "../types/Effects";
9
+ // import pixiTransfomer from "../utils/PixiTransformer";
10
+ import PixiTransformer from "../utils/PixiTransformer";
11
+ import { PixiBaseSpriteProps } from "../types/BaseProps";
12
+ import { TransformationEnd } from "../types/transformation";
13
+ import { GsapPixieContext } from "../providers/GsapPixieContextProvider";
14
+ import { getAnimByName } from "../utils/GsapAnim";
15
+ // @ts-ignore
16
+ import isEmpty from "lodash/isEmpty";
17
+
18
+ /** CYAN Filters */
19
+ const CYAN = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0];
20
+
21
+ const AbstractContainer: React.FC<PixiBaseSpriteProps> = (props) => {
22
+ //// State
23
+ const [isMounted, setIsMounted] = React.useState(false);
24
+ const [, setIsTransformerDragging] = useState(false);
25
+ const [, setIsMouseOverTransformer] = useState(false);
26
+
27
+ //// Refs
28
+ const containerRef = useRef<PIXI.Container>(null);
29
+ const parentNode = useRef<PIXI.Container>(null);
30
+ const imgGroupRef = useRef<PIXI.Container>(null);
31
+ const transformerRef = useRef<PIXI.Container>(null);
32
+
33
+ //// Context
34
+ const { tl } = useContext(GsapPixieContext);
35
+
36
+ const {
37
+ uniqueId,
38
+ startAt,
39
+ endAt,
40
+ initialAlpha,
41
+ transformation: { x, y, width, height, colorCorrection, animation, effect },
42
+ applyTransformer,
43
+ } = props;
44
+
45
+ console.log("all abstract props", props);
46
+
47
+ // color corrections
48
+ const {
49
+ contrast = 1,
50
+ saturation = 1,
51
+ brightness = 1,
52
+ alpha = 1,
53
+ blurRadius = 0,
54
+ } = colorCorrection || {};
55
+
56
+ /** adjustment filter */
57
+ const adjustments = {
58
+ ...(brightness > 1 && { brightness: brightness }),
59
+ ...(contrast > 1 && { contrast: contrast }),
60
+ ...(saturation > 1 && { saturation: saturation }),
61
+ ...(alpha !== 0 && { alpha: alpha }),
62
+ };
63
+
64
+ React.useEffect(() => {
65
+ if (containerRef.current) {
66
+ setIsMounted(true);
67
+ }
68
+ }, []);
69
+
70
+ React.useEffect(() => {
71
+ let ctx = gsap.context(() => {});
72
+ if (containerRef.current && tl.current) {
73
+ const data = {
74
+ duration: Number(endAt) - Number(startAt),
75
+ };
76
+
77
+ const ease = getAnimByName(animation || "None");
78
+ ctx = gsap.context(() => {
79
+ if (!isEmpty(ease.from)) {
80
+ tl.current
81
+ .from(containerRef.current, { ...ease.from }, startAt)
82
+ .to(imgGroupRef.current, { alpha: 1, duration: 0.2 }, startAt)
83
+ .from(imgGroupRef.current, { ...data }, startAt)
84
+ .to(containerRef.current, { alpha: 0, duration: 0.2 }, endAt - 0.2);
85
+ } else if (!isEmpty(ease.to)) {
86
+ tl.current
87
+ .to(
88
+ containerRef.current,
89
+ { alpha: 1, duration: 0.2, ...ease.to },
90
+ startAt
91
+ )
92
+ .from(imgGroupRef.current, { ...data }, startAt)
93
+ .to(containerRef.current, { alpha: 0, duration: 0.2 }, endAt - 0.2);
94
+ } else if (!isEmpty(ease.fromTo)) {
95
+ tl.current
96
+ .fromTo(
97
+ containerRef.current,
98
+ ease.fromTo?.from,
99
+ ease.fromTo?.to,
100
+ startAt
101
+ )
102
+ .from(imgGroupRef.current, { ...data }, startAt)
103
+ .to(containerRef.current, { alpha: 0, duration: 0.2 }, endAt - 0.2);
104
+ } else {
105
+ tl.current
106
+ .to(containerRef.current, { alpha: 1, duration: 0.01 }, startAt)
107
+ .from(imgGroupRef.current, { ...data }, startAt)
108
+ .to(
109
+ containerRef.current,
110
+ { alpha: 0, duration: 0.1 },
111
+ Number(endAt) - Number(0.1)
112
+ );
113
+ }
114
+ });
115
+ }
116
+ return () => ctx.revert(); // cleanup!
117
+ }, [animation, startAt, endAt]);
118
+
119
+ /** handle on tranformer onchange */
120
+ const handleOnTransformChange = React.useCallback(() => {
121
+ setIsTransformerDragging(true);
122
+ }, []);
123
+
124
+ // transformer to handle sprite transformation
125
+ const handleOnTransformEnd = React.useCallback(
126
+ (endData: TransformationEnd) => {
127
+ console.log("changeEnd", endData);
128
+ if (props.onAnchorTransformationEnd) {
129
+ console.log("running onAnchorTransformationEnd");
130
+ props.onAnchorTransformationEnd(endData);
131
+ }
132
+ },
133
+ []
134
+ );
135
+
136
+ const Filters = withFilters(Container, {
137
+ ...(blurRadius > 0 && { blur: PIXI.filters.BlurFilter }),
138
+ ...(!isEmpty(adjustments) && { adjust: AdjustmentFilter }),
139
+ matrix: PIXI.filters.ColorMatrixFilter,
140
+ });
141
+
142
+ const isFilterOrEffectEnabled =
143
+ colorCorrection?.enabled || (effect && effect !== Effects.Normal);
144
+
145
+ return (
146
+ <Container ref={parentNode}>
147
+ <Container
148
+ ref={containerRef}
149
+ alpha={initialAlpha}
150
+ position={[x, y]}
151
+ pivot={[x, y]}
152
+ width={width}
153
+ height={height}
154
+ >
155
+ {isFilterOrEffectEnabled && (
156
+ <Filters
157
+ scale={1}
158
+ blur={{ blur: blurRadius, quality: 4 }}
159
+ adjust={adjustments}
160
+ apply={({ matrix }: { matrix: any }) => {
161
+ if (effect === Effects.BlackAndWhite) {
162
+ matrix.desaturate();
163
+ } else if (effect === Effects.Sepia) {
164
+ matrix.sepia();
165
+ } else if (effect === Effects.RetroVintage) {
166
+ matrix.negative();
167
+ } else if (effect === Effects.NightVision) {
168
+ matrix.negative();
169
+ } else if (effect === Effects.Normal) {
170
+ matrix.reset();
171
+ }
172
+ }}
173
+ matrix={{
174
+ enabled: true,
175
+ // @ts-ignore
176
+ matrix: CYAN,
177
+ }}
178
+ >
179
+ {props.children}
180
+ </Filters>
181
+ )}
182
+ {!isFilterOrEffectEnabled && props.children}
183
+ </Container>
184
+ {applyTransformer && (
185
+ <PixiTransformer
186
+ pixiTransformerRef={transformerRef}
187
+ imageRef={containerRef}
188
+ isMounted={isMounted}
189
+ transformCommit={handleOnTransformEnd}
190
+ transformChange={handleOnTransformChange}
191
+ mouseoverEvent={setIsMouseOverTransformer}
192
+ uniqueId={uniqueId}
193
+ />
194
+ )}
195
+ </Container>
196
+ );
197
+ };
198
+
199
+ export default AbstractContainer;
@@ -0,0 +1,159 @@
1
+ import * as React from "react";
2
+ import { Container, withFilters } from "@pixi/react";
3
+ import { AdjustmentFilter } from "@pixi/filter-adjustment";
4
+ import * as PIXI from "pixi.js";
5
+ import { Effects } from "../types/Effects";
6
+ // import pixiTransfomer from "../utils/PixiTransformer";
7
+ import PixiTransformer from "../utils/PixiTransformer";
8
+ import { PixiBaseSpriteProps } from "../types/BaseProps";
9
+ import { TransformationEnd } from "../types/transformation";
10
+
11
+ const Filters = withFilters(Container, {
12
+ blur: PIXI.filters.BlurFilter,
13
+ adjust: AdjustmentFilter,
14
+ matrix: PIXI.filters.ColorMatrixFilter,
15
+ });
16
+
17
+ /** CYAN Filters */
18
+ const CYAN = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0];
19
+
20
+ const withFilterContainer = <P extends object>(
21
+ WrappedComponent: React.ComponentType<P & PixiBaseSpriteProps>
22
+ ) => {
23
+ return class WithPixi extends React.Component<P & PixiBaseSpriteProps> {
24
+ imageRef: React.RefObject<PIXI.Sprite>;
25
+ containerRef: React.RefObject<PIXI.Container<PIXI.DisplayObject>>;
26
+ parentNode: React.RefObject<PIXI.Container<PIXI.DisplayObject>>;
27
+ imgGroupRef: React.RefObject<PIXI.Container<PIXI.DisplayObject>>;
28
+ transformerRef: React.RefObject<PIXI.Container<PIXI.DisplayObject>>;
29
+ isMounted: React.RefObject<boolean>;
30
+
31
+ constructor(props: P & PixiBaseSpriteProps) {
32
+ super(props);
33
+ // create refs
34
+ this.imageRef = React.createRef<PIXI.Sprite>();
35
+ this.containerRef = React.createRef<PIXI.Container>();
36
+ this.parentNode = React.createRef<PIXI.Container>();
37
+ this.imgGroupRef = React.createRef<PIXI.Container>();
38
+ this.transformerRef = React.createRef<PIXI.Container>();
39
+ this.isMounted = React.createRef<boolean>();
40
+ // implement ismounted state
41
+ this.state = {
42
+ isMounted: false,
43
+ isTransformerDragging: false,
44
+ isMouseOverTransformer: false,
45
+ };
46
+ }
47
+
48
+ componentDidMount() {
49
+ /// this.app.stage.addChild(new PIXI.Sprite(PIXI.Texture.WHITE));
50
+ // console.log("this.app.stage", this.app.stage);
51
+ // set isMounted to true
52
+ // @ts-ignore
53
+ this.isMounted.current = true;
54
+ }
55
+
56
+ componentWillUnmount() {
57
+ // set isMounted to false
58
+ // @ts-ignore
59
+ this.isMounted.current = false;
60
+ }
61
+
62
+ /** handle on tranformer onchange */
63
+ handleOnTransformChange = () => {
64
+ this.setState({ isTransformerDragging: true });
65
+ };
66
+
67
+ // transformer to handle sprite transformation
68
+ handleOnTransformEnd = (endData: TransformationEnd) => {
69
+ console.log("changeEnd", endData);
70
+ const { onAnchorTransformationEnd } = this.props;
71
+ if (onAnchorTransformationEnd) {
72
+ console.log("running onAnchorTransformationEnd");
73
+ onAnchorTransformationEnd(endData);
74
+ }
75
+ };
76
+
77
+ // handle mouse over transformer state
78
+ setIsMouseOverTransformer = (isMouseOverTransformer: boolean) => {
79
+ this.setState({ isMouseOverTransformer });
80
+ };
81
+
82
+ render() {
83
+ const {
84
+ uniqueId,
85
+ initialAlpha,
86
+ transformation: { x, y, width, height, colorCorrection, effect },
87
+ applyTransformer,
88
+ } = this.props;
89
+
90
+ // color corrections
91
+ const {
92
+ contrast = 1,
93
+ saturation = 1,
94
+ exposure = 1,
95
+ alpha = 1,
96
+ blurRadius = 0,
97
+ } = colorCorrection || {};
98
+
99
+ /** adjustment filter */
100
+ const adjustments = {
101
+ brightness: exposure,
102
+ contrast,
103
+ saturation,
104
+ alpha,
105
+ };
106
+ return (
107
+ <Container ref={this.parentNode}>
108
+ <Container
109
+ ref={this.containerRef}
110
+ alpha={initialAlpha}
111
+ position={[x, y]}
112
+ pivot={[x, y]}
113
+ width={width}
114
+ height={height}
115
+ >
116
+ <Filters
117
+ scale={1}
118
+ blur={{ blur: blurRadius, quality: 4 }}
119
+ adjust={adjustments}
120
+ apply={({ matrix }: { matrix: any }) => {
121
+ if (effect === Effects.BlackAndWhite) {
122
+ matrix.desaturate();
123
+ } else if (effect === Effects.Sepia) {
124
+ matrix.sepia();
125
+ } else if (effect === Effects.RetroVintage) {
126
+ matrix.negative();
127
+ } else if (effect === Effects.NightVision) {
128
+ matrix.negative();
129
+ } else if (effect === Effects.Normal) {
130
+ matrix.reset();
131
+ }
132
+ }}
133
+ matrix={{
134
+ enabled: true,
135
+ // @ts-ignore
136
+ matrix: CYAN,
137
+ }}
138
+ >
139
+ <WrappedComponent {...this.props} />
140
+ </Filters>
141
+ </Container>
142
+ {applyTransformer && (
143
+ <PixiTransformer
144
+ pixiTransformerRef={this.transformerRef}
145
+ imageRef={this.containerRef}
146
+ isMounted={this.isMounted.current || false}
147
+ transformCommit={this.handleOnTransformEnd}
148
+ transformChange={this.handleOnTransformChange}
149
+ mouseoverEvent={this.setIsMouseOverTransformer}
150
+ uniqueId={uniqueId}
151
+ />
152
+ )}
153
+ </Container>
154
+ );
155
+ }
156
+ };
157
+ };
158
+
159
+ export default withFilterContainer;
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from "./components/PixiImageSprite";
1
+ export * from "./components";
2
2
  // Path: src\index.js
3
3
  export * from "./providers";
@@ -0,0 +1,41 @@
1
+ export enum Animations {
2
+ FADE_IN = "FADE_IN",
3
+ FADE_OUT = "FADE_OUT",
4
+ FADE_IN_OUT = "FADE_IN_OUT",
5
+ SLIDE_IN_LEFT = "SLIDE_IN_LEFT",
6
+ SLIDE_IN_RIGHT = "SLIDE_IN_RIGHT",
7
+ SLIDE_IN_TOP = "SLIDE_IN_TOP",
8
+ SLIDE_IN_BOTTOM = "SLIDE_IN_BOTTOM",
9
+ SLIDE_OUT_LEFT = "SLIDE_OUT_LEFT",
10
+ SLIDE_OUT_RIGHT = "SLIDE_OUT_RIGHT",
11
+ SLIDE_OUT_TOP = "SLIDE_OUT_TOP",
12
+ SLIDE_OUT_BOTTOM = "SLIDE_OUT_BOTTOM",
13
+ ZOOM_IN = "ZOOM_IN",
14
+ ZOOM_OUT = "ZOOM_OUT",
15
+ ZOOM_IN_OUT = "ZOOM_IN_OUT",
16
+ BOUNCE_IN = "BOUNCE_IN",
17
+ BOUNCE_OUT = "BOUNCE_OUT",
18
+ BOUNCE_IN_OUT = "BOUNCE_IN_OUT",
19
+ ELASTIC_IN = "ELASTIC_IN",
20
+ ELASTIC_OUT = "ELASTIC_OUT",
21
+ ELASTIC_IN_OUT = "ELASTIC_IN_OUT",
22
+ BACK_IN = "BACK_IN",
23
+ BACK_OUT = "BACK_OUT",
24
+ BACK_IN_OUT = "BACK_IN_OUT",
25
+ CIRCULAR_IN = "CIRCULAR_IN",
26
+ CIRCULAR_OUT = "CIRCULAR_OUT",
27
+ CIRCULAR_IN_OUT = "CIRCULAR_IN_OUT",
28
+ EXPO_IN = "EXPO_IN",
29
+ EXPO_OUT = "EXPO_OUT",
30
+ EXPO_IN_OUT = "EXPO_IN_OUT",
31
+ SINE_IN = "SINE_IN",
32
+ SINE_OUT = "SINE_OUT",
33
+ SINE_IN_OUT = "SINE_IN_OUT",
34
+ BOUNCE = "BOUNCE",
35
+ ELASTIC = "ELASTIC",
36
+ BACK = "BACK",
37
+ CIRCULAR = "CIRCULAR",
38
+ EXPO = "EXPO",
39
+ SINE = "SINE",
40
+ // Custom
41
+ }
@@ -0,0 +1,56 @@
1
+ import { Effects } from "./Effects";
2
+
3
+ export interface PixiBaseSpriteProps {
4
+ children?: React.ReactNode;
5
+ uniqueId: string;
6
+ src?: string;
7
+ path?: string;
8
+ text?: string;
9
+ startAt: number;
10
+ endAt: number;
11
+ initialAlpha: number;
12
+ transformation: {
13
+ x: number;
14
+ y: number;
15
+ width: number;
16
+ height: number;
17
+ anchor: number;
18
+ rotation?: number;
19
+ alpha?: number;
20
+ scale?: number | [number, number];
21
+ tint?: number;
22
+ blendMode?: number;
23
+ animation?: string;
24
+ colorCorrection?: {
25
+ enabled?: boolean;
26
+ temperature?: number;
27
+ hue?: number;
28
+ contrast?: number;
29
+ saturation?: number;
30
+ brightness?: number;
31
+ exposure?: number;
32
+ reset?: boolean;
33
+ sharpness?: number;
34
+ value?: number;
35
+ levels?: number;
36
+ luminance?: number;
37
+ enhance?: number;
38
+ blurRadius?: number;
39
+ red?: number;
40
+ green?: number;
41
+ blue?: number;
42
+ alpha?: number;
43
+ scaleInput?: number;
44
+ };
45
+ effect?: Effects;
46
+ };
47
+ pointerdown?: () => void;
48
+ pointerup?: () => void;
49
+ mousedown?: () => void;
50
+ mouseup?: () => void;
51
+ pointerover?: () => void;
52
+ mouseover?: () => void;
53
+ mouseout?: () => void;
54
+ applyTransformer?: boolean;
55
+ onAnchorTransformationEnd?: (endData: any) => void;
56
+ }
@@ -0,0 +1,19 @@
1
+ export enum BlendModes {
2
+ NORMAL = 0,
3
+ ADD = 1,
4
+ MULTIPLY = 2,
5
+ SCREEN = 3,
6
+ OVERLAY = 4,
7
+ DARKEN = 5,
8
+ LIGHTEN = 6,
9
+ COLOR_DODGE = 7,
10
+ COLOR_BURN = 8,
11
+ HARD_LIGHT = 9,
12
+ SOFT_LIGHT = 10,
13
+ DIFFERENCE = 11,
14
+ EXCLUSION = 12,
15
+ HUE = 13,
16
+ SATURATION = 14,
17
+ COLOR = 15,
18
+ LUMINOSITY = 16,
19
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Effects enum
3
+ */
4
+ export enum Effects {
5
+ BlackAndWhite = "BlackAndWhite",
6
+ Sepia = "Sepia",
7
+ RetroVintage = "RetroVintage",
8
+ NightVision = "NightVision",
9
+ Normal = "Normal",
10
+ None = "Normal",
11
+ }