Gathering detailed insights and metrics for @anacoelhovicente/react-spline
Gathering detailed insights and metrics for @anacoelhovicente/react-spline
Gathering detailed insights and metrics for @anacoelhovicente/react-spline
Gathering detailed insights and metrics for @anacoelhovicente/react-spline
npm install @anacoelhovicente/react-spline
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
4
4
react-spline allows you to export and use Spline scenes directly in your React websites.
🌈 Spline is a friendly 3d collaborative design tool for the web.
Website — Twitter — Community — Documentation
1yarn add @splinetool/react-spline @splinetool/runtime
or
1npm install @splinetool/react-spline @splinetool/runtime
To use react-spline, first you have to go to the Spline editor, click on the Export button, select "Code" and then "React".
You should see this:
You can copy the URL and pass it to the <Spline />
component in react:
1import Spline from '@splinetool/react-spline'; 2 3export default function App() { 4 return ( 5 <div> 6 <Spline scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode" /> 7 </div> 8 ); 9}
You should be able to see the scene you exported in your React app.
NOTE: If you are experiencing CORS issues, you can download the .splinecode file and self-host it; this will fix any CORS issue. To download, go to Spline's code export panel and click on the download icon visible in the prod.spline textarea.
You can use this library in Next.js as well to take advantage of Server Side Rendering. By default the library will render on the client only, but if you use the import @splinetool/react/next
the server will render an autogenerated blurred placeholder.
Here is an example. Export as Next.js
from the Spline editor to autogenerate the placeholder.
1import Spline from '@splinetool/react-spline/next'; 2 3export default function App() { 4 return ( 5 <div> 6 <Spline scene="https://prod.spline.design/KFonZGtsoUXP-qx7/scene.splinecode" /> 7 </div> 8 ); 9}
You can query any Spline object via findObjectByName
or findObjectById
.
(You can get the ID of the object by right-clicking on it and selecting Copy Development Object ID
).
1import { useRef } from 'react'; 2import Spline from '@splinetool/react-spline'; 3 4export default function App() { 5 const cube = useRef(); 6 7 function onLoad(spline) { 8 const obj = spline.findObjectByName('Cube'); 9 // or 10 // const obj = spline.findObjectById('8E8C2DDD-18B6-4C54-861D-7ED2519DE20E'); 11 12 // save it in a ref for later use 13 cube.current = obj; 14 } 15 16 function moveObj() { 17 console.log(cube.current); // Spline Object => { name: 'Cube', id: '8E8C2DDD-18B6-4C54-861D-7ED2519DE20E', position: {}, ... } 18 19 // move the object in 3D space 20 cube.current.position.x += 10; 21 } 22 23 return ( 24 <div> 25 <Spline 26 scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode" 27 onLoad={onLoad} 28 /> 29 <button type="button" onClick={moveObj}> 30 Move Cube 31 </button> 32 </div> 33 ); 34}
You can listen to any Spline Event you set in the Events panel of the editor by attaching a listener to the Spline component.
1import Spline from '@splinetool/react-spline'; 2 3export default function App() { 4 function onSplineMouseDown(e) { 5 if (e.target.name === 'Cube') { 6 console.log('I have been clicked!'); 7 } 8 } 9 10 return ( 11 <div> 12 <Spline 13 scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode" 14 onSplineMouseDown={onSplineMouseDown} 15 /> 16 </div> 17 ); 18}
You can find a list of all of the Spline Event listeners in the Spline Component Props section.
You can trigger any animation Event you set in the Events panel in the Spline Editor.
You can use the emitEvent
function via the spline ref, passing the event type and the ID of your object.
(You can get the ID of the object in the Develop
pane of the right sidebar).
1import { useRef } from 'react'; 2import Spline from '@splinetool/react-spline'; 3 4export default function App() { 5 const spline = useRef(); 6 7 function onLoad(splineApp) { 8 // save the app in a ref for later use 9 spline.current = splineApp; 10 } 11 12 function triggerAnimation() { 13 spline.current.emitEvent('mouseHover', 'Cube'); 14 } 15 16 return ( 17 <div> 18 <Spline 19 scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode" 20 onLoad={onLoad} 21 /> 22 <button type="button" onClick={triggerAnimation}> 23 Trigger Spline Animation 24 </button> 25 </div> 26 ); 27}
Or you can query the spline object first, and then trigger the event:
1import { useRef } from 'react'; 2import Spline from '@splinetool/react-spline'; 3 4export default function App() { 5 const objectToAnimate = useRef(); 6 7 function onLoad(spline) { 8 const obj = spline.findObjectByName('Cube'); 9 // save the object in a ref for later use 10 objectToAnimate.current = obj; 11 } 12 13 function triggerAnimation() { 14 objectToAnimate.current.emitEvent('mouseHover'); 15 } 16 17 return ( 18 <div> 19 <Spline 20 scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode" 21 onLoad={onLoad} 22 /> 23 <button type="button" onClick={triggerAnimation}> 24 Trigger Spline Animation 25 </button> 26 </div> 27 ); 28}
You can find a list of all of the Spline Events you can pass to the emitEvent
function in the Spline Events section.
To start loading react-spline after the whole website has finished loading, we can use lazy-loading. This technique can be achieved using React.lazy()
in combination with dynamic imports:
1import React, { Suspense } from 'react'; 2 3const Spline = React.lazy(() => import('@splinetool/react-spline')); 4 5export default function App() { 6 return ( 7 <div> 8 <Suspense fallback={<div>Loading...</div>}> 9 <Spline scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode" /> 10 </Suspense> 11 </div> 12 ); 13}
More info in the relative React documentation.
These are all the props you can pass to the <Spline />
component.
Name | Type | Description |
---|---|---|
scene | string | Scene file |
onLoad? | (spline: Application) => void | Gets called once the scene has loaded. The spline parameter is an instance of the Spline Application |
renderOnDemand? | boolean | Wether or not to enable on demand rendering. Default true . |
className? | string | CSS classes |
style? | object | CSS style |
id? | string | Canvas id |
ref? | React.Ref<HTMLDivElement> | A ref pointing to div container element. |
onSplineMouseDown? | (e: SplineEvent) => void | Gets called once a Spline Mouse Down event is fired |
onSplineMouseHover? | (e: SplineEvent) => void | Gets called once a Spline Mouse Hover event is fired |
onSplineMouseUp? | (e: SplineEvent) => void | Gets called once a Spline Mouse Up event is fired |
onSplineKeyDown? | (e: SplineEvent) => void | Gets called once a Spline Key Down event is fired |
onSplineKeyUp? | (e: SplineEvent) => void | Gets called once a Spline Key Up event is fired |
onSplineStart? | (e: SplineEvent) => void | Gets called once a Spline Start event is fired |
onSplineLookAt? | (e: SplineEvent) => void | Gets called once a Spline Look At event is fired |
onSplineFollow? | (e: SplineEvent) => void | Gets called once a Spline Mouse Up event is fired |
onSplineScroll? | (e: SplineEvent) => void | Gets called once a Spline Scroll event is fired |
The object exposed as a first argument of the onLoad
function, is a Spline Application. You can call all these different methods on it.
Name | Type | Description |
---|---|---|
emitEvent | (eventName: SplineEventName, nameOrUuid: string) => void | Triggers a Spline event associated to an object with provided name or uuid. |
emitEventReverse | (eventName: SplineEventName, nameOrUuid: string) => void | Triggers a Spline event associated to an object with provided uuid in reverse order. Starts from last state to first state. |
findObjectById | (uuid: string) => SPEObject | Searches through scene's children and returns the object with that uuid. |
findObjectByName | (name: string) => SPEObject | Searches through scene's children and returns the first object with that name. |
setZoom | (zoom: number) => void | Sets the initial zoom of the scene. |
These are all the Spline event types that you can pass to the emitEvent
or emitEventReverse
function.
Name | Description |
---|---|
mouseDown | Refers to the Spline Mouse Down event type |
mouseHover | Refers to the Spline Mouse Hover event type |
mouseUp | Refers to the Spline Mouse Up event type |
keyDown | Refers to the Spline Key Down event type |
keyUp | Refers to the Spline Key Up event type |
start | Refers to the Spline Start event type |
lookAt | Refers to the Spline Look At event type |
follow | Refers to the Spline Mouse Up event type |
We use yarn, install the dependencies like this:
1yarn
Serve the example
folder at localhost:3000
1yarn dev
1yarn build
1yarn publish
No vulnerabilities found.
No security vulnerabilities found.