How to end a forEachObject loop?

This question belongs to fabricJS and the canvas. In the following case, check if there is any object that has the true ( obj.background == true) property . There can be several images on the canvas. After the first discovery of the object, I want to end the loop. How am i doing this? I used return false;but does not work. Here is the function.

    canvas.forEachObject(function(obj){
        if(obj.isType('image') && obj.hasOwnProperty('background')){
            if(!obj.background == true){
                alert("true");
                return false;
            } 
        }
    });  
+4
source share
1 answer

Now I have not used FabricJS (waiting for spam votes), looking at the source, there is a method getObjects(). What this does is return an array of objects instead of a custom iterator, for example forEachObject().

, , forEach(), some(), every() .., , .

, , , , ? .

canvas
    .getObjects()
    .some(obj => {
        if (obj.isType('image') && obj.hasOwnProperty('background') && obj.background === true) {
            console.log('Aww shucks, you found me.');
            return true;
        }
    });

, . , , true. - true, some true.


EDIT .

, getObjects . .

canvas
    .getObjects('image')
    .some(obj => {
        if (obj.background === true) {
            console.log('Aww shucks, you found me.');
            return true;
        }
    });

fabricjs getObjects


EDIT: hasOwnProperty , . obj , , , undefined. === true.

+4

All Articles