|
| 1 | +# `<NuxtParticles>` |
| 2 | + |
| 3 | +The `<NuxtParticles>` component renders a [tsParticles](https://particles.js.org) instance inside of a `<div>` element |
| 4 | +with the chosen `id`. |
| 5 | + |
| 6 | +## Example |
| 7 | + |
| 8 | +```vue |
| 9 | +
|
| 10 | +<template> |
| 11 | + <NuxtParticles |
| 12 | + id="tsparticles" |
| 13 | + :options="options" |
| 14 | + @load="onLoad" |
| 15 | + > |
| 16 | +
|
| 17 | + </NuxtParticles> |
| 18 | +
|
| 19 | + <!-- or --> |
| 20 | +
|
| 21 | + <NuxtParticles |
| 22 | + id="tsparticles2" |
| 23 | + url="/path/to/particle-options.json" |
| 24 | + @load="onLoad" |
| 25 | + > |
| 26 | +
|
| 27 | + </NuxtParticles> |
| 28 | +</template> |
| 29 | +
|
| 30 | +<script setup lang="ts"> |
| 31 | +import type { Container } from 'tsparticles-engine' |
| 32 | +
|
| 33 | +// See tsParticles documentation for all available options |
| 34 | +const options = { |
| 35 | + fullScreen: { |
| 36 | + enable: true, |
| 37 | + zIndex: -1 |
| 38 | + }, |
| 39 | + background: { |
| 40 | + color: { |
| 41 | + value: '#fff' |
| 42 | + } |
| 43 | + }, |
| 44 | + particles: { |
| 45 | + color: { |
| 46 | + value: "#000" |
| 47 | + }, |
| 48 | + links: { |
| 49 | + color: "#000", |
| 50 | + enable: true |
| 51 | + }, |
| 52 | + move: { |
| 53 | + enable: true |
| 54 | + }, |
| 55 | + number: { |
| 56 | + value: 100 |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | +
|
| 61 | +const onLoad = (container: Container) => { |
| 62 | + // Do something with the container |
| 63 | + container.pause() |
| 64 | + setTimeout(() => container.play(), 2000) |
| 65 | +} |
| 66 | +</script> |
| 67 | +``` |
| 68 | + |
| 69 | +## Props |
| 70 | + |
| 71 | +### `id` |
| 72 | + |
| 73 | +- Type: `string` |
| 74 | +- Required: `true` |
| 75 | + |
| 76 | +Used by tsParticles to differentiate between multiple instances on the same page. Must be unique. |
| 77 | + |
| 78 | +### `options` |
| 79 | + |
| 80 | +- Type: `IOptions` (see [tsParticles docs](https://particles.js.org/docs/interfaces/tsParticles_Engine.Options_Interfaces_IOptions.IOptions.html)) |
| 81 | +- Required: `false` |
| 82 | + |
| 83 | +The options object to pass to tsParticles. May be used in place of the `url` prop to directly pass a JSON object instead |
| 84 | +of loading a configuration file. |
| 85 | + |
| 86 | +### `url` |
| 87 | + |
| 88 | +- Type: `string` |
| 89 | +- Required: `false` |
| 90 | + |
| 91 | +The URL of the JSON file containing the particle configuration. May be used in place of the `options` prop to |
| 92 | +load a configuration file instead of directly passing a JSON object. |
| 93 | + |
| 94 | +## Events |
| 95 | + |
| 96 | +### `load` |
| 97 | + |
| 98 | +- Type: `(container: Container) => void` (see [tsParticles docs](https://particles.js.org/docs/classes/tsParticles_Engine.Core_Container.Container.html)) |
| 99 | + |
| 100 | +Called when tsParticles has successfully loaded its canvas. Provides a `container` object that can be used to interact |
| 101 | +with the instance. Some useful methods include: |
| 102 | + |
| 103 | +```ts |
| 104 | +// Pause the animation |
| 105 | +container.pause() |
| 106 | +// Play the animation |
| 107 | +container.play() |
| 108 | +// Refresh the instance |
| 109 | +container.refresh() |
| 110 | +// Destroy the instance |
| 111 | +container.destroy() |
| 112 | +``` |
| 113 | + |
| 114 | +## Usage notes |
| 115 | + |
| 116 | +This component automatically lazy-loads the tsParticles library if the `particles.lazy` option is set to `true` in |
| 117 | +your Nuxt configuration. Otherwise, the library is loaded immediately before your entire application is rendered. |
| 118 | + |
| 119 | +Using multiple instances of this component on the same page is safe as long as each instance has a unique `id`. This |
| 120 | +may have performance implications, however, as each instance will be rendering its own canvas. Use one instance with |
| 121 | +multiple particle emitters whenever possible. |
| 122 | + |
| 123 | +If you are using a custom bundle, you must [manually initialize the library](/getting-started/configuration#custom-mode) |
| 124 | +in your application. |
0 commit comments