Convert Figma logo to code with AI

glomadrian logoGrav

Configurable animations based on points

2,710
328
2,710
18

Top Related Projects

15,508

A JavaScript Typing Animation Library

49,575

JavaScript animation engine

26,486

Animate on scroll library

A lightweight JavaScript library for creating particles

19,483

GSAP (GreenSock Animation Platform), a JavaScript animation library for the modern web

The javascript library for magical scroll interactions.

Quick Overview

Grav is an Android library that provides a custom view for creating particle animations. It allows developers to easily add dynamic, physics-based particle effects to their Android applications, enhancing the visual appeal and user experience.

Pros

  • Easy to integrate into existing Android projects
  • Highly customizable with various particle properties and behaviors
  • Smooth and performant animations
  • Supports both Java and Kotlin

Cons

  • Limited documentation and examples
  • May impact app performance if overused or not optimized
  • Requires API level 16 or higher
  • Not actively maintained (last update was in 2017)

Code Examples

  1. Basic Grav view setup:
val grav = findViewById<Grav>(R.id.grav)
val generator = PointGenerator(width, height)
val particle = BallGenerator.generate(ContextCompat.getColor(this, R.color.colorPrimary), 5)
grav.addGenerator(generator, particle, 100)

This code sets up a basic Grav view with 100 ball particles of a specific color and size.

  1. Custom particle behavior:
val gravityBehavior = GravityBehavior(Vector(0f, 0.5f))
val colorBehavior = ColorBehavior(Color.BLUE, Color.RED, 3000)
grav.addBehavior(gravityBehavior)
grav.addBehavior(colorBehavior)

This example adds gravity and color-changing behaviors to the particles.

  1. Starting and stopping animations:
grav.start()
// ... later in the code
grav.stop()

These methods control the start and stop of the particle animations.

Getting Started

To use Grav in your Android project, follow these steps:

  1. Add the JitPack repository to your project's build.gradle file:
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. Add the dependency to your app's build.gradle file:
dependencies {
    implementation 'com.github.glomadrian:Grav:1.1'
}
  1. Add the Grav view to your layout XML:
<com.github.glomadrian.grav.GravView
    android:id="@+id/grav"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
  1. Initialize and customize the Grav view in your activity or fragment:
val grav = findViewById<Grav>(R.id.grav)
val generator = PointGenerator(width, height)
val particle = BallGenerator.generate(Color.RED, 10)
grav.addGenerator(generator, particle, 50)
grav.start()

This sets up a basic Grav view with 50 red ball particles and starts the animation.

Competitor Comparisons

15,508

A JavaScript Typing Animation Library

Pros of Typed.js

  • Lightweight and focused on typing animation
  • Easy to implement with minimal setup
  • Supports multiple typing options (strings, arrays, JSON)

Cons of Typed.js

  • Limited to text-based animations
  • Lacks advanced visual effects or particle systems
  • May require additional libraries for more complex animations

Code Comparison

Typed.js:

var typed = new Typed('#element', {
  strings: ['First sentence.', 'Second sentence.'],
  typeSpeed: 30
});

Grav:

var grav = new Grav({
  container: document.getElementById('container'),
  particles: 100,
  color: '#ffffff'
});
grav.start();

Key Differences

Typed.js focuses on creating typing animations for text, while Grav is designed for particle-based animations and visual effects. Typed.js is more suitable for simple text animations, whereas Grav offers more complex and visually appealing particle systems.

Typed.js is easier to implement for basic typing effects, but Grav provides more flexibility for creating dynamic and interactive visual experiences. The choice between the two depends on the specific animation requirements of your project.

49,575

JavaScript animation engine

Pros of anime

  • Lightweight and flexible JavaScript animation library
  • Supports a wide range of animation properties and easing functions
  • Extensive documentation and examples available

Cons of anime

  • Limited to JavaScript animations, not suitable for physics-based animations
  • Requires more manual setup for complex animations compared to Grav

