mirror of
https://github.com/konvajs/konva.git
synced 2026-01-18 11:41:20 +08:00
I added the experimental folder to show some work on filters that
can be applied to an entire layer. Multiple filters can be applied
to a layer (in any order, multiple times). To hook into the layer I
use:
layer.on('draw', filterFunc);
Eventually, I would like to move that to `layer.filterFunc` and
automatically apply it after the draw. `filterFunc` looks like:
function filterFunc(){
// Get pixel data and create a temporary pixel buffer for working
var imageData = this.getContext().getImageData(0,0,this.getCanvas().width,this.getCanvas().height);
var scratchData = this.getContext().createImageData(imageData);
// Apply all filters here
ColorStretch(imageData,scratchData,{});
// Copy the pixel data back
this.getContext().putImageData(scratchData,0,0);
}
`ColorStretch` is an example of a filter. It takes 3 arguments: the
original image data, image data to write the result to, and an options
object.
27 lines
1.0 KiB
JavaScript
27 lines
1.0 KiB
JavaScript
Noise = (function () {
|
|
var Noise = function (src, dst, opt) {
|
|
var amount = opt.amount || 32,
|
|
affectAlpha = opt.affectAlpha || 0;
|
|
var srcPixels = src.data,
|
|
dstPixels = dst.data,
|
|
nPixels = srcPixels.length,
|
|
half = amount / 2,
|
|
i;
|
|
if (affectAlpha) {
|
|
for (i = 0; i < nPixels; i += 4) {
|
|
dstPixels[i + 0] = srcPixels[i + 0] + half - 2 * half * Math.random();
|
|
dstPixels[i + 1] = srcPixels[i + 1] + half - 2 * half * Math.random();
|
|
dstPixels[i + 2] = srcPixels[i + 2] + half - 2 * half * Math.random();
|
|
dstPixels[i + 3] = srcPixels[i + 3] + half - 2 * half * Math.random();
|
|
}
|
|
} else {
|
|
for (i = 0; i < nPixels; i += 4) {
|
|
dstPixels[i + 0] = srcPixels[i + 0] + half - 2 * half * Math.random();
|
|
dstPixels[i + 1] = srcPixels[i + 1] + half - 2 * half * Math.random();
|
|
dstPixels[i + 2] = srcPixels[i + 2] + half - 2 * half * Math.random();
|
|
dstPixels[i + 3] = srcPixels[i + 3];
|
|
}
|
|
}
|
|
};
|
|
return Noise;
|
|
})(); |