Table of Contents
The Canvas
object is your portal into Threejs. It renders Threejs elements, not DOM elements! Here is a small hello-world that you can try out:
import ReactDOM from "react-dom";import React from "react";import { Canvas } from "react-three-fiber";ReactDOM.render(<Canvas><pointLight position={[10, 10, 10]} /><mesh><sphereBufferGeometry attach="geometry" /><meshStandardMaterial attach="material" color="hotpink" /></mesh></Canvas>,document.getElementById("root"));
The canvas stretches to 100% of the next relative/absolute parent-container. Make sure your canvas is given space to show contents!
<Canvaschildren // Either a function child (which receives state) or regular childrengl // Props that go into the default webGL-renderercamera // Props that go into the default cameraraycaster // Props that go into the default raycastershadowMap // Props that go into gl.shadowMap, can also be set true for PCFsoftcolorManagement = false // Auto sRGBEncoding encoding for all colors and textures + ACESFilmicvr = false // Switches renderer to VR mode, then uses gl.setAnimationLoopgl2 = false // Enables webgl2concurrent = false // Enables React concurrent moderesize = undefined // Resize config, see react-use-measure's optionsorthographic = false // Creates an orthographic camera if truenoEvents = false // Switch off raytracing and event supportpixelRatio = undefined // You could provide window.devicePixelRatio if you likeinvalidateFrameloop = false // When true it only renders on changes, when false it's a game loopupdateDefaultCamera = true // Adjusts default camera on size changesonCreated // Callback when vdom is ready (you can block first render via promise)onPointerMissed /> // Response for pointer clicks that have missed a target
You can give it additional properties like style and className, which will be added to the container (a div) that holds the dom-canvas element.
Defaults that the canvas component sets up
Canvas will create a translucent WebGL-renderer with the following properties: antialias, alpha, setClearAlpha(0)
A default perspective camera: fov: 75, near: 0.1, far: 1000, z: 5, lookAt: [0,0,0]
A default orthographic camera if Canvas.orthographic is true: near: 0.1, far: 1000, z: 5, lookAt: [0,0,0]
A default shadowMap if Canvas.shadowMap is true: type: PCFSoftShadowMap
A default scene (into which all the JSX is rendered) and a raycaster.
A wrapping container with a resize observer: scroll: true, debounce: { scroll: 50, resize: 0 }
You do not have to use any of these objects, look under "Recipes" down below if you want to bring your own.
Edit this page on GitHub