简体   繁体   English

如何将后台 JS animation 集成到我的反应应用程序中?

[英]How do I integrate a background JS animation into my react app?

I'm trying to add a firefly animation into a component within react (and the code I'm trying to use is here https://codepen.io/celli/pen/xZgpvN ).我正在尝试将萤火虫 animation 添加到 react 中的组件中(我尝试使用的代码在这里https://codepen.io/celli/pen/xZgpvN )。 If I had a simple HTML / CSS / Javascript setup I'd know what to do, but I'm confused on how to integration this animation in the context of React and in the scope of a single component. If I had a simple HTML / CSS / Javascript setup I'd know what to do, but I'm confused on how to integration this animation in the context of React and in the scope of a single component. Where do I put the code below?我把下面的代码放在哪里? How do I reference it within the component?如何在组件中引用它?


var total=40,
    container=document.getElementById('container'),
    w=window.innerWidth,
    h=window.innerHeight,
    Tweens=[],
    SPs=1;



for (var i=total;i--;){ 
    var Div=document.createElement('div');
    TweenLite.set(Div,{attr:{class:'dot'},x:R(w),y:R(h),opacity:0});
    container.appendChild(Div); Anim(Div);  Tweens.push(Div);
};

function Anim(elm){ 
    elm.Tween=TweenLite.to(elm,R(20)+10,{bezier:{values:[{x:R(w),y:R(h)},{x:R(w),y:R(h)}]},opacity:R(1),scale:R(1)+0.5,delay:R(2),onComplete:Anim,onCompleteParams:[elm]})
};

for(var i=total;i--;){
  Tweens[i].Tween.play()};


function R(max){return Math.random()*max};

Secondly, where do I put the CSS code below?其次,我在哪里把 CSS 代码放在下面?

body{
  background-color: #222222;
  overflow:hidden;
}

.dot{
  width:4px;
  height:4px;
  position:absolute;
  background-color:#ff00cc;
  box-shadow:0px 0px 10px 2px #ff00cc;
  border-radius: 20px;
  z-index:2;
}

#container {
width:100%; 
height:100%;
}

React's page on Integrating With Other Libraries would probably be the best place to start. React 关于 与其他库集成的页面可能是最好的起点。

Firstly, here's an example that can be run directly on StackOverflow.首先,这是一个可以直接在 StackOverflow 上运行的示例。

 const { useRef, useLayoutEffect, } = React // import React, { useRef, useLayoutEffect } from 'react'; function R(max) { return Math.random() * max }; function Background(props) { const { total = 40 } = props const ref = useRef() useLayoutEffect(() => { const contanier = ref.current const w = window.innerWidth const h = window.innerHeight const dots = [] function addAnimation(elm) { return TweenLite.to(elm, R(20) + 10, { bezier: { values: [{ x: R(w), y: R(h) }, { x: R(w), y: R(h) }] }, opacity: R(1), scale: R(1) + 0.5, delay: R(2), onComplete: addAnimation, onCompleteParams: [elm] }) } for (let i = 0; i < total; i++) { const div = document.createElement('div') TweenLite.set(div, { attr: { class: 'dot' }, x: R(w), y: R(h), opacity: 0 }) container.appendChild(div); const dot = addAnimation(div) dot.play() dots.push(dot) } return () => { // Clear animations and whatever here dots.forEach(dot=>dot.kill()) container.innerHTML = '' } }, [total]) return <div className="fireflies" ref={ref} /> } function App() { return <Background total={25} / > } ReactDOM.render( <App / >, document.querySelector("#container"))
 body { background-color: #222222; overflow: hidden; }.dot { width: 4px; height: 4px; position: absolute; background-color: #ff00cc; box-shadow: 0px 0px 10px 2px #ff00cc; border-radius: 20px; z-index: 2; }.fireflies { width: 100%; height: 100%; }
 <div id="container" /> <:-- React --> <script src="https.//cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min:js"></script> <script src="https.//cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min:js"></script> <.-- TweenMax --> <script src="https.//cdnjs.cloudflare.com/ajax/libs/gsap/1.15.0/TweenMax.min.js"></script>

Now the explanation.现在解释。

A quick side note: the previously mentioned Integrating With Other Libraries page as of right now uses class components, but I like to use functional components because hooks are ₜₕₑ fᵤₜᵤᵣₑ.附注:前面提到的与其他库集成页面现在使用 class 组件,但我喜欢使用功能组件,因为钩子是ₜₕₑ fᵤₜᵤᵣₑ。 I also converted some of the code from old es5 to new modern es6 because modern js is ₜₕₑ fᵤₜᵤᵣₑ.我还将一些代码从旧的 es5 转换为新的现代 es6,因为现代 js 是ₜₕₑ fᵤₜᵤᵣₑ。

The basic idea is that when your component is first mounted, you run all the initial setup inside of a useLayoutEffect hook.基本思想是,当您的组件首次安装时,您在useLayoutEffect挂钩内运行所有初始设置。 That essentially boils down to throwing most of your current js inside of that hook.这基本上归结为将您当前的大部分 js 扔到该钩子中。

The only thing the component needs to render is the div that will contain all of the dots.组件唯一需要渲染的是包含所有点的 div。

The advantage here is that with react, you get to control aspects of your animation with props--demonstrated in the example by allowing the total variable to be passed as a prop.这里的优点是,通过 react,您可以使用 props 控制 animation 的各个方面——在示例中通过允许将total变量作为 prop 传递来进行演示。

Also, since React provides a way to get a reference to a dom node via the useRef hook, you can use that instead of document.querySelector('#contanier') .此外,由于 React 提供了一种通过useRef挂钩获取对 dom 节点的引用的方法,因此您可以使用它来代替document.querySelector('#contanier')

Where do I put the CSS我在哪里把 CSS

The best place really depends on how you set up your react application.最好的地方真的取决于你如何设置你的反应应用程序。

If you have an HTML template somewhere, you can just throw a style tag or a link tag in there as you would any other project.如果您在某处有 HTML 模板,您可以像在任何其他项目中一样在其中添加样式标签或链接标签。

If you used create-react-app or have a similar setup then you might just import them into your component by importing the stylesheet , by using css modules , or by modifying src/index.css .如果您使用 create-react-app 或具有类似设置,那么您可以通过导入样式表、使用css 模块或修改src/index.css将它们导入组件。

Another option is to use css-in-js solutions such as emotion or styled-components but those will probably take a little getting used to.另一种选择是使用 css-in-js 解决方案,例如emotionstyled-components ,但这些可能需要一点时间来适应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM