Giter Site home page Giter Site logo

react-spline's Introduction

react-spline

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

Table of Contents

Install

yarn add @splinetool/react-spline @splinetool/runtime

or

npm install @splinetool/react-spline @splinetool/runtime

Usage

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:

import Spline from '@splinetool/react-spline';

export default function App() {
  return (
    <div>
      <Spline scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode" />
    </div>
  );
}

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.

Next.js

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.

import Spline from '@splinetool/react-spline/next';

export default function App() {
  return (
    <div>
      <Spline scene="https://prod.spline.design/KFonZGtsoUXP-qx7/scene.splinecode" />
    </div>
  );
}

Read and modify Spline objects

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).

import { useRef } from 'react';
import Spline from '@splinetool/react-spline';

export default function App() {
  const cube = useRef();

  function onLoad(spline) {
    const obj = spline.findObjectByName('Cube');
    // or
    // const obj = spline.findObjectById('8E8C2DDD-18B6-4C54-861D-7ED2519DE20E');

    // save it in a ref for later use
    cube.current = obj;
  }

  function moveObj() {
    console.log(cube.current); // Spline Object => { name: 'Cube', id: '8E8C2DDD-18B6-4C54-861D-7ED2519DE20E', position: {}, ... }

    // move the object in 3D space
    cube.current.position.x += 10;
  }

  return (
    <div>
      <Spline
        scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode"
        onLoad={onLoad}
      />
      <button type="button" onClick={moveObj}>
        Move Cube
      </button>
    </div>
  );
}

Listen to Spline Events

You can listen to any Spline Event you set in the Events panel of the editor by attaching a listener to the Spline component.

import Spline from '@splinetool/react-spline';

export default function App() {
  function onSplineMouseDown(e) {
    if (e.target.name === 'Cube') {
      console.log('I have been clicked!');
    }
  }

  return (
    <div>
      <Spline
        scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode"
        onSplineMouseDown={onSplineMouseDown}
      />
    </div>
  );
}

You can find a list of all of the Spline Event listeners in the Spline Component Props section.

Trigger Spline events from outside

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).

import { useRef } from 'react';
import Spline from '@splinetool/react-spline';

export default function App() {
  const spline = useRef();

  function onLoad(splineApp) {
    // save the app in a ref for later use
    spline.current = splineApp;
  }

  function triggerAnimation() {
    spline.current.emitEvent('mouseHover', 'Cube');
  }

  return (
    <div>
      <Spline
        scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode"
        onLoad={onLoad}
      />
      <button type="button" onClick={triggerAnimation}>
        Trigger Spline Animation
      </button>
    </div>
  );
}

Or you can query the spline object first, and then trigger the event:

import { useRef } from 'react';
import Spline from '@splinetool/react-spline';

export default function App() {
  const objectToAnimate = useRef();

  function onLoad(spline) {
    const obj = spline.findObjectByName('Cube');
    // save the object in a ref for later use
    objectToAnimate.current = obj;
  }

  function triggerAnimation() {
    objectToAnimate.current.emitEvent('mouseHover');
  }

  return (
    <div>
      <Spline
        scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode"
        onLoad={onLoad}
      />
      <button type="button" onClick={triggerAnimation}>
        Trigger Spline Animation
      </button>
    </div>
  );
}

You can find a list of all of the Spline Events you can pass to the emitEvent function in the Spline Events section.

Lazy loading

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:

import React, { Suspense } from 'react';

const Spline = React.lazy(() => import('@splinetool/react-spline'));

export default function App() {
  return (
    <div>
      <Suspense fallback={<div>Loading...</div>}>
        <Spline scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode" />
      </Suspense>
    </div>
  );
}

More info in the relative React documentation.

API

Spline Component Props

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

Spline App Methods

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.

Spline Events

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

Contributing

We use yarn, install the dependencies like this:

yarn

Development

Serve the example folder at localhost:3000

yarn dev

Build Library

yarn build

Publish on npm

yarn publish

react-spline's People

Contributors

claudiabdm avatar dependabot[bot] avatar futurgh avatar marcofugaro avatar rickturner2001 avatar superguigui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-spline's Issues

Can I know the progress level of an animation ?

Hello!

