Convert Figma logo to code with AI

sarcadass logogranim.js

Create fluid and interactive gradient animations with this small javascript library.

5,312
232
5,312
29

Top Related Projects

106,888

JavaScript 3D Library.

22,842

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —

45,474

The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.

8,165

The Easel Javascript library provides a full, hierarchical display list, a core interaction model, and helper classes to make working with the HTML5 Canvas element much easier.

30,275

Javascript Canvas Library, SVG-to-Canvas (& canvas-to-SVG) Parser

14,796

The Swiss Army Knife of Vector Graphics Scripting – Scriptographer ported to JavaScript and the browser, using HTML5 Canvas. Created by @lehni & @puckey

Quick Overview

Granim.js is a lightweight JavaScript library for creating fluid and interactive gradient animations. It allows developers to easily create and customize animated gradients that can be used as backgrounds or elements in web applications. The library supports both linear and radial gradients with smooth transitions between multiple colors.

Pros

  • Easy to use with a simple API and minimal setup required
  • Lightweight (< 17 KB minified) and dependency-free
  • Supports multiple gradient types (linear and radial) and directions
  • Offers interactive features like pausing, playing, and changing states

Cons

  • Limited to gradient animations only, not suitable for more complex animations
  • May impact performance on older devices or when used with multiple instances
  • Documentation could be more comprehensive, especially for advanced use cases
  • No built-in support for SVG gradients

Code Examples

Creating a basic gradient animation:

var granimInstance = new Granim({
    element: '#canvas-basic',
    direction: 'left-right',
    isPausedWhenNotInView: true,
    states : {
        "default-state": {
            gradients: [
                ['#ff9966', '#ff5e62'],
                ['#00F260', '#0575E6'],
                ['#e1eec3', '#f05053']
            ]
        }
    }
});

Adding interactivity with state changes:

var granimInstance = new Granim({
    element: '#canvas-interactive',
    name: 'interactive-gradient',
    elToSetClassOn: '.canvas-interactive-wrapper',
    direction: 'diagonal',
    states : {
        "default-state": {
            gradients: [
                ['#B3FFAB', '#12FFF7'],
                ['#ADD100', '#7B920A'],
                ['#1A2980', '#26D0CE']
            ],
            transitionSpeed: 10000
        },
        "violet-state": {
            gradients: [
                ['#9D50BB', '#6E48AA'],
                ['#4776E6', '#8E54E9']
            ],
            transitionSpeed: 2000
        }
    }
});

// Trigger state change on button click
$('.canvas-interactive-wrapper').on('click', function(event) {
    granimInstance.changeState('violet-state');
});

Creating a radial gradient:

var granimInstance = new Granim({
    element: '#canvas-radial',
    direction: 'radial',
    isPausedWhenNotInView: true,
    states : {
        "default-state": {
            gradients: [
                ['#fad0c4', '#ffd1ff'],
                ['#ffecd2', '#fcb69f']
            ]
        }
    }
});

Getting Started

  1. Include the Granim.js library in your HTML file:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/granim/2.0.0/granim.min.js"></script>
    
  2. Create a canvas element in your HTML:

    <canvas id="granim-canvas"></canvas>
    
  3. Initialize Granim in your JavaScript:

    var granimInstance = new Granim({
        element: '#granim-canvas',
        direction: 'top-bottom',
        states : {
            "default-state": {
                gradients: [
                    ['#834d9b', '#d04ed6'],
                    ['#1CD8D2', '#93EDC7']
                ]
            }
        }
    });
    
  4. Style your canvas as needed:

    #granim-canvas {
        width: 100%;
        height: 100vh;
    }
    

Competitor Comparisons

106,888

JavaScript 3D Library.

Pros of three.js

  • Comprehensive 3D graphics library with extensive features
  • Large, active community and extensive documentation
  • Supports a wide range of 3D rendering techniques and effects

Cons of three.js

  • Steeper learning curve due to its complexity
  • Larger file size, which may impact page load times
  • Overkill for simple gradient animations

Code Comparison

three.js (3D scene setup):

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

granim.js (gradient animation setup):

var granimInstance = new Granim({
    element: '#canvas-basic',
    direction: 'left-right',
    isPausedWhenNotInView: true,
    states : {
        "default-state": {
            gradients: [
                ['#AA076B', '#61045F'],
                ['#02AAB0', '#00CDAC'],
                ['#DA22FF', '#9733EE']
            ]
        }
    }
});

three.js is a powerful 3D graphics library offering extensive capabilities for complex 3D scenes and animations. It's well-suited for advanced projects requiring detailed 3D rendering. granim.js, on the other hand, is a lightweight library specifically designed for creating fluid and interactive gradient animations. It's easier to use for simple gradient effects but lacks the 3D capabilities of three.js.

22,842

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —

Pros of p5.js

  • More comprehensive and versatile for creative coding and visual arts
  • Larger community and ecosystem with extensive documentation and examples
  • Supports a wide range of interactive and multimedia projects

Cons of p5.js

  • Steeper learning curve for beginners compared to Granim.js
  • Larger file size and potentially higher resource usage
  • May be overkill for simple gradient animations

Code Comparison

p5.js:

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  ellipse(mouseX, mouseY, 50, 50);
}

Granim.js:

var granimInstance = new Granim({
  element: '#canvas-basic',
  direction: 'left-right',
  isPausedWhenNotInView: true,
  states : {
    "default-state": {
      gradients: [
        ['#ff9966', '#ff5e62'],
        ['#00F260', '#0575E6'],
        ['#e1eec3', '#f05053']
      ]
    }
  }
});

While p5.js offers a more comprehensive toolkit for creative coding, Granim.js provides a simpler and more focused solution for gradient animations. p5.js is better suited for complex interactive projects, while Granim.js excels in creating smooth, customizable gradients with minimal setup.

45,474

The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.

Pros of PixiJS

  • More comprehensive and feature-rich, offering a complete 2D rendering engine
  • Supports complex animations, sprites, and interactive graphics
  • Larger community and ecosystem with extensive documentation and plugins

Cons of PixiJS

  • Steeper learning curve due to its extensive feature set
  • Larger file size, which may impact load times for simpler projects
  • Potentially overkill for basic gradient animations

Code Comparison

Granim.js (creating a gradient animation):

var granimInstance = new Granim({
    element: '#canvas-basic',
    direction: 'left-right',
    isPausedWhenNotInView: true,
    states : {
        "default-state": {
            gradients: [
                ['#AA076B', '#61045F'],
                ['#02AAB0', '#00CDAC'],
                ['#DA22FF', '#9733EE']
            ]
        }
    }
});

PixiJS (creating a simple gradient):

const app = new PIXI.Application();
document.body.appendChild(app.view);

const geometry = new PIXI.Geometry()
    .addAttribute('aVertexPosition', [-100, -100, 100, -100, 100, 100, -100, 100], 2)
    .addAttribute('aUvs', [0, 0, 1, 0, 1, 1, 0, 1], 2)
    .addIndex([0, 1, 2, 0, 2, 3]);

const shader = PIXI.Shader.from(`
    varying vec2 vUvs;
    void main() {
        vUvs = aUvs;
        gl_Position = vec4(aVertexPosition, 0.0, 1.0);
    }`,
    `varying vec2 vUvs;
    void main() {
        gl_FragColor = vec4(vUvs.x, vUvs.y, 0.0, 1.0);
    }
`);

const quad = new PIXI.Mesh(geometry, shader);
app.stage.addChild(quad);
8,165

The Easel Javascript library provides a full, hierarchical display list, a core interaction model, and helper classes to make working with the HTML5 Canvas element much easier.

Pros of EaselJS

  • More comprehensive and feature-rich, offering a full suite of tools for creating interactive graphics and animations
  • Better suited for complex projects and game development
  • Larger community and ecosystem, with more resources and support available

Cons of EaselJS

  • Steeper learning curve due to its extensive feature set
  • Larger file size, which may impact load times and performance for simpler projects
  • More complex setup and configuration compared to Granim.js

Code Comparison

EaselJS:

var stage = new createjs.Stage("canvas");
var circle = new createjs.Shape();
circle.graphics.beginFill("red").drawCircle(0, 0, 50);
circle.x = 100;
circle.y = 100;
stage.addChild(circle);

Granim.js:

var granimInstance = new Granim({
    element: '#canvas',
    direction: 'left-right',
    isPausedWhenNotInView: true,
    states : {
        "default-state": {
            gradients: [
                ['#834D9B', '#D04ED6'],
                ['#1CD8D2', '#93EDC7']
            ]
        }
    }
});

This comparison highlights the differences in complexity and focus between EaselJS and Granim.js. EaselJS provides more control and flexibility for creating complex graphics and animations, while Granim.js offers a simpler approach specifically for gradient animations.

30,275

Javascript Canvas Library, SVG-to-Canvas (& canvas-to-SVG) Parser

Pros of Fabric.js

  • More comprehensive and feature-rich canvas library
  • Supports complex object manipulation and interactive elements
  • Extensive documentation and large community support

Cons of Fabric.js

  • Larger file size and potentially heavier performance impact
  • Steeper learning curve due to its extensive feature set

Code Comparison

Granim.js (creating a gradient animation):

var granimInstance = new Granim({
    element: '#canvas-basic',
    direction: 'left-right',
    isPausedWhenNotInView: true,
    states : {
        "default-state": {
            gradients: [
                ['#AA076B', '#61045F'],
                ['#02AAB0', '#00CDAC'],
                ['#DA22FF', '#9733EE']
            ]
        }
    }
});

Fabric.js (creating and manipulating objects on canvas):

var canvas = new fabric.Canvas('canvas');
var rect = new fabric.Rect({
  left: 100,
  top: 100,
  fill: 'red',
  width: 20,
  height: 20
});
canvas.add(rect);

While Granim.js focuses specifically on gradient animations, Fabric.js provides a more comprehensive set of tools for canvas manipulation and object creation. Fabric.js offers greater flexibility but may be overkill for simple gradient effects.

14,796

The Swiss Army Knife of Vector Graphics Scripting – Scriptographer ported to JavaScript and the browser, using HTML5 Canvas. Created by @lehni & @puckey

Pros of Paper.js

  • More comprehensive and feature-rich library for vector graphics and animation
  • Supports both canvas and SVG rendering
  • Offers a powerful scripting environment with object-oriented programming

Cons of Paper.js

  • Steeper learning curve due to its extensive API and features
  • Larger file size, which may impact load times for simpler projects
  • May be overkill for basic gradient animations or effects

Code Comparison

Paper.js example:

paper.install(window);
window.onload = function() {
    paper.setup('myCanvas');
    var path = new Path.Circle({
        center: view.center,
        radius: 30,
        fillColor: 'red'
    });
}

Granim.js example:

var granimInstance = new Granim({
    element: '#canvas-basic',
    direction: 'left-right',
    isPausedWhenNotInView: true,
    states : {
        "default-state": {
            gradients: [
                ['#AA076B', '#61045F'],
                ['#02AAB0', '#00CDAC']
            ]
        }
    }
});

Paper.js is better suited for complex vector graphics and interactive animations, while Granim.js excels at creating simple, performant gradient animations with minimal setup.

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

Granim.js Build Status codecov gitter

Create fluid and interactive gradient animations with this small javascript library.

See the demo site.

Install

From NPM

  • Run npm install granim --save

From Bower

  • Run bower install granim

Static

How to use

<!-- Create a <canvas> element -->
<canvas id="granim-canvas"></canvas>

<!-- Call the script -->
<script src="granim.min.js"></script>

<!-- Create a Granim instance -->
<script>
var granimInstance = new Granim({
   element: '#granim-canvas',
   name: 'granim',
   opacity: [1, 1],
   states : {
       "default-state": {
           gradients: [
               ['#834D9B', '#D04ED6'],
               ['#1CD8D2', '#93EDC7']
           ]
       }
   }
});
</script>

NPM DownloadsLast 30 Days