openharmony 鸿蒙 ts-particle-animation

2025-06-12 浏览 (1)

Particle Animation (Particle)

Particle animation is an animation composed of a multitude of particles randomly generated within a certain range. The particles can be points or images. By animating different aspects of the particles, such as color, opacity, scale, velocity, acceleration, and spin angle, you can create engaging and dynamic aesthetics. For example, you can create an impressive snowfall animation by animating the particles – snowflakes.

The component used for producing particle animations is Particle.

NOTE

This component is supported since API version 10. Updates will be marked with a superscript to indicate their earliest API version.

Child Components

Not supported

APIs

interface ParticleInterface {
  <
    PARTICLE extends ParticleType,
    COLOR_UPDATER extends ParticleUpdater,
    OPACITY_UPDATER extends ParticleUpdater,
    SCALE_UPDATER extends ParticleUpdater,
    ACC_SPEED_UPDATER extends ParticleUpdater,
    ACC_ANGLE_UPDATER extends ParticleUpdater,
    SPIN_UPDATER extends ParticleUpdater
  >(particles: Particles<
      PARTICLE,
      COLOR_UPDATER,
      OPACITY_UPDATER,
      SCALE_UPDATER,
      ACC_SPEED_UPDATER,
      ACC_ANGLE_UPDATER,
      SPIN_UPDATER
    >): ParticleAttribute;
}

Atomic service API: This API can be used in atomic services since API version 11.

Parameters

NameTypeMandatoryDescription
particlesParticles<
PARTICLE,
COLOR_UPDATER,
OPACITY_UPDATER,
SCALE_UPDATER,
ACC_SPEED_UPDATER,
ACC_ANGLE_UPDATER,
SPIN_UPDATER
>
YesParticles. For details, see Particles.

Attributes

In addition to the universal attributes, the following attributes are supported.

disturbanceFields12+

disturbanceFields(fields: Array<DisturbanceFieldOptions>)

Sets the disturbance fields.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
fieldsArray<DisturbanceFieldOptions>YesArray of disturbance fields.

emitter12+

emitter(value: Array<EmitterProperty>)

Sets the emitter parameters.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
valueArray<EmitterProperty>YesArray of emitter parameters to set.

Events

The universal events are supported.

ParticleOptions

interface ParticleOptions<
  PARTICLE extends ParticleType,
  COLOR_UPDATER extends ParticleUpdater,
  OPACITY_UPDATER extends ParticleUpdater,
  SCALE_UPDATER extends ParticleUpdater,
  ACC_SPEED_UPDATER extends ParticleUpdater,
  ACC_ANGLE_UPDATER extends ParticleUpdater,
  SPIN_UPDATER extends ParticleUpdater