Code Comparison

anime:

anime({
  targets: '.element',
  translateX: 250,
  rotate: '1turn',
  duration: 800,
  easing: 'easeInOutQuad'
});

Grav:

Grav.build()
    .setDuration(800)
    .setPosition(250, 0)
    .setRotation(360)
    .start(view);

Key Differences

  • anime is a JavaScript library, while Grav is an Android library
  • Grav focuses on physics-based animations, while anime is more general-purpose
  • anime offers more flexibility in terms of animation properties and easing functions
  • Grav provides a simpler API for creating complex physics-based animations

Use Cases

  • Choose anime for web-based projects requiring diverse animation effects
  • Opt for Grav when developing Android apps with physics-based animations

Both libraries have their strengths, and the choice depends on the specific project requirements and platform.

26,486

Animate on scroll library

Pros of AOS

  • More actively maintained with recent updates
  • Wider browser compatibility, including older versions
  • Extensive documentation and examples available

Cons of AOS

  • Limited to scroll-based animations
  • Requires JavaScript, which may impact performance
  • Less customizable than Grav for complex animations

Code Comparison

AOS:

<div data-aos="fade-up" data-aos-duration="1000">
  <h1>Animated element</h1>
</div>

Grav:

<div class="grav-item" data-grav-animation="fadeIn" data-grav-duration="1000">
  <h1>Animated element</h1>
</div>

Both libraries use HTML attributes to define animations, but AOS focuses on scroll-based triggers, while Grav offers more flexibility in animation types and triggers.

Key Differences

  • AOS is primarily designed for scroll-based animations, while Grav provides a broader range of animation options
  • Grav offers more customization for complex animations, but AOS is easier to implement for simple scroll effects
  • AOS has better documentation and community support, whereas Grav may require more experimentation to achieve desired results

Use Cases

  • Choose AOS for quick implementation of scroll-based animations on websites
  • Opt for Grav when creating more complex, interactive animations or when performance is a critical factor

A lightweight JavaScript library for creating particles

Pros of particles.js

  • Lightweight and easy to implement
  • Highly customizable with numerous configuration options
  • Supports multiple shapes and behaviors for particles

Cons of particles.js

  • Limited to 2D particle effects
  • May impact performance on older devices with many particles
  • Less suitable for complex, physics-based animations

Code Comparison

particles.js:

particlesJS('particles-js', {
  particles: {
    number: { value: 80, density: { enable: true, value_area: 800 } },
    color: { value: '#ffffff' },
    shape: { type: 'circle' }
  }
});

Grav:

GravView gravView = (GravView) findViewById(R.id.grav);
gravView.setNumPoints(100);
gravView.setPointColor(Color.WHITE);
gravView.setPointSize(20);
gravView.animate();

Summary

particles.js is a lightweight JavaScript library for creating particle animations, offering extensive customization options. It's ideal for simple, decorative particle effects on websites. Grav, on the other hand, is an Android library focused on creating more complex, physics-based particle animations. While particles.js excels in ease of use and browser compatibility, Grav provides more advanced features for mobile app developers, including 3D effects and realistic particle behaviors.

19,483

GSAP (GreenSock Animation Platform), a JavaScript animation library for the modern web

Pros of GSAP

  • More comprehensive animation toolkit with a wider range of features
  • Extensive documentation and community support
  • Cross-browser compatibility and performance optimization

Cons of GSAP

  • Larger file size and potential performance impact for simpler projects
  • Steeper learning curve due to its extensive API
  • Commercial license required for some advanced features

Code Comparison

GSAP:

gsap.to(".box", {duration: 2, x: 100, y: 50, rotation: 360});

Grav:

Grav.to(".box", {duration: 2000, x: 100, y: 50, rotate: 360});

Key Differences

  • GSAP offers a more robust and feature-rich animation system
  • Grav focuses specifically on gravity-based animations
  • GSAP has a larger community and more frequent updates
  • Grav is lighter and may be easier to implement for simple gravity effects

