API

options

All the global options available.

Property Default Type Description
element (required) / String A CSS selector for any html element (e.g. '#grader') that will be used for the gradient animation.
name false String This property will be sent back in callback functions. For example in onLightChange.
gradientType 'linear' String The type of gradient:
  • 'linear'
  • 'radial'
gradientOrigin 'circle closest-side' String The orientation of the gradient, you select any as you can in css. For example:
  • 'to top'
  • 'to bottom right'
isPausedWhenNotInView false Boolean Does the animation stop when it's not in window view?
(Despite this parameter, the animation always pause when changing tab).
opacity (required) / Array of Int You can adapt the opacity of each color of the gradient.
if you have two colors [1, .5], or three [1, .5, 1]...

options.states

All the options available to customize the states and the different gradients.
Create a state with the name you want then add these parameters.
By default, the name of your first state is 'default-state', you can change it with options.defaultStateName.

Property Default Type Description
gradients (required) / Array of Arrays The different gradients with the different colors e.g. [ ['#FFF', '#000'], ['#000', '#FFF'] ].
or [ [{color: '#000', opacity: 0.3}, '#fff'], ['#000', {color: '#89253e', opacity: 0.85}] ].
The array can contain only one gradient.
transitionSpeed 5000 Int Transition duration between each gradient.
loop true Boolean When the animation has arrived to the last gradient, does it repeat?

Callbacks

You can set callbacks.

Property Default Type Description
onStart false Function Triggered when the animation start.
onGradientChange false Function Triggered when a gradient change and loop.
onEnd false Function Triggered when the animation end.

Emitted events

Granim will emit events you can listen to at key moments.

// You can listen to these events:
// - granim:start
// - granim:end
// - granim:loop
// - granim:gradientChange

// js var canvasElement = document.querySelector('#granim-canvas'); canvasElement.addEventListener('granim:start', function(event) { console.log(event); });
// with jQuery $('#granim-canvas').on('granim:start', function(event) { console.log(event); })

Methods

Methods to control the gradients.

// Change state by putting its name in the argument
granimInstance.changeState('state-name');

// Play the animation granimInstance.play();
// Pause the animation granimInstance.pause();
// Stop the animation and clear the gradient granimInstance.clear();

Basic configuration

Basic configuration with two states and callbacks set.

var granimInstance = new Granim({
    element: '',
    name: 'granim',
    elToSetClassOn: 'body',
    direction: 'diagonal',
    isPausedWhenNotInView: false,
    opacity: [1, 1],
    stateTransitionSpeed: 1000,
    states : {
        "default-state": {
            gradients: [
                ['#834d9b', '#d04ed6'],
                ['#1CD8D2', '#93EDC7']
            ],
            transitionSpeed: 5000,
            loop: true
        },
        "dark-state": {
            gradients: [
                ['#757F9A', '#D7DDE8'],
                ['#5C258D', '#4389A2']
            ],
            transitionSpeed: 5000,
            loop: true
        }
    },
    onStart: function() {
        console.log('Granim: onStart');
    },
    onGradientChange: function(colorDetails) {
        console.log('Granim: onGradientChange, details: ');
        console.log(colorDetails);
    },
    onEnd: function() {
        console.log('Granim: onEnd');
    }
);

You can use more than 2 colors for the gradients, but cannot have different length of colors in the different states. you will also have to adapt the opacity.

var granimInstance = new Granim({
    element: '',
    name: 'granim',
    elToSetClassOn: 'body',
    direction: 'diagonal',
    isPausedWhenNotInView: false,
    opacity: [1, 1, .5, 1],
    stateTransitionSpeed: 1000,
    states : {
        "default-state": {
            gradients: [
                ['#834d9b', '#d04ed6', '#1CD8D2', '#93EDC7'],
                ['#1CD8D2', '#93EDC7', '#757F9A', '#4389A2']
            ],
            transitionSpeed: 5000,
            loop: true
        },
        "dark-state": {
            gradients: [
                ['#757F9A', '#D7DDE8', '#1CD8D2', '#93EDC7'],
                ['#5C258D', '#4389A2', '#1CD8D2', '#93EDC7']
            ],
            transitionSpeed: 5000,
            loop: true
        }
    }
);