Is it possible to know the progress of the animation ? I have two types of animation, one on scroll and one on "lookat".

I want to make sure scrolling animation does not reverse and I want to enable the second animation only once scrolling animation is done.

'spline' not being recognised ts-(2552)

Hey team,

Loving my life in Spline, but I am having an issue in an environment using React 18.2.0 with Typescript 4.8.3.

Screenshot 2022-09-24 at 18 07 41

Correcting this /\ gives me this:

Screenshot 2022-09-24 at 18 11 41

Have I missed a clear-and-obvious?

Screenshot 2022-09-24 at 18 13 43

import Spline from '@splinetool/react-spline'; function LandingSpline() { const lightVWToggle = Spline.findObjectById('d5c18431-3315-4ac1-acda-bce61ab791aa'); return( <Spline className="landingAnim" style={{height: '100%', width: '100%'}} scene="https://prod.spline.design/YUkMhrHgIKKVFXHI/scene.splinecode" /> ) };

Thanks team,

Ed

Responsive Spline Element does not cover full screen?

Hey there

I am currently trying to integrate my spline design into a Next.js project but when I add it into my page. It only shows up at the top of the page like so:

Screenshot 2022-11-23 at 15 06 53

I tried wrapping it in a div that occupies the full height of screen but still displays in the same position.

Screenshot 2022-11-23 at 15 08 04

Any help would be appreciated
Mr Biskit

Texture marked for update but image is incomplete warning

Hey there,

I'm getting this warning:
THREE.WebGLRenderer: Texture marked for update but image is incomplete

My scenes are loading and displaying rather slowly, and its also making the webpage very slow (evident when scrolling past the scenes), and I believe it has something to do with this warning. Any fixes?

Spline does not work with Next JS

Spline is marketed to work with React Js out of the box but it does not work with NextJs unfortunately.

import React from "react";
import Spline from "@splinetool/react-spline";

export default function BgComponent() {
return (
<>

</>
);
}

using this code in a next js page always results in reference error - Image not defined.
image

Hydration failed because the initial UI does not match what was rendered on the server

Running into a super odd bug with React 18 & Next.js 13

CleanShot 2022-11-27 at 16 22 44

I suspect it's related to the infamous Hydration failed because the initial UI does not match what was rendered on the server. error: vercel/next.js#35773

However, even when I tried dynamically importing the Spline component with next/dynamic and workarounds like this, the problem still persisted.

I'm at a complete loss and had to remove the Spline component temporarily to not break my app πŸ˜“

Would love some help debugging this. Here's my code: https://github.com/steven-tey/dub/blob/main/components/app/welcome/intro.tsx#L33-L37

about the init size

The size of the model made in spline is 100x100x100 by default, but the default size of mesh in r3f is 1x1x1, which requires manual scaling after loading, which is very inconvenient. It is recommended to modify this problem

Does Spline work with Next.js and Typescript?

Hi, does Spline work with next ts?
Spline loads normally on dev, but when I start app build, I'm getting errors:
Application error: a client-side exception has occurred (see the browser console for more information).
And in console errors:
TypeError: Super constructor null of anonymous class is not a constructor
Uncaught Error: Minified React error #418 "Hydration failed because the initial UI does not match what was rendered on the server."
Uncaught Error: Minified React error #423 "There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering"

I tried to use dynamic import from next with suspense, ssr option set as false and both does not work in this case.
I also tried it in minified app with next app create, but I got the same error after build start.

event not work..

 <Spline
        scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode"
        onLoad={(spline: Application) => {
          cube.current = spline.findObjectByName('Cube');
        }}
        onMouseDown={onMouseDown}
        onMouseHover={(e)=>{
          console.log(e)
        }}
      />

help !! mouseDown mouseHover event not trigger....

[Bug] Unable to use react components

Tried with both plain react and next js, following the provided documentation results in the same error message

Error: fetch for http://localhost:3000/%22http:/localhost:3000/_assets/_fonts/roboto_regular.json%22

image

import Spline from '@splinetool/react-spline';

// the react code
export default function App() {
  return (
    <div>
	  <Spline scene="https://prod.spline.design/xn5XEHKULkcZrYz4/scene.spline" />
	</div>
  );
}
//the nextjs code
import dynamic from "next/dynamic";

