Socket
Socket
Sign inDemoInstall

@r2u/react-ar-components

Package Overview
Dependencies
103
Maintainers
2
Versions
66
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @r2u/react-ar-components

React components for Augmented Reality on the web.


Version published
Weekly downloads
171
increased by1121.43%
Maintainers
2
Created
Weekly downloads
 

Readme

Source

React AR Components

React components for Augmented Reality on the web.

Components

  • ModelViewer: used to interact with a 3D model on the browser (desktop)
  • WebXROverlay: DOM overlay used during a WebXR session (Chrome browser on mobile)

Methods

  • activateAR: tries to open a GLB model on SceneViewer or a USDZ model on quicklook
  • activateWebXR: tries to open a GLB model using WebXR, having activateAR as a fallback
ModelViewer

Attention: This component needs the importing of @google/model-viewer. We suggest it by adding the script to the page HTML, but it's also possible to add via NPM. Please refer to the model-viewer documentation

paramdescriptiondefault
model-viewer default paramsPlease refer to model-viewer documentation''
ref*React RefObject to get/set properties of ModelViewerundefined
classNameReact ClassName, same as HTML classundefined
styleobject containing custom CSS classes to customize this componentundefined
childrenReact component to nest inside model-viewerundefined
componentDidMountfunction to handle mount event() => undefined
onClickfunction to handle click event() => undefined
pandetermines if the model will have pan interactiontrue

*: Prop required to make this wrapper work. Can be passed as follows:

<ModelViewer ref={React.createRef()} {...props} />
activateAR
paramdescriptiontypedefaultApp
glburl of the GLB file for Android devicesstring''SceneViewer
usdzurl of the USDZ file for iOS devicesstring''QuickLook
titleDisplay title of object on model cardstringundefinedSceneViewer
shareTextString to display when sharing AR experiencestringundefinedBoth
soundURL to play as sound when model is placedstringundefinedSceneViewer
linkLink to show on model cardstringundefinedSceneViewer
scaledetermines if model allows rescale'fixed' | 'resizable''fixed'Both
placementdetermines the plane to render the model'floor' | 'wall'floorSceneViewer
disableOcclusionSceneViewer object blending disablingbooleanfalseSceneViewer
hashFallbackNew window.location.hash when AR cannot loadstring#no-ar-fallbackSceneViewer
activateWebXR
paramdescriptiontypedefault
model.glbUrl of the model's GLB filestringundefined
model.nameDisplay title of object on model cardstringundefined
model.descriptionDisplay text of object on model cardstring''
model.urlLink to show on model cardstring''
model.placementDetermines plane on which the model will be rendered'floor' | 'wall'undefined
model.resizableDetermines if model allows rescalebooleanundefined
beforeCloseFunction called before ending the experience*(container: HTMLElement) => voiddefaultBeforeClose
callToActionEnable CTA on AR experienceCallToActionundefined

*: If provided, beforeClose function MUST eventually dispatch the event r2u-webxr-close for the WebXR session to end properly.

Examples

activateAR
import React, { createRef } from 'react'
import ReactDOM from 'react-dom'

import '@google/model-viewer'

import { ModelViewer, activateAR } from '@r2u/react-ar-components'
import type { ModelViewerElement } from '@r2u/react-ar-components'

import style from './style.module.css'

const App: React.FC = () => {
  const usdz = 'http://localhost:8080/mesh_01000.usdz'
  const glb = 'http://localhost:8080/mesh_01000.glb'
  const imageUrl = 'https://via.placeholder.com/150'
  const viewerRef = createRef<ModelViewerElement>()

  return (
    <div>
      <div>
        <h1>AR</h1>
        <button type="button" onClick={() => activateAR({ glb, usdz })}>
          View in 3D
        </button>
      </div>
      <div>
        <h1>ModelViewer</h1>
        <ModelViewer
          ref={viewerRef}
          src={glb}
          alt="3D model"
          style={style}
          poster={imageUrl}
        />
      </div>
    </div>
  )
}

ReactDOM.render(<App />, document.getElementById('root'))
activateWebXR
import React from 'react'

import { activateWebXR } from '@r2u/react-ar-components'


const App: React.FC = () => {

  const model = {
    resizable: true,
    name: 'Chair',
    description: 'Comfy chair',
    glb:
      'http://localhost:8080/mesh_01000.glb',
    url: 'https://google.com',
    placement: 'floor',
  }

  return (
    <div>
      <h1>WebXR</h1>
      <button type="button" onClick={() => activateWebXR({ model })}>
        Floor
      </button>
    </div>
  )
}

ReactDOM.render(<App />, document.getElementById('root'))

Testing

make start
  • Access http://localhost:8080
  • Or expose local port if testing on another device or on mobile
    • Eg: npx localtunnel --port 8080
    • Access exposed link
  • Test banner.html

Analyze bundle size

React App build

yarn install
yarn build:app --analyze

Visit http://localhost:8888

SDK build

yarn install
yarn build:sdk --analyze

Visit http://localhost:8888

FAQs

Last updated on 24 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc