1.1.0 • Published 6 years ago

react-reviz3d v1.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

react-container-3d

A container for three.js canvas renderer as a react component

alt text

Install

npm install --save react-container-3d && three

Live Demo

Try it on CodeSandbox:

Edit 0360pzv070

Videos

Apps using this component

Usage

import * as THREE from 'three';
import Container3d from 'react-container3d';

...

<div className="canvas-3d">
<Container3d aspect={16 / 9} percentageWidth={"100%"}/>
</div>

Container Drawing Methods

p5.js (and the original Processing, of course) was used as inspiration to define the life cycle names: setup and update;

//called at when renderer is defined
Setup = (scene, camera, renderer) =>{
        console.log("starting 3d scene");
    }

//called every frame
Update = (scene, camera, renderer) =>{
        //console.log("scene", scene);
}

Visual style Component Props

prop nameprop typedescription
widthIntwidth in pixels
heightIntheight in pixels
percentageWidthString or Intpercentage related to screen width
aspectfloatwidth/height aspect ratio
fitScreenbooleanif the component will stretch it self to fit the screen open area
marginLeftIntmargin left in pixels
marginTopIntmargin top in pixels
marginBottomIntmargin bottom in pixels

Execution Methods

prop nameprop typeparameters
setupfunctionscene, camera, renderer
updatefunctionscene, camera, renderer
onHoverStartfunctionobject, scene, camera, renderer
onHoverEndfunctionobject, scene, camera, renderer
onErrorfunctionerror

3D Scene elements Props

prop nameprop typedescriptiondefault
addControlsbooleanif OrbitControls will be addedtrue
addGridbooleanif the GridHelper will be addedtrue
addLightbooleanif default light will be addedtrue
enableZoombooleanif the zoom from OrbitControls is enabledtrue
enableKeysbooleanif the keys from OrbitControls are enabledtrue
enablePanbooleanif the user can pan the viewport with the mouse. Feature from OrbitControlstrue

Default 3d elements

The grid, orbitControls and lights are by default added to the scene. they are also easily removed:

<Container3d aspect={1} percentageWidth={"100%"} addGrid={false} addControls={false} addLight = {false}/>

enableZoom, enablePan and enableKeys (from orbit controls) are also defined as boolean props.

FitScreen mode

Sometimes we just want a 3D environment to add some models and have some interactivity. Due to the drag of attention to this environment, most of times, this component occupies the main portion of the screen, so we added this simple 'fitScreen' parameter, if this is on the component will try to adjust to fill the height and width of the screen. you can use the props marginTop, marginLeft, marginBottom, marginRight to set pixels of distance. (note: we tried with css, however the renderer update wasn't happening when the screen changed).

Usage with react-cubeview

Both components were designed to work coupled - you can read more about react-cubeview here. They were separated due to development simplification (We might, at some point, have enough 3D UI components to create a simple UI Kit... but that's for the future).

import * as THREE from 'three';
import Container3d from 'react-container-3d';

//you need to install it using npm install --save react-cubeview
import CubeView from 'react-cubeview';
import 'react-cubeview/css/react-cubeview.css';

let object1;

class App extends Component {
    //will return the main container (canvas)
    getMainCanvas() {
        var mainCanvas = ReactDOM.findDOMNode(this.c3d);
        return mainCanvas;
    }
    //will update the camera angles/position from the orbitcontrols on the c3d
    updateAngles(phi, theta) {
        this.c3d.setAngles(phi, theta);
    }

    onHoverStart(object, scene, camera, renderer){
        console.log("hovering", object);
    }

    onHoverEnd(object, scene, camera, renderer){
        console.log("exiting", object);
    }
    //called when the scene is created
    Setup = (scene, camera, renderer) =>{
        //add cool 3d objects here ~ remember to import THREE
        var geometry = new THREE.SphereGeometry( 1 );
		var material = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true } );
        object1 = new THREE.Mesh( geometry, material );
				scene.add( object1 );
    }

    //called every frame
    Update = (scene, camera, renderer) =>{
        //animate things
        object1.rotation.x = time * 0.0005;
		object1.rotation.y = time * 0.001;
    }

    render(){
        return(
            <div className = "App">
                
                <div className="canvas-3d" >
                    <Container3d marginTop={30} aspect={16 / 9} percentageWidth={"100%"} fitScreen ref={(c) => this.c3d = c} key={"c3d"} marginBottom={110} update={this.Update} setup={this.Setup} onHoverStart={this.onHoverStart} onHoverEnd={this.onHoverEnd}/>
                </div>

                <div className="cube-view">
                    <CubeView aspect={1} hoverColor={0x0088FF} cubeSize={2} zoom={6} antialias={false} onUpdateAngles={this.updateAngles} relatedCanvas={this.getMainCanvas} />
                </div>

            </div>
        );
    }

}

Custom Size/Background (CSS)

If wrapped around a div component, you can play easily with the canvas size;

By default, the canvas has a transparent background, this means your background color must be added using css background. We wanted gradient backgrounds.

    <div className="canvas-3d">
        <Container3d ... >
    </div>
.canvas-3d{
    margin-top:30px;
    width:100%;
    min-width: 500px;
    background: linear-gradient(to bottom,  rgba(51,51,51,0) 0%,rgba(80,80,80,1) 100%);
  }

Loading 3D Models

You can use pretty much all three.js model loaders, however be awere if you're using ES6, make sure all of them are wrapped as ES6 classes or ES5 modules. Also, for .obj we tested the three-react-obj-loader combined with three-react-mtl-loader.

Storybook

clone or download the git page and run:

npm install 
npm start

check http://localhost:6006

1.1.0

6 years ago

0.1.31

6 years ago

0.1.21

6 years ago

0.1.20

6 years ago

0.1.19

6 years ago

0.1.18

6 years ago

0.1.17

6 years ago

0.1.16

6 years ago

0.1.15

6 years ago

0.1.14

6 years ago

0.1.13

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago