Published
Edited
Aug 15, 2021
Insert cell
Insert cell
Insert cell
Three = require("three")
Insert cell
RTF = import('https://cdn.skypack.dev/@react-three/fiber@7.0.6?min')
Insert cell
import { React, jsx } from '@mbostock/hello-jsx'
Insert cell
function Box(props){
// This reference will give us direct access to the THREE.Mesh object
const mesh = React.useRef();

// Set up state for the hovered and active state
const [hovered, setHover] = React.useState(false);
const [active, setActive] = React.useState(false);

// Subscribe this component to the render-loop, rotate the mesh every frame
React.useFrame((state, delta) => (mesh.current.rotation.x += 0.01));

// Return the view, these are regular Threejs elements expressed in JSX
return (
jsx`<mesh
{...props}
ref={mesh}
scale={active ? 1.5 : 1}
onClick={(event) => setActive(!active)}
onPointerOver={(event) => setHover(true)}
onPointerOut={(event) => setHover(false)}>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
</mesh>`
)
}
Insert cell
Canvas = RTF.Canvas
Insert cell
## Try one using included React
Insert cell
{
React.render(jsx({Canvas, Box})`
<Canvas>
<ambientLight />
<pointLight position={[10, 10, 10]} />
<Box position={[-1.2, 0, 0]} />
<Box position={[1.2, 0, 0]} />
</Canvas>
`)
}
Insert cell
Insert cell
{
RTF.render(jsx({Canvas, Box})`
<Canvas>
<ambientLight />
<pointLight position={[10, 10, 10]} />
<Box position={[-1.2, 0, 0]} />
<Box position={[1.2, 0, 0]} />
</Canvas>
`)
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more