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.
@@ -1,114 +1,27 @@
1
1
  import * as React from "react";
2
- import { useContext, useEffect, useRef, useState } from "react";
3
- // @ts-ignore
4
- import PropTypes from "prop-types";
5
- import { GsapPixieContext } from "../../providers/GsapPixieContextProvider";
6
- import { Sprite, Container, withFilters } from "@pixi/react";
7
- import { AdjustmentFilter } from "@pixi/filter-adjustment";
2
+ import { useRef } from "react";
3
+ import { Sprite, Container } from "@pixi/react";
8
4
  import * as PIXI from "pixi.js";
9
- import gsap from "gsap";
10
- import PixiTransformer from "../../utils/PixiTransformer";
11
-
12
5
  // @ts-ignore
13
6
  import isEmpty from "lodash/isEmpty";
14
- import { getAnimByName } from "../../utils/GsapAnim";
15
- import { TransformationEnd } from "../../types/transformation";
16
-
17
- type PixiImageSpriteProps = {
18
- uniqueId: string;
19
- src: string;
20
- startAt: number;
21
- endAt: number;
22
- initialAlpha: number;
23
- transformation: {
24
- x: number;
25
- y: number;
26
- width: number;
27
- height: number;
28
- anchor: number;
29
- rotation?: number;
30
- alpha?: number;
31
- scale?: number | [number, number];
32
- tint?: number;
33
- blendMode?: number;
34
- animation?: string;
35
- colorCorrection?: {
36
- enabled?: boolean;
37
- temperature?: number;
38
- hue?: number;
39
- contrast?: number;
40
- saturation?: number;
41
- exposure?: number;
42
- reset?: boolean;
43
- sharpness?: number;
44
- value?: number;
45
- levels?: number;
46
- luminance?: number;
47
- enhance?: number;
48
- blurRadius?: number;
49
- red?: number;
50
- green?: number;
51
- blue?: number;
52
- alpha?: number;
53
- scaleInput?: number;
54
- };
55
- effect?: string;
56
- };
57
- pointerdown?: () => void;
58
- pointerup?: () => void;
59
- mousedown?: () => void;
60
- mouseup?: () => void;
61
- pointerover?: () => void;
62
- mouseover?: () => void;
63
- mouseout?: () => void;
64
- applyTransformer?: boolean;
65
- onAnchorTransformationEnd?: (endData: any) => void;
66
- };
67
-
68
- const Filters = withFilters(Container, {
69
- blur: PIXI.filters.BlurFilter,
70
- adjust: AdjustmentFilter,
71
- matrix: PIXI.filters.ColorMatrixFilter,
72
- });
7
+ // setup hocs
8
+ import AbstractContainer from "../../hocs/AbstractContainer";
9
+ import { PixiBaseSpriteProps } from "../../types/BaseProps";
73
10
 
