npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

css-js-filter

v1.3.1

Published

Set of predefined CSS filters realised via JS along with utils to create your own.

Downloads

4

Readme

css-js-filter

Set of predefined CSS filters realised via JS along with utils to create your own.

Installation

yarn add css-js-filter

or

npm i css-js-filter

Usage

Basic

Each filter class contains realisation for both JS and CSS parts. So, if it is required to modify image directly (means modify its pixels), then JS part of filter should be used. In case when the only 1 required thing from filter is to display what will happen, when we apply it via JS,we could use filter's CSS part.

JavaScript / TypeScript

import {BrightnessFilter} from 'css-js-filter';

// Lets imagine, we have some canvas with image inside.
const canvas = document.getElementById('canvas');

// Get canvas context.
const context = canvas.getContext('2d');

// Get canvas image data which should be modified.
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);

// Decrease image brightness by 70%
const modifiedImageData = BrightnessFilter.processImage({
  image: imageData, 
  value: 30, 
  type: 'rgba',
});

// Put modified image data on canvas.
context.putImageData(modifiedImageData, 0, 0);

// Now we have new image with decreased brightness on canvas!

CSS

In case when there is no real need to modify image directly, it is strongly recommended to use CSS part of filters. Remember, that use of CSS works much faster than JS does.

You could use CSS filters when the only 1 thing is required from filters - to display what happens, when you apply them.

Then, when you should really apply filters to image and download it, you have to switch to JS way.

Let's look how it works.

import {BrightnessFilter, SaturationFilter} from 'css-js-filter';

const canvas = document.getElementById('canvas');

// Get filter CSS representation.
const brightnessFilter = BrightnessFilter.getCSSFilter(30);
const saturationFilter = SaturationFilter.getCSSFilter(23);

// As a result, we are getting here value "brightness(30%) saturate(23%)".
const cssFilter = [brightnessFilter, saturationFilter].join(' ');

// Then, we should use created filter in canvas style (or any other 
// element).
canvas.style.filter = cssFilter;

// ...and, thats all!

Moreover, created CSS filter is compatible with canvas context's filter property. Nevertheless, context's filter is not supported by some browsers.

import {BrightnessFilter, SaturationFilter} from 'css-js-filter';

const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');

// Get filter CSS representation.
const brightnessFilter = BrightnessFilter.getCSSFilter(30);
const saturationFilter = SaturationFilter.getCSSFilter(23);

// As a result, we are getting here value "brightness(30%) saturate(23%)".
const cssFilter = [brightnessFilter, saturationFilter].join(' ');
context.filter = cssFilter;

// Then, we should draw image or some another primitive. Pay attention to the 
// moment, that we are not using putImageData due to this method avoids all
// modifications and replaces pixels directly.
const image = new Image();
image.src = '...';
image.onload = () => context.drawImage(image, 0, 0);

Of course, you could use those filters not only for canvas but the other html elements. It only generates a string, compatible with CSS's filter property.

Advanced

Creating Instagram filter

As an example, we take Instagram's filter "1977". According to this link, we can find out which CSS filters are applied when this filter is used. So, lets try to create JS filter for "1977".

import {
  composeCSSFilters,
  SepiaFilter,
  HueRotationBrowserFilter,
  SaturationFilter,
} from 'css-js-filter';

// Remember instagram filter definition. Its order is important:
// sepia(.5) hue-rotate(-30deg) saturate(1.4)
const Inst1977CSSFilter = composeCSSFilters({
  name: 'Inst1977CSSFilter',
  filters: [
    [SepiaFilter, 50],
    [HueRotationBrowserFilter, -30],
    [SaturationFilter, 140],
  ],
});

Preview

Firstly, clone repository:

git clone [email protected]:wolframdeus/css-js-filter.git

After, when repository is cloned, install all dependencies:

yarn install

Finally, run preview and go to http://localhost:9000 :

yarn dev:browser