Git Product home page Git Product logo

webglimagefilter's Introduction

WebGLImageFilter

Construct a chain of image filters and apply them to an Image or Canvas element. All filters are executed by WebGL Shaders which makes them pretty fast.

Demo: phoboslab.org/log/2013/11/webgl-image-filter

Please also have a look at the excellent glfx.js by @evanw.

Usage

// Synopsis: create the filter object, add filters to it and apply
// it to an image

// Example:
try {
	var filter = new WebGLImageFilter();
}
catch( err ) {
	// Handle browsers that don't support WebGL
}

filter.addFilter('hue', 180);
filter.addFilter('negative');
filter.addFilter('blur', 7);

// inputImage may be an Image, or even an HTML Canvas!
var filteredImage = filter.apply(inputImage);

// The 'filteredImage' is a canvas element. You can draw it on a 2D canvas
// or just add it to your DOM

// Use .reset() to clear the current filter chain. This is faster than creating a new
// WebGLImageFilter instance
filter.reset();

Using an Existing Canvas element

Internally, WebGLImageFilter creates one canvas element, which is what the output filtered result is written to. If you have an existing canvas element that you want to render to, you can configure WebGLImageFilter to use it:

try {
	// in this case, filteredImage is an existing html canvas
	var filter = new WebGLImageFilter({ canvas: filteredImage });
}
catch( err ) { }

// .. filters setup here

filter.apply(inputImage); 

// at this point, filteredImage has already been updated

Filters

Main filters

  • colorMatrix( matrix ) apply a the 5x5 color matrix (Array[20]), similar to Flash's ColorMatrixFilter
  • convolution( matrix ) apply a 3x3 convolution matrix (Array[9])
  • blur( radius ) blur with radius in pixels

Presets using the main filters

  • brightness( amount ) change brightness. 1 increases the it two fold, -1 halves it
  • saturation( amount ) change saturation. 1 increases the it two fold, -1 halves it
  • contrast( amount ) change contrast. 1 increases the it two fold, -1 halves it
  • negative() invert colors
  • hue( rotation ) rotate the hue, values are 0-360
  • desaturate() desaturate the image by all channels equally
  • desaturateLuminance() desaturate the image taking the natural luminace of each channel into acocunt
  • sepia() sepia colors
  • brownie() vintage colors
  • vintagePinhole() vintage colors
  • kodachrome() vintage colors
  • technicolor() vintage colors
  • detectEdges() detect edges
  • sobelX() detect edges using a horizontal sobel operator
  • sobelY() detect edges using a vertical sobel operator
  • sharpen( amount ) sharpen
  • emboss( size ) emboss effect with size in pixels
  • polaroid() polaroid camera effect
  • shiftToBGR() shift colors from RGB to BGR
  • pixelate(amount) pixelate

License

MIT

webglimagefilter's People

Contributors

phoboslab avatar sgaurav avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webglimagefilter's Issues

Could you supply a feature( not create a new Image Object) for canvas ?

In the common use case, user pass a image, then WebGLImageFilter return a new image object .

Could you supply a feature for canvas that not create a new Image Object ( by a switch)

example:

var filteredImage = filter.apply( offscreen,  createNewImageOrNot );
if createNewImageOrNot===false ,  filteredImage==offscreen

How to get this to work with Webpack?

Trying to use your library in a Webpack environment with ESLint enabled.

I get all kinds of errors like :
Expected an object to be thrown
Expected an assignment or function call and instead saw an expression
Expected literal to be on the right side of ===

Any plans to fix these?

What's different between size (the parameter of blur-method) and radius ?

In all of image-editors (e.g. photoshop ,paint.net , painter...) , there is a parameter named "Radius" in the blur panel.

In this lib, no radius , it has a "size" parameter.
What's different between size and radius?

If no difference , could you change the name from "size" to "radius" ?
I think the radius is common.

any interest in a pixelation filter?