const Spline = dynamic(() => import("@splinetool/react-spline"), {
  ssr: false,
});

export default function Home() {
  return (
    <div className="container">
      <main className="main">
        <div>
          <Spline scene="https://prod.spline.design/xn5XEHKULkcZrYz4/scene.spline" />
        </div>
      </main>
    </div>
  );
}

public url

package.json

{
  "name": "spline-template",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@splinetool/react-spline": "^2.0.0",
    "@splinetool/runtime": "^0.9.33",
    "next": "12.1.1",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-hot-toast": "^2.2.0",
    "sass": "^1.49.9"
  },
  "devDependencies": {
    "eslint": "8.12.0",
    "eslint-config-next": "12.1.1"
  }
}
 

[Feature Request] Expose 2D positions of objects

Use case: clicking on an object can trigger an popover card (or dialogue box) open. Note that this dialogue box is a custom react component defined outside of spline.

Why need 2D positions: currently a SplineEvent in onMouseDown only contains the target’s name and ID without telling us where the object is. Without its 2D position, we can’t position the dialogue box properly.

Desired behavior: SplineEvent provides the 2D position of the object. Or alternatively, the results from findObjectByName and findObjectByID contains the 2D position of the object (currently only has 3D).

Please let me know if there’re other ways to implement this functionality, but otherwise please take a moment to review this feature request!

[Feature Request] 2D overlays

Hey, it would be nice to be able to create 2D overlays using objects or null objects as the base reference relative to the camera perspective. For example button components, text, ...

[Bug] Canvas size dosen't work properly with version 2.2.1

I've updated to the new version 2.2.1 and in my existing project and in the example project from the repository the canvas is sized randomly with 0x0 pixel on load. I've tested this behaviour in Chrome, Safari and Firefox and got the same result.

spline-sizing-bug.mp4

.

[Bug] Everything was working fine, and then it just broke on it's own.

image

As i said, everything was fine, and then it broke on my local machine and hosted on gh. Tried on different browsers and machines, and on phone. Tried updating in spline several times and changed some options, but still broken. Tried on different networks as well.
The only fix that worked, is that i promoted an older draft of my scene to production.

[Fix] Can't resize Spline scene.

As the title says, it's actually impossible but possible with a workaround here. #26
I tried that down below in the video but this is definitely not the result we want. πŸ˜“

The canvas size reverts to full viewport width/height as soon as the viewport is slightly resized.

Here's what it actually renders:

Spline.not.working.mov

Issue on generation and import

Hello, I tried to do this 'tuto', everything works fine as you can see here: Spline, but when I try to import it for react I haven't got my Blob, CodeSandbox, but it works fine with Vanilla JS for example. Vanilla.
I tried to on local with create vite react app.
πŸ™ Thank you

Next.Js Support

Hi there,

I love Spline and would like to use it in my Next.js app.
However, if I follow the instructions of a basic Next.js setup and react-spline setup, I get an error.

Steps to Reproduce:

  1. Create Next.js App: npx create-next-app@latest
  2. Install react-spline: npm install @splinetool/react-spline @splinetool/runtime
  3. Replace content in index.js with:
import Spline from '@splinetool/react-spline';

export default function App() {
  return (
    <div>
      <Spline scene="https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode" />
    </div>
  );
}
  1. Run the app: npm run dev

Expected Result:
Seeing the default spline animation

Actual Result:

SyntaxError: Unexpected token '.'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/Users/silasniewierra/Documents/Coding/test/test-spline/node_modules/@splinetool/react-spline/dist/react-spline.cjs.js:1:546)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10) {
  page: '/'
}
Warning: data for page "/_error" is 430 kB which exceeds the threshold of 128 kB, this amount of data can reduce performance.

Bildschirmfoto 2022-07-01 um 17 02 32

! Even if I don't use the <Spline> component and just import it on the top, the error appears.

Resizing Error: Effects not working on resize of Spline component.

Hi there,

I just figured out how to use react-spine, thanks to @ivanburlakov πŸ˜„
Now, I'm having trouble handling the proportions of my Spline component.
I want to resize them to fit my layout but if I change it's height, width, the hover effects are all messed up.
Does anyone know, or is there a tutorial on how to properly handle dimensions of the Spline component?

