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

gl-image

v0.0.15

Published

image filter utils based on webgl

Downloads

3

Readme

gl-image

gl-image is a util library based on webgl for image filter functions.
This project is inspired by @evanw.

Here is the main difference between glfx.js and gl-image:
glfx.js just support one shader program at one time.
But in many use case, we need to do multiple filters to one image, and every operation can be redoable. So gl-image do something more to support multiple filters (multiple shader programs inside at once).

And it is really simple for use.

demo1

install

npm install --S gl-image
import GLImage from 'gl-image';
const glImage = new GLImage();

glImage.loadImageSrc(src).then(() => {
  // do filter for image
  glImage.applyFilter('brightness', 0.3);
  glImage.applyFilter('hue', -0.3);
});

filters supported

| filter type name | default value | range | | :--------- | :------- | :---------- | | brightness | 0 | [-1, 1] | | contrast | 0 | [-1, 1] | | hue | 0 | [-1, 1] | | saturation | 0 | [-1, 1] | | sepia_amount | 0 | [0, 1] | | vibrance_amount | 0 | [-1, 1] | | vignette_amount | 0 | [0, 1] | | vignette_size | 0 | [0, 1] | | noise_amount | 0 | [0, 1] | | pixelate_block_size | 0 | [0, 20] |

default value in the above table means no filter effect.

note:
  1. please make sure the image source url is cross-origin supported, otherwise browser will block reading pixels from the image. gl-image will failed to do anything.
  2. please make sure your browser supports webgl.
  3. this npm package cannot run in nodejs environment.

API

import GLImage from 'gl-image';
const glImage = new GLImage();

In most cases, one GLImage instance is enough for usage, you don't need create a new one to handle another image.

1. load source image from image/canvas/url

  • async loadFromElement(source: HTMLImageElement | HTMLCanvasElement)
  • async loadImageSrc(url: string)

2. do filter action

  • applyFilter(filterName, filterValue)
  • applyFilters({name1: value1, ...})
    Please refer to the above table to find the available filter name and valid value range.

3. get the output

For performance consideration, it is using preserveDrawingBuffer: false mode inside. So that you would lose drawing data immediately in another event loop. So it is necessary to have another 2D canvas inside to store the drawing result.
The output is stored in another 2D canvas for outside usage.

  • getCanvas(): get the result canvas element which store the result image;
  • getImageData(): get the ImageData from result canvas element;
  • toDataULR(type?, quality?): get the data url from result canvas element;

You can use the result canvas in any way you like.

usage

use case 1. simple use

import GLImage from 'gl-image';

let glImage = new GLImage();

glImage.loadImageSrc(src).then(() => {

  // do single filter for image
  glImage.applyFilter('brightness', 0.3);
  glImage.applyFilter('hue', -0.3);

  // do batch filter for image
  glImage.applyFilters({
    'brightness': 0.3,
    'saturation': -0.7
  });

  // you can the base64 output data.
  const base64 = glImage.getDataURL();

});

use case 2: batch process images

import GLImage from 'gl-image';

const glImage = new GLImage();

const imageSrcList = ['xxx.png', 'yyy.png', ...];

async function processSingle(imageSrc) {
  await glImage.loadImageSrc(imageSrc);
  glImage.applyFilters({
    'brightness': 0.3,
    'saturation': -0.7
  });
  return glImage.getCanvas();
}

async function batchProcess(imageSrcList) {
  const result = [];
  for (let i = 0; i < imageSrcList.length; i++) {
    const res = await processSingle(imageSrcList[i]);
    result.push(res);
  }
  return result;
}

batchProcess(imageSrcList).then((result) => {
  console.log(result);
})

Notice:

getDataURL() is retired.
Please call getCanvas() instead. You can get the result 2D canvas element which store the result, then you can do anything on this canvas in any way you like.

fromLoadedImage() is retired. please use loadFromElement instead.