2

I have seen other questions on StackOverflow about this but none of them covered my error. I am building a web app on React and thought everything was ok until I realized that when I press F11 and go fullscreen particles.js doesnt cover the whole background. This is even more noticeable when playing with responsiveness. Driving me insane since I tried all the CSS configurations I saw here. Here is my app.css code:

/* Styles go here */
#account{
   font-size: 0.9rem;
   color: aliceblue;
}

.mi-navbar{
   height: 60px;
}

.mi-tabla{
   margin-top: 70px;
}

html, body {
   background-color: black;
   height: 25px;
}

p{
   color: aliceblue;
}

canvas {
   display: block;
   vertical-align: bottom;
}

#particles-js {
   position: absolute;
   width: 100%;
   height: 100vh;
}

.main-content{
   overflow: hidden;
   height: 100vh;
}

#loader{
   margin-top: 270px;
   text-align: center;
   font-size: 2rem;
}

My App.js file: (probably only relevant at the render and return functions)

import React, { Component } from 'react'
import Web3 from 'web3'
import DaiToken from '../abis/DaiToken.json'
import DappToken from '../abis/DappToken.json'
import TokenFarm from '../abis/TokenFarm.json'
import Navbar from './Navbar'
import Main from './Main'
import './App.css'
import ParticleBackground from '../particleBackground'

class App extends Component {

  async componentWillMount() {
    await this.loadWeb3()
    await this.loadBlockchainData()
  }

  async loadBlockchainData() {
    const web3 = window.web3

    const accounts = await web3.eth.getAccounts()
    this.setState({ account: accounts[0] })

    const networkId = await web3.eth.net.getId()

    // Load DaiToken
    const daiTokenData = DaiToken.networks[networkId]
    if(daiTokenData) {
      const daiToken = new web3.eth.Contract(DaiToken.abi, daiTokenData.address)
      this.setState({ daiToken })
      let daiTokenBalance = await daiToken.methods.balanceOf(this.state.account).call()
      this.setState({ daiTokenBalance: daiTokenBalance.toString() })
    } else {
      window.alert('DaiToken contract not deployed to detected network.')
    }

    // Load DappToken
    const dappTokenData = DappToken.networks[networkId]
    if(dappTokenData) {
      const dappToken = new web3.eth.Contract(DappToken.abi, dappTokenData.address)
      this.setState({ dappToken })
      let dappTokenBalance = await dappToken.methods.balanceOf(this.state.account).call()
      this.setState({ dappTokenBalance: dappTokenBalance.toString() })
    } else {
      window.alert('DappToken contract not deployed to detected network.')
    }

    // Load TokenFarm
    const tokenFarmData = TokenFarm.networks[networkId]
    if(tokenFarmData) {
      const tokenFarm = new web3.eth.Contract(TokenFarm.abi, tokenFarmData.address)
      this.setState({ tokenFarm })
      let stakingBalance = await tokenFarm.methods.stakingBalance(this.state.account).call()
      this.setState({ stakingBalance: stakingBalance.toString() })
    } else {
      window.alert('TokenFarm contract not deployed to detected network.')
    }

    this.setState({ loading: false })
  }

  async loadWeb3() {
    if (window.ethereum) {
      window.web3 = new Web3(window.ethereum)
      await window.ethereum.enable()
    }
    else if (window.web3) {
      window.web3 = new Web3(window.web3.currentProvider)
    }
    else {
      window.alert('Non-Ethereum browser detected. You should consider trying MetaMask!')
    }
  }

  stakeTokens = (amount) => {
    this.setState({ loading: true })
    this.state.daiToken.methods.approve(this.state.tokenFarm._address, amount).send({ from: this.state.account }).on('transactionHash', (hash) => {
      this.state.tokenFarm.methods.stakeTokens(amount).send({ from: this.state.account }).on('transactionHash', (hash) => {
        this.setState({ loading: false })
      })
    })
  }

  unstakeTokens = (amount) => {
    this.setState({ loading: true })
    this.state.tokenFarm.methods.unstakeTokens().send({ from: this.state.account }).on('transactionHash', (hash) => {
      this.setState({ loading: false })
    })
  }

  constructor(props) {
    super(props)
    this.state = {
      account: '0x0',
      daiToken: {},
      dappToken: {},
      tokenFarm: {},
      daiTokenBalance: '0',
      dappTokenBalance: '0',
      stakingBalance: '0',
      loading: true
    }
  }