Use Cases

  • Choose GSAP for complex, diverse animations across web projects
  • Opt for Grav when specifically needing gravity-based effects with a smaller footprint

The javascript library for magical scroll interactions.

Pros of ScrollMagic

  • More comprehensive documentation and examples
  • Supports a wider range of scroll-based animations and effects
  • Better integration with popular animation libraries like GSAP

Cons of ScrollMagic

  • Steeper learning curve due to more complex API
  • Larger file size, which may impact page load times
  • Requires more setup and configuration for basic animations

Code Comparison

ScrollMagic:

var controller = new ScrollMagic.Controller();
var scene = new ScrollMagic.Scene({
    triggerElement: "#trigger",
    duration: 300
})
.setTween("#animate", {scale: 2.5})
.addTo(controller);

Grav:

var grav = new Grav({
    element: '#animate',
    animation: {
        scale: 2.5
    },
    scrollStart: 0,
    scrollEnd: 300
});

ScrollMagic offers more flexibility and control over scroll-based animations, making it suitable for complex projects. It provides extensive documentation and examples, which can be beneficial for developers looking to create advanced scroll interactions.

On the other hand, Grav has a simpler API and requires less setup, making it easier to implement basic scroll animations quickly. It has a smaller file size, which can be advantageous for projects prioritizing performance.

The code comparison shows that ScrollMagic requires more lines of code and configuration, while Grav achieves similar results with a more concise syntax. This reflects the trade-off between flexibility and simplicity in these two libraries.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Grav

Download Hex.pm Platform

Drawing

Grav is a library for android which allow you to make multiple animations based on points.

Drawing Drawing Drawing Drawing Drawing Drawing

How to use it

The library is composed of generators, each generator takes care of one thing. Composing different generators you can obtain different results.

There are different kind of generators:

  • Point generator
  • Grav generator
  • Color generator
  • Animation generator

Point generator

The point generator is the class that creates the locations of the points that will be drawn using a Grav Generator. In order to use it you need to set the attribute:

app:pointGenerator="classname"

The first thing you need is a Point generator, the are 3 generators available:

Regular point generator

Generate points based on a cell and a variance.

app:pointGenerator="com.github.glomadrian.grav.generator.point.RegularPointGenerator"
app:regular_cell_size="200"
app:regular_variance="20"

(Using in combination with GravBallGeneartor)

Drawing

Circular point generator

Generate points based on a cell and a variance in a circular shape.

app:pointGenerator="com.github.glomadrian.grav.generator.point.CircularPointGenerator"
app:regular_cell_size="200"
app:regular_variance="200"

(Using in combination with GravBallGeneartor)

Drawing

Percent point generator

Generate the points based on percent locations array.

app:pointGenerator="com.github.glomadrian.grav.generator.point.PercentPointGenerator"
app:percent_points_array="@array/walla_points_percent_points"

Inside arrays.xml each pair of items represents a point in the view, which is defined by a width percent and a height percent.

<integer-array name="sample_points_percent_points">
   <item>10</item>
   <item>60</item>

   <item>8</item>
   <item>30</item>

   <item>25</item>
   <item>10</item>
 </integer-array>

(Using in combination with GravBallGeneartor)

Drawing

Grav Generator

Grav generator is the piece in charge of draw every point previously generated. To use you need to set the attribute:

app:gravGenerator="classname"

There are two Grav Generator available

Ball generator

Draws a ball.

Ball sizes can be generated randomly within a range using the following attributes:

app:gravGenerator="com.github.glomadrian.grav.generator.grav.BallGenerator"
app:ball_from_size="3dp"
app:ball_to_size="16dp"
Drawing

Rectangle generator

Draws rectangles.

Size of the rectangles can be changed using the following attributes:

app:gravGenerator="com.github.glomadrian.grav.generator.grav.RectangleGenerator"
app:rectangle_width="15dp"
app:rectangle_height="10dp"
Drawing

