1.1.0 • Published 5 years ago

floating-colors v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

JavaScript library for creating floating colors on the background of clipped shapes inside SVG-image.

Adds more colors into your life even if you don't want to.

Show me example!

Usage

Install

npm install floating-colors --save

Import

As module in modern web-project

import FloatingColors from 'floating-colors';

As global variable window.FloatingColors

  <script src="dist/floating-colors.min.js"></script>

Creating clipPath in SVG-image

Place shapes that should have floating colors in the background into clipPath tag.

NB! Be careful making this step - not all tags can be placed into clipPath tag. This is the reason why this step is manual.

Example before:

<svg id="svg" viewBox="0 0 300 300">
  <path d="..." />
  <circle r="150" cx="150" cy="150" />
</svg>

Example after:

<svg id="svg" viewBox="0 0 300 300">
  <defs>
    <clipPath id="clipPath">
      <path d="..." />
      <circle r="150" cx="150" cy="150" />
    </clipPath>
  </defs>
</svg>

Code

import FloatingColors from 'floating-colors';

// Initialization
const options = { /* custom settings */ };
const floatingColors = new FloatingColors(options);

// Attaching to SVG
const svg = document.getElementById('svg');
floatingColors.attachTo(svg);

// Some code ...

// Playing animation when everything is ready
floatingColors.play();

// Some code ...

// Pausing/toggling/stopping animation
floatingColors.pause();
floatingColors.toggle();
floatingColors.stop();

// Some code ...

// Releasing resources in the end
floatingColors.detach();
 

Options

NameValueDescription
playIntrobool = trueEnable/disable intro animation.
playIntroFromstring = 'left'Colors appears from left/top/right/bottom side.
backgroundColorstring = nullBackground of intro animation.
colorsstring[] = [...]Colors that will float during animation.
gridSettingsobject = {...}Setting for grid.
showPalettebool = falseEnable/disable visual debugging of colors in the grid.
containerIdstring = nullId of element in svg that will contains the grid (null === svg).
insertionMethodstring = 'append'append or prepend the grid into the container.
clipPathIdstring = nullId of clipPath that will be used on the grid (null === first in svg).
animationDelaynumber = 0Delay in milliseconds. Negative value means offset in time.
animationDurationnumber = 60 000How much time in milliseconds takes one cycle of animation.

How does it work?

  • On attachTo(svg) the library will create background as a grid of color-gradients.
  • On play() created background will be translated along animation path.

Grid

The image below shows grid that created based on the default gridSettings option.

Color gradients are placed into vertices of regular rectangles that created based on gridSettings:

NameValueDescription
colorsInLinenumber = 5How many colors in one line.
numberOfLinesnumber = 5How many lines in the grid.
colorRadiusnumber = 1.35Radius of circled gradient.
columnWidthnumber = 1Column width (length of side of triangle).

NB! colorRadius and columnWidth uses as number * max(svg.width, svg.height).

Animation

Animation will follow next scheme when playIntroFrom is left:

  • square is SVG-image;
  • dashed line is animation path;
  • left rectangle is background of into animation;
  • right rectangle is grid of colors.

Intro-animation follows straight line. Cycle-animation follows circle.

1.1.0

5 years ago

1.0.2

6 years ago

1.0.0

6 years ago