Fixed bug #527 - filters and cropping are now friends.
This commit is contained in:
ippo615
2013-09-29 14:00:51 -04:00
2 changed files with 97 additions and 31 deletions

View File

@@ -40,15 +40,13 @@
Kinetic.Shape.call(this, config); Kinetic.Shape.call(this, config);
this.className = IMAGE; this.className = IMAGE;
}, },
drawFunc: function(context) { drawFunc: function(context) {
var width = this.getWidth(), var width = this.getWidth(),
height = this.getHeight(), height = this.getHeight(),
params,
that = this, that = this,
cropX = this.getCropX() || 0, crop,
cropY = this.getCropY() || 0, params,
cropWidth = this.getCropWidth(),
cropHeight = this.getCropHeight(),
image; image;
//TODO: this logic needs to hook int othe new caching system //TODO: this logic needs to hook int othe new caching system
@@ -60,11 +58,26 @@
} }
// NOTE: this.filterCanvas may be set by the above code block // NOTE: this.filterCanvas may be set by the above code block
// In that case, cropping is already applied.
if (this.filterCanvas) { if (this.filterCanvas) {
image = this.filterCanvas._canvas; image = this.filterCanvas._canvas;
params = [image, 0, 0, width, height];
} }
else { else {
image = this.getImage(); image = this.getImage();
if (image) {
crop = this.getCrop();
if (crop) {
crop.x = crop.x || 0;
crop.y = crop.y || 0;
crop.width = crop.width || image.width - crop.x;
crop.height = crop.height || image.height - crop.y;
params = [image, crop.x, crop.y, crop.width, crop.height, 0, 0, width, height];
} else {
params = [image, 0, 0, width, height];
}
}
} }
context.beginPath(); context.beginPath();
@@ -72,16 +85,7 @@
context.closePath(); context.closePath();
context.fillStrokeShape(this); context.fillStrokeShape(this);
if(image) { if (image) {
// if cropping
if(cropWidth && cropHeight) {
params = [image, cropX, cropY, cropWidth, cropHeight, 0, 0, width, height];
}
// no cropping
else {
params = [image, 0, 0, width, height];
}
context.drawImage.apply(context, params); context.drawImage.apply(context, params);
} }
}, },
@@ -110,25 +114,37 @@
width = this.getWidth(), width = this.getWidth(),
height = this.getHeight(), height = this.getHeight(),
filter = this.getFilter(), filter = this.getFilter(),
crop = this.getCrop() || {},
filterCanvas, context, imageData; filterCanvas, context, imageData;
if (this.filterCanvas){ // Determine the region we are cropping
crop.x = crop.x || 0;
crop.y = crop.y || 0;
crop.width = crop.width || image.width - crop.x;
crop.height = crop.height || image.height - crop.y;
// Make a filterCanvas the same size as the cropped image
if (this.filterCanvas &&
this.filterCanvas.getWidth() === crop.width &&
this.filterCanvas.getHeight() === crop.height) {
filterCanvas = this.filterCanvas; filterCanvas = this.filterCanvas;
filterCanvas.getContext().clear(); filterCanvas.getContext().clear();
} }
else { else {
filterCanvas = this.filterCanvas = new Kinetic.SceneCanvas({ filterCanvas = this.filterCanvas = new Kinetic.SceneCanvas({
width: width, width: crop.width,
height: height, height: crop.height,
pixelRatio: 1 pixelRatio: 1,
}); });
} }
context = filterCanvas.getContext(); context = filterCanvas.getContext();
try { try {
context.drawImage(image, 0, 0, filterCanvas.getWidth(), filterCanvas.getHeight()); // Crop the image onto the filterCanvas then apply
imageData = context.getImageData(0, 0, filterCanvas.getWidth(), filterCanvas.getHeight()); // the filter to the filterCanvas
context.drawImage(image, crop.x, crop.y, crop.width, crop.height, 0,0,crop.width, crop.height);
imageData = context.getImageData(0, 0, crop.width, crop.height);
filter.call(this, imageData); filter.call(this, imageData);
context.putImageData(imageData, 0, 0); context.putImageData(imageData, 0, 0);
} }

View File

@@ -1,6 +1,6 @@
suite('Blur', function() { suite('Blur', function() {
// ====================================================== // ======================================================
test('basic blur', function() { test('basic blur', function(done) {
var stage = addStage(); var stage = addStage();
var imageObj = new Image(); var imageObj = new Image();
@@ -21,12 +21,14 @@ suite('Blur', function() {
darth.setFilterRadius(10); darth.setFilterRadius(10);
layer.draw(); layer.draw();
done();
}; };
imageObj.src = 'assets/darth-vader.jpg'; imageObj.src = 'assets/darth-vader.jpg';
}); });
// ====================================================== // ======================================================
test('tween blur', function() { test('tween blur', function(done) {
var stage = addStage(); var stage = addStage();
var imageObj = new Image(); var imageObj = new Image();
@@ -49,7 +51,7 @@ suite('Blur', function() {
var tween = new Kinetic.Tween({ var tween = new Kinetic.Tween({
node: darth, node: darth,
duration: 5.0, duration: 2.0,
filterRadius: 0, filterRadius: 0,
easing: Kinetic.Easings.EaseInOut easing: Kinetic.Easings.EaseInOut
}); });
@@ -62,12 +64,14 @@ suite('Blur', function() {
tween.reverse(); tween.reverse();
}); });
done();
}; };
imageObj.src = 'assets/darth-vader.jpg'; imageObj.src = 'assets/darth-vader.jpg';
}); });
// ====================================================== // ======================================================
test('crop blur', function() { test('crop blur', function(done) {
var stage = addStage(); var stage = addStage();
var imageObj = new Image(); var imageObj = new Image();
@@ -78,7 +82,7 @@ suite('Blur', function() {
x: 10, x: 10,
y: 10, y: 10,
image: imageObj, image: imageObj,
crop: {x:48, y:48, width:256, height:256}, crop: {x:128, y:48, width:256, height:128},
draggable: true draggable: true
}); });
@@ -89,6 +93,52 @@ suite('Blur', function() {
darth.setFilterRadius(10); darth.setFilterRadius(10);
layer.draw(); layer.draw();
done();
};
imageObj.src = 'assets/darth-vader.jpg';
});
// ======================================================
test('crop tween blur', function(done) {
var stage = addStage();
var imageObj = new Image();
imageObj.onload = function() {
var layer = new Kinetic.Layer();
darth = new Kinetic.Image({
x: 10,
y: 10,
image: imageObj,
crop: {x:128, y:48, width:256, height:128},
draggable: true
});
layer.add(darth);
stage.add(layer);
darth.setFilter(Kinetic.Filters.Blur);
darth.setFilterRadius(100);
layer.draw();
var tween = new Kinetic.Tween({
node: darth,
duration: 2.0,
filterRadius: 0,
easing: Kinetic.Easings.EaseInOut
});
darth.on('mouseover', function() {
tween.play();
});
darth.on('mouseout', function() {
tween.reverse();
});
done();
}; };
imageObj.src = 'assets/darth-vader.jpg'; imageObj.src = 'assets/darth-vader.jpg';
}); });