Color generator

Decides how the Grav is painted.

To use you need to set the attribute:

appcolorGenerator="classname"

There are two Color Generator available.

Single color generator

Paint in one single color.

app:colorGenerator="com.github.glomadrian.grav.generator.paint.SingleColorGenerator"
app:single_color="@color/colorPrimary"

Array color generator

Paint using array of colors

app:colorGenerator="com.github.glomadrian.grav.generator.paint.ArrayColorGenerator"
app:array_colors="@array/Spectral"
Drawing

Animation generator

The animation generator takes care of the position, size and properties animation. The animation generator can be one or an array of animation generators.

Single animation generator

To use you need to set the attribute:

app:animationGenerator="classname"

Array animation generator

To use you need to set the attribute:

app:animationGenerators="@array/array_reference"

<string-array name="array_reference">
  <item>com.github.glomadrian.grav.generator.animation.PathAnimator</item>
  <item>com.github.glomadrian.grav.generator.animation.BallSizeAnimator</item>
</string-array>

Shake animator

Moves the Grav within a range of values.

app:animationGenerator=" com.github.glomadrian.grav.generator.animation.ShakeAnimator"
//Min animation duration
app:shake_min_duration="1000"
//Max animation duration
app:shake_max_duration="3000"
//Direction horizontal or vertical
app:shake_direction="horizontal"
//The size of the movement
app:shake_variance="15dp"
Drawing

Side to side animator

Aniamtes the translation of the Grav to a different side.

app:animationGenerator="com.github.glomadrian.grav.generator.animation.SideToSideAnimator"
//Min animation duration
app:side_to_side_min_duration="1000"
//Max animation duration
app:side_to_side_max_duration="3000"
//Direction leftToRight | rightToLeft | upToDown | downToUp
app:side_to_side_direction="leftToRight"

Also you can use a interpolator.

side_to_side_interpolator="interpolator class"
Drawing

Alpha animator

Applies an alpha animation to a Grav.

app:animationGenerator="com.github.glomadrian.grav.generator.animation.AlphaAnimator"
//Min animation duration
app:alpha_min_duration="1000"
//Max animation duration
app:alpha_max_duration="3000"
//From and to in a range (0-255)
app:alpha_from="0"
app:alpha_to="255"

Ball size animator

Animates the size of the ball.

app:animationGenerator="com.github.glomadrian.grav.generator.animation.BallSizeAnimator"
//Min animation duration
app:ball_size_min_duration="1000"
//Max animation duration
app:ball_size_max_duration="3000"
app:ball_size_from_size="3dp"
app:ball_size_to_size="8dp"
Drawing

Path animator

The path animator moves the grav following a path.

The path (sample)

<string name="circle">
M527.023,71.8233 C780.213,71.8233,985.464,277.075,985.464,530.265
C985.464,783.455,780.213,988.707,527.023,988.707
C273.832,988.707,68.5809,783.455,68.5809,530.265
C68.5809,277.075,273.832,71.8233,527.023,71.8233 Z
</string>
<integer name="circle_original_width">1062</integer>
<integer name="circle_original_height">1062</integer>

Using path animator

app:animationGenerator="com.github.glomadrian.grav.generator.animation.PathAnimator"
//Variance is the random margin given to the grav
app:path_variance_from="1dp"
app:path_variance_to="20dp"
//Min animation duration
app:path_min_duration="2000"
//Max animation duration
app:path_max_duration="2300"
//String that define the path
app:path="@string/circle"
app:path_original_width="@integer/circle_original_width"
app:path_original_height="@integer/circle_original_height"
Drawing

XML Samples

The full samples can be found in the demo app source code.

