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

Something wrong in vue3 #92

Closed 6lszxz closed 1 year ago

6lszxz commented 1 year ago

I have a project using vue3, and here is part of my code:

<script setup>
import Granim from 'granim';
const logoAnimation = new Granim({
  element:'.logoCanvas',
})
</script>

<template>
  <div class="list">
    <canvas class="logoCanvas"></canvas>
    </div>

  </div>

</template>

The browser throws an error with Uncaught (in promise) Error:.logoCanvascould not be found in the DOM, how to solve the problem. :(

6lszxz commented 1 year ago

Okay, I have found a way to solve the problem. Just use the function nextTick( ). The code is like this:

<script setup>
import Granim from 'granim';
import {ref, nextTick} from 'vue';
const logoCanvas=ref(null);
nextTick(()=>{
  const logoAnimation = new Granim({
    element: logoCanvas.value,
    direction: 'left-right',
    isPausedWhenNotInView: true,
    states : {
        "default-state": {
            gradients: [
                ['#ff9966', '#ff5e62'],
                ['#00F260', '#0575E6'],
                ['#e1eec3', '#f05053']
            ]
        }
    }
  })
});
</script>
<canvas id="logoCanvas" ref="logoCanvas"></canvas>
</template>