sarcadass / granim.js

Create fluid and interactive gradient animations with this small javascript library.
https://sarcadass.github.io/granim.js/
MIT License
5.29k stars 234 forks source link

Add example using reactjs #16

Closed nerijusgood closed 8 years ago

nerijusgood commented 8 years ago

Hi,

I have been trying to use it with reactjs. Everytime I try to load it (on client side after content is loaded), I always get the Uncaught TypeError: this.canvas.getContext is not a function(…) error from Granim.js.

pranaygp commented 8 years ago

Could you please show me an example snippet of your code?

nerijusgood commented 8 years ago

Thats what I did. I even tried with event listener:

componentDidMount() {
    window.addEventListener('load', this.handleBgEffects.bind(this))
  }

  handleBgEffects() {
    const granimInstance = new Granim({
      element: '#canvas-basic',
      name: 'basic-gradient',
      direction: 'left-right',
      opacity: [1, 1],
      isPausedWhenNotInView: true,
      states: {
        'default-state': {
          gradients: [
            ['#AA076B', '#61045F'],
            ['#02AAB0', '#00CDAC'],
            ['#DA22FF', '#9733EE'],
          ],
        },
      },
    })
    granimInstance()
  }
pranaygp commented 8 years ago

I don't believe you need an event listener for this. You also shouldn't invoke the instance directly. If I'm right, it should be pretty straightforward.

Try something like this and let me know if it works:

componentDidMount() {
   this.handleBgEffects();
  }

  handleBgEffects() {
    this.granimInstance = new Granim({
      element: '#canvas-basic',
      name: 'basic-gradient',
      direction: 'left-right',
      opacity: [1, 1],
      isPausedWhenNotInView: true,
      states: {
        'default-state': {
          gradients: [
            ['#AA076B', '#61045F'],
            ['#02AAB0', '#00CDAC'],
            ['#DA22FF', '#9733EE'],
          ],
        },
      },
    })
  }
nerijusgood commented 8 years ago

Still same issue: Dynamic page loading failed TypeError: this.canvas.getContext is not a function(…)

pranaygp commented 8 years ago

Is this componentDidMount in the same class that is rendering '#canvas-basic'?

sarcadass commented 8 years ago

@nerijusgood Do you have a <canvas id="canvas-basic"></canvas> set in your HTML? I think the error is due to the fact that your options.element don't aim a <canvas> element.

pranaygp commented 8 years ago

@sarcadass It's a little different with React since he isn't directly writing HTML, rather using JSX and getting React to render these dynamically into the DOM

sarcadass commented 8 years ago

Yeah I know, I wasn't very clear in my last comment but is the element in the DOM when the Granim instance is intiated?

Le 15 sept. 2016 9:42 PM, "Pranay Prakash" notifications@github.com a écrit :

@sarcadass https://github.com/sarcadass It's a little different with React since he isn't directly writing HTML, rather using JSX and getting React to render these dynamically into the DOM

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sarcadass/granim.js/issues/16#issuecomment-247430906, or mute the thread https://github.com/notifications/unsubscribe-auth/ACVse-Db3tCMM8rNOn4nlGYjDZCPEBAsks5qqZ-6gaJpZM4J90xe .

pranaygp commented 8 years ago

@nerijusgood I'm gonna close this issue since it's not directly related to the codebase, but let's continue this conversation on gitter

nerijusgood commented 8 years ago

@sarcadass you are right - I actually needed specifically use it on canvas element. I actually used regular div.

It works perfect with <canvas id="canvas-basic"></canvas>

Thank you.