Skip to content

Commit

Permalink
refactored filtering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ericdrowell committed Aug 19, 2012
1 parent e19dae3 commit e99312e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
20 changes: 10 additions & 10 deletions dist/kinetic-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* http://www.kineticjs.com/
* Copyright 2012, Eric Rowell
* Licensed under the MIT or GPL Version 2 licenses.
* Date: Aug 18 2012
* Date: Aug 19 2012
*
* Copyright (C) 2011 - 2012 by Eric Rowell
*
Expand Down Expand Up @@ -181,8 +181,8 @@ Kinetic.Transition.prototype = {
}
};

Kinetic.Filters.Grayscale = function() {
var data = this.imageData.data;
Kinetic.Filters.Grayscale = function(imageData) {
var data = imageData.data;
for(var i = 0; i < data.length; i += 4) {
var brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
// red
Expand Down Expand Up @@ -4545,14 +4545,14 @@ Kinetic.Image = Kinetic.Shape.extend({
* filter has been applied
*/
applyFilter: function(config) {
try {
var trans = this._clearTransform();
this.saveImageData(this.getWidth(), this.getHeight());
this._setTransform(trans);

config.filter.call(this, config);
var canvas = new Kinetic.Canvas(this.attrs.width, this.attrs.height);
var context = canvas.getContext();
context.drawImage(this.attrs.image, 0, 0);
try {
var imageData = context.getImageData(0, 0, canvas.getWidth(), canvas.getHeight());
config.filter(imageData, config);
var that = this;
Kinetic.Type._getImage(this.getImageData(), function(imageObj) {
Kinetic.Type._getImage(imageData, function(imageObj) {
that.setImage(imageObj);

if(config.callback) {
Expand Down
6 changes: 3 additions & 3 deletions dist/kinetic-core.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/filters/Grayscale.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Kinetic.Filters.Grayscale = function() {
var data = this.imageData.data;
Kinetic.Filters.Grayscale = function(imageData) {
var data = imageData.data;
for(var i = 0; i < data.length; i += 4) {
var brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
// red
Expand Down
14 changes: 7 additions & 7 deletions src/shapes/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ Kinetic.Image = Kinetic.Shape.extend({
* filter has been applied
*/
applyFilter: function(config) {
try {
var trans = this._clearTransform();
this.saveImageData(this.getWidth(), this.getHeight());
this._setTransform(trans);

config.filter.call(this, config);
var canvas = new Kinetic.Canvas(this.attrs.width, this.attrs.height);
var context = canvas.getContext();
context.drawImage(this.attrs.image, 0, 0);
try {
var imageData = context.getImageData(0, 0, canvas.getWidth(), canvas.getHeight());
config.filter(imageData, config);
var that = this;
Kinetic.Type._getImage(this.getImageData(), function(imageObj) {
Kinetic.Type._getImage(imageData, function(imageObj) {
that.setImage(imageObj);

if(config.callback) {
Expand Down
5 changes: 1 addition & 4 deletions tests/js/unitTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1610,8 +1610,7 @@ Test.prototype.tests = {
x: 10,
y: 10,
image: imageObj,
draggable: true,
stroke: 'red'
draggable: true
});

layer.add(darth);
Expand Down Expand Up @@ -1649,8 +1648,6 @@ Test.prototype.tests = {
//height: 300,
image: imageObj,
draggable: true,
stroke: 'red',
strokeWidth: 5,
rotationDeg: 10,
scale: 0.3
});
Expand Down

0 comments on commit e99312e

Please sign in to comment.