Methods
ParticleText.js instances expose several methods for controlling animation and querying state. Access these methods through the object returned by initParticleJS().
Available Methods
Section titled “Available Methods”const particle = initParticleJS('#canvas', { colors: ['#695aa6'] });
// Available methods:particle.start Animation();particle.forceRequestAnimationFrame();particle.getCurrentBreakpoint();startAnimation()
Section titled “startAnimation()”Manually starts the animation. Use this when autoAnimate is set to false.
Signature
Section titled “Signature”particle.startAnimation()Parameters
Section titled “Parameters”None
Returns
Section titled “Returns”undefined
// Initialize without auto-startconst particle = initParticleJS('#canvas', {colors: ['#695aa6'],autoAnimate: false // Don't start automatically});
// Start animation when user clicks buttondocument.getElementById('startBtn').addEventListener('click', () => {particle.startAnimation();console.log('Animation started!');});- Calling
startAnimation()multiple times has no effect after the first call - Animation state is tracked via
isAnimatingproperty - Once started, animation cannot be stopped (by design)
Example: Delayed Start
Section titled “Example: Delayed Start”const particle = initParticleJS('#canvas', {colors: ['#695aa6'],autoAnimate: false});
// Start after 2 secondssetTimeout(() => {particle.startAnimation();}, 2000);Example: User-Triggered Animation
Section titled “Example: User-Triggered Animation”const particle = initParticleJS('#hero-canvas', {text: 'Welcome',colors: ['#695aa6'],autoAnimate: false});
// Start when page is visibledocument.addEventListener('DOMContentLoaded', () => {if (document.visibilityState === 'visible') { particle.startAnimation();}});forceRequestAnimationFrame()
Section titled “forceRequestAnimationFrame()”Forces an immediate single-frame render of the animation. Useful for debugging or manual frame-by-frame control.
Signature
Section titled “Signature”particle.forceRequestAnimationFrame()Parameters
Section titled “Parameters”None
Returns
Section titled “Returns”undefined
const particle = initParticleJS('#canvas', {colors: ['#695aa6'],autoAnimate: false // Manual control});
// Render single frameparticle.forceRequestAnimationFrame();
// Render another frameparticle.forceRequestAnimationFrame();- This renders only ONE frame
- Primarily for debugging and testing
- Does not start continuous animation
- Can be called before or after
startAnimation()
Example: Step-Through Animation
Section titled “Example: Step-Through Animation”const particle = initParticleJS('#canvas', {colors: ['#695aa6'],autoAnimate: false});
let frame = 0;
// Render frame-by-framedocument.getElementById('nextFrame').addEventListener('click', () => {particle.forceRequestAnimationFrame();console.log(`Rendered frame ${++frame}`);});getCurrentBreakpoint()
Section titled “getCurrentBreakpoint()”Returns the current responsive breakpoint based on viewport width. Can be overridden for custom breakpoint logic.
Signature
Section titled “Signature”particle.getCurrentBreakpoint()Parameters
Section titled “Parameters”None
Returns
Section titled “Returns”String - Current breakpoint key
Possible values: 'xxxs', 'xxs', 'xs', 'sm', 'md', 'lg', 'xl', 'xxl', 'xxxl'
const particle = initParticleJS('#canvas', {colors: ['#695aa6']});
// Check current breakpointconst breakpoint = particle.getCurrentBreakpoint();console.log(breakpoint); // e.g., 'lg' on desktop
// Use in conditional logicif (breakpoint === 'xs' || breakpoint === 'xxs') {console.log('Mobile device detected');}Default Breakpoints
Section titled “Default Breakpoints”| Breakpoint | Width Range |
|---|---|
xxxs | 0px - 320px |
xxs | 320px - 375px |
xs | 375px - 768px |
sm | 768px - 1024px |
md | 1024px - 1440px |
lg | 1440px - 2560px |
xl | 2560px - 3440px |
xxl | 3440px - 3840px |
xxxl | 3840px+ |
Example: Responsive Logic
Section titled “Example: Responsive Logic”const particle = initParticleJS('#canvas', {colors: ['#695aa6']});
// Adjust behavior based on breakpointconst bp = particle.getCurrentBreakpoint();
if (bp === 'xs' || bp === 'xxs' || bp === 'xxxs') {console.log('Optimizing for mobile');// Could adjust particle count, etc.}Overriding for Custom Breakpoints
Section titled “Overriding for Custom Breakpoints”You can override this method to implement custom breakpoint logic:
const particle = initParticleJS('#canvas', {colors: ['#695aa6']});
// Override with custom logicparticle.getCurrentBreakpoint = function() {const width = window.innerWidth;
if (width < 600) return 'mobile';if (width < 1200) return 'tablet';return 'desktop';};
console.log(particle.getCurrentBreakpoint()); // 'mobile', 'tablet', or 'desktop'Note: When overriding, ensure your custom breakpoints match those used in particleRadius and explosionRadius configurations.
Method Chaining
Section titled “Method Chaining”Methods can be called sequentially, but they don’t return the instance (no chaining):
const particle = initParticleJS('#canvas', {colors: ['#695aa6'],autoAnimate: false});
// Call methodsparticle.startAnimation();particle.forceRequestAnimationFrame(); // Not chained
const bp = particle.getCurrentBreakpoint();console.log(bp);Next Steps
Section titled “Next Steps”- Properties - Access animation state
- Configuration - Configure behavior
- Examples - See methods in action
- Advanced Examples - Manual animation control