WebSpatial
website·Indexed 32 days ago
https://webspatial.dev/docsA set of spatial APIs and an SDK designed to extend standard 2D web capabilities, providing integration patterns and installation procedures for web projects and platforms.
What's inside WebSpatial
- WebSpatial is a set of spatial APIs and a ready-to-use SDK designed to extend the standard 2D Web ecosystem to support spatial computing across platforms. It allows HTML and CSS-based web applications to achieve spatial capabilities comparable to native spatial apps (such as visionOS apps) while maintaining the advantages of the open web.
Overview of WebSpatial capabilities
WebSpatial provides minimal extensions to HTML, CSS, and DOM APIs, along with a polyfill-style SDK. It allows web developers to move from 2D layouts to spatial computing by enabling HTML content to have real volume, support natural spatial interactions, and integrate 3D programming while maintaining the standard web development workflow and cross-platform nature.Understand WebSpatial App requirements and behavior
A WebSpatial App is a standard Web App (specifically a PWA with a Web App Manifest) that uses the WebSpatial API and runs within a WebSpatial Runtime. It enhances a normal webpage by enabling spatial computing capabilities (such as transparent backgrounds and floating UI elements) when running on a supported spatial computing platform.
Key behaviors:
- Fallback: If the app runs on a platform without spatial computing support (e.g., standard desktop or mobile browsers), it functions as a normal webpage/PWA with no spatial effects.
- Runtime Requirement: To enable spatial features, the OS must support spatial computing and unified rendering, and the environment must include the WebSpatial Runtime.
- WebXR Integration: WebXR content can be embedded within a WebSpatial App. However, starting a WebXR session takes over the entire space, hiding the original webpages and spatialized UI. Note that on visionOS, Packaged WebSpatial Apps currently cannot start WebXR sessions because WKWebView does not support WebXR.
Understand Spatial App and Unified Rendering concepts
WebSpatial follows a "2D containing 3D" development paradigm.
- Spatial App: An app that runs in a Spatial OS (like visionOS or PICO OS 6) and is rendered in a unified way by the OS. A standard 2D Web App becomes a spatial app by implementing WebSpatial APIs to allow 2D content to enter 3D space or mix with volumetric 3D content.
- Unified Rendering: The process where the Spatial OS manages 2D/3D content containers (spatial scenes) to ensure multiple apps share the same coordinate system, lighting, occlusion, and shadows.
- Spatial Runtime: The single 3D rendering engine managed by the OS.
- Shared Space: Multiple spatial apps coexist.
- Full Space: One app exclusively occupies the space, allowing access to private data like eye tracking and hand movement after user authorization.
WebSpatial SDK Overview and Compatibility
The WebSpatial SDK provides a polyfill for proposed HTML/DOM/CSS spatial APIs, allowing developers to use Z-axis and spatial UI features immediately without waiting for native browser support.
Key compatibility features include:
- Cross-version compatibility: The SDK shields developers from instability as APIs enter Web standards, ensuring backward compatibility.
- Cross-platform behavior: On spatial computing platforms, it enables unified spatial UI features. On non-spatial platforms (standard desktop/mobile browsers), the SDK automatically ignores WebSpatial APIs and skips loading the full implementation to avoid impacting performance.
- Custom Logic: Developers can use feature detection and runtime detection to implement specific handling for JS/DOM API calls that cannot be automatically ignored.
Initialize WebSpatial using the SpatialBoot component
Wrap your application with the<SpatialBoot>component from@webspatial/react-sdk. This component activates spatial capabilities on supported platforms and allows the app to function as a standard website in ordinary browsers. For SSR projects,<SpatialBoot>must be placed within client-rendered code.import { createRoot } from "react-dom/client"; import { SpatialBoot } from "@webspatial/react-sdk"; createRoot(document.getElementById("root")).render( <SpatialBoot> <App /> </SpatialBoot>, );Apply true 3D Spatial Transforms to elements
WebSpatial enhances standard CSS
transformandtransform-originproperties to produce true 3D spatial effects instead of 2D projections.Requirements:
- The element must be marked as a spatialized HTML element.
- 3D container elements like
<Model>and<Reality>are automatically spatialized and can use these transforms as a backplate.
Supported Functions:
translateZ()andtranslate3d(): Translate along the Z-axis perpendicular to the webpage.rotateX(),rotateY(),rotateZ(), androtate3d(): Rotate the 2D plane into 3D space.scaleZ()andscale3d(): Scale parts of the element that have entered 3D space via rotation.
Notes:
transform: perspective()andtransform-styleare ignored and no longer needed.- Spatial transforms do not change the element's actual layout position or its local coordinate system; they only change the visual appearance.
.list-menu { position: fixed; top: 200px; left: 0; transform-origin: top left; transform: translateZ(320px) rotateY(80deg); }Handle spatial interactions and mixed content
WebSpatial extends interaction and alignment capabilities for 3D web environments:
- Spatial Interaction Events: Supports 3D-aware events like tap, drag, and rotate on spatialized HTML elements and 3D mesh surfaces/bounding boxes, providing coordinates in a 3D system.
- Mixed Content Integration: Provides APIs for coordinate and unit conversion to align and couple 2D CSS-based content with dynamic 3D engine content.
Spatialized HTML elements using JSX markers
The WebSpatial SDK can transform ordinary HTML elements into spatialized HTML elements. To do this, developers must add specific markers to the elements in JSX. There are three supported marking styles for the
enable-xrmarker:- HTML Attribute: Pass
enable-xras an attribute. - CSS Class: Add
__enableXr__to theclassName. - Inline Style: Add
enableXr: trueto thestyleobject.
// 1. HTML Attribute <div className="card" enable-xr></div> // 2. CSS Class <div className="card __enableXr__"></div> // 3. Inline Style <div className="card" style={{ enableXr: true, marginTop: "10px" }}></div>- HTML Attribute: Pass
Integrate WebSpatial SDK into React Projects
The WebSpatial SDK currently provides a React SDK. To enable WebSpatial APIs, the SDK must be integrated into the JSX Runtime.
Project Requirements:
- TypeScript Projects: Requires one line of configuration (though some build tools like Rspack may need extra steps).
- JavaScript Projects: Configuration must be handled within the web build tool.
Supported Build Tools:
- Vite
- Next.js, Remix (SSR is supported)
- Rspack / Rsbuild / Webpack
Known Limitation: The current version of the WebSpatial SDK does not support
styled-componentsdue to a bug.Manage spatialized elements in React
When using the WebSpatial SDK with React, follow these constraints for spatialized HTML elements:
- Declarative Updates: CSS updates must be done via declarative code (e.g., changing
classNameorstylein JSX) following the Rules of React. - DOM Access: To query the current state via DOM APIs, use
useRefto obtain the DOM object. Direct queries likequerySelectorare not supported. - Animations: CSS animations for spatial properties are not supported; use JS-based animation approaches instead.
- Declarative Updates: CSS updates must be done via declarative code (e.g., changing
Create new Spatial Scenes via links
WebSpatial Apps can create additional Spatial Scenes by opening links in new windows. The target URL must match thescopedefined in the Web App Manifest to be opened as a Spatial Scene; otherwise, it opens as a standard third-party browser window. If the new scene has a name, the WebSpatial SDK'sinitSceneAPI can be used to provide custom initialization properties before the scene is created.<!-- Open in a new scene --> <a href={newSceneUrl} target="_blank">Open Scene</a> <a href={newSceneUrl} target="newSceneName">Open Named Scene</a> <script> // Programmatically open a scene window.open(newSceneUrl); window.open(newSceneUrl, "newSceneName"); </script>