Maybe @ivanburlakov 😁

Text doesn't work

I am using Spine component in react Js with next Js and I get the following errors on my browser window,

Unhandled Runtime Error
Error: fetch for ["http://localhost:3000/_assets/_fonts/poppins_extralight_regular.json"](http://localhost:3000/%22http://localhost:3000/_assets/_fonts/poppins_extralight_regular.json%22) responded with 404: Not Found
Call Stack
eval
node_modules\@splinetool\runtime\build\runtime.js (2994:158346)

and also this similar error

Unhandled Runtime Error
Error: fetch for ["http://localhost:3000/_assets/_fonts/roboto_regular.json"](http://localhost:3000/%22http://localhost:3000/_assets/_fonts/roboto_regular.json%22) responded with 404: Not Found
Call Stack
eval
node_modules\@splinetool\runtime\build\runtime.js (2994:158346)

I am using

node version -v14.18.1
react version- 17.0.2
next -12.0.10

React Native Issues

When building for React Native I run into quite a few issues, some I have been able to resolve, but now I'm stuck. I looked through the closed issues and it seems React Native is supported. Is there a guide or example somewhere?

Here's the steps I've taken...

First I needed to add react-native-canvas and react-native-fs to the project.

Then I polyfilled window and navigator

global.window = {
  navigator: {
    userAgent: 'any',
    platform: 'any',
  },
  location: { search: 'location' },
}
global.navigator = {
  userAgent: 'any',
  platform: 'any',
}

I modified the code to use Canvas

/* eslint-disable no-inner-declarations */
import React, { useEffect, useRef, useState, forwardRef } from 'react'
import { Application } from '@splinetool/runtime'
import type {
  SPEObject,
  SplineEvent,
  SplineEventName,
} from '@splinetool/runtime'
import Canvas from 'react-native-canvas'
import { StyleProp, ViewStyle } from 'react-native'

export type { SPEObject, SplineEvent, SplineEventName }

export function mergeRefs<T = any>(
  refs: Array<React.MutableRefObject<T> | React.LegacyRef<T>>,
): React.RefCallback<T> {
  return (value) => {
    refs.forEach((ref) => {
      if (typeof ref === 'function') {
        ref(value)
      } else if (ref != null) {
        const typedRef = ref as React.MutableRefObject<T | null>
        typedRef.current = value
      }
    })
  }
}

type CanvasAttributes = React.CanvasHTMLAttributes<HTMLCanvasElement>
export interface SplineProps
  extends Omit<
    CanvasAttributes,
    | 'onLoad'
    | 'onMouseDown'
    | 'onMouseUp'
    | 'onMouseHover'
    | 'onKeyDown'
    | 'onKeyUp'
    | 'onWheel'
    | 'style'
  > {
  scene: string
  onLoad?: (e: Application) => void
  onMouseDown?: (e: SplineEvent) => void
  onMouseUp?: (e: SplineEvent) => void
  onMouseHover?: (e: SplineEvent) => void
  onKeyDown?: (e: SplineEvent) => void
  onKeyUp?: (e: SplineEvent) => void
  onStart?: (e: SplineEvent) => void
  onLookAt?: (e: SplineEvent) => void
  onFollow?: (e: SplineEvent) => void
  onWheel?: (e: SplineEvent) => void
  autoRender?: boolean
  style?: StyleProp<ViewStyle>
}

const Spline = forwardRef<HTMLCanvasElement, SplineProps>(
  (
    {
      scene,
      style,
      onMouseDown,
      onMouseUp,
      onMouseHover,
      onKeyDown,
      onKeyUp,
      onStart,
      onLookAt,
      onFollow,
      onWheel,
      onLoad,
      autoRender = false,
      ...props
    },
    ref,
  ) => {
    const canvasRef = useRef<HTMLCanvasElement>(null)
    const [, setIsLoading] = useState(true)

    // Initialize runtime when component is mounted
    useEffect(() => {
      setIsLoading(true)

      let speApp: Application
      const events: {
        name: SplineEventName
        cb?: (e: SplineEvent) => void
      }[] = [
        {
          name: 'mouseDown',
          cb: onMouseDown,
        },
        {
          name: 'mouseUp',
          cb: onMouseUp,
        },
        {
          name: 'mouseHover',
          cb: onMouseHover,
        },
        {
          name: 'keyDown',
          cb: onKeyDown,
        },
        {
          name: 'keyUp',
          cb: onKeyUp,
        },
        {
          name: 'start',
          cb: onStart,
        },
        {
          name: 'lookAt',
          cb: onLookAt,
        },
        {
          name: 'follow',
          cb: onFollow,
        },
        {
          name: 'scroll',
          cb: onWheel,
        },
      ]

      if (canvasRef.current) {
        speApp = new Application(canvasRef.current, { autoRender })

        async function init() {
          await speApp.load(scene)

          for (const event of events) {
            if (event.cb) {
              speApp.addEventListener(event.name, event.cb)
            }
          }

          setIsLoading(false)
          onLoad?.(speApp)
        }

        init()
      }

      return () => {
        for (const event of events) {
          if (event.cb) {
            speApp.removeEventListener(event.name, event.cb)
          }
        }
        speApp.dispose()
      }
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [scene])

    return (
      <Canvas
        ref={mergeRefs<HTMLCanvasElement>([ref, canvasRef])}
        style={style}
        {...props}
      />
    )
  },
)

export default Spline

Now I'm getting

ReferenceError: Can't find variable: document

and

TypeError: undefined is not an object (evaluating '_$$_REQUIRE(_dependencyMap[7], "@splinetool/runtime").Application')

It seems I'm going down a rabbit hole. Is there a better way?

Uncaught TypeError: L.Application is not a constructor

Hi, I'm having some issues rendering my spline onto my project. It all looks fine on the CodeSanbox link provided but when using it within my own app, there seems to be an issue. I am using React w/ Typescript and webpack. Here is the component:

image

Whenever I try to render the component, a TypeError is thrown regarding L.Application is not a constructor:

image

I'm not really sure what the error means but it was addressing a ForwardRef component so it could potentially be something around the ForwardRefExocticComponent as shown below

image

Please let me know if you have any ideas. Thanks πŸ™‚

[Bug] Post-Processing breakes rendering on mobile.

i tried multiple simple scenes, when in visual studio on a browser emulating mobile, it works fine, but when deployed on gh-pages and actually opened on a phone, it just turns into a black screen and it breaks. When post-processing gets turned off and the link updated, it starts working fine again and it renders out the scene normaly. Any other variable doesn't seem to matter, even when post processing is turned on but options within it are off, it still doesn't work.

[Bug] Spline scene will sometimes not render in a production build of React app

Environment is CRA and deployed using GitHub Pages. When you visit the site, one of two things will happen:

  1. Scene does not render (sometimes you see a flash before it goes)
  2. Scene renders after a brief delay

Here is a post from Stack Overflow describing a similar problem:
https://stackoverflow.com/questions/72244874/react-spline-bug-after-built-react-project-the-spline-content-is-gone-in-spli

I've tried a variety of workarounds and potential solutions to no avail. Note that the issue described above is not encountered during development.

React Native Support ?

Is this under development or shipped already or not planned?
If shipped, an example would be noiceee πŸ”₯

Couldn't infer the status from #39

breaking the frame

Screenshot_20221129_011229_Chrome.jpg
After some clicks on the iconTheme (moon) a little face appears in the upper corner of the keyboard, this blue background appears (it does not exist before breaking) and it does not work anymore

Error handling

Hey!

Is there any callback that's invoked should the spline fail to load? Do you have any recommendations for error handling such as ErrorBoundaries?

How to control the camera in react?

If the spline file already set special camera, How to control the time timeStamp or keyframes? The camera is move too slow. (I can't edit spline project.)
image

React Native not working

Reading through the issues it seems that this library supports React Native, but I'm struggling to get it to work.
I'm using react-native-canvas in place of canvas, but am now getting...

Invariant Violation: View config getter callback for component div must be a function (received undefined). Make sure to start component names with a capital letter.

div isn't available in react native, which makes me think maybe there is some setup I'm missing?

Here's what I have so far

/* eslint-disable no-inner-declarations */
import React, { useEffect, useRef, useState, forwardRef } from 'react'
import { Application } from '@splinetool/runtime'
import type {
  SPEObject,
  SplineEvent,
  SplineEventName,
} from '@splinetool/runtime'
import Canvas from 'react-native-canvas'
import { StyleProp, ViewStyle } from 'react-native'

export type { SPEObject, SplineEvent, SplineEventName }

export function mergeRefs<T = any>(
  refs: Array<React.MutableRefObject<T> | React.LegacyRef<T>>,
): React.RefCallback<T> {
  return (value) => {
    refs.forEach((ref) => {
      if (typeof ref === 'function') {
        ref(value)
      } else if (ref != null) {
        const typedRef = ref as React.MutableRefObject<T | null>
        typedRef.current = value
      }
    })
  }
}

type CanvasAttributes = React.CanvasHTMLAttributes<HTMLCanvasElement>
export interface SplineProps
  extends Omit<
    CanvasAttributes,
    | 'onLoad'
    | 'onMouseDown'
    | 'onMouseUp'
    | 'onMouseHover'
    | 'onKeyDown'
    | 'onKeyUp'
    | 'onWheel'
    | 'style'
  > {
  scene: string
  onLoad?: (e: Application) => void
  onMouseDown?: (e: SplineEvent) => void
  onMouseUp?: (e: SplineEvent) => void
  onMouseHover?: (e: SplineEvent) => void
  onKeyDown?: (e: SplineEvent) => void
  onKeyUp?: (e: SplineEvent) => void
  onStart?: (e: SplineEvent) => void
  onLookAt?: (e: SplineEvent) => void
  onFollow?: (e: SplineEvent) => void
  onWheel?: (e: SplineEvent) => void
  autoRender?: boolean
  style?: StyleProp<ViewStyle>
}

const Spline = forwardRef<HTMLCanvasElement, SplineProps>(
  (
    {
      scene,
      style,
      onMouseDown,
      onMouseUp,
      onMouseHover,
      onKeyDown,
      onKeyUp,
      onStart,
      onLookAt,
      onFollow,
      onWheel,
      onLoad,
      autoRender = false,
      ...props
    },
    ref,
  ) => {
    const canvasRef = useRef<HTMLCanvasElement>(null)
    const [isLoading, setIsLoading] = useState(true)

    // Initialize runtime when component is mounted
    useEffect(() => {
      setIsLoading(true)

      let speApp: Application
      const events: {
        name: SplineEventName
        cb?: (e: SplineEvent) => void
      }[] = [
        {
          name: 'mouseDown',
          cb: onMouseDown,
        },
        {
          name: 'mouseUp',
          cb: onMouseUp,
        },
        {
          name: 'mouseHover',
          cb: onMouseHover,
        },
        {
          name: 'keyDown',
          cb: onKeyDown,
        },
        {
          name: 'keyUp',
          cb: onKeyUp,
        },
        {
          name: 'start',
          cb: onStart,
        },
        {
          name: 'lookAt',
          cb: onLookAt,
        },
        {
          name: 'follow',
          cb: onFollow,
        },
        {
          name: 'scroll',
          cb: onWheel,
        },
      ]

      if (canvasRef.current) {
        speApp = new Application(canvasRef.current, { autoRender })

        async function init() {
          await speApp.load(scene)

          for (const event of events) {
            if (event.cb) {
              speApp.addEventListener(event.name, event.cb)
            }
          }

          setIsLoading(false)
          onLoad?.(speApp)
        }

        init()
      }

      return () => {
        for (const event of events) {
          if (event.cb) {
            speApp.removeEventListener(event.name, event.cb)
          }
        }
        speApp.dispose()
      }
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [scene])

    return (
      <Canvas
        ref={mergeRefs<HTMLCanvasElement>([ref, canvasRef])}
        style={style}
        {...props}
      />
    )
  },
)

export default Spline

CORS: Unhandled Runtime Error

Hey, i'm developing a site locally on localhost:3000 and I'm receiving the following error ion console:

Access to fetch at 'https://prod.spline.design/PVMoc332wUkkwrCV/scene.splinecode' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I've taken a look at the docs but can't see anything that suggests i cant put this into my project locally ?

import Spline from '@splinetool/react-spline';

export default function App() {
  return (
    <Spline scene="https://prod.spline.design/PVMoc332wUkkwrCV/scene.splinecode" />
  );
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.