1.0.0 • Published 12 months ago

bubbles-threlte v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Bubbles-Threlte: Svelte Bubble Generator

A highly customizable bubble generator component for Svelte applications using Threlte (Three.js for Svelte). Create stunning bubble effects with ease, perfect for interactive backgrounds, particle systems, and more.

Installation

npm install bubbles-threlte

Usage

Import the BubbleEmitter component in your Svelte file:

import BubbleEmitter from 'bubbles-threlte';

Set up your bubble and emitter configurations:

<script>
  import { Vector3 } from 'three';
  import type { BubbleEmitterParams, BubbleParams } from 'bubbles-threlte';
  import { useFrame } from "@threlte/core";

  const eventEmitter = new BubbleEventEmitter();

  // Optionally, setup event listeners
  onMount(() => {
    eventEmitter.addListener('creationStarted', (event) => console.log("Bubble creation started", event.bubble));
    eventEmitter.addListener('creationFinished', (event) => console.log("Bubble creation finished", event.bubble));
    eventEmitter.addListener('removalStarted', (event) => console.log("Bubble removal started", event.bubble));
    eventEmitter.addListener('removalFinished', (event) => console.log("Bubble removal finished", event.bubble));
    eventEmitter.addListener('move', (event) => console.log("Bubble moved", event.bubble));
  });

  const emitterParams: BubbleEmitterParams = {
    // Your emitter configuration
  };

  const bubbleParams: BubbleParams = {
    // Your bubble configuration
  };

  useFrame(() => {
    // Update params of your emitterParams and/or bubbleParams to generate new bubbles with this new data
  });
</script>

Use the BubbleEmitter component in your Svelte template:

<BubbleEmitter {emitterParams} {bubbleParams} {eventEmitter} />

Configuration Parameters

The BubbleParams interface provides extensive customization for individual bubbles:

ParameterTypeDescription
idstringUnique identifier for the bubble.
state"creating" \| "active" \| "removing"Current state of the bubble in its lifecycle.
positionVector3The bubble's position in 3D space.
originVector3The original spawn position of the bubble.
velocityVector3The bubble's current velocity.
sizenumberThe size of the bubble.
opacitynumberThe overall opacity of the bubble.
geometrySphereGeometryThe Three.js geometry used for the bubble.
animationSpeednumberSpeed of the bubble's animation.
smoothDeformWaveSinnumberFrequency of sinusoidal deformation.
smoothDeformWaveCosnumberFrequency of cosinusoidal deformation.
smoothDeformSpeedSinnumberSpeed of sinusoidal deformation.
smoothDeformSpeedCosnumberSpeed of cosinusoidal deformation.
smoothDeformStrengthSinnumberStrength of sinusoidal deformation.
smoothDeformStrengthCosnumberStrength of cosinusoidal deformation.
baseColorVector3The base color of the bubble (RGB).
borderColorVector3The color of the bubble's border (RGB).
reflectionColorVector3The color of reflections on the bubble (RGB).
baseAlphanumberThe base alpha (transparency) of the bubble.
borderAlphanumberThe alpha (transparency) of the bubble's border.
glowStrengthnumberThe strength of the bubble's glow effect.
gradientStrengthnumberThe strength of the color gradient effect.
gradientSpeednumberThe speed of the color gradient animation.
fresnelVector3Parameters for the Fresnel effect (edge highlighting).
fresnelStrengthnumberThe strength of the Fresnel effect.
noiseFrequencynumberThe frequency of noise applied to the bubble's surface.
noiseAmplitudenumberThe amplitude of noise applied to the bubble's surface.
seednumberA random seed for consistent randomization.
bubbleFilter(bubble: BubbleParams) => booleanA function to determine if a bubble should be removed.
bubbleModifier(bubble: BubbleParams) => voidA function to modify bubble properties on each frame.
createAnimationBubbleAnimationAnimation parameters for bubble creation.
removeAnimationBubbleAnimationAnimation parameters for bubble removal.

The BubbleAnimation interface is used for both createAnimation and removeAnimation:

ParameterTypeDescription
params(bubble: BubbleParams) => voidA function to update bubble parameters during the animation.
durationnumberThe duration of the animation in milliseconds.

These parameters provide extensive control over both the emission of bubbles and the appearance and behavior of individual bubbles. By adjusting these values, you can create a wide range of effects, from realistic soap bubbles to abstract particle systems.

ParameterTypeDescription
spawnConditionbooleanDetermines whether new bubbles should be spawned.
velocityVector3The initial velocity applied to newly spawned bubbles.
bubbleSize{ min: number; max: number }Defines the size range for spawned bubbles.
scatterAngle{ min: number; max: number }Sets the range of angles (in degrees) for random direction scatter upon spawn.
maxBubblesnumberThe maximum number of bubbles that can exist simultaneously.

ParameterDescription
creationStartedFired when a bubble starts its creation animation
creationFinishedFired when a bubble completes its creation animation
removalStartedFired when a bubble starts its removal animation
removalFinishedFired when a bubble completes its removal animation
moveFired on each frame update for active bubbles

These events can be used to trigger custom behaviors or synchronize other elements of your application with the bubble animations.

Examples

ShinyPop: Interactive Mouse-driven Bubbles

The ShinyPop example demonstrates how to create interactive bubbles that respond to mouse movement. Key features:

  • Bubbles spawn based on mouse velocity
  • Custom bubble physics for realistic movement
  • Smooth creation and removal animations

This example showcases the potential for creating engaging, interactive backgrounds or particle effects that respond to user input.

ScreenCover: Full-screen Bubble Animation

The ScreenCover example illustrates how to create a full-screen bubble animation. Key features:

  • Bubbles rise from the bottom of the screen
  • Subtle wave-like horizontal movement
  • Gradual size increase and opacity changes

This example is perfect for creating ambient background animations or loading screens with a soothing, aquatic feel.

VoidStar: Abstract Cosmic Effect

The VoidStar example creates an abstract, cosmic-like effect using a single, highly detailed bubble. Key features:

  • High-resolution sphere geometry
  • Subtle deformations for an organic feel

This example demonstrates how to use the bubble generator for creating unique, abstract visual effects beyond traditional bubble animations.

Waves: Dynamic Wave Simulation

The Waves example showcases how to create a dynamic wave-like effect using a single, large bubble. Key features:

  • Custom deformation parameters for wave-like movement
  • High-frequency noise for detailed surface texture
  • Fresnel effect for light interaction

This example illustrates the potential for using the bubble generator to create more complex, fluid-like animations and effects.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

1.0.0

12 months ago

0.0.1

12 months ago