Open cisen opened 5 years ago
在react里面使用three.js https://github.com/drcmda/react-three-fiber
// React.memo export const Canvas = React.memo(({ children, props, camera, style, ...rest }) => { })
useEffect(() => { state.current.gl = new THREE.WebGLRenderer({ canvas: canvas.current, antialias: true, alpha: true, ...props }) state.current.gl.setPixelRatio((window.devicePixelRatio || 2) * 0.5) state.current.gl.setClearAlpha(0) state.current.canvas = canvas.current state.current.scene = new THREE.Scene() state.current.scene.__interaction = [] const renderLoop = function() { if (!state.current.active) return requestAnimationFrame(renderLoop) if (state.current.ready) { state.current.subscribers.forEach(fn => fn(state.current)) if (!state.current.manual && state.current.scene.children.length) { state.current.gl.render(state.current.scene, state.current.camera) } } } // Start render-loop requestAnimationFrame(renderLoop) // Clean-up return () => { state.current.active = false unmountComponentAtNode(state.current.scene) // TODO: Clean up and dispose scene ... } }, [])
通过不断在渲染空隙执行state.current.gl.render更新canvas里面的图像
state.current.gl.render
还以为是 react-three-fiber 源码解读
说明
在react里面使用three.js https://github.com/drcmda/react-three-fiber
好东西
在无状态组件里面安装pureComponent
动画原理
通过不断在渲染空隙执行
state.current.gl.render
更新canvas里面的图像问答