### Run Test Web App
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/integration-guide.md
Instructions to navigate to the test web app directory, install dependencies, and start the development server.
```bash
cd apps/test-web-app
npm install
npm run dev
```
--------------------------------
### Install TypeGPU Confetti and React Native WGPU
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/integration-guide.md
Install the necessary packages for React Web integration.
```bash
npm install react-native-wgpu typegpu-confetti
```
--------------------------------
### Install typegpu-confetti
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/README.md
Install the main typegpu-confetti package after setting up react-native-wgpu.
```sh
npm install typegpu-confetti
```
--------------------------------
### Custom Initialization Function Example
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/api-reference/exported-accessors-and-slots.md
An example of a custom particle initialization function using random values and accessing global particle data. Ensure 'gpu' is used for GPU execution context.
```typescript
import { d } from 'typegpu';
import { particlesAccess, timeAccess, maxDurationTimeSlot } from 'typegpu-confetti';
import { randf } from '@typegpu/noise';
const customInit: InitParticleFn = (i) => {
'use gpu';
randf.seed2(d.vec2f(d.f32(i), d.f32(timeAccess.$ % 1111)));
const particle = particlesAccess.$[i];
particle.position = d.vec2f(randf.sample() * 2 - 1, 1);
particle.velocity = d.vec2f(randf.sample() * 2 - 1, -randf.sample());
particle.timeLeft = maxDurationTimeSlot.$ * 1000;
particle.seed = randf.sample();
};
```
--------------------------------
### Install react-native-wgpu
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/README.md
Install the react-native-wgpu package to use typegpu-confetti in React Native. Ensure you run `expo prebuild` as Expo Go is not supported.
```sh
npm install react-native-wgpu
```
--------------------------------
### Install unplugin-typegpu Babel Plugin
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/README.md
Install the unplugin-typegpu Babel plugin to pass JavaScript functions marked with the "use gpu" directive to the Confetti component.
```sh
npm install unplugin-typegpu
```
--------------------------------
### Global Confetti Setup (React Web)
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/api-reference/confetti-provider.md
Recommended pattern: Wrap your entire React application with ConfettiProvider for global access to confetti effects.
```typescript
import { ConfettiProvider } from 'typegpu-confetti/react';
function App() {
return (
);
}
```
--------------------------------
### Custom Gravity Function Example
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/api-reference/exported-accessors-and-slots.md
An example of a custom gravity function that pulls particles towards the screen center. Ensure 'gpu' is used for GPU execution context.
```typescript
import { d } from 'typegpu';
import { aspectRatioAccess, particlesAccess } from 'typegpu-confetti';
const customGravity: GravityFn = (pos) => {
'use gpu';
// Gravity that pulls toward screen center
const center = d.vec2f(0, 0);
const direction = center.minus(pos).normalize();
const strength = 0.2;
return direction.mul(strength);
};
```
--------------------------------
### Particle Data Initialization Example
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/types.md
Initializes particle state including position, velocity, seed, and timeLeft. Requires 'use gpu', 'typegpu', and '@typegpu/noise' imports, and access to global GPU data.
```typescript
const particle = particlesAccess.$[index];
particle.position = d.vec2f(x, y); // vec2f - normalized coordinates
particle.velocity = d.vec2f(vx, vy); // vec2f - per-second movement
particle.seed = randf.sample(); // f32 - random value for oscillation
particle.timeLeft = maxDurationTime.$ * 1000; // f32 - milliseconds remaining
```
--------------------------------
### Custom Radial Gravity Example
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/configuration.md
This example demonstrates how to implement custom gravity behavior using a GPU function. The `gravity` prop accepts a function that calculates acceleration based on particle position, here simulating a pull towards the center.
```typescript
import { Confetti, gravityFn } from 'typegpu-confetti/react';
import { d } from 'typegpu';
function RadialGravity() {
const radialGravity = (pos: d.v2f) => {
'use gpu';
// Pull particles toward center
const center = d.vec2f(0, 0);
const direction = center.minus(pos).normalize();
return direction.mul(0.1);
};
return ;
}
```
--------------------------------
### Large, Long-Duration Confetti Example
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/configuration.md
This example shows how to configure larger confetti particles with a longer animation duration and increased particle counts. It utilizes the `size`, `maxDurationTime`, `initParticleAmount`, and `maxParticleAmount` props.
```typescript
import { Confetti } from 'typegpu-confetti/react';
function LongConfetti() {
return (
);
}
```
--------------------------------
### Custom InitParticle Function with 'use gpu' Directive
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/api-reference/confetti-component.md
An example of a custom initParticle function requiring the 'use gpu' directive for proper transpilation.
```typescript
const customInit: InitParticleFn = (i) => {
'use gpu';
const particle = particlesAccess.$[i];
particle.position = d.vec2f(randf.sample() * 2 - 1, randf.sample());
particle.velocity = d.vec2f(randf.sample() - 0.5, -randf.sample());
};
```
--------------------------------
### Custom Color Palette Example
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/configuration.md
This example demonstrates how to set a custom array of RGBA colors for the confetti particles. The `colorPalette` prop accepts an array of `[r, g, b, a]` arrays.
```typescript
import { Confetti } from 'typegpu-confetti/react';
function CustomColors() {
const colors: [number, number, number, number][] = [
[255, 0, 0, 1], // Red
[0, 255, 0, 1], // Green
[0, 0, 255, 1], // Blue
[255, 255, 0, 1], // Yellow
];
return ;
}
```
--------------------------------
### ConfettiProvider Setup (React Web)
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/api-reference/confetti-provider.md
Wrap your application with ConfettiProvider to enable confetti animations. Use the useConfetti hook within child components to access confetti functionality.
```typescript
import React from 'react';
import { ConfettiProvider, useConfetti } from 'typegpu-confetti/react';
function App() {
return (
);
}
function YourApp() {
const confettiRef = useConfetti();
return (
);
}
```
--------------------------------
### Initialize Particle Data in GPU Function
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/api-reference/exported-accessors-and-slots.md
Example of initializing a particle's properties within a GPU function using the `particlesAccess` accessor. This sets initial position, velocity, seed, and timeLeft for a given particle index.
```typescript
const initParticle: InitParticleFn = (i) => {
'use gpu';
const particle = particlesAccess.$[i];
particle.position = d.vec2f(0, 0);
particle.velocity = d.vec2f(0, -1);
particle.seed = 0.5;
particle.timeLeft = 2000;
};
```
--------------------------------
### Confetti Provider Setup
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/README.md
Wrap a top-level component with `ConfettiProvider` to make the `useConfetti` hook accessible throughout the application and ensure confetti covers the entire screen.
```tsx
import { ConfettiProvider } from 'typegpu-confetti/react-native';
function SomeHighLevelContainerComponent() {
return (
);
}
```
--------------------------------
### ConfettiProvider Setup (React Native)
Source: https://github.com/software-mansion-labs/typegpu-confetti/blob/main/_autodocs/api-reference/confetti-provider.md
Wrap your application with ConfettiProvider to enable confetti effects. Use the useConfetti hook in descendant components to control confetti.
```typescript
import React from 'react';
import { View } from 'react-native';
import { ConfettiProvider, useConfetti } from 'typegpu-confetti/react-native';
function App() {
return (
);
}
function YourApp() {
const confettiRef = useConfetti();
return (