Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use this with nextjs? #7

Open
krishnerkar opened this issue May 24, 2021 · 5 comments
Open

How to use this with nextjs? #7

krishnerkar opened this issue May 24, 2021 · 5 comments

Comments

@krishnerkar
Copy link

krishnerkar commented May 24, 2021

I am not able to use this in nextjs, everytime i import it in a class and try to call it in componentDidMount(), the import throws an error called window not defined

and when i try dynamic importing like this

const grained = dynamic(
    () => {
      return import ('../../grained.min.js');
    },
    { ssr: false }
  );

and call it in componentdidmount like this

componentDidMount(){
        grained('#container', {});
    }

i get an error, cannot call a class as a function

@larsmosr
Copy link

larsmosr commented Feb 26, 2022

Put grained.js in your public folder and than load script via Nextjs Script Tag:

<Script
      src="/grained.js" 
      onLoad={() => { 
          var options = { 
                animate: true, 
                patternWidth: 100, 
                patternHeight: 100, 
                grainOpacity: 0.5, 
                grainDensity: 1, 
                grainWidth: 1, 
                grainHeight: 1, 
           };

 grained("#container", options); }} />

@SaadBazaz
Copy link
Contributor

@larovski Thanks, this worked!

@SaadBazaz
Copy link
Contributor

SaadBazaz commented Jun 17, 2022

Hint: This might create some issues when rerouting. E.g. Suppose you have two pages, A and B.
If grained #container is on A, and you route to B, then route back to A, the grained container might start shaking violently and causing problems in scrolling.

To prevent this, I encapsulated the #container in a parent which had overflow:hidden applied on it.

An example:

import { useRef, } from 'react';
import Script from 'next/script';

export default function PageA() {

  const grained_container = useRef();

  return (
        <div style={{
          position: "absolute",
          zIndex: 999,
          height: "100%",
          width: "100%",
          pointerEvents: "none",
          top:0,
          paddingLeft: 0,
          paddingRight: 0,
          paddingTop: 0,
          overflow: "hidden",
        }}>
        <div 
        ref={grained_container}
        id="container" style={{
          height: "100%",
          width: "100%",
        }}/>
        { typeof window !== "undefined" ? 
        window.grained ? <></> :  
        <Script
        src="/grained.js" 
        onLoad={() => {
            var options = { 
                  animate: true, 
                  patternWidth: 300, 
                  patternHeight: 300, 
                  grainOpacity: 0.05, 
                  grainDensity: 1, 
                  grainWidth: 1, 
                  grainHeight: 1, 
                  grainChaos: 0.2,
                  grainSpeed: 10
              };
  
          grained(grained_container.current, options);

        }} />
        :
        <></>  
        }
        </div>
        )
}

Note: I use useRef because I've edited grained.js . You can choose to use it as "#container".

@larsmosr
Copy link

@SaadBazaz thx for coming back and provide an extended solution 👍🏻

@sudhanshu0001
Copy link

sudhanshu0001 commented Oct 12, 2023

@larovski you are right but we create build and then showed the error like grained is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants