mirror of
https://github.com/konvajs/konva.git
synced 2025-04-05 20:48:28 +08:00
updated Threshold filter and test
This commit is contained in:
parent
4074eef3aa
commit
bbfbadfee2
@ -4,58 +4,31 @@
|
|||||||
* Threshold Filter. Pushes any value above the mid point to
|
* Threshold Filter. Pushes any value above the mid point to
|
||||||
* the max and any value below the mid point to the min.
|
* the max and any value below the mid point to the min.
|
||||||
* This affects the alpha channel.
|
* This affects the alpha channel.
|
||||||
* Performs w*h pixel reads and w*h pixel writes.
|
|
||||||
* @function
|
* @function
|
||||||
* @author ippo615
|
|
||||||
* @memberof Kinetic.Filters
|
* @memberof Kinetic.Filters
|
||||||
* @param {ImageData} src, the source image data (what will be transformed)
|
* @param {Object} imageData
|
||||||
* @param {ImageData} dst, the destination image data (where it will be saved)
|
* @author ippo615
|
||||||
* @param {Object} opt
|
|
||||||
* @param {Number} [opt.thresholdLevel=128] the value which divides
|
|
||||||
* the channel value into 2 groups (between 0 and 255)
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Threshold = function (src, dst, opt) {
|
Kinetic.Filters.Threshold = function (imageData) {
|
||||||
var level = 128;
|
var level = this.threshold() * 255,
|
||||||
if( opt.hasOwnProperty ){
|
data = imageData.data,
|
||||||
level = opt.thresholdLevel;
|
len = data.length,
|
||||||
}
|
i;
|
||||||
var srcPixels = src.data,
|
|
||||||
dstPixels = dst.data,
|
for (i = 0; i < len; i += 1) {
|
||||||
nPixels = srcPixels.length,
|
data[i] = data[i] < level ? 0 : 255;
|
||||||
i;
|
|
||||||
for (i = 0; i < nPixels; i += 1) {
|
|
||||||
if (srcPixels[i] < level) {
|
|
||||||
dstPixels[i] = 0;
|
|
||||||
} else {
|
|
||||||
dstPixels[i] = 255;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Kinetic.Factory.addFilterGetterSetter(Kinetic.Node, 'threshold', 128);
|
Kinetic.Factory.addFilterGetterSetter(Kinetic.Node, 'threshold', 0.5);
|
||||||
|
|
||||||
Kinetic.Filters.Threshold = function(src,dst,opt){
|
/**
|
||||||
if( this === Kinetic.Filters ){
|
* get/set threshold. Value between 0 and 1
|
||||||
Threshold(src, dst||src, opt );
|
* @name threshold
|
||||||
}else{
|
* @method
|
||||||
Threshold.call(this, src, dst||src, opt || {
|
* @memberof Kinetic.Image.prototype
|
||||||
thresholdLevel: this.getThresholdLevel()
|
* @param {Number} threshold
|
||||||
});
|
* @returns {Number}
|
||||||
}
|
*/
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get threshold level. Returns the level which divides the color channel (0-255).
|
|
||||||
* @name getThresholdLevel
|
|
||||||
* @method
|
|
||||||
* @memberof Kinetic.Image.prototype
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* set threshold level. Sets the level which divides the color channel (0-255).
|
|
||||||
* @name setThresholdLevel
|
|
||||||
* @method
|
|
||||||
* @memberof Kinetic.Image.prototype
|
|
||||||
*/
|
|
||||||
})();
|
})();
|
@ -92,8 +92,8 @@
|
|||||||
<!--<script src="unit/filters/Polar-test.js"></script>-->
|
<!--<script src="unit/filters/Polar-test.js"></script>-->
|
||||||
<script src="unit/filters/Pixelate-test.js"></script>
|
<script src="unit/filters/Pixelate-test.js"></script>
|
||||||
<script src="unit/filters/Noise-test.js"></script>
|
<script src="unit/filters/Noise-test.js"></script>
|
||||||
<!--<script src="unit/filters/Threshold-test.js"></script>
|
<script src="unit/filters/Threshold-test.js"></script>
|
||||||
<script src="unit/filters/Levels-test.js"></script>
|
<!--<script src="unit/filters/Levels-test.js"></script>
|
||||||
<script src="unit/filters/Flip-test.js"></script>
|
<script src="unit/filters/Flip-test.js"></script>
|
||||||
<script src="unit/filters/Mirror-test.js"></script>
|
<script src="unit/filters/Mirror-test.js"></script>
|
||||||
<script src="unit/filters/Sepia-test.js"></script>
|
<script src="unit/filters/Sepia-test.js"></script>
|
||||||
|
@ -1,68 +1,4 @@
|
|||||||
suite('Threshold', function () {
|
suite('Threshold', function () {
|
||||||
// ======================================================
|
|
||||||
test('threshold', function (done) {
|
|
||||||
var stage = addStage();
|
|
||||||
|
|
||||||
var shapesLayer = new Kinetic.Layer();
|
|
||||||
|
|
||||||
// The important line!
|
|
||||||
shapesLayer.on('draw', function () {
|
|
||||||
var imageData = this.getContext().getImageData(0,0,this.getCanvas().width/2,this.getCanvas().height);
|
|
||||||
var scratchData = this.getContext().createImageData(imageData); // only size copied
|
|
||||||
Kinetic.Filters.Threshold(imageData,scratchData,{thresholdLevel:64});
|
|
||||||
this.getContext().putImageData(scratchData,0,0);
|
|
||||||
});
|
|
||||||
|
|
||||||
var triangle = new Kinetic.RegularPolygon({
|
|
||||||
x: stage.getWidth() / 4,
|
|
||||||
y: stage.getHeight() / 2,
|
|
||||||
sides: 3,
|
|
||||||
radius: 80,
|
|
||||||
fillRadialGradientStartPoint: 0,
|
|
||||||
fillRadialGradientStartRadius: 0,
|
|
||||||
fillRadialGradientEndPoint: 0,
|
|
||||||
fillRadialGradientEndRadius: 70,
|
|
||||||
fillRadialGradientColorStops: [0, '#881111', 0.5, '#888811', 1, '#000088'],
|
|
||||||
stroke: 'black',
|
|
||||||
strokeWidth: 4,
|
|
||||||
draggable: true
|
|
||||||
});
|
|
||||||
|
|
||||||
var circle = new Kinetic.Circle({
|
|
||||||
x: 3 * stage.getWidth() / 4,
|
|
||||||
y: stage.getHeight() / 2,
|
|
||||||
radius: 70,
|
|
||||||
fill: '#880000',
|
|
||||||
stroke: 'black',
|
|
||||||
strokeWidth: 4,
|
|
||||||
draggable: true,
|
|
||||||
id: 'myCircle'
|
|
||||||
});
|
|
||||||
|
|
||||||
for( var i=0; i<10; i+=1 ){
|
|
||||||
for( var j=0; j<10; j+=1 ){
|
|
||||||
var rect = new Kinetic.Rect({
|
|
||||||
x: i/10*stage.getWidth(),
|
|
||||||
y: j/10*stage.getHeight(),
|
|
||||||
width: stage.getWidth()/10,
|
|
||||||
height: stage.getHeight()/10,
|
|
||||||
fill: (i+j)%2===0?'#FF0000':'#FFFF00',
|
|
||||||
stroke: 'black',
|
|
||||||
strokeWidth: 4,
|
|
||||||
draggable: true
|
|
||||||
});
|
|
||||||
shapesLayer.add(rect);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
shapesLayer.add(circle);
|
|
||||||
shapesLayer.add(triangle);
|
|
||||||
|
|
||||||
stage.add(shapesLayer);
|
|
||||||
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
test('image tween', function(done) {
|
test('image tween', function(done) {
|
||||||
var stage = addStage();
|
var stage = addStage();
|
||||||
@ -81,14 +17,15 @@ suite('Threshold', function () {
|
|||||||
layer.add(darth);
|
layer.add(darth);
|
||||||
stage.add(layer);
|
stage.add(layer);
|
||||||
|
|
||||||
darth.setFilter(Kinetic.Filters.Threshold);
|
darth.cache();
|
||||||
darth.setThresholdLevel(255);
|
darth.filters([Kinetic.Filters.Threshold]);
|
||||||
|
darth.threshold(1);
|
||||||
layer.draw();
|
layer.draw();
|
||||||
|
|
||||||
var tween = new Kinetic.Tween({
|
var tween = new Kinetic.Tween({
|
||||||
node: darth,
|
node: darth,
|
||||||
duration: 5.0,
|
duration: 5.0,
|
||||||
thresholdLevel: 0,
|
threshold: 0,
|
||||||
easing: Kinetic.Easings.EaseInOut
|
easing: Kinetic.Easings.EaseInOut
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user