-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f6ae83
commit 5e80ebf
Showing
8 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
canvas { | ||
width: 500px!important; | ||
height: 500px!important; | ||
} |
18 changes: 18 additions & 0 deletions
18
examples/stories/components/bareBonesRiveCanvas/Example.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import BareBonesRiveCanvas from './Example'; | ||
|
||
const meta = { | ||
title: 'Components/BareBonesRiveCanvas', | ||
component: BareBonesRiveCanvas, | ||
argTypes: { | ||
backgroundColor: { control: 'color' }, | ||
}, | ||
} satisfies Meta<typeof BareBonesRiveCanvas>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Primary: Story = { | ||
args: {}, | ||
}; |
102 changes: 102 additions & 0 deletions
102
examples/stories/components/bareBonesRiveCanvas/Example.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import React, { useState, useRef, useEffect } from 'react'; | ||
|
||
import Bird from './components/Bird'; | ||
import Tree from './components/Tree'; | ||
|
||
export default function App() { | ||
return ( | ||
<div className="flex size-full justify-center items-center gap-4"> | ||
<RiveTest /> | ||
<StaticTest /> | ||
</div> | ||
); | ||
} | ||
|
||
function StaticTest() { | ||
const [counter, setCounter] = useState<number>(0); | ||
|
||
const [running, setRunning] = useState<boolean>(false); | ||
const intervalRef = useRef<number>(); | ||
useEffect(() => { | ||
if (running) { | ||
intervalRef.current = setInterval(() => { | ||
setCounter((prev) => prev + 1); | ||
}, 100); | ||
} else { | ||
clearInterval(intervalRef.current); | ||
} | ||
}, [running]); | ||
|
||
return ( | ||
<div className="size-full flex items-center justify-center flex-col gap-4"> | ||
<h1>Static</h1> | ||
<Button onClick={() => setRunning((prev) => !prev)}> | ||
{running ? 'remounting...' : 'Start'} | ||
</Button> | ||
<FakeAsset id="π²" key={counter + 'tree'} /> | ||
<FakeAsset id="π" key={counter + 'bird'} /> | ||
</div> | ||
); | ||
} | ||
|
||
function RiveTest() { | ||
const [counter, setCounter] = useState<number>(0); | ||
|
||
const [running, setRunning] = useState<boolean>(false); | ||
const intervalRef = useRef<number>(); | ||
useEffect(() => { | ||
if (running) { | ||
intervalRef.current = setInterval(() => { | ||
setCounter((prev) => prev + 1); | ||
}, 100); | ||
} else { | ||
clearInterval(intervalRef.current); | ||
} | ||
}, [running]); | ||
|
||
return ( | ||
<div className="size-full flex items-center justify-center flex-col gap-4"> | ||
<h1>Rive</h1> | ||
<Button onClick={() => setRunning((prev) => !prev)}> | ||
{running ? 'remounting...' : 'Start'} | ||
</Button> | ||
<Tree key={counter + 'tree'} /> | ||
<Bird key={counter + 'bird'} /> | ||
</div> | ||
); | ||
} | ||
|
||
const useId = () => { | ||
// random id | ||
return Math.random().toString(36).substring(7); | ||
}; | ||
|
||
function FakeAsset({ id }: { id: string; key: string }) { | ||
const internalId = useId(); | ||
useEffect(() => { | ||
console.log(`[${internalId}] mount static ${id}`); | ||
return () => { | ||
console.log(`[${internalId}] unmount static ${id}`); | ||
}; | ||
}, [id, internalId]); | ||
|
||
return ( | ||
<div | ||
id={`Static-${id}`} | ||
className="size-[100px] ring-1 text-2xl flex items-center justify-center" | ||
> | ||
{id} | ||
</div> | ||
); | ||
} | ||
|
||
// --- | ||
type ButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement>; | ||
|
||
function Button({ children, ...props }: ButtonProps) { | ||
return ( | ||
<button className="ring-1 p-4" {...props}> | ||
{children} | ||
</button> | ||
); | ||
} |
24 changes: 24 additions & 0 deletions
24
examples/stories/components/bareBonesRiveCanvas/components/Bird.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useRive } from '../../../../../src'; | ||
import React, { useEffect } from 'react'; | ||
|
||
export default function Bird({}) { | ||
const { RiveComponent } = useRive({ | ||
stateMachines: 'State Machine 1', | ||
artboard: 'bird', | ||
src: 'map-accessories.riv', | ||
autoplay: true, | ||
}); | ||
|
||
useEffect(() => { | ||
// console.log(`mount rive π`); | ||
return () => { | ||
// console.log(`unmount rive π`); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div id="Rive-π" className="size-[100px] ring-1"> | ||
<RiveComponent /> | ||
</div> | ||
); | ||
} |
25 changes: 25 additions & 0 deletions
25
examples/stories/components/bareBonesRiveCanvas/components/Tree.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useRive } from '../../../../../src'; | ||
|
||
import React, { useEffect } from 'react'; | ||
|
||
export default function Tree({}) { | ||
const { RiveComponent } = useRive({ | ||
stateMachines: 'State Machine 1', | ||
artboard: 'treeComponent', | ||
src: 'map-accessories.riv', | ||
autoplay: true, | ||
}); | ||
|
||
useEffect(() => { | ||
// console.log(`mount rive π²`); | ||
return () => { | ||
// console.log(`unmount rive π²`); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div id="Rive-π²" className="size-[100px] ring-1"> | ||
<RiveComponent /> | ||
</div> | ||
); | ||
} |
Binary file added
BIN
+1.76 MB
examples/stories/components/bareBonesRiveCanvas/rive/map-accessories.riv
Binary file not shown.