1import React, { useRef, useState } from 'react'
2import { Canvas, useFrame } from 'react-three-fiber'
3
4function Box({ position, rotationSpeed, color }) {
5const mesh = useRef()
6const [active, setActive] = useState(false)
7
8useFrame(() => (mesh.current.rotation.x = mesh.current.rotation.y += rotationSpeed / 100))
9
10return (
11<mesh
12position={position}
13ref={mesh}
14scale={active ? [1.5, 1.5, 1.5] : [1, 1, 1]}
15onClick={e => setActive(!active)}>
16<boxBufferGeometryBufferGeometry attach="geometry" args={[1, 1, 1]} />
17<meshStandardMaterial attach="material" color={color}/>
18</mesh>
19)
20}
21
22export default function App() {
23return <Canvas>
24<ambientLight />
25<pointLight position={[10, 10, 10]} />
26<Box position={[0,0,0]} rotationSpeed={1} color="#7f0000" />
27</Canvas>
28}
1// this code will change
2const ratio = window.innerWidth / window.innerHeight
3const scene = new THREE.Scene()
4const camera = new THREE.PerspectiveCamera(75, ratio, 0.1, 1000)
5const renderer = new THREE.WebGLRenderer()
6
7renderer.setSize(window.innerWidth, window.innerHeight)
8document.body.appendChild(renderer.domElement)
9
10const geometry = new THREE.BoxGeometry()
11const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })
12const cube = new THREE.Mesh(geometry, material)
13scene.add(cube)
14
15camera.position.z = 5
16
17const animate = function () {
18requestAnimationFrame(animate)
19
20cube.rotation.x += 0.01
21cube.rotation.y += 0.01
22
23renderer.render(scene, camera)
24}
25
26animate()
27
1import React, { useRef, useState } from 'react'
2import { Canvas, useFrame } from 'react-three-fiber'
3
4function Box({ position, rotationSpeed, color }) {
5const mesh = useRef()
6const [active, setActive] = useState(false)
7
8useFrame(() => (mesh.current.rotation.x = mesh.current.rotation.y += rotationSpeed / 100))
9
10return (
11<mesh
12position={position}
13ref={mesh}
14scale={active ? [1.5, 1.5, 1.5] : [1, 1, 1]}
15onClick={(e) => setActive(!active)}>
16<BoxBufferGeometry attach="geometry" args={[1, 1, 1]} />
17<basicMaterial attach="material" color={color} />
18</mesh>
19)
20}
21