首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过道具react-three-fiber禁用效果

通过道具react-three-fiber禁用效果
EN

Stack Overflow用户
提问于 2020-11-11 07:37:34
回答 1查看 169关注 0票数 0

我有用react-three-fiber创建的场景,其中有一个使用react-postprocessing npm包的具有Glitch效果的EffectsComposer

我想使用html输入复选框来切换Glitch效果的active状态,然而,切换active属性的状态并不会禁用该效果。禁用复选框输入效果的正确方法是什么?

我在这里设置了一个沙箱,问题是https://codesandbox.io/s/tender-star-lw07h?file=/src/App.tsx:0-1488

这是代码。

代码语言:javascript
复制
import React, {
  Suspense,
  useRef,
  useEffect,
  useState,
  createRef,
  Fragment
} from "react";
import Outline from "./components/Outline";
import { EffectComposer, Glitch, Noise } from "react-postprocessing";
import { Canvas } from "react-three-fiber";
import { OrbitControls } from "drei";
import { Mesh } from "three";
import { GlitchMode } from "postprocessing";

const Box = (props: any) => (
  <mesh {...props}>
    <meshBasicMaterial attach="material" color="red" />
    <boxGeometry args={[1, 1, 1]} attach="geometry" />
  </mesh>
);

const Composer = ({ active }: any) => {
  const ref = React.useRef();

  return (
    <EffectComposer>
      <Glitch ref={ref} active={active} mode={GlitchMode.CONSTANT_WILD} />
    </EffectComposer>
  );
};

const App = () => {
  const ref = useRef();

  const [selection, select] = useState<Mesh>();
  const [active, activate] = useState<boolean>(true);

  useEffect(() => {
    if (selection) {
      console.log(ref.current);
    }
  }, [selection]);

  return (
    <Fragment>
      <div style={{ background: "#fff" }}>
        <label htmlFor="">Active {JSON.stringify(active)}</label>
        <input
          type="checkbox"
          defaultChecked={active}
          onChange={($event) => {
            activate($event.target.checked);
          }}
        />
      </div>
      <Canvas>
        <Box onClick={(mesh) => select(mesh)} />
        <Composer active={active} />
      </Canvas>
    </Fragment>
  );
};
EN

回答 1

Stack Overflow用户

发布于 2021-09-22 07:38:06

只要做就行了

代码语言:javascript
复制
const App = () => {
    ...
    <Canvas>
      <Box .../>
      {active && <Composer/>}
    </Canvas>

代码语言:javascript
复制
const Composer = () => {
  return (
    <EffectComposer>
      <Glitch active={true} mode={GlitchMode.CONSTANT_WILD} />
    </EffectComposer>
  );
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64778383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档