-
Notifications
You must be signed in to change notification settings - Fork 16
/
VisWithHooks.jsx
99 lines (82 loc) · 2.3 KB
/
VisWithHooks.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React, { useEffect, useRef, useState } from 'react'
import * as THREE from 'three'
const VisWithHooks = () => {
const mount = useRef(null)
const [isAnimating, setAnimating] = useState(true)
const controls = useRef(null)
useEffect(() => {
let width = mount.current.clientWidth
let height = mount.current.clientHeight
let frameId
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000)
const renderer = new THREE.WebGLRenderer({ antialias: true })
const geometry = new THREE.BoxGeometry(1, 1, 1)
const material = new THREE.MeshBasicMaterial({ color: 0xff00ff })
const cube = new THREE.Mesh(geometry, material)
camera.position.z = 4
scene.add(cube)
renderer.setClearColor('#000000')
renderer.setSize(width, height)
const renderScene = () => {
renderer.render(scene, camera)
}
const handleResize = () => {
width = mount.current.clientWidth
height = mount.current.clientHeight
renderer.setSize(width, height)
camera.aspect = width / height
camera.updateProjectionMatrix()
renderScene()
}
const animate = () => {
cube.rotation.x += 0.01
cube.rotation.y += 0.01
renderScene()
frameId = window.requestAnimationFrame(animate)
}
const start = () => {
if (!frameId) {
frameId = requestAnimationFrame(animate)
}
}
const stop = () => {
cancelAnimationFrame(frameId)
frameId = null
}
mount.current.appendChild(renderer.domElement)
window.addEventListener('resize', handleResize)
start()
controls.current = { start, stop }
return () => {
stop()
window.removeEventListener('resize', handleResize)
mount.current.removeChild(renderer.domElement)
scene.remove(cube)
geometry.dispose()
material.dispose()
}
}, [])
useEffect(
() => {
if (isAnimating) {
controls.current.start()
} else {
controls.current.stop()
}
},
[isAnimating],
)
/* eslint-disable
jsx-a11y/click-events-have-key-events,
jsx-a11y/no-static-element-interactions
*/
return (
<div
className="vis"
ref={mount}
onClick={() => setAnimating(!isAnimating)}
/>
)
}
export default VisWithHooks