> {
  emitter: EmitterOptions<PARTICLE>;
  color?: ParticleColorPropertyOptions<COLOR_UPDATER>;
  opacity?: ParticlePropertyOptions<number, OPACITY_UPDATER>;
  scale?: ParticlePropertyOptions<number, SCALE_UPDATER>;
  velocity?: VelocityOptions;
  acceleration?: AccelerationOptions<ACC_SPEED_UPDATER, ACC_ANGLE_UPDATER>;
  spin?: ParticlePropertyOptions<number, SPIN_UPDATER>;
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
emitterEmitterOptions<PARTICLE>YesParticle emitter.
colorParticleColorPropertyOptions<COLOR_UPDATER>NoParticle color.
NOTE
Default value: { range:[Color.White,Color.White] }. Colors cannot be set for image particles.
opacityParticlePropertyOptions<number, OPACITY_UPDATER>NoParticle opacity.
Default value: { range:[1.0,1.0] }
scaleParticlePropertyOptions<number, SCALE_UPDATER>NoParticle scale.
Default value: { range:[1.0,1.0] }
velocityVelocityOptionsNoParticle velocity.
NOTE
speed indicates the time rate at which the particle moves. angle indicates the direction (in angles) in which the particle moves, with the geometric center of the element as the coordinate origin and the horizontal direction as the x-axis. A positive number indicates clockwise rotation.
Default value: {speed: [0.0,0.0],angle: [0.0,0.0] }
accelerationAccelerationOptions<ACC_SPEED_UPDATER, ACC_ANGLE_UPDATER>NoParticle acceleration.
NOTE
speed indicates the acceleration speed, and angle indicates the acceleration direction (in angles).
Default value: { speed:{range:[0.0,0.0]},angle:{range:[0.0,0.0]} }
spinParticlePropertyOptions<number, SPIN_UPDATER>NoParticle spin angle.
Default value: {range:[0.0,0.0]}
Direction: A positive number indicates clockwise spinning, and a negative number indicates anticlockwise spinning.

EmitterOptions

Provides particle emitter configuration.

interface EmitterOptions<PARTICLE extends ParticleType> {   
  particle: EmitterParticleOptions<PARTICLE>;
  emitRate?: number;
  shape?: ParticleEmitterShape;
  position?: ParticleTuple<Dimension, Dimension>;
  size?: ParticleTuple<Dimension, Dimension>;
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
particleEmitterParticleOptions<PARTICLE>YesParticle configuration.
- type: particle type, which can be IMAGE or POINT.

Atomic service API: This API can be used in atomic services since API version 11.
- config: configuration of the particle type.
- The value type of config is subject to the value of type.
1. If type is ParticleType.POINT, the config type is PointParticleParameters.
2. If type is ParticleType.IMAGE, the config type is ImageParticleParameters.
Atomic service API: This API can be used in atomic services since API version 11.
- count: number of particles. The value is greater than or equal to -1. The value -1 indicates that the number of particles is infinite.
Atomic service API: This API can be used in atomic services since API version 11.
- lifetime: lifetime of a single particle. The default value is 1000 (that is, 1000 ms, 1s). The value is greater than or equal to -1. The value -1 indicates that the lifetime of the particle is infinite. If the value specified is less than -1, the default value is used.
NOTE
Setting the lifespan to -1 is not recommended unless continuous animation is required, as it may significantly impact performance.
Atomic service API: This API can be used in atomic services since API version 11.
- lifeTimeRange: range of particle lifetime values. After lifeTimeRange is set, the lifetime of a particle is a random integer between [lifetime – lifeTimeRange, lifetime + lifeTimeRange]. lifeTimeRange has a default value of 0, and its value range is from 0 to positive infinity. If it is set to a negative value, the default value is used.
Atomic service API: This API can be used in atomic services since API version 12.
emitRatenumberNoEmit rate (that is, the number of particles emitted per second).
Default value: 5. If the value specified is less than 0, the default value is used.
The emitRate value can significantly impact performance when it exceeds 5000; you are advised to set it to be less than 5000.
shapeParticleEmitterShapeNoEmitter shape.
Default value: ParticleEmitterShape.RECTANGLE
positionParticleTuple<Dimension, Dimension>NoEmitter position (distance from the upper left corner of the component). The first parameter indicates the relative offset along the x-axis, and the second parameter indicates the relative offset along the y-axis.
Default value: [0.0, 0.0]
sizeParticleTuple<Dimension, Dimension>NoSize of the emit window. The first parameter indicates the emitter width, and the second parameter indicates the emitter height.
Default value: ['100%', '100%'] (that is, the emit window fully occupies the component).

ParticleConfigs

interface ParticleConfigs { 
  [ParticleType.POINT]: PointParticleParameters;
  [ParticleType.IMAGE]: ImageParticleParameters;
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
[ParticleType.POINT]PointParticleParametersYesPoint particle configuration.
[ParticleType.IMAGE]ImageParticleParametersYesImage particle configuration.

PointParticleParameters

interface PointParticleParameters {
  radius: VP;
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
radiusVPYesParticle radius.

ImageParticleParameters

interface ImageParticleParameters {
  src: ResourceStr;
  size: ParticleTuple<Dimension, Dimension>;
  objectFit?: ImageFit;
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
srcResourceStrYesImage path. SVG images are not supported.
sizeParticleTuple<Dimension, Dimension>YesImage size.
objectFitImageFitNoImage display mode.

ParticleColorPropertyOptions

interface ParticleColorPropertyOptions<UPDATER extends ParticleUpdater> {
  range: ParticleTuple<ResourceColor, ResourceColor>; 
  distributionType?: DistributionType;
  updater?: ParticleColorUpdaterOptions<UPDATER>;
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
rangeParticleTuple<ResourceColor, ResourceColor>YesInitial color range of the particle. The initial color of particles generated by the particle emitter is randomly selected in this range.
Default value: range:[Color.White,Color.White]
distributionType12+DistributionTypeNoType of random value distribution for the initial color of the particle. Both uniform distribution and normal (Gaussian) distribution are supported.
Default value: DistributionType.UNIFORM
Atomic service API: This API can be used in atomic services since API version 12.
updaterParticleColorUpdaterOptions<UPDATER>NoHow the color property is updated. The available options of type are as follows:
1. ParticleUpdater.NONE: The property does not change. In this case, the config type is ParticleColorPropertyUpdaterConfigs[ParticleUpdater.NONE].
2. ParticleUpdater.RANDOM: The property changes randomly. In this case, the config type is ParticleColorPropertyUpdaterConfigs[ParticleUpdater.RANDOM].
3. ParticleUpdater.CURVE: The property changes with the animation curve. In this case, the config type is ParticleColorPropertyUpdaterConfigs[ParticleUpdater.CURVE].
The default value of type is ParticleUpdater.NONE.
NOTE
When type is set to ParticleUpdater.RANDOM or ParticleUpdater.CURVE, the color configuration in updater has higher priority than that in range. During the animation time period configured in updater, the color configuration from updater is used. Outside of the animation time period configured in updater, the color configuration from range is used.

ParticleColorPropertyUpdaterConfigs

interface ParticleColorPropertyUpdaterConfigs {
  [ParticleUpdater.NONE]: void;
  [ParticleUpdater.RANDOM]: ParticleColorOptions;
  [ParticleUpdater.CURVE]: Array<ParticlePropertyAnimation<ResourceColor>>;
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
[ParticleUpdater.NONE]voidYesThe color does not change.
[ParticleUpdater.RANDOM]ParticleColorOptionsYesThe color changes randomly, with the per-second change difference being a value randomly generated from the range. The target color is obtained by applying the change difference to the current color value of each of the R, G, B, A channels.
[ParticleUpdater.CURVE]Array<ParticlePropertyAnimation<ResourceColor>>YesThe color changes with the animation curve. The array type indicates that multiple animation segments can be set for the current property, for example, 0–3000 ms, 3000–5000 ms, and 5000–8000 ms.

ParticlePropertyOptions

interface ParticlePropertyOptions<TYPE, UPDATER extends ParticleUpdater> {
  range: ParticleTuple<TYPE, TYPE>;
  updater?: ParticleUpdaterOptions<TYPE, UPDATER>;
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
rangeParticleTuple<TYPE, TYPE>YesInitial property value range of the particle. The initial property value of particles generated by the particle emitter is randomly selected in this range.
NOTE
If a property is set to an invalid value, the default value will be used. If the maximum value is less than the minimum value, the default range will be used. TYPE is number.
The default value varies by property:
1. opacity property: range:[1.0,1.0]; the value range is [0, 1]; the default value is 0.0.
2. scale property: range:[1.0,1.0]; the value range is [0, 10000]; the default value is 0.0.
3. acceleration speed property: range:[0.0,0.0]; the value range is [0, 10000]; the default value is 0.0.
4. acceleration angle property: range:[0.0,0.0]; the value range is [-10000, 10000]; the default value is 0.0.
5. spin speed property: range:[0.0,0.0]; the value range is [-10000, 10000]; the default value is 0.0.
updaterParticleUpdaterOptions<TYPE, UPDATER>NoHow the property is updated. The available options of type are as follows:
1. ParticleUpdater.NONE: The property does not change. In this case, the config type is ParticlePropertyUpdaterConfigs[ParticleUpdater.NONE].
2. ParticleUpdater.RANDOM: The property changes randomly. In this case, the config type is ParticlePropertyUpdaterConfigs[ParticleUpdater.RANDOM].
3. ParticleUpdater.CURVE: The property changes with the animation curve. In this case, the config type is ParticlePropertyUpdaterConfigs[ParticleUpdater.CURVE].
The default value of type is ParticleUpdater.NONE.

ParticlePropertyUpdaterConfigs

interface ParticlePropertyUpdaterConfigs<T> {
    [ParticleUpdater.NONE]: void;
    [ParticleUpdater.RANDOM]: ParticleTuple<T, T>;
    [ParticleUpdater.CURVE]: Array<ParticlePropertyAnimation<T>>;
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
[ParticleUpdater.NONE]voidYesThe property does not change.
[ParticleUpdater.RANDOM]ParticleTuple<T, T>YesThe property changes randomly, with the per-second change difference being a value randomly generated from the range.
The target property value is obtained by applying the change difference to the current property value. For example, if the current property value is 0.2 and config is set to [0.1,1.0], then:
1. When the random change difference is 0.5, the target property value is 0.2 + 0.5 = 0.7.
2. The change difference may also be a negative value. For example, if the current property value is 0.2 and config is set to [-3.0,2.0], then when the random change difference is -2.0, the target property value is 0.2 - 2.0 = -1.8.
NOTE
config sets the value range of the change difference. While the change difference does not have a maximum or minimum value limit, the target property value does. Therefore, if the target property value is greater than the maximum property value, the maximum property value will be used instead; if the target property value is less than the minimum property value, the minimum property value will be used instead. T represents a number.
For example, if the value range of opacity is [0.0, 1.0], then if the target property value is greater than 1.0, 1.0 will be used instead.
[ParticleUpdater.CURVE]Array<ParticlePropertyAnimation<T>>YesThe property changes with the animation curve. The array type indicates that multiple animation segments can be set for the current property, for example, 0-3000 ms, 3000-5000 ms, and 5000-8000 ms. T represents a number.

ParticlePropertyAnimation

interface ParticlePropertyAnimation<T> {
  from: T;
  to: T;
  startMillis: number;
  endMillis: number;
  curve?: Curve|ICurve;
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
fromTYesInitial value of the property. If the value is invalid, the default value will be used.
toTYesTarget value of the property. If the value is invalid, the default value will be used.
startMillisnumberYesStart time of the animation.
Unit: ms.
Value range: [0, +∞).
endMillisnumberYesEnd time of the animation.
Unit: ms.
Value range: [0, +∞).
curveCurve |ICurveNoAnimation curve.
Default value: Curve.Linear

ParticleType

enum ParticleType {
  POINT = 'point',
  IMAGE = 'image',
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameDescription
POINTPoint particle.
IMAGEImage particle.

ParticleEmitterShape

enum ParticleEmitterShape {
  RECTANGLE = 'rectangle',
  CIRCLE = 'circle',
  ELLIPSE = 'ellipse',
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameDescription
RECTANGLEThe particle emitter is rectangular.
CIRCLEThe particle emitter is circular.
ELLIPSEThe particle emitter is elliptical.

DistributionType12+

enum DistributionType {
  UNIFORM = 0,
  GAUSSIAN = 1,
}

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameDescription
UNIFORMThe initial color random values are distributed uniformly.
GAUSSIANThe initial color random values are distributed according to a Gaussian distribution.

ParticleUpdater

enum ParticleUpdater {
  NONE = 'none',
  RANDOM = 'random',
  CURVE = 'curve',
}

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameDescription
NONENo change.
RANDOMRandom change.
CURVEChange with the animation curve.

DisturbanceFieldOptions12+

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
strengthnumberNoField strength, which indicates the intensity of the repulsive force from the center outward. The default value is 0. Positive values indicate a repulsive force directed outward, while negative values indicate an attractive force directed inward.
Value range: (-∞, +∞).
shapeDisturbanceFieldShapeNoShape of the field.
Default value: DisturbanceFieldShape.RECT
sizeSizeT<number>NoSize of the field.
Default value: {width:0, height:0}.
Value range of width and height: [0, +∞).
positionPositionT<number>NoPosition of the field.
Default value: {x:0, y:0}.
Value range of x and y: (-∞, +∞).
feathernumberNoFeather value, which represents the degree of attenuation from the center of the field to its edges. The value is an integer ranging from 0 to 100. A value of 0 indicates that the field is rigid, and all particles within its range are repelled. The higher the feather value, the more gradual the field becomes, resulting in more particles close to the center point appearing within the field's range.
Default value: 0.
noiseScalenumberNoNoise scale, used to control the overall size of the noise pattern. The value is greater than or equal to 0.
Default value: 1.
noiseFrequencynumberNoNoise frequency. The higher the frequency, the finer the noise. The value is greater than or equal to 0.
Default value: 1.
noiseAmplitudenumberNoNoise amplitude, which indicates the range of noise fluctuations. The greater the amplitude, the greater the difference between the noises. The value is greater than or equal to 0.
Default value: 1.

DisturbanceFieldShape12+

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameDescription
RECTRectangle.
CIRCLECircle.
ELLIPSEEllipse.

EmitterProperty12+

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
indexnumberYesIndex of the emitter based on the index array of the emitters in the initialization parameters. The value is rounded to the nearest whole number. The default value 0 is used in case of exceptions.
emitRatenumberNoEmit rate, that is, the number of particles emitted per second.
If no value is passed in, the current emit rate is retained. If a value less than 0 is passed in, the default value 5 is used. The emitRate value can significantly impact performance when it exceeds 5000; you are advised to set it to be less than 5000.
positionPositionT<number>NoArray of emitter positions. Only the number type is supported.
If no value is passed in, the current emitter position is retained. Two valid values must be passed in; if either is an invalid value, position will not take effect.
sizeSizeT<number>NoSize of the emit window. Only the number type is supported.
If no value is passed in, the current emitter window size is retained. Two valid values greater than 0 must be passed in; if either is an invalid value, size will not take effect.

ParticleTuple14+

declare type ParticleTuple<T1, T2> = [T1, T2];

Defines the tuple type representing a pair of animation parameters.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.ArkUI.ArkUI.Full

TypeDescription
[T1, T2]A pair of animation parameters, which can be of any type.

Particles14+

interface Particles<
  PARTICLE extends ParticleType,
  COLOR_UPDATER extends ParticleUpdater,
  OPACITY_UPDATER extends ParticleUpdater,
  SCALE_UPDATER extends ParticleUpdater,
  ACC_SPEED_UPDATER extends ParticleUpdater,
  ACC_ANGLE_UPDATER extends ParticleUpdater,
  SPIN_UPDATER extends ParticleUpdater
> {
  particles: Array<
    ParticleOptions<
      PARTICLE,
      COLOR_UPDATER,
      OPACITY_UPDATER,
      SCALE_UPDATER,
      ACC_SPEED_UPDATER,
      ACC_ANGLE_UPDATER,
      SPIN_UPDATER
    >
  >;
}

Defines an array of particle options.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
particlesArray<
ParticleOptions<
PARTICLE,
COLOR_UPDATER,
OPACITY_UPDATER,
SCALE_UPDATER,
ACC_SPEED_UPDATER,
ACC_ANGLE_UPDATER,
SPIN_UPDATER
>
>
YesAn array of particle options, each of which covers the emitter, color, opacity, scale, velocity, acceleration, and spin speed of particles. For details, see ParticleOptions.

VelocityOptions14+

declare interface VelocityOptions {
  speed: ParticleTuple<number, number>;
  angle: ParticleTuple<number, number>;
}

Defines the particle velocity.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
speedParticleTuple<number, number>YesTime rate at which the particle moves.
Default value: {range:[0.0,0.0]}
angleParticleTuple<number, number>YesDirection (in angles) in which the particle moves, with the geometric center of the element as the coordinate origin and the horizontal direction as the x-axis. A positive number indicates clockwise rotation.
Default value: {range:[0.0,0.0]}

AccelerationOptions14+

declare interface AccelerationOptions<
  ACC_SPEED_UPDATER extends ParticleUpdater,
  ACC_ANGLE_UPDATER extends ParticleUpdater
> {
  speed?: ParticlePropertyOptions<number, ACC_SPEED_UPDATER>;
  angle?: ParticlePropertyOptions<number, ACC_ANGLE_UPDATER>;
}

Defines the particle acceleration.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
speedParticlePropertyOptions<number, ACC_SPEED_UPDATER>NoAcceleration speed.
Default value: {range:[0.0,0.0]}
angleParticlePropertyOptions<number, ACC_SPEED_UPDATER>NoAcceleration direction (in angles).
Default value: {range:[0.0,0.0]}

EmitterParticleOptions14+

interface EmitterParticleOptions<PARTICLE extends ParticleType> {
  type: PARTICLE;
  config: ParticleConfigs[PARTICLE];
  count: number;
  lifetime?: number;
  lifetimeRange?: number;
}

Defines the particle configuration.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
typePARTICLEYesParticle type, which can be IMAGE or POINT.
configParticleConfigs[PARTICLE]YesConfiguration of the particle type.
The value type of config is subject to the value of type.
1. If type is ParticleType.POINT, the config type is PointParticleParameters.
2. If type is ParticleType.IMAGE, the config type is ImageParticleParameters.
countnumberYesNumber of particles. The value is greater than or equal to -1. The value -1 indicates that the number of particles is infinite.
lifetimenumberNoLifetime of a single particle. The default value is 1000 (that is, 1000 ms, 1s). The value is greater than or equal to -1. The value -1 indicates that the lifetime of the particle is infinite. If the value specified is less than -1, the default value is used.
NOTE
Setting the lifespan to -1 is not recommended unless continuous animation is required, as it may significantly impact performance.
lifeTimeRangenumberNoRange of particle lifetime values. After lifeTimeRange is set, the lifetime of a particle is a random integer between [lifetime – lifeTimeRange, lifetime + lifeTimeRange]. lifeTimeRange has a default value of 0, and its value range is from 0 to positive infinity. If it is set to a negative value, the default value is used.

ParticleUpdaterOptions14+

interface ParticleUpdaterOptions<TYPE, UPDATER extends ParticleUpdater> {
  type: UPDATER;
  config: ParticlePropertyUpdaterConfigs<TYPE>[UPDATER];
}

Defines how the color property is updated.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
typeUPDATERYesType of property updating.
The default value of type is ParticleUpdater.NONE.
configUPDATERYesHow the property is updated. The available options of type are as follows:
1. ParticleUpdater.NONE: The property does not change. In this case, the config type is ParticlePropertyUpdaterConfigs[ParticleUpdater.NONE].
2. ParticleUpdater.RANDOM: The property changes randomly. In this case, the config type is ParticlePropertyUpdaterConfigs[ParticleUpdater.RANDOM].
3. ParticleUpdater.CURVE: The property changes with the animation curve. In this case, the config type is ParticlePropertyUpdaterConfigs[ParticleUpdater.CURVE].

ParticleColorUpdaterOptions14+

interface ParticleColorUpdaterOptions<UPDATER extends ParticleUpdater> {
  type: UPDATER;
  config: ParticleColorPropertyUpdaterConfigs[UPDATER];
}

Defines how the color property is updated.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
typeUPDATERYesType of property updating.
The default value of type is ParticleUpdater.NONE.
configUPDATERYesThe available options of type are as follows:
1. ParticleUpdater.NONE: The property does not change. In this case, the config type is ParticleColorPropertyUpdaterConfigs[ParticleUpdater.NONE].
2. ParticleUpdater.RANDOM: The property changes randomly. In this case, the config type is ParticleColorPropertyUpdaterConfigs[ParticleUpdater.RANDOM].
3. ParticleUpdater.CURVE: The property changes with the animation curve. In this case, the config type is ParticleColorPropertyUpdaterConfigs[ParticleUpdater.CURVE].
NOTE
When type is set to ParticleUpdater.RANDOM or ParticleUpdater.CURVE, the color configuration in updater has higher priority than that in range. During the animation time period configured in updater, the color configuration from updater is used. Outside of the animation time period configured in updater, the color configuration from range is used.

ParticleColorOptions14+

interface ParticleColorOptions {
  r: ParticleTuple<number, number>;
  g: ParticleTuple<number, number>;
  b: ParticleTuple<number, number>;
  a: ParticleTuple<number, number>;
}

Defines the particle color options. The color changes randomly, with the per-second change difference being a value randomly generated from the range. The target color is obtained by applying the change difference to the current color value of each of the R, G, B, A channels.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
rParticleTuple<number, number>YesDifference value for the red color channel.
gParticleTuple<number, number>YesDifference value for the green color channel.
bParticleTuple<number, number>YesDifference value for the blue color channel.
aParticleTuple<number, number>YesDifference value for the alpha (transparency) channel.

Example

Example 1: Initializing Particles with Circular Shapes

This example demonstrates the basic usage of particle animations by initializing particles with circular shapes.

// xxx.ets
@Entry
@Component
struct ParticleExample {
  build() {
    Stack() {
      Text()
        .width(300).height(300).backgroundColor(Color.Black)
      Particle({
        particles: [
          {
            emitter: {
              particle: {
                type: ParticleType.POINT, // Particle type.
                config: {
                  radius: 10// Point radius.
                },
                count: 500, // Total number of particles.
                lifetime: 10000, // Particle lifetime, in ms.
                lifetimeRange: 100// Range of particle lifetime values, in ms.
              },
              emitRate: 10, // Number of particles emitted per second.
              position: [0, 0],
              shape: ParticleEmitterShape.RECTANGLE// Emitter shape.
            },
            color: {
              range: [Color.Red, Color.Yellow], // Initial color range.
              updater: {
                type: ParticleUpdater.CURVE, // Change with the animation curve.
                config: [
                  {
                    from: Color.White, // Initial value of the change.
                    to: Color.Pink, // Target value of the change.
                    startMillis: 0, // Start time.
                    endMillis: 3000, // End time.
                    curve: Curve.EaseIn// Animation curve.
                  },
                  {
                    from: Color.Pink,
                    to: Color.Orange,
                    startMillis: 3000,
                    endMillis: 5000,
                    curve: Curve.EaseIn
                  },
                  {
                    from: Color.Orange,
                    to: Color.Pink,
                    startMillis: 5000,
                    endMillis: 8000,
                    curve: Curve.EaseIn
                  },
                ]
              }
            },
            opacity: {
              range: [0.0, 1.0], // The initial value of particle opacity is randomly generated from the [0.0, 1.0] range.
              updater: {
                type: ParticleUpdater.CURVE,
                config: [
                  {
                    from: 0.0,
                    to: 1.0,
                    startMillis: 0,
                    endMillis: 3000,
                    curve: Curve.EaseIn
                  },
                  {
                    from: 1.0,
                    to: 0.0,
                    startMillis: 5000,
                    endMillis: 10000,
                    curve: Curve.EaseIn
                  }
                ]
              }
            },
            scale: {
              range: [0.0, 0.0],
              updater: {
                type: ParticleUpdater.CURVE,
                config: [
                  {
                    from: 0.0,
                    to: 0.5,
                    startMillis: 0,
                    endMillis: 3000,
                    curve: Curve.EaseIn
                  }
                ]
              }
            },
            acceleration: {
              // Acceleration. speed indicates the acceleration speed, and angle indicates the acceleration direction.
              speed: {
                range: [3, 9],
                updater: {
                  type: ParticleUpdater.RANDOM, // The speed changes randomly.
                  config: [1, 20]
                }
              },
              angle: {
                range: [90, 90]
              }
            }

          }
        ]
      }).width(300).height(300)
    }.width("100%").height("100%").align(Alignment.Center)
  }
}

particle

Example 2: Initializing Particles with Images

This example demonstrates the basic usage of particle animations by initializing particles with images.

@Entry
@Component
struct ParticleExample {
  @State
  myCount: number = 100
  flag: boolean = false;

  build() {
    Column() {
      Stack() {
        Particle({
          particles: [
            {
              emitter: {
                particle: {
                  type: ParticleType.IMAGE,
                  config: {
                    src: $r("app.media.book"),
                    size: [10, 10]
                  },
                  count: this.myCount,
                  lifetime: 10000,
                  lifetimeRange: 100
                },
                emitRate: 3,
                shape: ParticleEmitterShape.CIRCLE
              },
              color: {
                range: [Color.White, Color.White]
              },
              opacity: {
                range: [1.0, 1.0],
                updater: {
                  type: ParticleUpdater.CURVE,
                  config: [
                    {
                      from: 0,
                      to: 1.0,
                      startMillis: 0,
                      endMillis: 6000
                    },
                    {
                      from: 1.0,
                      to: .0,
                      startMillis: 6000,
                      endMillis: 10000
                    }
                  ]
                }
              },
              scale: {
                range: [0.1, 1.0],
                updater: {
                  type: ParticleUpdater.CURVE,
                  config: [
                    {
                      from: 0,
                      to: 1.5,
                      startMillis: 0,
                      endMillis: 8000,
                      curve: Curve.EaseIn
                    }

                  ]
                }
              },
              acceleration: {
                speed: {
                  range: [3, 9],
                  updater: {
                    type: ParticleUpdater.CURVE,
                    config: [
                      {
                        from: 10,
                        to: 20,
                        startMillis: 0,
                        endMillis: 3000,
                        curve: Curve.EaseIn
                      },
                      {
                        from: 10,
                        to: 2,
                        startMillis: 3000,
                        endMillis: 8000,
                        curve: Curve.EaseIn
                      }
                    ]
                  }
                },
                angle: {
                  range: [0, 180],
                  updater: {
                    type: ParticleUpdater.CURVE,
                    config: [{
                      from: 1,
                      to: 2,
                      startMillis: 0,
                      endMillis: 1000,
                      curve: Curve.EaseIn
                    },
                      {
                        from: 50,
                        to: -50,
                        startMillis: 1000,
                        endMillis: 3000,
                        curve: Curve.EaseIn
                      },
                      {
                        from: 3,
                        to: 5,
                        startMillis: 3000,
                        endMillis: 8000,
                        curve: Curve.EaseIn
                      }
                    ]
                  }
                }
              },
              spin: {
                range: [0.1, 1.0],
                updater: {
                  type: ParticleUpdater.CURVE,
                  config: [
                    {
                      from: 0,
                      to: 360,
                      startMillis: 0,
                      endMillis: 8000,
                      curve: Curve.EaseIn
                    }
                  ]
                }
              },
            }
            , {
            emitter: {
              particle: {
                type: ParticleType.IMAGE,
                config: {
                  src: $r('app.media.heart'),
                  size: [10, 10]
                },
                count: this.myCount,
                lifetime: 10000,
                lifetimeRange: 100
              },
              emitRate: 3,
              shape: ParticleEmitterShape.CIRCLE
            },
            color: {
              range: [Color.White, Color.White]
            },
            opacity: {
              range: [1.0, 1.0],
              updater: {
                type: ParticleUpdater.CURVE,
                config: [
                  {
                    from: 0,
                    to: 1.0,
                    startMillis: 0,
                    endMillis: 6000
                  },
                  {
                    from: 1.0,
                    to: .0,
                    startMillis: 6000,
                    endMillis: 10000
                  }
                ]
              }
            },
            scale: {
              range: [0.1, 1.0],
              updater: {
                type: ParticleUpdater.CURVE,
                config: [
                  {
                    from: 0,
                    to: 2.0,
                    startMillis: 0,
                    endMillis: 10000,
                    curve: Curve.EaseIn
                  }

                ]
              }
            },
            acceleration: {
              speed: {
                range: [3, 9],
                updater: {
                  type: ParticleUpdater.CURVE,
                  config: [
                    {
                      from: 10,
                      to: 20,
                      startMillis: 0,
                      endMillis: 3000,
                      curve: Curve.EaseIn
                    },
                    {
                      from: 10,
                      to: 2,
                      startMillis: 3000,
                      endMillis: 8000,
                      curve: Curve.EaseIn
                    }
                  ]
                }
              },
              angle: {
                range: [0, 180],
                updater: {
                  type: ParticleUpdater.CURVE,
                  config: [{
                    from: 1,
                    to: 2,
                    startMillis: 0,
                    endMillis: 1000,
                    curve: Curve.EaseIn
                  },
                    {
                      from: 50,
                      to: -50,
                      startMillis: 0,
                      endMillis: 3000,
                      curve: Curve.EaseIn
                    },
                    {
                      from: 3,
                      to: 5,
                      startMillis: 3000,
                      endMillis: 10000,
                      curve: Curve.EaseIn
                    }
                  ]
                }
              }
            },
            spin: {
              range: [0.1, 1.0],
              updater: {
                type: ParticleUpdater.CURVE,
                config: [
                  {
                    from: 0,
                    to: 360,
                    startMillis: 0,
                    endMillis: 10000,
                    curve: Curve.EaseIn
                  }
                ]
              }
            },
          }, {
            emitter: {
              particle: {
                type: ParticleType.IMAGE,
                config: {
                  src: $r('app.media.sun'),
                  size: [10, 10]
                },
                count: this.myCount,
                lifetime: 10000,
                lifetimeRange: 100
              },
              emitRate: 3,
              shape: ParticleEmitterShape.CIRCLE
            },
            color: {
              range: [Color.White, Color.White]
            },
            opacity: {
              range: [1.0, 1.0],
              updater: {
                type: ParticleUpdater.CURVE,
                config: [
                  {
                    from: 0,
                    to: 1.0,
                    startMillis: 0,
                    endMillis: 6000
                  },
                  {
                    from: 1.0,
                    to: .0,
                    startMillis: 6000,
                    endMillis: 10000
                  }
                ]
              }
            },
            scale: {
              range: [0.1, 1.0],
              updater: {
                type: ParticleUpdater.CURVE,
                config: [
                  {
                    from: 0,
                    to: 2.0,
                    startMillis: 0,
                    endMillis: 10000,
                    curve: Curve.EaseIn
                  }

                ]
              }
            },
            acceleration: {
              speed: {
                range: [3, 9],
                updater: {
                  type: ParticleUpdater.CURVE,
                  config: [
                    {
                      from: 10,
                      to: 20,
                      startMillis: 0,
                      endMillis: 3000,
                      curve: Curve.EaseIn
                    },
                    {
                      from: 10,
                      to: 2,
                      startMillis: 3000,
                      endMillis: 8000,
                      curve: Curve.EaseIn
                    }
                  ]
                }
              },
              angle: {
                range: [0, 180],
                updater: {
                  type: ParticleUpdater.CURVE,
                  config: [{
                    from: 1,
                    to: 2,
                    startMillis: 0,
                    endMillis: 1000,
                    curve: Curve.EaseIn
                  },
                    {
                      from: 50,
                      to: -50,
                      startMillis: 1000,
                      endMillis: 3000,
                      curve: Curve.EaseIn
                    },
                    {
                      from: 3,
                      to: 5,
                      startMillis: 3000,
                      endMillis: 8000,
                      curve: Curve.EaseIn
                    }
                  ]
                }
              }
            },
            spin: {
              range: [0.1, 1.0],
              updater: {
                type: ParticleUpdater.CURVE,
                config: [
                  {
                    from: 0,
                    to: 360,
                    startMillis: 0,
                    endMillis: 10000,
                    curve: Curve.EaseIn
                  }
                ]
              }
            },
          }
          ]
        }).width(300).height(300)

      }.width(500).height(500).align(Alignment.Center)
    }.width("100%").height("100%")

  }
}

particle

Example 3: Changing Motion Trajectories with the Particle Disturbance Field

This example demonstrates how to change the motion trajectories of particles by applying disturbances through the particle disturbance field.

@Entry
@Component
struct ParticleExample {
  build() {
    Stack() {
      Text()
        .width(300).height(300).backgroundColor(Color.Black)
      Particle({
        particles: [
          {
            emitter: {
              particle: {
                type: ParticleType.POINT, // Particle type.
                config: {
                  radius: 10// Point radius.
                },
                count: 500, // Total number of particles.
                lifetime: 10000// Particle lifetime, in ms.
              },
              emitRate: 10, // Number of particles emitted per second.
              position: [0, 0],
              shape: ParticleEmitterShape.RECTANGLE// Emitter shape.
            },
            color: {
              range: [Color.Red, Color.Yellow], // Initial color range.
              updater: {
                type: ParticleUpdater.CURVE, // Change with the animation curve.
                config: [
                  {
                    from: Color.White, // Initial value of the change.
                    to: Color.Pink, // Target value of the change.
                    startMillis: 0, // Start time.
                    endMillis: 3000, // End time.
                    curve: Curve.EaseIn// Animation curve.
                  },
                  {
                    from: Color.Pink,
                    to: Color.Orange,
                    startMillis: 3000,
                    endMillis: 5000,
                    curve: Curve.EaseIn
                  },
                  {
                    from: Color.Orange,
                    to: Color.Pink,
                    startMillis: 5000,
                    endMillis: 8000,
                    curve: Curve.EaseIn
                  },
                ]
              }
            },
            opacity: {
              range: [0.0, 1.0], // The initial value of particle opacity is randomly generated from the [0.0, 1.0] range.
              updater: {
                type: ParticleUpdater.CURVE,
                config: [
                  {
                    from: 0.0,
                    to: 1.0,
                    startMillis: 0,
                    endMillis: 3000,
                    curve: Curve.EaseIn
                  },
                  {
                    from: 1.0,
                    to: 0.0,
                    startMillis: 5000,
                    endMillis: 10000,
                    curve: Curve.EaseIn
                  }
                ]
              }
            },
            scale: {
              range: [0.0, 0.0],
              updater: {
                type: ParticleUpdater.CURVE,
                config: [
                  {
                    from: 0.0,
                    to: 0.5,
                    startMillis: 0,
                    endMillis: 3000,
                    curve: Curve.EaseIn
                  }
                ]
              }
            },
            acceleration: {
              // Acceleration. speed indicates the acceleration speed, and angle indicates the acceleration direction.
              speed: {
                range: [3, 9],
                updater: {
                  type: ParticleUpdater.RANDOM,
                  config: [1, 20]
                }
              },
              angle: {
                range: [90, 90]
              }
            }

          }
        ]
      }).width(300).height(300).disturbanceFields([{
        strength: 10,
        shape: DisturbanceFieldShape.RECT,
        size: { width: 100, height: 100 },
        position: { x: 100, y: 100 },
        feather: 15,
        noiseScale: 10,
        noiseFrequency: 15,
        noiseAmplitude: 5
      }])
    }.width("100%").height("100%").align(Alignment.Center)
  }
}

particle

Example 4: Adjusting the Emitter Position

This example demonstrates how to adjust the position of the particle emitter through emitter().

@Entry
@Component
struct ParticleExample {
  @State emitterProperties: Array<EmitterProperty> = [
    {
      index: 0,
      emitRate: 100,
      position: { x: 60, y: 80 },
      size: { width: 200, height: 200 }
    }
  ]

  build() {
    Stack() {
      Text()
        .width(300).height(300).backgroundColor(Color.Black)
      Particle({
        particles: [
          {
            emitter: {
              particle: {
                type: ParticleType.POINT, // Particle type.
                config: {
                  radius: 5// Point radius.
                },
                count: 400, // Total number of particles.
                lifetime: -1// Particle lifetime. The value -1 indicates that the lifetime of the particle is infinite.
              },
              emitRate: 10, // Number of particles emitted per second.
              position: [0, 0], // Emitter position.
              shape: ParticleEmitterShape.CIRCLE// Emitter shape.
            },
            color: {
              range: [Color.Red, Color.Yellow], // Initial color range.
              updater: {
                type: ParticleUpdater.CURVE, // Change with the animation curve.
                config: [
                  {
                    from: Color.White,
                    to: Color.Pink,
                    startMillis: 0,
                    endMillis: 3000,
                    curve: Curve.EaseIn
                  },
                  {
                    from: Color.Pink,
                    to: Color.Orange,
                    startMillis: 3000,
                    endMillis: 5000,
                    curve: Curve.EaseIn
                  },
                  {
                    from: Color.Orange,
                    to: Color.Pink,
                    startMillis: 5000,
                    endMillis: 8000,
                    curve: Curve.EaseIn
                  },
                ]
              }
            },
          },
        ]
      })
        .width(300)
        .height(300)
        .emitter(this.emitterProperties)
    }.width("100%").height("100%").align(Alignment.Center)
  }
}

particle

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArcButton

harmony 鸿蒙ArcSlider

harmony 鸿蒙Chip

harmony 鸿蒙ChipGroup

harmony 鸿蒙ComposeListItem

harmony 鸿蒙ComposeTitleBar

harmony 鸿蒙advanced.Counter

harmony 鸿蒙Dialog Box (Dialog)

harmony 鸿蒙DialogV2

harmony 鸿蒙DownloadFileButton

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/wLMdGh