# Leonids **Repository Path**: HarmonyOS-tpc/Leonids ## Basic Information - **Project Name**: Leonids - **Description**: Leonids Particle Systems is graphic library, which add Playful effects in applications. - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-04-15 - **Last Updated**: 2025-02-14 ## Categories & Tags **Categories**: harmonyos-image **Tags**: None ## README ## Leonids ## Introduction Leonids Particle Systems is graphic library, which add Playful effects in applications. Particle systems are often used in games for a wide range of purposes: Explosions, fire, smoke, etc. Particle System are also implemented using OPENGL, which is more complex and time consuming. Leonids is made to fill this gap, bringing particle sytems to developers that use the standard openharmony UI. The library is extremely lightweight, having just 26Kb. ## Usage Instructions ## Basic usage Creating and firing a one-shot particle system is very easy, just few lines of code. ```java new ParticleSystem(mRootId, 50, ResUtil.getPixelMapElementByResId(getContext(), ResourceTable.Media_star_pink), 3000) .setAcceleration(0.00013f, 90) .setSpeedByComponentsRange(0f, 0f, 0.05f, 0.1f) .setFadeOut(200, Animator.CurveType.ACCELERATE) .emitWithGravity(component, LayoutAlignment.BOTTOM, 30); ``` Note that the ParticleSystem checks the position of the anchor component when oneShot (or emit) is called, so it requires the components to be measured. This means that **ParticleSystem won't work properly if you call oneShot or emit during onCreate**. When you create the particle system, you tell how many particles will it use as a maximum, the resourceId of the drawable you want to use for the particles and for how long the particles will live. Then you configure the particle system. In this case we specify that the particles will have a speed between 0.2 and 0.5 pixels per milisecond (support for dips will be included in the future). Since we did not provide an angle range, it will be considered as "any angle". Finally, we call oneShot, passing the component from which the particles will be launched and saying how many particles we want to be shot. ## Emitters You can configure emitters, which have a constant ratio of particles being emited per second. This is the code for the Confeti example: ```java new ParticleSystem(mRootId, 24, ResUtil.getPixelMapElementByResId(getContext(), ResourceTable.Media_confeti2), 10000) .setSpeedModuleAndAngleRange(0f, 0.1f, 180, 180) .setRotationSpeed(144) .setAcceleration(0.000017f, 90) .emit(mRootId.findComponentById(ResourceTable.Id_emiter_top_right), 8); new ParticleSystem(mRootId, 24, ResUtil.getPixelMapElementByResId(getContext(), ResourceTable.Media_confeti3), 10000) .setSpeedModuleAndAngleRange(0f, 0.1f, 0, 0) .setRotationSpeed(144) .setAcceleration(0.000017f, 90) .emit(mRootId.findComponentById(ResourceTable.Id_emiter_top_left), 8); ``` It uses an initializer for the Speed as module and angle ranges, a fixed speed rotaion and extenal acceleration. ## Available Methods List of the methods available on the class ParticleSystem. ### Constructors All constructors use the activity, the maximum number of particles and the time to live. The difference is in how the image for the particles is specified. Supported drawables are: PixelMapElement and FrameAnimationElement. * _ParticleSystem(ComponentContainer parentComponent, int maxParticles, Element drawable, long timeToLive)_ ### Configuration Available methods on the Particle system for configuration are: * _setSpeedRange(float speedMin, float speedMax)_: Uses 0-360 as the angle range * _setSpeedModuleAndAngleRange(float speedMin, float speedMax, int minAngle, int maxAngle)_ * _setSpeedByComponentsRange(float speedMinX, float speedMaxX, float speedMinY, float speedMaxY)_ * _setInitialRotationRange (int minAngle, int maxAngle)_ * _setScaleRange(float minScale, float maxScale)_ * _setRotationSpeed(float rotationSpeed)_ * _setRotationSpeedRange(float minRotationSpeed, float maxRotationSpeed)_ * _setAcceleration(float acceleration, float angle)_ * _setFadeOut(long milisecondsBeforeEnd, CurveType curvetype)_: Utility method for a simple fade out effect using an curvetype * _setFadeOut(long duration)_:Utility method for a simple fade out You can start the particle system "in the future" if you want to have the particles already created and moving using _setStartTime(int time)_ For more complex modifiers, you can use the method _addModifier(ParticleModifier modifier)_. Available modifiers are: * _AlphaModifier (int initialValue, int finalValue, long startMilis, long endMilis)_ * _AlphaModifier (int initialValue, int finalValue, long startMilis, long endMilis, Curvetype curvetype)_ * _ScaleModifier (float initialValue, float finalValue, long startMilis, long endMilis)_ * _ScaleModifier (float initialValue, float finalValue, long startMilis, long endMilis, Curvetype curvetype)_ ### One shot Make one shot using from the anchor component using the number of particles specified, an interpolator is optional * _oneShot(Component anchor, int numParticles)_ * _oneShot(Component anchor, int numParticles, Curvetype curvetype)_ ### Emitters Emits the number of particles per second from the emitter. If emittingTime is set, the emitter stops after that time, otherwise it is continuous. #### Basic emitters * _emit (Component emitter, int particlesPerSecond)_ * _emit (Component emitter, int particlesPerSecond, int emittingTime)_ #### Emit based on (x,y) coordinates * _emit (int emitterX, int emitterY, int particlesPerSecond)_ * _emit (int emitterX, int emitterY, int particlesPerSecond, int emitingTime)_ #### Emit with Gravity * _emitWithGravity (Component emiter, int gravity, int particlesPerSecond)_ * _emitWithGravity (Component emiter, int gravity, int particlesPerSecond, int emitingTime)_ #### Update, stop, and cancel * _updateEmitPoint (int emitterX, int emitterY)_ Updates dynamically the point of emission. * _updateEmitPoint (Component emiter, int gravity)_ Updates dynamically the point of emission using gravity. * _stopEmitting ()_ Stops the emission of new particles, but the active ones are updated. * _cancel ()_ Stops the emission of new particles and cancles the active ones. ## Other details The library is Free Software, you can use it, extended with no requirement to open source your changes. You can also make paid apps using it. Each Particle System only uses one image for the particles. If you want different particles to be emitted, you need to create a Particle System for each one of them. ## Installation Instructions ``` Method 1: Generate har package from library and add it to lib folder. add following code to gradle of entry implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) Method 2: allprojects{ repositories{ mavenCentral() } } implementation 'io.openharmony.tpc.thirdlib:Leonids:1.0.1' ```