react-two.js 0.8.22 → 0.8.23
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/README.md +74 -3
- package/dist/Context.d.ts +12 -5
- package/dist/Image.d.ts +1 -0
- package/dist/ImageSequence.d.ts +1 -1
- package/dist/Provider.d.ts +2 -0
- package/dist/SVG.d.ts +22 -0
- package/dist/Sprite.d.ts +1 -1
- package/dist/Text.d.ts +1 -1
- package/dist/Texture.d.ts +2 -2
- package/dist/main.d.ts +1 -0
- package/dist/react-two-main.es.js +831 -1096
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -67,8 +67,8 @@ npm install react-two.js react react-dom two.js
|
|
|
67
67
|
```
|
|
68
68
|
|
|
69
69
|
**Requirements** as peer dependencies:
|
|
70
|
-
- React
|
|
71
|
-
- Two.js v0.8.
|
|
70
|
+
- React 19+
|
|
71
|
+
- Two.js v0.8.22+
|
|
72
72
|
|
|
73
73
|
> [!IMPORTANT]
|
|
74
74
|
> react-two.js is a React renderer, it must pair with a major version of React, like react-dom.
|
|
@@ -158,10 +158,11 @@ function Component() {
|
|
|
158
158
|
const { two, width, height } = useTwo()
|
|
159
159
|
|
|
160
160
|
useEffect(() => {
|
|
161
|
+
if (!two) return;
|
|
161
162
|
two.play();
|
|
162
163
|
console.log('Canvas size:', width, height)
|
|
163
164
|
console.log('Two.js instance:', instance)
|
|
164
|
-
}, [])
|
|
165
|
+
}, [two])
|
|
165
166
|
}
|
|
166
167
|
```
|
|
167
168
|
|
|
@@ -189,6 +190,7 @@ function Component() {
|
|
|
189
190
|
- **`<Text>`** — Text rendering
|
|
190
191
|
|
|
191
192
|
#### Advanced
|
|
193
|
+
- **`<SVG>`** — Load and interpret SVG files or inline SVG markup
|
|
192
194
|
- **`<Image>`** - Basic image class inspired by Figma
|
|
193
195
|
- **`<Sprite>`** — Animated sprite sheets
|
|
194
196
|
- **`<ImageSequence>`** — Animated image sequence
|
|
@@ -307,6 +309,75 @@ function () {
|
|
|
307
309
|
}
|
|
308
310
|
```
|
|
309
311
|
|
|
312
|
+
### Loading SVG Files
|
|
313
|
+
|
|
314
|
+
Load external SVG files or use inline SVG markup with the `<SVG>` component:
|
|
315
|
+
|
|
316
|
+
```jsx
|
|
317
|
+
import { SVG } from 'react-two.js'
|
|
318
|
+
|
|
319
|
+
// Load from external URL
|
|
320
|
+
function Logo() {
|
|
321
|
+
return (
|
|
322
|
+
<SVG
|
|
323
|
+
src="/assets/logo.svg"
|
|
324
|
+
x={100}
|
|
325
|
+
y={100}
|
|
326
|
+
onLoad={(group, svg) => {
|
|
327
|
+
console.log('SVG loaded with', group.children.length, 'objects')
|
|
328
|
+
}}
|
|
329
|
+
onError={(error) => {
|
|
330
|
+
console.error('Failed to load SVG:', error)
|
|
331
|
+
}}
|
|
332
|
+
/>
|
|
333
|
+
)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Use inline SVG markup
|
|
337
|
+
function Icon() {
|
|
338
|
+
return (
|
|
339
|
+
<SVG
|
|
340
|
+
content={`
|
|
341
|
+
<svg viewBox="0 0 100 100">
|
|
342
|
+
<circle cx="50" cy="50" r="40" fill="#FF6B6B" />
|
|
343
|
+
<circle cx="35" cy="40" r="8" fill="white" />
|
|
344
|
+
<circle cx="65" cy="40" r="8" fill="white" />
|
|
345
|
+
</svg>
|
|
346
|
+
`}
|
|
347
|
+
x={200}
|
|
348
|
+
y={200}
|
|
349
|
+
scale={0.5}
|
|
350
|
+
/>
|
|
351
|
+
)
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Animate loaded SVG
|
|
355
|
+
function AnimatedIcon() {
|
|
356
|
+
const svgRef = useRef()
|
|
357
|
+
|
|
358
|
+
useFrame((elapsed) => {
|
|
359
|
+
if (svgRef.current) {
|
|
360
|
+
svgRef.current.rotation = Math.sin(elapsed) * 0.5
|
|
361
|
+
svgRef.current.scale = 1 + Math.sin(elapsed * 2) * 0.1
|
|
362
|
+
}
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
return <SVG ref={svgRef} src="/icon.svg" x={400} y={300} />
|
|
366
|
+
}
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
**SVG Props:**
|
|
370
|
+
- `src` — URL to external .svg file
|
|
371
|
+
- `content` — Inline SVG markup string
|
|
372
|
+
- `x`, `y` — Position
|
|
373
|
+
- `scale`, `rotation` — Transform properties
|
|
374
|
+
- `onLoad(group, svg)` — Callback when SVG loads successfully
|
|
375
|
+
- `onError(error)` — Callback when loading fails
|
|
376
|
+
- All Two.js Group properties (fill, stroke, opacity, etc.)
|
|
377
|
+
|
|
378
|
+
> [!NOTE]
|
|
379
|
+
> The SVG component uses Two.js's `load()` method which supports a subset of SVG 1.1 features. Complex SVG features like filters, animations (SMIL), and some advanced elements may not be fully supported. Refer to [Two.js SVG documentation](https://two.js.org/docs/two/#two-interpret) for details on supported features.
|
|
380
|
+
|
|
310
381
|
## Learn More
|
|
311
382
|
|
|
312
383
|
- **[Two.js Documentation](https://two.js.org/docs/)** — Complete Two.js API reference
|
package/dist/Context.d.ts
CHANGED
|
@@ -2,14 +2,21 @@ import Two from 'two.js';
|
|
|
2
2
|
import type { Group } from 'two.js/src/group';
|
|
3
3
|
import type { Shape } from 'two.js/src/shape';
|
|
4
4
|
import type { EventHandlers } from './Events';
|
|
5
|
-
export interface
|
|
5
|
+
export interface TwoCoreContextValue {
|
|
6
6
|
two: Two | null;
|
|
7
|
+
registerEventShape: (shape: Shape | Group, handlers: Partial<EventHandlers>, parent?: Group) => void;
|
|
8
|
+
unregisterEventShape: (shape: Shape | Group) => void;
|
|
9
|
+
}
|
|
10
|
+
export interface TwoParentContextValue {
|
|
7
11
|
parent: Group | null;
|
|
12
|
+
}
|
|
13
|
+
export interface TwoSizeContextValue {
|
|
8
14
|
width: number;
|
|
9
15
|
height: number;
|
|
10
|
-
registerEventShape: (shape: Shape | Group, handlers: Partial<EventHandlers>, parent?: Group) => void;
|
|
11
|
-
unregisterEventShape: (shape: Shape | Group) => void;
|
|
12
16
|
}
|
|
13
|
-
export declare const
|
|
14
|
-
export declare const
|
|
17
|
+
export declare const TwoCoreContext: import("react").Context<TwoCoreContextValue>;
|
|
18
|
+
export declare const TwoParentContext: import("react").Context<TwoParentContextValue>;
|
|
19
|
+
export declare const TwoSizeContext: import("react").Context<TwoSizeContextValue>;
|
|
20
|
+
export declare const Context: import("react").Context<TwoCoreContextValue>;
|
|
21
|
+
export declare const useTwo: () => TwoCoreContextValue & TwoParentContextValue & TwoSizeContextValue;
|
|
15
22
|
export declare const useFrame: (callback: (elapsed: number, frameDelta: number) => void, deps?: unknown[]) => void;
|
package/dist/Image.d.ts
CHANGED
package/dist/ImageSequence.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ export declare const ImageSequence: React.ForwardRefExoticComponent<{
|
|
|
42
42
|
frameRate?: number | undefined;
|
|
43
43
|
index?: number | undefined;
|
|
44
44
|
} & {
|
|
45
|
-
|
|
45
|
+
src?: string | string[] | Texture | Texture[];
|
|
46
46
|
x?: number;
|
|
47
47
|
y?: number;
|
|
48
48
|
autoPlay?: boolean;
|
package/dist/Provider.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ type TwoConstructorProps = ConstructorParameters<typeof Two>[0];
|
|
|
4
4
|
type TwoConstructorPropsKeys = NonNullable<TwoConstructorProps>;
|
|
5
5
|
type ComponentProps = React.PropsWithChildren<TwoConstructorPropsKeys & {
|
|
6
6
|
onPointerMissed?: (event: PointerEvent) => void;
|
|
7
|
+
} & {
|
|
8
|
+
container?: React.ComponentProps<'div'>;
|
|
7
9
|
}>;
|
|
8
10
|
export declare const Provider: React.FC<ComponentProps>;
|
|
9
11
|
export {};
|
package/dist/SVG.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { Group as Instance } from 'two.js/src/group';
|
|
3
|
+
import { ShapeProps, type EventHandlers } from './Properties';
|
|
4
|
+
type GroupProps = ShapeProps | 'fill' | 'stroke' | 'linewidth' | 'cap' | 'join' | 'miter' | 'closed' | 'curved' | 'automatic';
|
|
5
|
+
type ComponentProps = React.PropsWithChildren<{
|
|
6
|
+
[K in Extract<GroupProps, keyof Instance>]?: Instance[K];
|
|
7
|
+
} & ({
|
|
8
|
+
src: string;
|
|
9
|
+
content?: never;
|
|
10
|
+
} | {
|
|
11
|
+
src?: never;
|
|
12
|
+
content: string;
|
|
13
|
+
}) & {
|
|
14
|
+
x?: number;
|
|
15
|
+
y?: number;
|
|
16
|
+
onLoad?: (group: Instance, svg: SVGElement | SVGElement[]) => void;
|
|
17
|
+
onError?: (error: Error) => void;
|
|
18
|
+
shallow?: boolean;
|
|
19
|
+
} & Partial<EventHandlers>>;
|
|
20
|
+
export type RefSVG = Instance;
|
|
21
|
+
export declare const SVG: React.ForwardRefExoticComponent<ComponentProps & React.RefAttributes<Instance>>;
|
|
22
|
+
export {};
|
package/dist/Sprite.d.ts
CHANGED
package/dist/Text.d.ts
CHANGED
|
@@ -28,10 +28,10 @@ export declare const Text: React.ForwardRefExoticComponent<{
|
|
|
28
28
|
offset?: number;
|
|
29
29
|
}) | undefined;
|
|
30
30
|
size?: number | undefined;
|
|
31
|
+
style?: import("two.js/src/text").StyleProperties | undefined;
|
|
31
32
|
family?: string | undefined;
|
|
32
33
|
leading?: number | undefined;
|
|
33
34
|
alignment?: import("two.js/src/text").AlignmentProperties | undefined;
|
|
34
|
-
style?: import("two.js/src/text").StyleProperties | undefined;
|
|
35
35
|
weight?: string | number | undefined;
|
|
36
36
|
decoration?: import("two.js/src/text").DecorationProperties | undefined;
|
|
37
37
|
direction?: import("two.js/src/text").DirectionProperties | undefined;
|
package/dist/Texture.d.ts
CHANGED
|
@@ -14,11 +14,11 @@ export declare const Texture: React.ForwardRefExoticComponent<{
|
|
|
14
14
|
className?: string | undefined;
|
|
15
15
|
repeat?: import("two.js/src/effects/texture").RepeatProperties | undefined;
|
|
16
16
|
src?: string | undefined;
|
|
17
|
+
image?: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | undefined;
|
|
17
18
|
loaded?: boolean | undefined;
|
|
18
19
|
offset?: import("two.js/src/vector").Vector | undefined;
|
|
19
|
-
image?: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | undefined;
|
|
20
20
|
} & {
|
|
21
|
-
|
|
21
|
+
src?: string | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement;
|
|
22
22
|
} & {
|
|
23
23
|
children?: React.ReactNode | undefined;
|
|
24
24
|
} & React.RefAttributes<Instance>>;
|
package/dist/main.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { Provider as Canvas } from './Provider';
|
|
2
2
|
export { Context, useTwo, useFrame } from './Context';
|
|
3
3
|
export { Group, type RefGroup } from './Group';
|
|
4
|
+
export { SVG, type RefSVG } from './SVG';
|
|
4
5
|
export { Path, type RefPath } from './Path';
|
|
5
6
|
export { Points, type RefPoints } from './Points';
|
|
6
7
|
export { Text, type RefText } from './Text';
|