Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if function exists in array

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?

like image 518
user2924536 Avatar asked Mar 23 '26 10:03

user2924536


1 Answers

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)
like image 186
sabithpocker Avatar answered Mar 24 '26 23:03

sabithpocker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!