74
- /** CYAN Filters */
75
- const CYAN = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0];
76
-
77
- const PixiImageSprite: React.FC<PixiImageSpriteProps> = (props) => {
11
+ const PixiImageSprite: React.FC<PixiBaseSpriteProps> = (props) => {
78
12
  //// State
79
- const [isMounted, setIsMounted] = React.useState(false);
80
- const [, setIsTransformerDragging] = useState(false);
81
- const [, setIsMouseOverTransformer] = useState(false);
82
13
 
83
- console.log("allProps", props);
84
14
  //// Refs
85
15
  const imageRef = useRef<PIXI.Sprite>(null);
86
- const containerRef = useRef<PIXI.Container>(null);
87
- const parentNode = useRef<PIXI.Container>(null);
88
16
  const imgGroupRef = useRef<PIXI.Container>(null);
89
- const transformerRef = useRef<PIXI.Container>(null);
90
17
 
91
18
  //// Context
92
- const { tl } = useContext(GsapPixieContext);
93
19
 
94
20
  /// 1001
95
21
  // console.log("contxt Values", tl);
96
22
  const {
97
- uniqueId,
98
23
  src,
99
- startAt,
100
- endAt,
101
- initialAlpha,
102
- transformation: {
103
- x,
104
- y,
105
- width,
106
- height,
107
- anchor,
108
- animation,
109
- colorCorrection,
110
- effect,
111
- },
24
+ transformation: { x, y, width, height, anchor },
112
25
  pointerdown,
113
26
  pointerup,
114
27
  mousedown,
@@ -116,193 +29,31 @@ const PixiImageSprite: React.FC<PixiImageSpriteProps> = (props) => {
116
29
  pointerover,
117
30
  mouseover,
118
31
  mouseout,
119
- applyTransformer,
120
- onAnchorTransformationEnd,
121
32
  } = props;
122
33
 
123
- // color corrections
124
- const {
125
- contrast = 1,
126
- saturation = 1,
127
- exposure = 1,
128
- blurRadius = 0,
129
- alpha = 1,
130
- } = colorCorrection || {};
131
-
132
- /** adjustment filter */
133
- const adjustments = {
134
- brightness: exposure,
135
- contrast,
136
- saturation,
137
- alpha,
138
- };
139
-
140
- /** handle on tranformer onchange */
141
- const handleOnTransformChange = React.useCallback(() => {
142
- setIsTransformerDragging(true);
143
- }, []);
144
-
145
- // transformer to handle sprite transformation
146
- const handleOnTransformEnd = React.useCallback(
147
- (endData: TransformationEnd) => {
148
- console.log("changeEnd", endData);
149
- if (onAnchorTransformationEnd) {
150
- console.log("running onAnchorTransformationEnd");
151
- onAnchorTransformationEnd(endData);
152
- }
153
- },
154
- []
155
- );
156
-
157
- useEffect(() => {
158
- let ctx = gsap.context(() => {});
159
- if (containerRef.current && tl.current) {
160
- const data = {
161
- duration: Number(endAt) - Number(startAt),
162
- };
163
-
164
- const ease = getAnimByName(animation || "None");
165
- ctx = gsap.context(() => {
166
- if (!isEmpty(ease.from)) {
167
- tl.current
168
- .from(containerRef.current, { ...ease.from }, startAt)
169
- .to(imgGroupRef.current, { alpha: 1, duration: 0.2 }, startAt)
170
- .from(imgGroupRef.current, { ...data }, startAt)
171
- .to(containerRef.current, { alpha: 0, duration: 0.2 }, endAt - 0.2);
172
- } else if (!isEmpty(ease.to)) {
173
- tl.current
174
- .to(
175
- containerRef.current,
176
- { alpha: 1, duration: 0.2, ...ease.to },
177
- startAt
178
- )
179
- .from(imgGroupRef.current, { ...data }, startAt)
180
- .to(containerRef.current, { alpha: 0, duration: 0.2 }, endAt - 0.2);
181
- } else if (!isEmpty(ease.fromTo)) {
182
- tl.current
183
- .fromTo(
184
- containerRef.current,
185
- ease.fromTo?.from,
186
- ease.fromTo?.to,
187
- startAt
188
- )
189
- .from(imgGroupRef.current, { ...data }, startAt)
190
- .to(containerRef.current, { alpha: 0, duration: 0.2 }, endAt - 0.2);
191
- } else {
192
- tl.current
193
- .to(containerRef.current, { alpha: 1, duration: 0.01 }, startAt)
194
- .from(imgGroupRef.current, { ...data }, startAt)
195
- .to(
196
- containerRef.current,
197
- { alpha: 0, duration: 0.1 },
198
- Number(endAt) - Number(0.1)
199
- );
200
- }
201
- });
202
- }
203
- return () => ctx.revert(); // cleanup!
204
- }, [animation, startAt, endAt]);
205
-
206
- React.useEffect(() => {
207
- if (containerRef.current) {
208
- setIsMounted(true);
209
- }
210
- }, []);
211
-
212
34
  return (
213
- // @ts-ignore
214
- <Container ref={parentNode}>
215
- {/* @ts-ignore */}
216
- <Container
217
- ref={containerRef}
218
- alpha={initialAlpha}
219
- position={[x, y]}
220
- pivot={[x, y]}
221
- width={width}
222
- height={height}
223
- >
224
- {colorCorrection && colorCorrection.enabled ? (
225
- <Filters
226
- scale={1}
227
- blur={{ blur: blurRadius, quality: 4 }}
228
- adjust={adjustments}
229
- apply={({ matrix }: { matrix: any }) => {
230
- if (effect === "BlackAndWhite") {
231
- matrix.desaturate();
232
- } else if (effect === "Sepia") {
233
- matrix.sepia();
234
- } else if (effect === "RetroVintage") {
235
- matrix.negative();
236
- } else if (effect === "NightVision") {
237
- matrix.negative();
238
- } else if (effect === "Normal") {
239
- matrix.reset();
240
- }
241
- }}
242
- matrix={{
243
- enabled: true,
244
- // @ts-ignore
245
- matrix: CYAN,
246
- }}
247
- >
248
- {/* @ts-ignore */}
249
- <Container ref={imgGroupRef}>
250
- <Sprite
251
- image={src}
252
- width={width}
253
- height={height}
254
- anchor={anchor}
255
- ref={imageRef}
256
- x={x}
257
- y={y}
258
- // @ts-ignore
259
- interactive={true}
260
- pointerdown={pointerdown}
261
- pointerup={pointerup}
262
- pointerover={pointerover}
263
- mousedown={mousedown}
264
- mouseup={mouseup}
265
- mouseover={mouseover}
266
- mouseout={mouseout}
267
- />
268
- </Container>
269
- </Filters>
270
- ) : (
35
+ <AbstractContainer {...props}>
36
+ <Container ref={imgGroupRef}>
37
+ <Sprite
38
+ image={src}
39
+ width={width}
40
+ height={height}
41
+ anchor={anchor}
42
+ ref={imageRef}
43
+ x={x}
44
+ y={y}
271
45
  // @ts-ignore
272
- <Container ref={imgGroupRef}>
273
- <Sprite
274
- image={src}
275
- width={width}
276
- height={height}
277
- anchor={anchor}
278
- ref={imageRef}
279
- x={x}
280
- y={y}
281
- // @ts-ignore
282
- interactive={true}
283
- pointerdown={pointerdown}
284
- pointerup={pointerup}
285
- pointerover={pointerover}
286
- mousedown={mousedown}
287
- mouseup={mouseup}
288
- mouseover={mouseover}
289
- mouseout={mouseout}
290
- />
291
- </Container>
292
- )}
293
- </Container>
294
- {applyTransformer && (
295
- <PixiTransformer
296
- pixiTransformerRef={transformerRef}
297
- imageRef={containerRef}
298
- isMounted={isMounted}
299
- transformCommit={handleOnTransformEnd}
300
- transformChange={handleOnTransformChange}
301
- mouseoverEvent={setIsMouseOverTransformer}
302
- uniqueId={uniqueId}
46
+ interactive={true}
47
+ pointerdown={pointerdown}
48
+ pointerup={pointerup}
49
+ pointerover={pointerover}
50
+ mousedown={mousedown}
51
+ mouseup={mouseup}
52
+ mouseover={mouseover}
53
+ mouseout={mouseout}
303
54
  />
304
- )}
305
- </Container>
55
+ </Container>
56
+ </AbstractContainer>
306
57
  );
307
58
  };
308
59
 
@@ -84,11 +84,14 @@ export const Normal: Story = {
84
84
  args: {
85
85
  uniqueId: "svg001", // uniqueId of the sprite
86
86
  path: "M 100 100 L 300 100 L 200 300 z",
87
+ applyTransformer: false,
88
+ startAt: 0,
89
+ endAt: 10,
87
90
  transformation: {
88
- x: 100,
89
- y: 100,
90
- width: 150,
91
- height: 150,
91
+ x: 200,
92
+ y: 200,
93
+ width: 200,
94
+ height: 200,
92
95
  anchor: 0.5,
93
96
  rotation: 0,
94
97
  alpha: 1,
@@ -98,9 +101,6 @@ export const Normal: Story = {
98
101
  colorCorrection: {},
99
102
  fill: "#FFC0CB",
100
103
  },
101
- applyTransformer: false,
102
- startAt: 0,
103
- endAt: 10,
104
104
  initialAlpha: 1,
105
105
  },
106
106
  };
@@ -114,11 +114,14 @@ export const FadeIn: Story = {
114
114
  args: {
115
115
  uniqueId: "surya002", // uniqueId of the sprite
116
116
  path: "M 100, 100 m -75, 0 a 75,75 0 1,0 150,0 a 75,75 0 1,0 -150,0",
117
+ applyTransformer: false,
118
+ startAt: 0,
119
+ endAt: 10,
117
120
  transformation: {
118
- x: 100,
119
- y: 100,
120
- width: 150,
121
- height: 150,
121
+ x: 200,
122
+ y: 200,
123
+ width: 200,
124
+ height: 200,
122
125
  anchor: 0.5,
123
126
  rotation: 0,
124
127
  alpha: 1,
@@ -129,9 +132,43 @@ export const FadeIn: Story = {
129
132
  colorCorrection: {},
130
133
  fill: "#fe2c54",
131
134
  },
135
+ initialAlpha: 0,
136
+ },
137
+ };
138
+
139
+ // More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args
140
+ export const Filters: Story = {
141
+ render: (args: any) => (
142
+ <AppStateContextProvider {...args}>
143
+ <PixiSvgPathSprite {...args} />
144
+ </AppStateContextProvider>
145
+ ),
146
+ args: {
147
+ uniqueId: "svgfilters001", // uniqueId of the sprite
148
+ path: "M 100 100 L 300 100 L 200 300 z",
132
149
  applyTransformer: false,
133
150
  startAt: 0,
134
151
  endAt: 10,
135
- initialAlpha: 0,
152
+ transformation: {
153
+ x: 200,
154
+ y: 200,
155
+ width: 200,
156
+ height: 200,
157
+ anchor: 0.5,
158
+ rotation: 0,
159
+ alpha: 1,
160
+ scale: 1,
161
+ tint: 0xffffff,
162
+ blendMode: 0,
163
+ colorCorrection: {
164
+ enabled: true,
165
+ contrast: 1,
166
+ saturation: 2,
167
+ exposure: 1,
168
+ blurRadius: 3,
169
+ },
170
+ fill: "#FFC0CB",
171
+ },
172
+ initialAlpha: 1,
136
173
  },
137
174
  };