Author: Raphael Marandet
Philter is a JQuery plugin and provides various image processing filters, like blur, sharpen, emboss, edge detection. The plugin works both on images and canvases.
Philter development has started on 2015-12-16. Which means that at the moment, the CSS counterpart of filter effects is still experimental. CSS is visual-only. Whereas Philter physically manipulates pixels.
Don't forget to include jquery as well.
Any version of jquery will do.
<script src="path/to/jquery.js" type="application/javascript"></script>
<script src="path/to/philter.js" type="application/javascript"></script>
Philter works on images and canvases and will simply ignore all other elements. Here are some examples :
This will run a blur filter on all images :
$('img').philter('blur');
This will run a level 100 contrast filter on the first image of the document :
$('img').eq(0).philter('contrast', {level: 100});
Here is a list of filters provided by the Philter plugin. Each filter has its own set of options. There are filter-specific options and general options (common to some or all filters).
- blurs the image
- use radius option to specify the blur radius in pixels
- sharpens the image, like unblur or something...
- if the more option is set to true, the sharpen effect will be greater, showing edges excessively.
- gives some sort of 3D shadow effect to the image, good for creating bump maps
- if the more option is set to true, the emboss effect will be greater.
- applies edge detection filter, the image will be darken, but the edges of shapes will be enlightened
- resizes the image, changing its dimension
- use width and height options to specify new size in pixels.
- example :
$('img').philter('resize', {width:64, height: 64});
- applies a grayscaled palette on the image, removing all colors.
- example :
$('img').philter('grayscale');
- applies a sepia colored layer on the image.
- example :
$('img').philter('sepia');
- changes the image brightness
- use factor option with float value.
- A value of 0.5 means 50% darker
- A value of 2 means +100% lighter
- example :
$('img').philter('brightness', {factor: 0.75}); // all images are a bit darker now
- adds noise to the image
- use level option (default value : 50) to control the amount of noise
- adjusts image contrast
- use level option (default value : 50) to control the amount of contrast
- negative values of level will decrease contrast
- negates the color of all pixels
- get the color of all pixels
- use result option to callback the result.
- example :
$('img').philter('sample', {result: function(image, statistics) { console.log(statistics); }});
- adjust hue, saturation and lightness of an image.
- use hue, saturation and lightness options with values between -1 and 1.
for pixel transformation filters like blur, sharpen, emboss, brightness... you can use the special option channels to specify what color (or alpha) channel will be affected.
There are two notations for the channels option
- the object notation : {red: boolean, green: boolean, blue: boolean, alpha: boolean}
- the string notation : a string containing one or more characters : "r", "g", "b" and "a"
$('img').philter('brightness', {factor: 2, channels: {red: true, green: false, blue: false}});
will only lighten the "red" channel
this is equivalent to :
$('img').philter('brightness', {factor: 2, channels: 'r'});
If you want to run several filters in a row, an important thing to keep in mind is that Philter works in an asynchronous way. That is because any changes to an image is likely to trigger a load event, which is asynchronous.
Philter triggers various custom events for each images being processed. You must attach event handlers on images.
$('img').on('philter.complete', function(oEvent, data) {
console.log('filter', data.filter, 'has been applied in', data.time, 'milliseconds');
});
$('img').on('philter.progress', function(oEvent, data) {
console.log('filter', data.filter, 'is', data.f, '% done');
});
You may declare several filters in a row. They will be queued are run one after another.
$('img')
.philter('blur', {radius: 8})
.philter('negate')
.philter('contrast', {level: 30})
.philter('brightness', {factor: 1.3});
One can simply declare new filters.
First you should create a filter function :
var myFilter = function(sc, oMethods) {
....
});
this function must accept two parameters
- an object called "shadow canvas" which is a structure embedding the target image currently being processed.
- an object containing several usefull methods to process image
Just call philter with the proper parameters :
- a name for the new filter
- the function
This example shows how to create a simple filter. This one fills images with random colored pixels.
$(window).philter('random', function(sc, oMethods) {
oMethods.pixelProcess(sc, function(sc, x, y, p) {
p.r = Math.random() * 256 | 0;
p.g = Math.random() * 256 | 0;
p.b = Math.random() * 256 | 0;
}, oMethods.commit);
});
Another example. This one makes every odd pixel lines disapear.
$(window).philter('scanline', function(sc, oMethods) {
oMethods.pixelProcess(sc, function(sc, x, y, pixel) {
if (y & 1) {
pixel.a = 0;
}
}, oMethods.commit);
});
Another example. This one makes a simple pixelate effect.
$(window).philter('pixelate', function(sc, oMethods) {
scd = oMethods.buildShadowCanvas(sc.image);
scd = oMethods.buildShadowCanvas(sc.image);
scd.context.mozImageSmoothingEnabled = true;
sc.context.mozImageSmoothingEnabled = false;
scd.context.webkitImageSmoothingEnabled = true;
sc.context.webkitImageSmoothingEnabled = false;
var w = sc.options.width || 1;
var h = sc.options.height || 1;
scd.context.drawImage(sc.canvas, 0, 0, sc.width, sc.height, 0, 0, sc.width / w | 0, sc.height / h | 0);
sc.context.drawImage(scd.canvas, 0, 0, sc.width / w | 0, sc.height / h | 0, 0, 0, sc.width, sc.height);
oMethods.commit(sc);
});