3

I have a kinecticjs image object I am applying filters to based on some parameters, however when it needs to be redrawn it will apply the filter again, meaning it will duplicate the filter.

I know i should be doing some sort of check but its applies a function right to an array.

//create object
var image = new Kinetic.Image({
    image: alreadyLoadedImageObject
});

function addFilter(shouldAdd){
    if(shouldAdd){
        var filters = image.filters() || [];

        filters.push(Kinetic.Filters.RGB);

        image.filters(filters);
    }
}

layer.add(image);
stage.add(layer);

addFilter(true);
stage.draw();

setTimeout(function(){
    addFilter(true);
    stage.draw();
},500);

That should roughly show what i am currently doing, in this case once the time out runs there will be 2 Kinetic.Filters.RGB applied to the image object.

I consider this a general javascript question rather then just kineticjs, as the root is that I need to somehow know that there already is a Kinetic.Filters.RGB function within the filters array.

So 2 questions, is there anyway to check with something like Array.indexOf that a function name already exists in the array? or is there already some sort of prebuilt method in KineticJS for this I have not seen?

2 Answers 2

5
x = function(){};
y = function(){};
a = [];
a.push(x);
a.indexOf(x); //0
a.indexOf(y); //-1

So you can try:

if(filters.indexOf(Kinetic.Filters.RGB) !== -1)
Sign up to request clarification or add additional context in comments.

1 Comment

Never thought this would work, everything i seen said indexOf was string only. I ended up going with another solution that fit my project but this is good to know, tested it with a jsfiddle here jsfiddle.net/q3umxbcz
0

You can also check using the some() function.

if(!filters.some((val) => {val == Kinetic.Filters.RGB}))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.