Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ npm install astro-particles

or

```shell
pnpm install astro-particles
```

or

```shell
yarn add astro-particles
```
Expand All @@ -27,7 +33,7 @@ yarn add astro-particles
```astro
---
import Particles from "astro-particles"
import type { ISourceOptions } from "tsparticles-engine";
import type { ISourceOptions } from "@tsparticles/engine";

const options: ISourceOptions = {
background: {
Expand All @@ -47,37 +53,15 @@ const options: ISourceOptions = {
};
---

<script>
import { type Container, type Engine, tsParticles } from "tsparticles-engine";
import { loadFull } from "tsparticles";

// the function name is the parameter passed to the init attribute
// required
// add the function to window is mandatory, it will be searched there
window.particlesInit = async function (engine: Engine) {
await loadFull(engine);
}

// the function name is the parameter passed to the loaded attribute
// optional
// add the function to window is mandatory, it will be searched there
window.particlesLoaded = function (container: Container) {
console.log("particlesLoaded callback");
}
</script>

<Particles id="tsparticles" options={options} init="particlesInit" />
```

### Props

| Prop | Type | Definition |
|---------|--------|------------------------------------------------------------------------|
| id | string | The id of the element. |
| init | string | The name of the function to call when the particles instance is ready. |
| loaded | string | The name of the function to call when the particles are loaded. |
| options | object | The options of the particles instance. |
| url | string | The remote options url, called using an AJAX request |
| Prop | Type | Definition |
| ------- | ------ | -------------------------------------- |
| id | string | The id of the element. |
| options | object | The options of the particles instance. |

#### particles.json

Expand Down
9 changes: 5 additions & 4 deletions apps/astro/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "astro-particles-demo",
"type": "module",
"version": "2.10.0",
"version": "3.40.0",
"private": true,
"scripts": {
"dev": "astro dev",
Expand All @@ -12,9 +12,10 @@
"astro": "astro"
},
"dependencies": {
"astro": "^2.6.4",
"astro": "^4.11.0",
"astro-particles": "workspace:^",
"tsparticles": "^2.10.1",
"tsparticles-engine": "^2.10.1"
"@tsparticles/all": "^3.4.0",
"@tsparticles/configs": "^3.4.0",
"@tsparticles/engine": "^3.4.0"
}
}
22 changes: 9 additions & 13 deletions apps/astro/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import Particles from "astro-particles";
import Layout from "../layouts/Layout.astro";
import Card from "../components/Card.astro";
import type { ISourceOptions } from "tsparticles-engine";
import {default as defaultOptions} from "@tsparticles/configs"

// Use your own options or use the default options
// You can always modify the defaults by { ...defaultOptions.chosenDefault, ...yourOptions }
const options = {
background: {
color: "#fff"
Expand All @@ -24,23 +26,17 @@ const options = {
},
move: {
enable: true
},
hover: {
mode: "repulse"
}
}
},

};
---

<script>
import { type Engine, tsParticles } from "tsparticles-engine";
import { loadFull } from "tsparticles";

window.particlesInit = async (engine: Engine) => {
await loadFull(engine);
};
</script>

<Layout title="Welcome to Astro.">
<main>
<Particles id="tsparticles" options={options} init="particlesInit" />
<Particles id="tsparticles" options={defaultOptions.reactSimple} />
<h1>Welcome to <span class="text-gradient">Astro</span></h1>
<p class="instructions">
To get started, open the directory <code>src/pages</code> in your project.<br />
Expand Down
5 changes: 3 additions & 2 deletions components/astro/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "astro-particles",
"version": "2.10.0",
"version": "3.40.0",
"description": "Official tsParticles Astro Component - Easily create highly customizable particle, confetti and fireworks animations and use them as animated backgrounds for your website. Ready to use components available also for Web Components, Vue.js (2.x and 3.x), Angular, Svelte, jQuery, Preact, React.js, Riot.js, Solid.js, Inferno.",
"scripts": {
"build": "echo build",
Expand Down Expand Up @@ -91,6 +91,7 @@
".": "./index.ts"
},
"dependencies": {
"tsparticles-engine": "^2.10.1"
"@tsparticles/all": "^3.4.0",
"@tsparticles/engine": "^3.4.0"
}
}
75 changes: 28 additions & 47 deletions components/astro/src/Particles.astro
Original file line number Diff line number Diff line change
@@ -1,55 +1,36 @@
---
import type { Container, Engine, ISourceOptions } from "tsparticles-engine";

export interface IParticlesProps {
id: string;
init: string;
loaded?: string;
options?: ISourceOptions;
url?: string;
import type { ISourceOptions } from "@tsparticles/engine";
interface Props {
id: string;
options: ISourceOptions;
}

const { id, init, loaded, options, url } = Astro.props as IParticlesProps;
const { id, options } = Astro.props;
---
<astro-particles data-id={id} data-options={JSON.stringify(options)} data-url={url} data-init={init}
data-loaded={loaded}>
<canvas></canvas>
</astro-particles>

<astro-particles data-options={JSON.stringify(options)} id={id}
></astro-particles>
<script>
import { tsParticles } from "tsparticles-engine";

class AstroParticles extends HTMLElement {
constructor() {
super();

(async () => {
if (window.hasOwnProperty(this.dataset.init)) {
const initFn = window[this.dataset.init];

if (initFn instanceof Function) {
await initFn(tsParticles);
}
}

let container: Container | undefined;

if (this.dataset.url) {
container = await tsParticles.loadJSON(this.dataset.id, this.dataset.url);
} else if (this.dataset.options) {
container = await tsParticles.load(this.dataset.id, JSON.parse(this.dataset.options));
}

if (this.dataset.loaded && window.hasOwnProperty(this.dataset.loaded)) {
const loadedFn = window[this.dataset.loaded];

if (loadedFn instanceof Function) {
loadedFn(container);
}
}
})();
}
import { tsParticles, type ISourceOptions } from "@tsparticles/engine";
import { loadAll } from "@tsparticles/all";
class AstroParticles extends HTMLElement {
constructor() {
super();
const options = this.dataset.options || "{}";
const id = this.id;
const parsedOptions = JSON.parse(options) as ISourceOptions;
if (!id) {
throw new Error("tsParticles: No id provided");
}
if (!options) {
throw new Error("tsParticles: No config provided");
}
loadAll(tsParticles);
tsParticles.load({
id: this.id,
options: parsedOptions,
});
}
}

customElements.define("astro-particles", AstroParticles);
customElements.define("astro-particles", AstroParticles);
</script>
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
},
"license": "MIT",
"dependencies": {
"@commitlint/cli": "^17.6.5",
"@commitlint/config-conventional": "^17.6.5",
"@parcel/core": "^2.9.2",
"@parcel/transformer-sass": "^2.9.2",
"husky": "^8.0.3",
"lerna": "^7.0.2",
"parcel": "^2.9.2"
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@parcel/core": "^2.12.0",
"@parcel/transformer-sass": "^2.12.0",
"husky": "^9.0.11",
"lerna": "^8.1.5",
"parcel": "^2.12.0"
},
"workspaces": [
"apps/*",
Expand Down
Loading