Drawing
<com.github.glomadrian.grav.GravView
     android:id="@+id/grav"
     android:layout_centerInParent="true"
     android:layout_width="400dp"
     android:layout_height="400dp"
     app:colorGenerator="com.github.glomadrian.grav.generator.paint.ArrayColorGenerator"
     app:array_colors="@array/red"
     app:pointGenerator="com.github.glomadrian.grav.generator.point.RegularPointGenerator"
     app:regular_cell_size="150"
     app:regular_variance="100"
     app:gravGenerator="com.github.glomadrian.grav.generator.grav.BallGenerator"
     app:ball_size_from_size="3dp"
     app:ball_size_to_size="6dp"
     app:animationGenerators="@array/path"
     app:path_variance_from="-10dp"
     app:path_variance_to="12dp"
     app:path="@string/circle"
     app:path_original_width="@integer/circle_original_width"
     app:path_original_height="@integer/circle_original_height"
     app:path_min_duration="5000"
     app:path_max_duration="6000"
     />
Drawing
<com.github.glomadrian.grav.GravView
     android:id="@+id/grav"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_centerInParent="true"
     app:colorGenerator="com.github.glomadrian.grav.generator.paint.ArrayColorGenerator"
     app:array_colors="@array/bubble"
     app:pointGenerator="com.github.glomadrian.grav.generator.point.RegularPointGenerator"
     app:regular_cell_size="300"
     app:regular_variance="200"
     app:gravGenerator="com.github.glomadrian.grav.generator.grav.BallGenerator"
     app:ball_from_size="10dp"
     app:ball_to_size="20dp"
     app:animationGenerators="@array/BubbleAnimations"
     app:side_to_side_min_duration="10000"
     app:side_to_side_max_duration="10000"
     app:side_to_side_direction="leftToRight"
     app:shake_direction="vertical"
     app:shake_min_duration="10000"
     app:shake_max_duration="20000"
     app:shake_variance="500dp"
    />
Drawing
<com.github.glomadrian.grav.GravView
      android:id="@+id/grav"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:colorGenerator="com.github.glomadrian.grav.generator.paint.ArrayColorGenerator"
      app:array_colors="@array/falcon"
      app:pointGenerator="com.github.glomadrian.grav.generator.point.RegularPointGenerator"
      app:regular_cell_size="100"
      app:regular_variance="200"
      app:gravGenerator="com.github.glomadrian.grav.generator.grav.RectangleGenerator"
      app:rectangle_width="15dp"
      app:rectangle_height="2dp"
      app:animationGenerators="@array/FalconAnimations"
      app:side_to_side_min_duration="400"
      app:side_to_side_max_duration="800"
      app:side_to_side_direction="rightToLeft"
      app:shake_variance="5dp"
      app:shake_direction="vertical"
      />
Drawing
<com.github.glomadrian.grav.GravView
    android:id="@+id/grav"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:colorGenerator="com.github.glomadrian.grav.generator.paint.OneColorGenerator"
    app:single_color="#FFF"
    app:pointGenerator="com.github.glomadrian.grav.generator.point.PercentPointGenerator"
    app:ball_from_size="2dp"
    app:ball_to_size="5dp"
    app:animationGenerators="@array/WallaIcon"
    app:alpha_from="100"
    app:alpha_to="200"
    app:alpha_min_duration="1600"
    app:alpha_max_duration="3000"
    app:shake_direction="vertical"
    app:shake_variance="3dp"
    app:shake_min_duration="1100"
    app:shake_max_duration="3500"
    app:percent_points_array="@array/walla_points_percent_points"
    app:gravGenerator="com.github.glomadrian.grav.generator.grav.BallGenerator"
    />

Attributions

  • Thanks to @manolovn for Trianglify where I obtained the point generators.
  • Thanks to @jlmd for the corrections made to this Readme

For Gradle

Add repository

repositories {
  maven {
    url "http://dl.bintray.com/glomadrian/maven"
  }
}

Add dependency

compile 'com.github.glomadrian:Grav:1.1'

Developed By

Adrián García Lomas - glomadrian@gmail.com

License

  Copyright 2017 Adrián García Lomas

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.