mirror of
https://github.com/rive-app/rive-react.git
synced 2026-03-13 08:22:30 +08:00
Compare commits
1 Commits
v4.18.2
...
improvemen
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c3a061f8b0 |
10
README.md
10
README.md
@@ -121,15 +121,19 @@ function Example() {
|
||||
autoplay: true,
|
||||
});
|
||||
|
||||
const onClickInput = useStateMachineInput(rive, 'button', 'onClick');
|
||||
const onClickInput = useStateMachineInput({
|
||||
rive: rive,
|
||||
stateMachineName: 'button',
|
||||
inputName: 'onClick',
|
||||
});
|
||||
|
||||
return <RiveComponent onClick={() => onClickInput && onClickInput.fire())} />;
|
||||
return <RiveComponent onClick={() => onClickInput && onClickInput.fire()} />;
|
||||
}
|
||||
|
||||
export default Example;
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
#### params
|
||||
|
||||
- `rive`: A `Rive` object. This is returned by the `useRive` hook.
|
||||
- `stateMachineName`: Name of the state machine.
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"react": "^17.0.2",
|
||||
"react-dom": "^17.0.2",
|
||||
"react-scripts": "4.0.3",
|
||||
"rive-react": "0.0.1",
|
||||
"rive-react": "file:../..",
|
||||
"web-vitals": "^1.1.2"
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import Rive from "rive-react";
|
||||
import Rive from 'rive-react';
|
||||
|
||||
function App() {
|
||||
return (
|
||||
// The animation will fit to the parent element.
|
||||
<div style={{ height: "500px", width: "500px" }}>
|
||||
<div style={{ height: '500px', width: '500px' }}>
|
||||
<Rive src="poison-loader.riv" autoplay />
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -6,7 +6,7 @@ import React, {
|
||||
ComponentProps,
|
||||
RefCallback,
|
||||
} from 'react';
|
||||
import { Rive, EventType } from 'rive-js';
|
||||
import { Rive, EventType, StateMachineInputType } from 'rive-js';
|
||||
import {
|
||||
UseRiveParameters,
|
||||
UseRiveOptions,
|
||||
@@ -228,7 +228,34 @@ export default function useRive(
|
||||
);
|
||||
}, []);
|
||||
|
||||
const debug = useCallback(() => {
|
||||
if (!rive) {
|
||||
console.log({
|
||||
loaded: false,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const stateMachines = rive.stateMachineNames.map((stateMachineName) => ({
|
||||
stateMachineName,
|
||||
stateMachineInputs: (rive.stateMachineInputs(stateMachineName) ?? []).map(
|
||||
(stateMachineInput) => ({
|
||||
name: stateMachineInput.name,
|
||||
value: stateMachineInput.value,
|
||||
type: StateMachineInputType[stateMachineInput.type],
|
||||
})
|
||||
),
|
||||
}));
|
||||
|
||||
console.log({
|
||||
activeArtboard: rive.activeArtboard,
|
||||
animationsNames: rive.animationNames,
|
||||
stateMachines,
|
||||
});
|
||||
}, [rive]);
|
||||
|
||||
return {
|
||||
debug,
|
||||
canvas: canvasRef.current,
|
||||
setCanvasRef,
|
||||
setContainerRef,
|
||||
|
||||
@@ -1,28 +1,29 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Rive, StateMachineInput } from 'rive-js';
|
||||
import { UseStateMachineInputParameters } from '../types';
|
||||
|
||||
/**
|
||||
* Custom hook for fetching a stateMachine input from a rive file.
|
||||
*
|
||||
* @param rive - Rive instance
|
||||
* @param stateMachineName - Name of the state machine
|
||||
* @param inputName - Name of the input
|
||||
* @param params.rive - Rive Instance
|
||||
* @param params.stateMachineName - Name of the state machine
|
||||
* @param params.inputName - Name of the state machine input
|
||||
* @returns
|
||||
*/
|
||||
export default function useStateMachineInput(
|
||||
rive: Rive | null,
|
||||
stateMachineName?: string,
|
||||
inputName?: string
|
||||
params: UseStateMachineInputParameters
|
||||
) {
|
||||
const [input, setInput] = useState<StateMachineInput | null>(null);
|
||||
|
||||
const { rive, stateMachineName, inputName } = params;
|
||||
|
||||
useEffect(() => {
|
||||
if (!rive || !stateMachineName || !inputName) {
|
||||
setInput(null);
|
||||
}
|
||||
|
||||
if (rive && stateMachineName && inputName) {
|
||||
const inputs = rive.stateMachineInputs(stateMachineName);
|
||||
const inputs = (rive as Rive).stateMachineInputs(stateMachineName);
|
||||
if (inputs) {
|
||||
const selectedInput = inputs.find((input) => input.name === inputName);
|
||||
setInput(selectedInput || null);
|
||||
|
||||
13
src/types.ts
13
src/types.ts
@@ -1,6 +1,18 @@
|
||||
import { RefCallback, ComponentProps } from 'react';
|
||||
import { Rive, RiveParameters } from 'rive-js';
|
||||
|
||||
/**
|
||||
* @typedef UseStateMachineInputParameters
|
||||
* @property rive - Rive Instance
|
||||
* @property stateMachineName - Name of the state machine
|
||||
* @property inputName - Name of the input
|
||||
*/
|
||||
export type UseStateMachineInputParameters = {
|
||||
rive: Rive | null;
|
||||
stateMachineName?: string;
|
||||
inputName?: string;
|
||||
};
|
||||
|
||||
export type UseRiveParameters = Partial<Omit<RiveParameters, 'canvas'>> | null;
|
||||
|
||||
export type UseRiveOptions = {
|
||||
@@ -24,6 +36,7 @@ export type Dimensions = {
|
||||
* @property rive - The loaded Rive Animation
|
||||
*/
|
||||
export type RiveState = {
|
||||
debug: () => void;
|
||||
canvas: HTMLCanvasElement | null;
|
||||
setCanvasRef: RefCallback<HTMLCanvasElement>;
|
||||
setContainerRef: RefCallback<HTMLElement>;
|
||||
|
||||
Reference in New Issue
Block a user