更新:我在底部添加了一个代码框,更详细地说明了我需要做什么。
所以我不太了解 React 中的类是如何工作的,我是新来的 react 并且使用过一点 useState,但从来不知道如何处理一个类。我正在使用这个 react 包,它有一个示例,说明如何使用控制器让您使用现在称为 rive 的耀斑构建的动画交互。https://github.com/2d-inc/Flare-React#readme
当我将鼠标悬停在生成的画布元素上时,我想要实现的是运行不同的动画或再次运行相同的动画。我可以在 flate(rive) 中创建第二个动画,它仍然会在同一个 .flr 文件中输出,然后我应该能够在控制器中引用它并在悬停时运行它,只是坚持如何做那部分,或者甚至让这个控制器工作。需要注意的一件事是我可以让动画在没有控制器的情况下正常运行。
在文档中,他们有这个示例代码
class PenguinController extends FlareComponent.Controller
{
constructor()
{
super();
this._MusicWalk = null;
this._Walk = null;
this._WalkTime = 0;
}
initialize(artboard)
{
this._MusicWalk = artboard.getAnimation("music_walk");
this._Walk = artboard.getAnimation("walk");
}
advance(artboard, elapsed)
{
// advance the walk time
this._WalkTime += elapsed;
const { _MusicWalk: musicWalk, _Walk: walk, _WalkTime: walkTime } = this;
// mix the two animations together by applying one and then the other (note that order matters).
walk.apply(walkTime % walk.duration, artboard, 1.0);
// if you want to slowly disable the head bobbing (musicWalk animation) you could ramp down the
// final argument (the mix argument) to 0.0 over some time. For now we're mixing at full strength.
musicWalk.apply(walkTime % musicWalk.duration, artboard, 1.0);
// keep rendering
return true;
}
}
首先什么是构造函数?超级是什么意思?那么他们在构造函数中定义了什么,是某种状态,我如何确定在这里定义什么?
对于初始化,我假设我将它与上面的状态匹配,并以我在flare(rive)中命名的名称获取动画
我不太明白的高级部分是我们将动画设置为this._WalkTime += elapsed;
动画运行多长时间?我想我理解应用部分,它将持续时间应用于动画。
接下来它有这个代码
class MyComponent extends React.Component
{
constructor()
{
this.state = { penguinController: new PenguinController() };
}
render()
{
return <FlareComponent controller={this.state.penguinController} /*... more properties here ...*/ />;
}
}
这是我尝试的代码,目前我收到以下错误
TypeError: Cannot set property 'state' of undefined
import React from "react"
import PropTypes from "prop-types"
import { useStaticQuery, graphql } from "gatsby"
import FlareComponent from 'flare-react';
import styled from 'styled-components'
import Header from "./header"
import "../sass/index.scss"
const LogoWrapper = styled.div`
width:200px;
height:200px;
`
class AnimationController extends FlareComponent.Controller
{
constructor()
{
super();
this._MusicWalk = null;
}
initialize(artboard)
{
this._MusicWalk = artboard.getAnimation("Wispy Appear");
}
advance(artboard, elapsed)
{
const { _MusicWalk: musicWalk } = this;
musicWalk.apply(musicWalk.duration, artboard, 1.0);
// keep rendering
return true;
}
}
const Layout = ({ children }) => {
const data = useStaticQuery(graphql`
query SiteTitleQuery {
site {
siteMetadata {
title
}
}
}
`)
this.state = { AnimationController: new AnimationController() };
return (
<>
<LogoWrapper>
<FlareComponent width={200} height={200} animationName="Wispy Appear" controller={this.state.AnimationController} file="/wispy-2.flr"/>
</LogoWrapper>
<main className="main-wrapper">{children}</main>
<footer>
© {new Date().getFullYear()}, Built with
{` `}
<a href="https://www.gatsbyjs.org">Gatsby</a>
</footer>
</>
)
}
Layout.propTypes = {
children: PropTypes.node.isRequired,
}
export default Layout
作为参考,有一个关于如何使动画交互的教程,但它是用于颤振的,但它对那里的 api https://medium.com/rive/building-a-responsive-house-with-flare-flutter-有一些见解31af823ba805
更新
这是我尝试阅读 es6 中的类后的新尝试代码,我仍然需要了解更多信息。
那么我该如何在点击或悬停或任何事件上运行第二个动画。动画现在运行一次,但我不知道如何使用控制器?
import React from "react"
import Img from 'gatsby-image'
import styled from "styled-components"
import FlareComponent from 'flare-react';
import Layout from "../components/layout"
const LogoWrapper = styled.div`
width:200px;
height:200px;
`
class wispyController extends FlareComponent.Controller
{
constructor()
{
super();
this._Animate = null;
}
initialize(artboard)
{
this._Animate = artboard.getAnimation("Wispy Appear");
}
advance(artboard, elapsed)
{
const { _Animate: animate } = this;
animate.apply(5, artboard, 1.0);
// keep rendering
return true;
}
}
class IndexPage extends React.Component {
constructor()
{
super();
this.state = { wispyController: new wispyController() };
}
render(){
const {data} = this.props;
return(
<Layout>
<LogoWrapper>
<FlareComponent width={200} height={200} animationName="Wispy Appear" controller={this.state.wispyController} file="/Wispy.flr"/>
</LogoWrapper>
{data.allSanityBlogPost.edges.map(({ node: post }) => (
<article key={post.id}>
<h1>{post.name}</h1>
<img src={post.imageGif.asset.url}/>
</article>
))}
</Layout>
)
}
}
export default IndexPage
export const query = graphql`
query IndexQuery {
allSanityBlogPost {
edges {
node {
name
id
imageGif {
asset {
url
}
}
}
}
}
}
`
好的希望有人可以在这里提供帮助,我制作了一个代码框,以便有人可以看到我想要实现的目标。有两个动画,在页面上你可以看到第一个有一个控制器应该混合两者,然后是另外两个动画。我想要发生的是第一个动画运行,然后是第二个在悬停时运行。这里是代码沙箱https://codesandbox.io/s/frosty-water-jnj9m