I've implemented a pixelation filter for my in-progress game. Here is a screen shot of the pixelation level being updated in real-time:

pixelation

If this is something we want, it's a pretty small shader.

would you accept a PR for es modules support?

The majority of browsers natively support the new es modules feature, would you be willing/interested to accept a PR for this, or would you prefer to keep WebGLImageFilter as a window global?

Image Overlay ?

Hey, great (and fast!) Library!
I found this as most modern js image processing libraries are quite outdated, yours could be one of the best around if you gave it an update. :)
Also, using .png images as an overlay would be great as a new feature.

how to use custom shaders?

hi how can i use cutom fragment shaders? as far as i can see i can only pass a custom colorMatrix toaddFilter. suppose i want to achieve bloom effect how can i go about it?

[REQUEST] Supports "ploygon area" & circle area

Example:

filter.apply(image,  [  [ v1X, v1Y] ,  [ v2X, v2Y,  [ v3X, v3Y] .... [ vnX, vnY]   ] );
filter.apply(image,  centreX, centreY, radius);

then the filter only affects the "ploygon area" and "circle area" in the image.

Why vertices with 4 coordinates ?

Good morning !

I can't find any documentation on how your vertex array should work or what represent the v and w coordinates :

  // Create the vertex buffer for the two triangles [x, y, v, w] * 6
  var vertices = new Float32Array([
  -1, -1, 0, 1, 1, -1, 1, 1, -1, 1, 0, 0,
  -1, 1, 0, 0, 1, -1, 1, 1, 1, 1, 1, 0
  ]);

For me, vertices only need 2, or 3 coordinates with the depth. I guess you're not specifying any depth with these points, but I can't manage do find what v and w are. Can you give me a hint on this, please ? Thanks (a lot) in advance !

WebGLImageFilter is not compatible with Web Workers

WebGLImageFilter uses DOM methods to create canvas (e.g. document.createElement()) - and DOM is not available inside a web worker.

Should use OffscreenCanvas instead.

Which then leads to second problem, canvas and it's webgl context are initialized upon class creation and only resized upon subsequent access - but OffscreenCanvas cannot be resized, it has to be initialized with fixed size. Which means that initialization code should be modified so canvas is dynamically created (and it's context updated).

Render big images in 'n' chunks to avoid 'out of memory'

On mobile devices, like Firefox OS phones with low memory, it's not possible to apply those effects to larger images (like from 8 MP cameras).

Quote from a stackoverflow answer to the question how to deal with this:

So the way to deal with that is that you subdivide your image into smaller tiles and process each tile one at a time.

There are two ways you can deal with borders if you have filter kernels.

  • You could pass in 9 textures and branch on texture lookup to address the correct one (that might be slow, some GPUs don't support branching and will execute all codepaths). Border textures would just be 2x2 so would have little ram footprint. Note that between textures there would be no interpolation, so don't rely on it, or substitute your own.
  • You could add a half kernel sized border around each tile taking pixels from neighboring tiles in a preprocessing step.

[Source: http://stackoverflow.com/a/15039655/1692735]

Licence and reuse

First of all thanks for the great work. In a project of mine I also need a couple of image filter, so I stumbled over your project. Unlucky my use case doesn't work so good with your API, so I started to work out a new API on a fork of this repo. It comes out, that I rewrote in my fork big parts of the hole library beside the shaders and filters. I thought now about to publish my results as a further image filter library (of course with reference to your work) or as a fork of you. I would prefer to publish it at a new project (of course under a open source licence), because I used other build tools and the API is mainly different from yours. Would this be okay?

Further I did like to know under which licence this project is published? Maybe you can add one.

Thanks again for your work. jacmendt

About variable's name: "size"

In many image-edit-tools , the blur has an agrument named "radius"

I found in WebGLImageFilter, there is a variable named "size" , blurSize.

I want know whether the size equals the radius . or size == 2 * radius ?

if size==radius , could you change the "size" to "radius" ?
because "radius" is more generic .

thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.