  render() {
    let content
    //CHANGED THIS.STATE.LOADING TO FALSE SO THAT APP WORKS WITHOUT GANACHE (BUT WONT BE ABLE TO STAKE TOKENS)
    if(this.state.loading == false) {
      content = <p id="loader" className="text-center">Loading...</p>
    } else {
      content = <Main
        daiTokenBalance={this.state.daiTokenBalance}
        dappTokenBalance={this.state.dappTokenBalance}
        stakingBalance={this.state.stakingBalance}
        stakeTokens={this.stakeTokens}
        unstakeTokens={this.unstakeTokens}
      />
    }

    return (
      <div>
        <Navbar account={this.state.account} />
        <ParticleBackground/>
        <div className="main-content">
          <div className="container-fluid mt-5">
            <div className="row">
              <main role="main" className="col-lg-12 ml-auto mr-auto" style={{ maxWidth: '600px' }}>
                <div className="content mr-auto ml-auto">
                  <a
                    href="#"
                    target="_blank"
                    rel="noopener noreferrer"
                  >
                  </a>
                  {content}
                </div>
              </main>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

export default App;

My particles component:

import React from "react"
import Particles from 'react-particles-js';
import particleConfig from './config/particle-config';

export default function ParticleBackground() {
  return (
    <>
        <div id="particles-js">
            <Particles params={particleConfig}></Particles> 
        </div>
    </>
  );
}

and my particles-config.js file:

const particleConfig = {
        particles: {
          number: {
            value: 160,
            density: {
              enable: true,
              value_area: 800
            }
          },
          color: {
            value: "#ffffff"
          },
          shape: {
            type: "circle",
            stroke: {
              width: 0,
              color: "#000000"
            },
            polygon: {
              nb_sides: 5
            },
            image: {
              src: "img/github.svg",
              width: 100,
              height: 100
            }
          },
          opacity: {
            value: 1,
            random: true,
            anim: {
              enable: true,
              speed: 1,
              opacity_min: 0,
              sync: false
            }
          },
          size: {
            value: 3,
            random: true,
            anim: {
              enable: false,
              speed: 4,
              size_min: 0.3,
              sync: false
            }
          },
          line_linked: {
            enable: false,
            distance: 150,
            color: "#ffffff",
            opacity: 0.4,
            width: 1
          },
          move: {
            enable: true,
            speed: 1,
            direction: "none",
            random: true,
            straight: false,
            out_mode: "out",
            bounce: false,
            attract: {
              enable: false,
              rotateX: 600,
              rotateY: 600
            }
          }
        },
        interactivity: {
          detect_on: "window",
          events: {
            onhover: {
              enable: true,
              mode: "repulse"
            },
            onclick: {
              enable: true,
              mode: "push",
            },
            resize: true
          },
          modes: {
            grab: {
              distance: 400,
              line_linked: {
                opacity: 1
              }
            },
            bubble: {
              distance: 400,
              size: 40,
              duration: 2,
              opacity: 1,
              speed: 3
            },
            repulse: {
              distance: 150,
              duration: 0.5
            },
            push: {
              particles_nb: 20
            },
            remove: {
              particles_nb: 2
            }
          }
        },
        retina_detect: true
      }
    export default particleConfig;

https://no-ganache--wolfgangfarming.netlify.app/ if anyone wants to see the actual website and inspect over there.

Thanks in advance

Facu Bozzi
  • 31
  • 5
  • Can you share your `ParticleBackground` component code as well so we see what you are attempting to render? BTW, your netlify link renders a blank page "root" div is empty. – Drew Reese Nov 04 '21 at 04:16
  • @DrewReese just edited adding the particles component... forgot about that. As of the netlify link I just made a new deploy for a branch that doesnt require anything blockchain related. Here is the link: https://no-ganache--wolfgangfarming.netlify.app/ I will also update it in the post. thanks! – Facu Bozzi Nov 04 '21 at 05:33
  • #tsparticles{height:100%;} ? – A.J.Bauer Nov 04 '21 at 05:43
  • 1
    Does this answer your question, setting the `width` and `height` props on the `Particles` component? https://stackoverflow.com/a/62888121/8690857 Or this? https://stackoverflow.com/a/62826410/8690857 – Drew Reese Nov 04 '21 at 05:45
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Nov 04 '21 at 19:45
  • @A.J.Bauer for some reason i dont remember i installed tsparticles but never actually used it in the project, no components or html/css. Just appears on my package.json – Facu Bozzi Nov 04 '21 at 21:48
  • 1
    @DrewReese god bless you Drew Reese, setting it in the component itself as 100vh and vw did it for me. Feel free to post it as an answer so I can accept it. Thanks – Facu Bozzi Nov 04 '21 at 22:00

0 Answers0