Skip to content

m00nbyte/letter-glitch

Repository files navigation

Letter Glitch

Description

This package is a customizable letter glitch background effect for web pages. It allows you to create an interactive and dynamic background effect, with customizable options such as letter color, letter amount, letter size, and animation speed.

Demos

demos

Usage

Option 1: Using ES Module

<div class="glitch">
    <!-- content -->
</div>
import createGlitchEffect from 'https://cdn.jsdelivr.net/gh/m00nbyte/letter-glitch@latest/dist/index.es.min.js';

const options = {
    container: '.glitch',
    ...
};

createGlitchEffect(options);

Option 2: Using IIFE

<div class="glitch">
    <!-- content -->
</div>

<script src="https://cdn.jsdelivr.net/gh/m00nbyte/letter-glitch@latest/dist/index.iife.min.js"></script>
const options = {
    container: '.glitch',
    ...
};

window.createGlitchEffect(options);

Default options

const options = {
    containerSelector: null,
    chars: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$&*-_+=/[]{};:<>,0123456789',
    colors: ['#FFFFFF', '#868686', '#373737'],
    fontSize: 16,
    charWidth: 10,
    charHeight: 18,
    updateSpeed: 50,
    blendOver: false,
    backgroundColor: '#101010',
    fullVignetteOpacity: 0.9,
    sideVignettesOpacity: 0.7,
    specialTexts: [],
    specialTextColor: '#f0b100',
    createElements: false
};

Properties

The createGlitchEffect function accepts an options object with the following properties:

container

Type: String

A CSS selector for the container element where the letter glitch will be rendered. This parameter is required and cannot be updated later.

chars

Type: String

The character set used for the glitch effect. You can customize this to use any characters you prefer.

colors

Type: Array<String>

An array of colors for the glitch characters. Characters will randomly cycle through these colors.

fontSize

Type: Number

The font size in pixels for the glitch characters.

charWidth

Type: Number

The width in pixels for each character cell. Used to calculate grid dimensions.

charHeight

Type: Number

The height in pixels for each character cell. Used to calculate grid dimensions.

updateSpeed

Type: Number

The base speed for character updates in milliseconds. Lower values make the animation faster.

blendOver

Type: Boolean

When true, the canvas clears from the bottom-right corner. When false, it clears from the top-left corner.

backgroundColor

Type: String

The background color for the animation. Any valid CSS color value is accepted.

fullVignetteOpacity

Type: Number

Opacity of the side vignette effect. Set to 0 to disable.

specialTexts

Type: Array<String>

An array of strings to display as special highlighted text in the center of the animation. These will appear one at a time with animation effects.

specialTextColor

Type: String

The color for special text animations. Any valid CSS color value is accepted.

createElements

Type: Boolean

When true, all created elements will be removed during cleanup. When false, only event listeners and intervals will be cleared.

Update options

updateOptions

Type: Function

This function allows you to dynamically update the options of the letter glitch after the initial animation has started. This is useful for making real-time changes to the background effect, such as modifying the speed, color, or character appearance without needing to restart the animation.

Just pass an object containing the properties to update. You can provide any combination of the options defined in the initial options. Only the specified properties will be updated, the others will remain unchanged.

const options = {
    container: '.glitch',
    ...
};

const glitchInstance = createGlitchEffect(options);

// update the options anytime
if (glitchInstance) {
    glitchInstance.updateOptions({
        colors: ['#FF0000', '#00FF00', '#0000FF'],
        specialTexts: ['HELLO', 'WORLD'],
        updateSpeed: 100
    });
}

Contribution

Feel free to submit issues or pull requests.

Like my work?

This project needs a ⭐ from you. Don't forget to leave a star.

Buy Me A Coffee

About

Customizable animated letter glitch background effect for web pages

Resources

License

Stars

Watchers

Forks

Contributors